src/Controller/Backend/EaBackendController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Backend;
  3. use App\Entity\User;
  4. use App\Form\UserType;
  5. use App\Model\Entity\AccountInterface;
  6. use App\Service\UserManager;
  7. use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\Form\FormBuilder;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * All backends common functionalities. Each of these can be overriden in the appropriate controller.
  16.  */
  17. class EaBackendController extends AdminController
  18. {
  19.     protected $userManager;
  20.     public function __construct(UserManager $userManager) {
  21.         $this->userManager $userManager;
  22.     }
  23.     /**
  24.      * @Route("/", name="easyadmin")
  25.      */
  26.     public function indexAction(Request $request): Response {
  27.         return parent::indexAction($request);
  28.     }
  29.     /**
  30.      * Callback for menu action 'currentUserProfile' for entity 'CurrentUser'
  31.      */
  32.     protected function currentUserProfileAction(): Response {
  33.         return $this->redirectToRoute('easyadmin', [
  34.             'action' => 'edit',
  35.             'entity' => $this->request->query->get('entity'),
  36.             'id' => $this->get('security.token_storage')->getToken()->getUser()->getId(),
  37.         ]);
  38.     }
  39.     /**
  40.      * Callback for menu action 'currentAccountProfile' for entity 'CurrentUser'
  41.      */
  42.     protected function currentAccountProfileAction(): Response {
  43.         return $this->redirectToRoute('easyadmin', [
  44.             'action' => 'edit',
  45.             'entity' => $this->request->query->get('entity'),
  46.             'id' => $this->getCurrentUser()->getAccount()->getId(),
  47.         ]);
  48.     }
  49.     protected function createEntityFormBuilder($entity$view): FormBuilder {
  50.         $builder parent::createEntityFormBuilder($entity$view);
  51.         if($entity instanceof AccountInterface) {
  52.             $civilities AccountInterface::CIVILITIES_CHOICES;
  53.             $builder->add('civility'ChoiceType::class, [
  54.                 'label' => "Civilité",
  55.                 'choices' => array_combine($civilities$civilities),
  56.                 'placeholder' => "-",
  57.                 'required' => false,
  58.             ]);
  59.         }
  60.         return $builder;
  61.     }
  62.     protected function createCurrentUserEntityForm($entity, array $entityProperties$view): FormInterface {
  63.         return $this->createForm(UserType::class, $entity, ['show_system_infos' => true]);
  64.     }
  65.     protected function getCurrentUser(): User {
  66.         return $this->get('security.token_storage')->getToken()->getUser();
  67.     }
  68. }