<?php
namespace App\Service;
use App\Model\Search\AdvancedSearch;
use App\Model\Search\SearchCriteria;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Util\Exception;
use Symfony\Component\HttpFoundation\Request;
class AdvancedSearchService
{
private const PARAMS_PATH = __DIR__ . '/../../config/ffcb/search_params.json';
private const EXPORT_PARAMS_PATH = __DIR__ . '/../../config/ffcb/export_params.json';
public function __construct(
private EntityManagerInterface $em
) {}
public function getSearchParameters(string $entity): array
{
$searchParams = json_decode(file_get_contents(self::PARAMS_PATH), true);
if (isset($searchParams[strtolower($entity)])) {
$searchParams = $searchParams[strtolower($entity)];
} else {
throw new Exception('L\'entité "'.strtolower($entity).'" n\'a pas de configuration valide !');
}
return $searchParams;
}
public function getExportParameters(string $entity): array
{
$searchParams = json_decode(file_get_contents(self::EXPORT_PARAMS_PATH), true);
if (isset($searchParams[strtolower($entity)])) {
$searchParams = $searchParams[strtolower($entity)];
} else {
throw new Exception('L\'entité "'.strtolower($entity).'" n\'a pas de configuration valide !');
}
return $searchParams;
}
public function initAndUpdateAdvancedSearchByRequest(
Request $request,
string $entity,
array $parameters,
?bool $forSmsSending = false,
?bool $forEmailSending = false,
?bool $forDisplayArchived = false
): AdvancedSearch
{
$advancedSearch = new AdvancedSearch($entity, $parameters);
$advancedSearch
->setIsForSmsSending($forSmsSending)
->setIsForEmailSending($forEmailSending)
->setIsForDisplayArchived($forDisplayArchived)
;
if (!is_null($request->request->get('advanced_search', null))) {
$search = $request->request->get('advanced_search', null);
if (isset($search['searchCriteria'])) {
foreach ($search['searchCriteria'] as $search) {
$criteria = new SearchCriteria();
$criteria
->setEntity($search['entity'])
->setField($search['field'])
->setTerm(
$advancedSearch->transformValue($search['entity'], $search['field'], $search['term'])
);
$advancedSearch->addSearchCriterium($criteria);
}
}
}
return $advancedSearch;
}
public function addDefaultFilter(AdvancedSearch $advancedSearch, string $entity, array $parameters): AdvancedSearch
{
if (isset($parameters['default_filter']) && count($parameters['default_filter']) > 0) {
foreach ($parameters['default_filter'] as $filter) {
$criteria = new SearchCriteria();
$criteria
->setEntity($entity)
->setField($filter['field'])
->setTerm($filter['term'])
->setParams($parameters['default_filter']['params'] ?? [])
;
$advancedSearch->addSearchCriterium($criteria);
}
}
return $advancedSearch;
}
public function getEntityClass(array $parameters, string $entityName): ?string
{
foreach ($parameters['entities'] as $entityParameters) {
if (
trim($entityParameters['value']) === trim($entityName) ||
(isset($entityParameters['customValue']) && trim($entityParameters['customValue']) === trim($entityName))
) {
return $entityParameters['value'];
}
}
return null;
}
public function getEntityNamespace(array $parameters, string $entityName): ?string
{
foreach ($parameters['entities'] as $entityParameters) {
if (
trim($entityParameters['value']) === trim($entityName) ||
(isset($entityParameters['customValue']) && trim($entityParameters['customValue']) === trim($entityName))
) {
return isset($entityParameters['namespace']) ? $entityParameters['namespace'] : 'App\\Entity\\';
}
}
return null;
}
public function getRepository(array $parameters, string $entityName, string $className)
{
foreach ($parameters['entities'] as $entityParameters) {
if (
trim($entityParameters['value']) === trim($entityName) ||
(isset($entityParameters['customValue']) && trim($entityParameters['customValue']) === trim($entityName))
) {
return isset($entityParameters['customRepository']) ?
new $entityParameters['customRepository']($this->em) :
$this->em->getRepository($className)
;
}
}
return null;
}
}