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.

TextFilter.php 1.9KB

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