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.

197 lines
7.1KB

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