You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilterManager.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Lc\SovBundle\Field\Filter;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\QueryBuilder;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  6. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  9. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  10. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  13. use Symfony\Component\Form\Extension\Core\Type\DateType;
  14. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. use Symfony\Component\Form\Form;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  19. /**
  20. * @author La clic ! <contact@laclic.fr>
  21. */
  22. class FilterManager
  23. {
  24. protected $em;
  25. use FilterTrait;
  26. public function __construct(SessionInterface $session, EntityManagerInterface $entityManager)
  27. {
  28. $this->session = $session;
  29. $this->em = $entityManager;
  30. }
  31. public function handleFiltersForm(QueryBuilder $queryBuilder, Form $filtersForm, $fields, EntityDto $entityDto)
  32. {
  33. foreach ($fields as $field) {
  34. if ($field instanceof FieldInterface) {
  35. $fieldDto = $field->getAsDto();
  36. } else {
  37. $fieldDto = $field;
  38. }
  39. if ($fieldDto->isDisplayedOn(Crud::PAGE_INDEX)) {
  40. if ($filtersForm->has($this->getFieldPropertySnake($fieldDto->getProperty()))) {
  41. if ($fieldDto->getFormType() === DateTimeType::class || $fieldDto->getFormType(
  42. ) === DateType::class) {
  43. $filteredValue['dateStart'] = $this->getFilteredValue(
  44. $filtersForm,
  45. $entityDto->getFqcn(),
  46. $fieldDto->getProperty(),
  47. 'dateStart'
  48. );
  49. $filteredValue['dateEnd'] = $this->getFilteredValue(
  50. $filtersForm,
  51. $entityDto->getFqcn(),
  52. $fieldDto->getProperty(),
  53. 'dateEnd'
  54. );
  55. }else{
  56. $filteredValue = $this->getFilteredValue(
  57. $filtersForm,
  58. $entityDto->getFqcn(),
  59. $fieldDto->getProperty()
  60. );
  61. }
  62. $this->applyFilter($queryBuilder, $fieldDto, $filteredValue);
  63. }
  64. }
  65. }
  66. }
  67. public function applyFilter(QueryBuilder $queryBuilder, FieldDto $fieldDto, $filteredValue)
  68. {
  69. switch ($fieldDto->getFormType()) {
  70. case CheckboxType::class:
  71. $checkboxFilter = new CheckboxFilter();
  72. $checkboxFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
  73. break;
  74. case ChoiceType::class:
  75. $choiceFilter = new ChoiceFilter();
  76. $choiceFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
  77. break;
  78. case IntegerType::class:
  79. $integerFilter = new IntegerFilter();
  80. $integerFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
  81. break;
  82. case TextType::class:
  83. $textFilter = new TextFilter();
  84. $textFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
  85. break;
  86. case EntityType::class:
  87. $textFilter = new AssociationFilter();
  88. $textFilter->applyFilter($queryBuilder, $fieldDto->getProperty(), $filteredValue);
  89. break;
  90. case DateTimeType::class:
  91. case DateType::class:
  92. $textFilter = new DateFilter();
  93. $textFilter->applyFilter(
  94. $queryBuilder,
  95. $fieldDto->getProperty(),
  96. $filteredValue['dateStart'],
  97. $filteredValue['dateEnd']
  98. );
  99. break;
  100. }
  101. }
  102. public function getFilteredValue(
  103. Form $filtersForm,
  104. string $entityFqcn,
  105. string $field,
  106. string $dateExtraField = null
  107. ) {
  108. $field = $this->getFieldPropertySnake($field);
  109. $sessionParam = $entityFqcn.$field.$dateExtraField;
  110. $formField = $this->getFormField($filtersForm, $field, $dateExtraField);
  111. $value = $formField->getData();
  112. //Il existe une valeur posté dans le formulaire
  113. if ($value !== null) {
  114. $this->session->set($sessionParam, $value);
  115. return $value;
  116. }
  117. //action reset
  118. if ($filtersForm->get('reset')->getData() == 'clearAll') {
  119. $this->session->remove($sessionParam);
  120. return null;
  121. }
  122. //Récupération des valeurs stocké en sessions si le forrmFilters n'a pas été posté
  123. if ($this->session->get($sessionParam) && !$filtersForm->isSubmitted() && $formField) {
  124. $value = $this->session->get($sessionParam);
  125. //Champ date
  126. if ($formField->getConfig()->getOption('input') == 'datetime') {
  127. $filtersForm->get($field)->get($dateExtraField)->setData($value);
  128. //Champ association
  129. } elseif ($formField->getConfig()->getOption('class')) {
  130. $valFormated = $this->em->getRepository(
  131. $formField->getConfig()->getOption('class')
  132. )->find($value);
  133. $filtersForm->get($field)->setData($valFormated);
  134. } else {
  135. //Champ noramux
  136. $filtersForm->get($field)->setData($value);
  137. }
  138. return $value;
  139. }
  140. }
  141. public
  142. function getFormField(
  143. Form $filtersForm,
  144. string $field,
  145. string $extraField = null
  146. ): Form {
  147. if ($extraField) {
  148. return $filtersForm->get($field)->get($extraField);
  149. } else {
  150. return $filtersForm->get($field);
  151. }
  152. }
  153. }