src/Controller/Admin/UserController.php line 172

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\User;
  4. use App\Event\Account\UserDeletedEvent;
  5. use App\Manager\ActivityManager;
  6. use App\Manager\DonneeCompteClientManager;
  7. use App\Manager\Mailer;
  8. use App\Manager\MailTypeInterface;
  9. use App\Manager\UserActivator;
  10. use App\Repository\Multi\UserRepository;
  11. use App\Traits\TranslatorByDomainTrait;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  14. use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
  15. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  16. use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityRemoveException;
  17. use Symfony\Component\EventDispatcher\EventDispatcher;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. use Twig\Environment;
  24. class UserController extends EasyAdminController
  25. {
  26.     use TranslatorByDomainTrait;
  27.     /** @var UserRepository */
  28.     protected UserRepository $userRepository;
  29.     /** @var Mailer */
  30.     protected Mailer $mailer;
  31.     /** @var TranslatorInterface */
  32.     protected $translator;
  33.     /** @var EventDispatcher */
  34.     protected $dispatcher;
  35.     /** @var EntityManagerInterface */
  36.     protected EntityManagerInterface $objectManager;
  37.     /** @var UserActivator */
  38.     protected UserActivator $userActivator;
  39.     /** @var ActivityManager */
  40.     protected ActivityManager $activityManager;
  41.     protected DonneeCompteClientManager $donneeCompteClientManager;
  42.     /**
  43.      * UserController constructor.
  44.      */
  45.     public function __construct(
  46.         UserRepository $userRepository,
  47.         Mailer $mailer,
  48.         TranslatorInterface $translator,
  49.         EventDispatcherInterface $dispatcher,
  50.         EntityManagerInterface $objectManager,
  51.         UserActivator $userActivator,
  52.         ActivityManager $activityManager,
  53.         DonneeCompteClientManager $donneeCompteClientManager
  54.     )
  55.     {
  56.         $this->userRepository $userRepository;
  57.         $this->mailer $mailer;
  58.         $this->translator $translator;
  59.         $this->dispatcher $dispatcher;
  60.         $this->objectManager $objectManager;
  61.         $this->userActivator $userActivator;
  62.         $this->activityManager $activityManager;
  63.         $this->donneeCompteClientManager $donneeCompteClientManager;
  64.     }
  65.     protected function listAction(): Response
  66.     {
  67.         $this->dispatch(EasyAdminEvents::PRE_LIST);
  68.         $search trim($this->request->query->get('query'));
  69.         $maxPerPage $this->entity['list']['max_results'];
  70.         $currentPage $this->request->query->get('page'1);
  71.         $offset = ($currentPage 1) * $maxPerPage;
  72.         $totalResults $this->userRepository->countByNotContact($search);
  73.         $nbPages ceil($totalResults $maxPerPage);
  74.         $users $this->userRepository->findAllNotContactWithPagination(
  75.             $offset,
  76.             $maxPerPage,
  77.             $search,
  78.             $this->request->query->get('sortField'),
  79.             $this->request->query->get('sortDirection')
  80.         );
  81.         foreach ($users as $user) {
  82.             $user->setDonneeCompteClient(
  83.                 $this->donneeCompteClientManager->getDonneeCompteClientByNumeroClient($user->getCustomerNumber())
  84.             );
  85.         }
  86.         $parameters = [
  87.             'paginator' => [
  88.                 'currentPageResults' => $users,
  89.                 'haveToPaginate' => $nbPages 1,
  90.                 'currentPage' => $currentPage,
  91.                 'nbPages' => $nbPages,
  92.                 'currentPageOffsetStart' => $offset 1,
  93.                 'currentPageOffsetEnd' => min($totalResults$offset $maxPerPage),
  94.                 'nbResults' => $totalResults,
  95.                 'hasNextPage' => $currentPage $nbPages,
  96.                 'hasPreviousPage' => $currentPage 1,
  97.                 'previousPage' => $currentPage 1,
  98.                 'nextPage' => $currentPage 1,
  99.             ],
  100.             'fields' => $this->entity['list']['fields'],
  101.             'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  102.         ];
  103.         return $this->executeDynamicMethod('render<EntityName>Template', ['list'$this->entity['templates']['list'], $parameters]);
  104.     }
  105.     protected function removeMainAccountInformation(User $user)
  106.     {
  107.         /** @var User $subAccount */
  108.         foreach ($user->getContacts() as $subAccount) {
  109.             $this->removeSubAccount($subAccount);
  110.         }
  111.     }
  112.     protected function removeSubAccount(User $user)
  113.     {
  114.         try {
  115.             $this->executeDynamicMethod(
  116.                 'remove<EntityName>Entity',
  117.                 [$user$this->createDeleteForm($this->entity['name'], $this->request->query->get('id'))]
  118.             );
  119.         } catch (ForeignKeyConstraintViolationException $e) {
  120.             throw new EntityRemoveException(['entity_name' => $this->entity['name'], 'message' => $e->getMessage()]);
  121.         }
  122.     }
  123.     protected function removeUserEntity(User $user)
  124.     {
  125.         if ($user->isMainAccount()) {
  126.             $this->removeMainAccountInformation($user);
  127.         }
  128.         $this->activityManager->removeActivity($user);
  129.         $this->removeEntity($user);
  130.         // Update donneeCompteClient and send emails
  131.         $this->dispatcher->dispatch(
  132.             new UserDeletedEvent(
  133.                 $user,
  134.                 [
  135.                     'notifyMain' => $this->request->query->get('notify-main'false),
  136.                     'notifyGuests' => $this->request->query->get('notify-guests'false)
  137.                 ]
  138.             ),
  139.         );
  140.     }
  141.     protected function searchAction(): Response
  142.     {
  143.         return $this->listAction();
  144.     }
  145.     /**
  146.      * @Route("/connect-as/{id}", name="user_connect_as")
  147.      */
  148.     public function connectAsAction(User $user): Response
  149.     {
  150.         $this->get('session')->set('admin4preview'$user->getEmail());
  151.         return $this->redirectToRoute('app_login_4_preview');
  152.     }
  153.     /**
  154.      * @Route("/send-activation/{id}", name="user_send_activation")
  155.      */
  156.     public function sendActivationAction(User $user): JsonResponse
  157.     {
  158.         $this->userActivator->sendActivation($user);
  159.         return $this->json(['result' => 'ok']);
  160.     }
  161. }