|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
-
- namespace Lc\SovBundle\Field\Filter;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Doctrine\ORM\QueryBuilder;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
- use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
- use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
- use Symfony\Component\Form\Extension\Core\Type\DateType;
- use Symfony\Component\Form\Extension\Core\Type\IntegerType;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
- use Symfony\Component\Form\Form;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\HttpFoundation\Session\SessionInterface;
-
-
- /**
- * @author La clic ! <contact@laclic.fr>
- */
- class FilterManager
- {
- protected $em;
-
- use FilterTrait;
-
- public function __construct(SessionInterface $session, EntityManagerInterface $entityManager)
- {
- $this->session = $session;
- $this->em = $entityManager;
- }
-
-
- public function handleFiltersForm(QueryBuilder $queryBuilder, Form $filtersForm, $fields, EntityDto $entityDto)
- {
- foreach ($fields as $field) {
- if ($field instanceof FieldInterface) {
- $fieldDto = $field->getAsDto();
- } else {
- $fieldDto = $field;
- }
- if ($fieldDto->isDisplayedOn(Crud::PAGE_INDEX)) {
- if ($filtersForm->has($this->getFieldPropertySnake($fieldDto->getProperty()))) {
-
-
-
- if ($fieldDto->getFormType() === DateTimeType::class || $fieldDto->getFormType(
- ) === DateType::class) {
- $filteredValue['dateStart'] = $this->getFilteredValue(
- $filtersForm,
- $entityDto->getFqcn(),
- $fieldDto->getProperty(),
- 'dateStart'
- );
- $filteredValue['dateEnd'] = $this->getFilteredValue(
- $filtersForm,
- $entityDto->getFqcn(),
- $fieldDto->getProperty(),
- 'dateEnd'
- );
- }else{
- $filteredValue = $this->getFilteredValue(
- $filtersForm,
- $entityDto->getFqcn(),
- $fieldDto->getProperty()
- );
- }
- $this->applyFilter($queryBuilder, $fieldDto, $filteredValue);
- }
- }
- }
- }
-
- public function applyFilter(QueryBuilder $queryBuilder, FieldDto $fieldDto, $filteredValue)
- {
- switch ($fieldDto->getFormType()) {
- case CheckboxType::class:
- $checkboxFilter = new CheckboxFilter();
- $checkboxFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
- break;
- case ChoiceType::class:
- $choiceFilter = new ChoiceFilter();
- $choiceFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
- break;
- case IntegerType::class:
- $integerFilter = new IntegerFilter();
- $integerFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
- break;
- case TextType::class:
- $textFilter = new TextFilter();
- $textFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
- break;
- case EntityType::class:
- $textFilter = new AssociationFilter();
- $textFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
- break;
- case DateTimeType::class:
- case DateType::class:
- $textFilter = new DateFilter();
- $textFilter->applyFilter(
- $queryBuilder,
- $fieldDto->getProperty(),
- $filteredValue['dateStart'],
- $filteredValue['dateEnd']
- );
- break;
- }
- }
-
- public function getFilteredValue(
- Form $filtersForm,
- string $entityFqcn,
- string $field,
- string $dateExtraField = null
- ) {
- $field = $this->getFieldPropertySnake($field);
- $sessionParam = $entityFqcn.$field.$dateExtraField;
-
- $formField = $this->getFormField($filtersForm, $field, $dateExtraField);
-
- $value = $formField->getData();
-
- //Il existe une valeur posté dans le formulaire
- if ($value !== null) {
- $this->session->set($sessionParam, $value);
- return $value;
- }
-
- //action reset
- if ($filtersForm->get('reset')->getData() == 'clearAll') {
- $this->session->remove($sessionParam);
- return null;
- }
-
- //Récupération des valeurs stocké en sessions si le forrmFilters n'a pas été posté
- if ($this->session->get($sessionParam) && !$filtersForm->isSubmitted() && $formField) {
- $value = $this->session->get($sessionParam);
-
- //Champ date
- if ($formField->getConfig()->getOption('input') == 'datetime') {
- $filtersForm->get($field)->get($dateExtraField)->setData($value);
- //Champ association
- } elseif ($formField->getConfig()->getOption('class')) {
- $valFormated = $this->em->getRepository(
- $formField->getConfig()->getOption('class')
- )->find($value);
- $filtersForm->get($field)->setData($valFormated);
- } else {
- //Champ noramux
- $filtersForm->get($field)->setData($value);
- }
-
- return $value;
- }
-
- }
-
- public
- function getFormField(
- Form $filtersForm,
- string $field,
- string $extraField = null
- ): Form {
- if ($extraField) {
- return $filtersForm->get($field)->get($extraField);
- } else {
- return $filtersForm->get($field);
- }
- }
- }
|