How create custom confugured batch action in sonata Admin Bundle. When I'm select batch action I'm need to coufigure some changes with selected List View Items then apply batch.
I'm solved this:
SonataAdmin
public function getBatchActions()
{
$actions = parent::getBatchActions();
// check user permissions
$actions['change'] = [
'label' => $this->trans('action_group_change', [], 'admin'),
'ask_confirmation' => false,
];
return $actions;
}
//In controller
public function batchActionChange(ProxyQueryInterface $selectedModelQuery, Request $request = null)
{
// !$this->admin->isGranted('EDIT')
// TODO: при необходимости
//
//
$modelManager = $this->admin->getModelManager();
$selectedModels = $selectedModelQuery->execute();
$ids = [];
foreach ($selectedModels as $selectedModel) {
$ids[] = $selectedModel->getId();
}
return new RedirectResponse(
$this->admin->generateUrl('change', [
'ids' => $ids,
])
);
}
public function changeAction(Request $request)
{
$ids = $request->get('ids');
if ($ids === null) {
$this->addFlash('sonata_flash_error', 'Ничего не выбрано');
return new RedirectResponse($this->admin->generateUrl('list'));
}
$form = $this->createForm(new ChangeProxyAdminType());
if ($request->getMethod()==='POST'){
$formHandler = $this->get('webface_character.handler.change_proxy');
$handleResult =$formHandler->handle($form,$request);
if ($handleResult){
return new RedirectResponse($this->admin->generateUrl('list'));
}
}
return $this->render('WebfaceCharacterBundle:ProxyAdmin:change.html.twig', [
'action' => 'change',
'form' => $form->createView(),
]);
Form Type:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user', EntityType::class, [
'class' => User::class,
'empty_data' => null,
'required' => false,
])
->add('username', TextType::class, [
'attr' => [
'class' => 'form-control',
],
'required' => false,
])
->add('password', PasswordType::class, [
'attr' => [
'class' => 'form-control',
],
'required' => false,
])
->add('isArchive', CheckboxType::class, [
'attr' => [
'class' => 'form-control',
],
'required' => false,
]);
}
Form handler:
public function handle(FormInterface $form, Request $request)
{
if (!$request->isMethod('POST')) {
return false;
}
$form->handleRequest($request);
if (!$form->isValid()) {
return false;
}
$data = $form->getData();
$ids = $request->get('ids');
$qb = $this->em->createQueryBuilder();
$query = $qb->update('WebfaceCharacterBundle:Proxy', 'p');
if ($data['user'] !== null) {
$user = $this->em->getRepository('WebfaceUserBundle:User')->findOneBy(['id' => $data['user']]);
dump($user);
$query->set('p.user', $user->getId());
}
if ($data['password'] !== null) {
$query->set('p.password', $qb->expr()->literal($data['password']));
}
if ($data['username'] !== null) {
$query->set('p.username',$qb->expr()->literal($data['username']));
}
$query->set('p.isArchive', $qb->expr()->literal($data['isArchive']));
$query->where($qb->expr()->in('p.id', implode(',', $ids)))
->getQuery()
->getResult();
return true;
}
I'm sure this is not best solution. But i'm dont know how create best solution for this problem.
This solution not works with batch action to 1000 items, because in my solution all ids pass to Query and redirect.
P.S. (This is draft)