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.

DateFilter.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(
  41. QueryBuilder $queryBuilder,
  42. string $fieldProperty,
  43. \DateTime $dateStart = null,
  44. \DateTime $dateEnd = null
  45. ) {
  46. if ($dateStart) {
  47. $queryBuilder->andWhere(
  48. 'entity.'.$fieldProperty.' >= :dateStart'
  49. )->setParameter('dateStart', $dateStart);
  50. }
  51. if ($dateEnd) {
  52. $dateEnd->setTime(23, 59, 59);
  53. $queryBuilder->andWhere(
  54. 'entity.'.$fieldProperty.' <= :dateEnd'
  55. )->setParameter('dateEnd', $dateEnd);
  56. }
  57. }
  58. }