<?php
namespace App\Controller\Backend;
use App\Entity\User;
use App\Form\UserType;
use App\Model\Entity\AccountInterface;
use App\Service\UserManager;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* All backends common functionalities. Each of these can be overriden in the appropriate controller.
*/
class EaBackendController extends AdminController
{
protected $userManager;
public function __construct(UserManager $userManager) {
$this->userManager = $userManager;
}
/**
* @Route("/", name="easyadmin")
*/
public function indexAction(Request $request): Response {
return parent::indexAction($request);
}
/**
* Callback for menu action 'currentUserProfile' for entity 'CurrentUser'
*/
protected function currentUserProfileAction(): Response {
return $this->redirectToRoute('easyadmin', [
'action' => 'edit',
'entity' => $this->request->query->get('entity'),
'id' => $this->get('security.token_storage')->getToken()->getUser()->getId(),
]);
}
/**
* Callback for menu action 'currentAccountProfile' for entity 'CurrentUser'
*/
protected function currentAccountProfileAction(): Response {
return $this->redirectToRoute('easyadmin', [
'action' => 'edit',
'entity' => $this->request->query->get('entity'),
'id' => $this->getCurrentUser()->getAccount()->getId(),
]);
}
protected function createEntityFormBuilder($entity, $view): FormBuilder {
$builder = parent::createEntityFormBuilder($entity, $view);
if($entity instanceof AccountInterface) {
$civilities = AccountInterface::CIVILITIES_CHOICES;
$builder->add('civility', ChoiceType::class, [
'label' => "Civilité",
'choices' => array_combine($civilities, $civilities),
'placeholder' => "-",
'required' => false,
]);
}
return $builder;
}
protected function createCurrentUserEntityForm($entity, array $entityProperties, $view): FormInterface {
return $this->createForm(UserType::class, $entity, ['show_system_infos' => true]);
}
protected function getCurrentUser(): User {
return $this->get('security.token_storage')->getToken()->getUser();
}
}