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.

63 lines
2.0KB

  1. <?php
  2. namespace Lc\SovBundle\Field\Filter;
  3. use Doctrine\ORM\QueryBuilder;
  4. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  5. use Lc\SovBundle\Translation\TranslatorAdmin;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use function Symfony\Component\String\u;
  9. /**
  10. * @author La clic ! <contact@laclic.fr>
  11. */
  12. class CheckboxFilter
  13. {
  14. use FilterTrait;
  15. public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto, $options = array())
  16. {
  17. $builder->add($fieldDto->getProperty(), ChoiceType::class, array(
  18. 'choices'=> array(
  19. 'Oui' => 1,
  20. 'Non' => 0,
  21. ),
  22. 'placeholder'=> '--',
  23. 'required'=>false,
  24. 'attr'=>array(
  25. 'class'=> 'select2 input-sm',
  26. 'form'=> 'filters-form'
  27. )
  28. ));
  29. }
  30. public function applyFilter(QueryBuilder $queryBuilder, string $fieldProperty, string $filteredValue= null)
  31. {
  32. if ($filteredValue !== null) {
  33. if ($this->isRelationField($fieldProperty)) {
  34. $aliasRelation = $this->getFieldPropertyRelationAlias($fieldProperty);
  35. if (array_search($aliasRelation, $queryBuilder->getAllAliases()) === false) {
  36. $queryBuilder->innerJoin('entity.'.$aliasRelation, $aliasRelation);
  37. }
  38. $queryBuilder->andWhere(
  39. $fieldProperty.' = :'.$this->getFieldPropertySnake($fieldProperty).''
  40. );
  41. $queryBuilder->setParameter(
  42. $this->getFieldPropertySnake($fieldProperty),
  43. $filteredValue
  44. );
  45. } else {
  46. $queryBuilder->andWhere(
  47. 'entity.'.$fieldProperty.' = :'.$fieldProperty.''
  48. );
  49. $queryBuilder->setParameter($fieldProperty, $filteredValue);
  50. }
  51. }
  52. }
  53. }