Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

68 lines
2.5KB

  1. <?php
  2. namespace Lc\SovBundle\Field\Filter;
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\ORM\QueryBuilder;
  5. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  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. $classImplements = class_implements($targetEntity);
  18. $builder->add(
  19. $fieldDto->getProperty(),
  20. EntityType::class,
  21. array(
  22. 'class' => $targetEntity,
  23. 'placeholder' => '--',
  24. 'query_builder' => function (EntityRepository $repo) use ($classImplements) {
  25. return $repo->createQueryBuilder('entity');
  26. },
  27. 'required' => false,
  28. 'attr' => array(
  29. 'class' => 'select2 input-sm',
  30. 'form' => 'filters-form',
  31. ),
  32. )
  33. );
  34. }
  35. public function applyFilter(QueryBuilder $queryBuilder, string $fieldProperty, string $filteredValue = null)
  36. {
  37. if ($filteredValue !== null) {
  38. $queryBuilder->andWhere('entity.'.$fieldProperty.' = :'.$fieldProperty.'');
  39. $queryBuilder->setParameter($fieldProperty, $filteredValue);
  40. /* //TODO Faut généraliser avec TreeInterface, ça ne doit pas être ici
  41. if ($field['property'] == 'productCategories') {
  42. $queryBuilder->andWhere(':' . $field['property'] . ' MEMBER OF entity.' . $field['property'] . ' OR product_categories.parent = :' . $field['property']);
  43. $queryBuilder->setParameter($field['property'], $filter);
  44. if ($field['type_options']['multiple']) {
  45. $queryBuilder->andWhere(':' . $field['property'] . ' MEMBER OF entity.' . $field['property'] . '');
  46. } else {
  47. }
  48. if ($filter instanceof TreeInterface && $filter->getParent() == null) {
  49. $queryBuilder->setParameter($field['property'], array_merge(array($filter), $filter->getChildrens()->toArray()));
  50. } else {
  51. }*/
  52. }
  53. }
  54. }