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.

179 lines
6.5KB

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