src/Controller/Admin/User/UserController.php line 89

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin\User;
  3. use App\Entity\User\User;
  4. use App\Form\User\UserType;
  5. use App\Service\User\StatusManager;
  6. use App\Service\Manager\AdminManager;
  7. use App\Repository\User\UserRepository;
  8. use App\Controller\Admin\CoreController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use App\Form\Search\Custom\UserType as CustomUserType;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. #[Route("/admin/users")]
  15. class UserController extends CoreController
  16. {
  17.     
  18.     #[Route('/'name'admin_users_index')]
  19.     public function index(Request $request,
  20.                             UserRepository $repository): Response
  21.     {
  22.         $form $this->createForm(CustomUserType::class, null, [
  23.             'method' => 'GET',
  24.             'idLangue' => $this->getCurrentLanguage(),
  25.         ]);
  26.         $form->handleRequest($request);
  27.         $formParams $form->getData() ?? [];
  28.         return $this->render('admin/user/index.html.twig', [
  29.             'lists' => $repository->paginateAllVisible(
  30.                 $this->getCurrentLanguage(),
  31.                 $request->query->getInt('page'1), 
  32.                 $formParams,
  33.             ),
  34.             'form' => $form->createView(),
  35.             'table' => $this->getTable(),
  36.         ]);
  37.     }
  38.     public function getTable()
  39.     {
  40.         return [
  41.             'id' => 'u',
  42.             'fields' => [
  43.                 [
  44.                     'field' => 'firstName',
  45.                     'order' => 'u.firstName'
  46.                 ],
  47.                 [
  48.                     'field' => 'lastname',
  49.                     'order' => 'u.lastname'
  50.                 ],
  51.                 [
  52.                     'field' => 'email',
  53.                     'order' => 'u.email'
  54.                 ],
  55.               
  56.                 [
  57.                     'field' => 'statusName',
  58.                     'order' => 'statusName',
  59.                     'type' => 'status',
  60.                 ],
  61.             ],
  62.             'actions'=> [
  63.                 [
  64.                     'label' => 'edit',
  65.                     'icon' => 'edit',
  66.                     'path' => 'admin_users_edit',
  67.                 ],
  68.                
  69.                 [
  70.                     'label' => 'delete',
  71.                     'type' => 'delete',
  72.                     'path' => 'admin_users_delete',
  73.                 ]
  74.             ]
  75.         ];
  76.     }
  77.  
  78.     #[Route('/new'name'admin_users_new')]
  79.     #[Route('/edit/{id}'name'admin_users_edit')]
  80.     public function new(
  81.                         Request $request,
  82.                         AdminManager $adminManager,
  83.                         StatusManager $statusManager,
  84.                         UserPasswordHasherInterface $passwordHasher,
  85.                         User $user null)
  86.     {
  87.         if(!$user) {
  88.             $user = new User();
  89.             $oldStatut null;
  90.         }
  91.         else  {
  92.             $oldStatut $user->getStatus();
  93.         }
  94.         $form $this->createForm(UserType::class, $user, [
  95.             'idLangue' => $this->getCurrentLanguage(),
  96.         ]);
  97.         $form->handleRequest($request);
  98.         if ($form->isSubmitted() && $form->isValid()) {
  99.             /** Manage Password */
  100.             if ($form->get('plainPassword')->getData()) {
  101.                 $user->setPassword (
  102.                     $passwordHasher->hashPassword(
  103.                         $user,
  104.                         $form->get('plainPassword')->getData()
  105.                     )
  106.                 );
  107.             }
  108.             // if(isset(ProfileRole::LISTS[$user->getProfile()])) {
  109.             //     $user->setRoles([ProfileRole::LISTS[$user->getProfile()]]);
  110.             // }
  111.          
  112.             $adminManager->saveEntity($user);
  113.             return $this->redirectToRoute('admin_users_index');
  114.         }
  115.         return $this->render('admin/user/form.html.twig', [
  116.             'field' => $user,
  117.             'form' => $form->createView(),
  118.         ]);
  119.     }
  120.     
  121.    
  122.     #[Route('/delete/{id}'name'admin_users_delete')]
  123.     public function delete(Request $request
  124.                             User $user
  125.                             AdminManager $adminManager): Response
  126.     {
  127.         if ($this->isCsrfTokenValid('delete'$user->getId(), $request->query->get('_token'))) {
  128.             $user->setEmail($user->getEmail().uniqid());
  129.             $adminManager->deleteEntity($user);
  130.         }
  131.        
  132.         return $this->redirectToRoute('admin_users_index');
  133.     }
  134. }