Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

76 lines
2.8KB

  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, string $fieldProperty, $filteredValue = null)
  48. {
  49. if ($filteredValue !== null) {
  50. $repositoryQuery->andWhere('.' . $fieldProperty . ' = :' . $fieldProperty . '');
  51. $repositoryQuery->setParameter($fieldProperty, $filteredValue);
  52. /* //TODO Faut généraliser avec TreeInterface, ça ne doit pas être ici
  53. if ($field['type_options']['multiple']) {
  54. $repositoryQuery->andWhere(':' . $field['property'] . ' MEMBER OF entity.' . $field['property'] . '');
  55. } else {
  56. }
  57. if ($filter instanceof TreeInterface && $filter->getParent() == null) {
  58. $repositoryQuery->setParameter($field['property'], array_merge(array($filter), $filter->getChildrens()->toArray()));
  59. } else {
  60. }*/
  61. }
  62. }
  63. }