src/Controller/Admin/CustomActionAdminController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Service\UserSeasonService;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Sonata\AdminBundle\Controller\CRUDController;
  6. use Sonata\AdminBundle\Exception\ModelManagerException;
  7. use Symfony\Component\Form\FormInterface;
  8. use Symfony\Component\Form\FormRenderer;
  9. use Symfony\Component\Form\FormView;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  16. class CustomActionAdminController extends CRUDController
  17. {
  18.     protected $authorizationChecker;
  19.     protected $userSeasonService;
  20.     protected $cloneObject;
  21.     public function __construct(AuthorizationCheckerInterface $authorizationCheckerUserSeasonService $userSeasonService)
  22.     {
  23.         $this->authorizationChecker $authorizationChecker;
  24.         $this->userSeasonService $userSeasonService;
  25.     }
  26.     public function editAction(Request $request): Response
  27.     {
  28.         $brand $this->userSeasonService->getBrand($this->getUser());
  29.         $id $request->get($this->admin->getIdParameter());
  30.         $existingObject $this->admin->getObject($id);
  31.         if (!$existingObject) {
  32.             throw $this->createNotFoundException(sprintf('unable to find the object with id: %s'$id));
  33.         }
  34.         if (
  35.             !empty($brand) &&
  36.             !$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN') &&
  37.             $existingObject->getBrand() !== $brand
  38.         ) {
  39.             $this->addFlash(
  40.                 'sonata_flash_error',
  41.                 $this->trans(
  42.                     'app.admin.edit.to_show',
  43.                     [],
  44.                     'messages'
  45.                 ));
  46.             return new RedirectResponse($this->admin->generateUrl('show', ['id' => $existingObject->getId()]));
  47.         }
  48.         return parent::editAction($request);
  49.     }
  50.     public function cloneAction(Request $requestEntityManagerInterface $em$id)
  51.     {
  52.         // the key used to lookup the template
  53.         $templateKey 'edit';
  54.         $this->admin->checkAccess('create');
  55.         $class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass());
  56.         if ($class->isAbstract()) {
  57.             return $this->renderWithExtraParams(
  58.                 '@SonataAdmin/CRUD/select_subclass.html.twig',
  59.                 [
  60.                     'base_template' => $this->getBaseTemplate(),
  61.                     'admin' => $this->admin,
  62.                     'action' => 'create',
  63.                 ],
  64.                 null
  65.             );
  66.         }
  67.         //Custom Action
  68.         $object $this->admin->getSubject();
  69.         if (!$object) {
  70.             throw new NotFoundHttpException(sprintf('unable to find the object with id: %s'$id));
  71.         }
  72.         $newObject $object->copy();
  73.         if (method_exists($newObject'setBrand')) {
  74.             $brand $this->userSeasonService->getBrand($this->getUser());
  75.             $newObject->setBrand($brand);
  76.         }
  77.         // Copy editAction
  78.         $preResponse $this->preCreate($request$newObject);
  79.         if (null !== $preResponse) {
  80.             return $preResponse;
  81.         }
  82.         $this->admin->setSubject($newObject);
  83.         $form $this->admin->getForm();
  84.         $form->setData($newObject);
  85.         $form->handleRequest($request);
  86.         if ($form->isSubmitted()) {
  87.             $isFormValid $form->isValid();
  88.             // persist if the form was valid and if in preview mode the preview was approved
  89.             if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
  90.                 $submittedObject $form->getData();
  91.                 $this->admin->setSubject($submittedObject);
  92.                 $this->admin->checkAccess('create'$submittedObject);
  93.                 try {
  94.                     $newObject $this->admin->create($submittedObject);
  95.                     if ($this->isXmlHttpRequest()) {
  96.                         return $this->handleXmlHttpRequestSuccessResponse($request$newObject);
  97.                     }
  98.                     $this->addFlash(
  99.                         'sonata_flash_success',
  100.                         $this->trans(
  101.                             'flash_create_success',
  102.                             ['%name%' => $this->escapeHtml($this->admin->toString($newObject))],
  103.                             'SonataAdminBundle'
  104.                         )
  105.                     );
  106.                     // redirect to edit mode
  107.                     return $this->redirectTo($newObject);
  108.                 } catch (ModelManagerException $e) {
  109.                     $this->handleModelManagerException($e);
  110.                     $isFormValid false;
  111.                 }
  112.             }
  113.             // show an error message if the form failed validation
  114.             if (!$isFormValid) {
  115.                 if ($this->isXmlHttpRequest() && null !== ($response $this->handleXmlHttpRequestErrorResponse($request$form))) {
  116.                     return $response;
  117.                 }
  118.                 $this->addFlash(
  119.                     'sonata_flash_error',
  120.                     $this->trans(
  121.                         'flash_create_error',
  122.                         ['%name%' => $this->escapeHtml($this->admin->toString($newObject))],
  123.                         'SonataAdminBundle'
  124.                     )
  125.                 );
  126.             } elseif ($this->isPreviewRequested()) {
  127.                 // pick the preview template if the form was valid and preview was requested
  128.                 $templateKey 'preview';
  129.                 $this->admin->getShow();
  130.             }
  131.         }
  132.         $formView $form->createView();
  133.         // set the theme for the current Admin Form
  134.         $this->setFormTheme($formView$this->admin->getFormTheme());
  135.         // NEXT_MAJOR: Remove this line and use commented line below it instead
  136.         //$template = $this->admin->getTemplate($templateKey);
  137.         // $template = $this->templateRegistry->getTemplate($templateKey);
  138.         return $this->renderWithExtraParams('@SonataAdmin/CRUD/edit.html.twig', [
  139.             'action' => 'create',
  140.             'form' => $formView,
  141.             'object' => $newObject,
  142.             'objectId' => null,
  143.         ], null);
  144.     }
  145. }