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.

AssociationFilter.php 2.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Lc\SovBundle\Field\Filter;
  3. use Doctrine\ORM\EntityRepository;
  4. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  5. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  6. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. /**
  9. * @author La clic ! <contact@laclic.fr>
  10. */
  11. class AssociationFilter
  12. {
  13. use FilterTrait;
  14. public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto, $options = array())
  15. {
  16. $param = array();
  17. $targetEntity = $options['entity_dto']->getPropertyMetadata($fieldDto->getProperty())->get('targetEntity');
  18. if ($fieldDto->getCustomOption('choices')) {
  19. $choices = $fieldDto->getCustomOption('choices');
  20. } elseif ($fieldDto->getFormTypeOption('choices') != null) {
  21. $choices = $fieldDto->getFormTypeOption('choices');
  22. } else {
  23. $choices = array();
  24. }
  25. if ($fieldDto->getFormTypeOption('choice_label') != null) {
  26. $param['choice_label'] = $fieldDto->getFormTypeOption('choice_label');
  27. }
  28. //todo utiliser choices plutot que query_builder voir ProductCategoriesFilter
  29. $builder->add(
  30. $fieldDto->getProperty(),
  31. EntityType::class,
  32. array_merge(
  33. $param,
  34. array(
  35. 'class' => $targetEntity,
  36. 'placeholder' => '--',
  37. 'choices' => $choices,
  38. 'required' => false,
  39. 'attr' => array(
  40. 'class' => 'select2 input-sm',
  41. 'form' => 'filters-form',
  42. ),
  43. )
  44. )
  45. );
  46. }
  47. public function applyFilter(RepositoryQueryInterface $repositoryQuery, FieldDto $fieldDto, $filteredValue = null)
  48. {
  49. $fieldProperty = $this->getFieldProperty($fieldDto);
  50. if ($filteredValue !== null) {
  51. if ($fieldDto->getFormTypeOption('multiple') || ($fieldDto->getTemplatePath() != null && str_contains($fieldDto->getTemplatePath(), 'association_many'))) {
  52. $repositoryQuery->andWhere(':' . $fieldProperty . ' MEMBER OF .' . $fieldProperty . '');
  53. } else {
  54. $repositoryQuery->andWhere('.' . $fieldProperty . ' = :' . $fieldProperty . '');
  55. }
  56. $repositoryQuery->setParameter($fieldProperty, $filteredValue);
  57. //
  58. // if ($filter instanceof TreeInterface && $filter->getParent() == null) {
  59. // $repositoryQuery->setParameter($field['property'], array_merge(array($filter), $filter->getChildrens()->toArray()));
  60. // } else {
  61. // }
  62. }
  63. }
  64. }