Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

79 rindas
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, 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. }