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.

65 lines
1.9KB

  1. <?php
  2. namespace Lc\SovBundle\Field\Filter;
  3. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  4. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  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 ChoiceFilter
  13. {
  14. use FilterTrait;
  15. protected $translatorAdmin;
  16. public function __construct(?TranslatorAdmin $translatorAdmin = null)
  17. {
  18. $this->translatorAdmin = $translatorAdmin;
  19. }
  20. public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto, $options = array())
  21. {
  22. if($fieldDto->getCustomOption('choices')){
  23. $choices = $fieldDto->getCustomOption('choices');
  24. }else if($fieldDto->getFormTypeOption('choices') !=null){
  25. $choices = $fieldDto->getFormTypeOption('choices');
  26. }else{
  27. $choices = array();
  28. }
  29. $builder->add(
  30. $fieldDto->getProperty(),
  31. ChoiceType::class,
  32. array(
  33. 'required' => false,
  34. 'choices' => $choices,
  35. 'attr' => array(
  36. 'class' => 'select2 input-sm',
  37. 'form' => 'filters-form',
  38. ),
  39. )
  40. );
  41. }
  42. public function applyFilter(RepositoryQueryInterface $repositoryQuery , FieldDto $fieldDto, string $filteredValue = null)
  43. {
  44. $fieldProperty = $this->getFieldProperty($fieldDto);
  45. if ($filteredValue !== null) {
  46. $repositoryQuery->andWhere(
  47. '.'.$fieldProperty.' LIKE :'.$fieldProperty.''
  48. );
  49. $repositoryQuery->setParameter($fieldProperty, $filteredValue);
  50. }
  51. }
  52. }