src/Service/AdvancedSearchService.php line 139

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Search\AdvancedSearch;
  4. use App\Model\Search\SearchCriteria;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use PHPUnit\Util\Exception;
  7. use Symfony\Component\HttpFoundation\Request;
  8. class AdvancedSearchService
  9. {
  10.     private const PARAMS_PATH __DIR__ '/../../config/ffcb/search_params.json';
  11.     private const EXPORT_PARAMS_PATH __DIR__ '/../../config/ffcb/export_params.json';
  12.     public function __construct(
  13.         private EntityManagerInterface $em
  14.     ) {}
  15.     public function getSearchParameters(string $entity): array
  16.     {
  17.         $searchParams json_decode(file_get_contents(self::PARAMS_PATH), true);
  18.         if (isset($searchParams[strtolower($entity)])) {
  19.             $searchParams $searchParams[strtolower($entity)];
  20.         } else {
  21.             throw new Exception('L\'entité "'.strtolower($entity).'" n\'a pas de configuration valide !');
  22.         }
  23.         return $searchParams;
  24.     }
  25.     public function getExportParameters(string $entity): array
  26.     {
  27.         $searchParams json_decode(file_get_contents(self::EXPORT_PARAMS_PATH), true);
  28.         if (isset($searchParams[strtolower($entity)])) {
  29.             $searchParams $searchParams[strtolower($entity)];
  30.         } else {
  31.             throw new Exception('L\'entité "'.strtolower($entity).'" n\'a pas de configuration valide !');
  32.         }
  33.         return $searchParams;
  34.     }
  35.     public function initAndUpdateAdvancedSearchByRequest(
  36.         Request $request,
  37.         string $entity,
  38.         array $parameters,
  39.         ?bool $forSmsSending false,
  40.         ?bool $forEmailSending false,
  41.         ?bool $forDisplayArchived false
  42.     ): AdvancedSearch
  43.     {
  44.         $advancedSearch = new AdvancedSearch($entity$parameters);
  45.         $advancedSearch
  46.             ->setIsForSmsSending($forSmsSending)
  47.             ->setIsForEmailSending($forEmailSending)
  48.             ->setIsForDisplayArchived($forDisplayArchived)
  49.         ;
  50.         if (!is_null($request->request->get('advanced_search'null))) {
  51.             $search $request->request->get('advanced_search'null);
  52.             if (isset($search['searchCriteria'])) {
  53.                 foreach ($search['searchCriteria'] as $search) {
  54.                     $criteria = new SearchCriteria();
  55.                     $criteria
  56.                         ->setEntity($search['entity'])
  57.                         ->setField($search['field'])
  58.                         ->setTerm(
  59.                             $advancedSearch->transformValue($search['entity'], $search['field'], $search['term'])
  60.                         );
  61.                     $advancedSearch->addSearchCriterium($criteria);
  62.                 }
  63.             }
  64.         }
  65.         return $advancedSearch;
  66.     }
  67.     public function addDefaultFilter(AdvancedSearch $advancedSearchstring $entity, array $parameters): AdvancedSearch
  68.     {
  69.         if (isset($parameters['default_filter']) && count($parameters['default_filter']) > 0) {
  70.             foreach ($parameters['default_filter'] as $filter) {
  71.                 $criteria = new SearchCriteria();
  72.                 $criteria
  73.                     ->setEntity($entity)
  74.                     ->setField($filter['field'])
  75.                     ->setTerm($filter['term'])
  76.                     ->setParams($parameters['default_filter']['params'] ?? [])
  77.                 ;
  78.                 $advancedSearch->addSearchCriterium($criteria);
  79.             }
  80.         }
  81.         return $advancedSearch;
  82.     }
  83.     public function getEntityClass(array $parametersstring $entityName): ?string
  84.     {
  85.         foreach ($parameters['entities'] as $entityParameters) {
  86.             if (
  87.                 trim($entityParameters['value']) === trim($entityName) ||
  88.                 (isset($entityParameters['customValue']) && trim($entityParameters['customValue']) === trim($entityName))
  89.             ) {
  90.                 return $entityParameters['value'];
  91.             }
  92.         }
  93.         return null;
  94.     }
  95.     public function getEntityNamespace(array $parametersstring $entityName): ?string
  96.     {
  97.         foreach ($parameters['entities'] as $entityParameters) {
  98.             if (
  99.                 trim($entityParameters['value']) === trim($entityName) ||
  100.                 (isset($entityParameters['customValue']) && trim($entityParameters['customValue']) === trim($entityName))
  101.             ) {
  102.                 return isset($entityParameters['namespace']) ? $entityParameters['namespace'] : 'App\\Entity\\';
  103.             }
  104.         }
  105.         return null;
  106.     }
  107.     public function getRepository(array $parametersstring $entityNamestring $className)
  108.     {
  109.         foreach ($parameters['entities'] as $entityParameters) {
  110.             if (
  111.                 trim($entityParameters['value']) === trim($entityName) ||
  112.                 (isset($entityParameters['customValue']) && trim($entityParameters['customValue']) === trim($entityName))
  113.             ) {
  114.                 return isset($entityParameters['customRepository']) ?
  115.                     new $entityParameters['customRepository']($this->em) :
  116.                     $this->em->getRepository($className)
  117.                 ;
  118.             }
  119.         }
  120.         return null;
  121.     }
  122. }