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.

61 lines
1.8KB

  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\DateType;
  6. use Symfony\Component\Form\Extension\Core\Type\FormType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. /**
  9. * @author La clic ! <contact@laclic.fr>
  10. */
  11. class DateFilter
  12. {
  13. use FilterTrait;
  14. public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto, $options = array())
  15. {
  16. $builder->add(
  17. $builder->create(
  18. str_replace('.', '_', $fieldDto->getProperty()),
  19. FormType::class,
  20. array('inherit_data' => true)
  21. )
  22. ->add(
  23. 'dateStart',
  24. DateType::class,
  25. array(
  26. 'widget' => 'single_text',
  27. 'required' => false,
  28. )
  29. )
  30. ->add(
  31. 'dateEnd',
  32. DateType::class,
  33. array(
  34. 'widget' => 'single_text',
  35. 'required' => false,
  36. )
  37. )
  38. );
  39. }
  40. public function applyFilter(QueryBuilder $queryBuilder, string $fieldProperty, \DateTime $dateStart = null, \DateTime $dateEnd = null)
  41. {
  42. if ($dateStart) {
  43. $queryBuilder->andWhere(
  44. 'entity.' . $fieldProperty . ' >= :dateStart'
  45. )->setParameter('dateStart', $dateStart);
  46. }
  47. if ($dateEnd) {
  48. $dateEnd->setTime(23, 59, 59);
  49. $queryBuilder->andWhere(
  50. 'entity.' . $fieldProperty . ' <= :dateEnd'
  51. )->setParameter('dateEnd', $dateEnd);
  52. }
  53. }
  54. }