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.

68 lines
2.4KB

  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. $targetEntity = $options['entity_dto']->getPropertyMetadata($fieldDto->getProperty())->get('targetEntity');
  17. if($fieldDto->getCustomOption('choices')){
  18. $choices = $fieldDto->getCustomOption('choices');
  19. }else if($fieldDto->getFormTypeOption('choices') !=null){
  20. $choices = $fieldDto->getFormTypeOption('choices');
  21. }else{
  22. $choices = array();
  23. }
  24. //todo utiliser choices plutot que query_builder voir ProductCategoriesFilter
  25. $builder->add(
  26. $fieldDto->getProperty(),
  27. EntityType::class,
  28. array(
  29. 'class' => $targetEntity,
  30. 'placeholder' => '--',
  31. 'choices' => $choices,
  32. 'required' => false,
  33. 'attr' => array(
  34. 'class' => 'select2 input-sm',
  35. 'form' => 'filters-form',
  36. ),
  37. )
  38. );
  39. }
  40. public function applyFilter(RepositoryQueryInterface $repositoryQuery, string $fieldProperty, $filteredValue = null)
  41. {
  42. if ($filteredValue !== null) {
  43. $repositoryQuery->andWhere('.'.$fieldProperty.' = :'.$fieldProperty.'');
  44. $repositoryQuery->setParameter($fieldProperty, $filteredValue);
  45. /* //TODO Faut généraliser avec TreeInterface, ça ne doit pas être ici
  46. if ($field['type_options']['multiple']) {
  47. $repositoryQuery->andWhere(':' . $field['property'] . ' MEMBER OF entity.' . $field['property'] . '');
  48. } else {
  49. }
  50. if ($filter instanceof TreeInterface && $filter->getParent() == null) {
  51. $repositoryQuery->setParameter($field['property'], array_merge(array($filter), $filter->getChildrens()->toArray()));
  52. } else {
  53. }*/
  54. }
  55. }
  56. }