<?php
namespace App\Controller\Admin;
use App\Service\UserSeasonService;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class CustomActionAdminController extends CRUDController
{
protected $authorizationChecker;
protected $userSeasonService;
protected $cloneObject;
public function __construct(AuthorizationCheckerInterface $authorizationChecker, UserSeasonService $userSeasonService)
{
$this->authorizationChecker = $authorizationChecker;
$this->userSeasonService = $userSeasonService;
}
public function editAction(Request $request): Response
{
$brand = $this->userSeasonService->getBrand($this->getUser());
$id = $request->get($this->admin->getIdParameter());
$existingObject = $this->admin->getObject($id);
if (!$existingObject) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
if (
!empty($brand) &&
!$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN') &&
$existingObject->getBrand() !== $brand
) {
$this->addFlash(
'sonata_flash_error',
$this->trans(
'app.admin.edit.to_show',
[],
'messages'
));
return new RedirectResponse($this->admin->generateUrl('show', ['id' => $existingObject->getId()]));
}
return parent::editAction($request);
}
public function cloneAction(Request $request, EntityManagerInterface $em, $id)
{
// the key used to lookup the template
$templateKey = 'edit';
$this->admin->checkAccess('create');
$class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass());
if ($class->isAbstract()) {
return $this->renderWithExtraParams(
'@SonataAdmin/CRUD/select_subclass.html.twig',
[
'base_template' => $this->getBaseTemplate(),
'admin' => $this->admin,
'action' => 'create',
],
null
);
}
//Custom Action
$object = $this->admin->getSubject();
if (!$object) {
throw new NotFoundHttpException(sprintf('unable to find the object with id: %s', $id));
}
$newObject = $object->copy();
if (method_exists($newObject, 'setBrand')) {
$brand = $this->userSeasonService->getBrand($this->getUser());
$newObject->setBrand($brand);
}
// Copy editAction
$preResponse = $this->preCreate($request, $newObject);
if (null !== $preResponse) {
return $preResponse;
}
$this->admin->setSubject($newObject);
$form = $this->admin->getForm();
$form->setData($newObject);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$isFormValid = $form->isValid();
// persist if the form was valid and if in preview mode the preview was approved
if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
$submittedObject = $form->getData();
$this->admin->setSubject($submittedObject);
$this->admin->checkAccess('create', $submittedObject);
try {
$newObject = $this->admin->create($submittedObject);
if ($this->isXmlHttpRequest()) {
return $this->handleXmlHttpRequestSuccessResponse($request, $newObject);
}
$this->addFlash(
'sonata_flash_success',
$this->trans(
'flash_create_success',
['%name%' => $this->escapeHtml($this->admin->toString($newObject))],
'SonataAdminBundle'
)
);
// redirect to edit mode
return $this->redirectTo($newObject);
} catch (ModelManagerException $e) {
$this->handleModelManagerException($e);
$isFormValid = false;
}
}
// show an error message if the form failed validation
if (!$isFormValid) {
if ($this->isXmlHttpRequest() && null !== ($response = $this->handleXmlHttpRequestErrorResponse($request, $form))) {
return $response;
}
$this->addFlash(
'sonata_flash_error',
$this->trans(
'flash_create_error',
['%name%' => $this->escapeHtml($this->admin->toString($newObject))],
'SonataAdminBundle'
)
);
} elseif ($this->isPreviewRequested()) {
// pick the preview template if the form was valid and preview was requested
$templateKey = 'preview';
$this->admin->getShow();
}
}
$formView = $form->createView();
// set the theme for the current Admin Form
$this->setFormTheme($formView, $this->admin->getFormTheme());
// NEXT_MAJOR: Remove this line and use commented line below it instead
//$template = $this->admin->getTemplate($templateKey);
// $template = $this->templateRegistry->getTemplate($templateKey);
return $this->renderWithExtraParams('@SonataAdmin/CRUD/edit.html.twig', [
'action' => 'create',
'form' => $formView,
'object' => $newObject,
'objectId' => null,
], null);
}
}