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.

78 lines
2.6KB

  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. // if ($this->isRelationField($fieldProperty)) {
  47. // $aliasRelation = $this->getFieldPropertyRelationAlias($fieldProperty);
  48. // if (array_search($aliasRelation, $repositoryQuery->getAllAliases()) === false) {
  49. // $repositoryQuery->innerJoin('entity.'.$aliasRelation, $aliasRelation);
  50. // }
  51. // $repositoryQuery->andWhere(
  52. // $fieldProperty.' LIKE :'.$this->getFieldPropertySnake($fieldProperty).''
  53. // );
  54. // $repositoryQuery->setParameter(
  55. // $this->getFieldPropertySnake($fieldProperty),
  56. // '%'.$filteredValue.'%'
  57. // );
  58. // } else {
  59. $repositoryQuery->andWhere(
  60. '.'.$fieldProperty.' LIKE :'.$fieldProperty.''
  61. );
  62. $repositoryQuery->setParameter($fieldProperty, '%'.$filteredValue.'%');
  63. }
  64. }
  65. }