src/Controller/Backend/AdvancedSearchController.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Backend;
  3. use App\Controller\BaseController;
  4. use App\Form\Backend\AdvancedSearchType;
  5. use App\Service\AdvancedSearchService;
  6. use App\Service\ExportService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. #[IsGranted('ROLE_USER'message"Vous n'avez pas l'autorisation pour cette action !")]
  18. #[Route('/admin')]
  19. class AdvancedSearchController extends BaseController
  20. {
  21.     public function __construct(
  22.         ManagerRegistry $doctrine,
  23.         private AdvancedSearchService $advancedSearchService
  24.     )
  25.     {
  26.         parent::__construct($doctrine);
  27.     }
  28.     #[Route(path'/advanced-search/{entity}'name'advanced_search'methods: ['GET''POST'])]
  29.     public function advancedSearch(
  30.         Request $request,
  31.         PaginatorInterface $paginator,
  32.         string $entity
  33.     ): Response
  34.     {
  35.         $searchParams $this->advancedSearchService->getSearchParameters($entity);
  36.         $forEmailSending = (bool) $request->request->get('for_email'false);
  37.         $forSmsSending = (bool) $request->request->get('for_sms'false);
  38.         $forDisplayArchived $request->request->get('for_not_display_achived'false);
  39.         $advancedSearch $this->advancedSearchService->initAndUpdateAdvancedSearchByRequest(
  40.             $request,
  41.             $entity,
  42.             $searchParams,
  43.             $forSmsSending,
  44.             $forEmailSending,
  45.             $forDisplayArchived
  46.         );
  47.         $advancedSearchForm $this->createForm(AdvancedSearchType::class, $advancedSearch, [
  48.             'parameters' => $searchParams,
  49.             'entityName' => $entity,
  50.         ]);
  51.         $advancedSearchForm->handleRequest($request);
  52.         $sendingRouteName $forEmailSending 'send_email' 'send_sms';
  53.         $class $this->advancedSearchService->getEntityNamespace($searchParams$entity).ucfirst($this->advancedSearchService->getEntityClass($searchParams$entity));
  54.         $advancedSearch $this->advancedSearchService->addDefaultFilter($advancedSearch$entity$searchParams);
  55.         $repository $this->advancedSearchService->getRepository($searchParams$entity$class);
  56.         $userFilters $this->getUser()->getFiltersByRole($entity);
  57.         $query $repository->retrieveAllData(
  58.             $advancedSearch,
  59.             $forEmailSending,
  60.             $forSmsSending,
  61.             $forDisplayArchived,
  62.             $searchParams,
  63.             [],
  64.             $this->getUser()
  65.         );
  66.         $results $paginator->paginate(
  67.             $query,
  68.             $request->query->getInt('page'1),
  69.             self::ITEM_PER_PAGE
  70.         );
  71.         $renderParams = [
  72.             'menu' => strtolower($entity)
  73.         ];
  74.         if (isset($searchParams['menu'])) {
  75.             $renderParams['menu'] = $searchParams['menu'];
  76.         }
  77.         if (isset($searchParams['submenu'])) {
  78.             $renderParams['submenu'] = $searchParams['submenu'];
  79.         }
  80.         return $this->renderForm('backend/advanced_search/advanced_search.html.twig'array_merge([
  81.             'advancedSearchForm' => $advancedSearchForm,
  82.             'entity' => $entity,
  83.             'advancedSearchResults' => $results,
  84.             'forEmailSending' => $forEmailSending,
  85.             'forSmsSending' => $forSmsSending,
  86.             'formRouteName' => $sendingRouteName,
  87.             'forNotDisplayArchived' => $forDisplayArchived,
  88.             'searchParams' => $searchParams,
  89.             'title' => $searchParams['title'] ?? 'Inconnu'
  90.         ], $renderParams));
  91.     }
  92.     #[Route("/advanced-export/{entity}/{type}"name'advanced_export'methods: ['POST'])]
  93.     public function exportAdvancedSearch(
  94.         Request $request,
  95.         ParameterBagInterface $parameters,
  96.         string $entity,
  97.         string $type
  98.     ): JsonResponse
  99.     {
  100.         if (!in_array($type, ['excel''csv'])) {
  101.             throw new \Exception('Seul les export "excel" et "csv" sont disponibles.');
  102.         }
  103.         $forDisplayArchived $request->request->get('for_not_display_achived'false);
  104.         $searchParams $this->advancedSearchService->getSearchParameters($entity);
  105.         $exportParams $this->advancedSearchService->getExportParameters($entity);
  106.         $advancedSearch $this->advancedSearchService->initAndUpdateAdvancedSearchByRequest(
  107.             $request,
  108.             $entity,
  109.             $searchParams,
  110.             false,
  111.             false,
  112.             $forDisplayArchived
  113.         );
  114.         $advancedSearchForm $this->createForm(AdvancedSearchType::class, $advancedSearch, [
  115.             'parameters' => $searchParams,
  116.             'entityName' => $entity,
  117.         ]);
  118.         $advancedSearchForm->handleRequest($request);
  119.         $class $this->advancedSearchService->getEntityNamespace($searchParams$entity).ucfirst($this->advancedSearchService->getEntityClass($searchParams$entity));
  120.         $advancedSearch $this->advancedSearchService->addDefaultFilter($advancedSearch$entity$searchParams);
  121.         $repository $this->advancedSearchService->getRepository($searchParams$entity$class);
  122.         $userFilters $this->getUser()->getFiltersByRole($entity);
  123.         $datas $repository->retrieveAllData(
  124.             $advancedSearch,
  125.             false,
  126.             false,
  127.             $forDisplayArchived,
  128.             $searchParams,
  129.             [],
  130.             $this->getUser()
  131.         )->getResult();
  132.         $exportService = new ExportService(
  133.             $entity,
  134.             $type,
  135.             $exportParams,
  136.             $datas,
  137.             $parameters
  138.         );
  139.         try {
  140.             $filename $exportService->export();
  141.             return new JsonResponse([
  142.                 'status' => 'ok',
  143.                 'message' => 'Export effectué avec succès',
  144.                 'filename' => $filename
  145.             ]);
  146.         } catch (\Exception $exception) {
  147.             return new JsonResponse([
  148.                 'status' => 'nok',
  149.                 'message' => $exception->getFile().' '.$exception->getLine().' - '.$exception->getMessage(),
  150.             ]);
  151.         }
  152.     }
  153.     #[Route('/advanced-export-download/{filename}'name'advanced_export_download'methods: ['GET'])]
  154.     public function downloadExportAdvancedSearch(ParameterBagInterface $parametersstring $filename): BinaryFileResponse
  155.     {
  156.         $filepath $parameters->get('export_path').$filename;
  157.         return $this->file($filepath$filename);
  158.     }
  159.     #[Route('/advanced-selection-persist'name'update_session_selection_contact'methods: ['POST'])]
  160.     public function updateSessionSelection(Request $request): JsonResponse
  161.     {
  162.         $datas json_decode($request->getContent(), true);
  163.         $request->getSession()->set('contact_selection'$datas);
  164.         return new JsonResponse([
  165.             'status' => 'ok',
  166.             'message' => 'Mise à jour de la sélection effectuée avec succès'
  167.         ]);
  168.     }
  169.     #[Route('/advanced-search-entity-list'name'advanced_search_entity_list'methods: ['POST'])]
  170.     public function getEntityList(Request $requestEntityManagerInterface $em): JsonResponse
  171.     {
  172.         $datas json_decode($request->getContent(), true);
  173.         $entityResults $em->getRepository($datas['entity'])->findAll();
  174.         $results = [];
  175.         foreach ($entityResults as $entityResult) {
  176.             $results[] = [
  177.                 'id' => $entityResult->getId(),
  178.                 'value' => $entityResult->{'get'.ucfirst($datas['choice_label'])}()
  179.             ];
  180.         }
  181.         return new JsonResponse($results);
  182.     }
  183. }