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.

63 line
1.8KB

  1. <?php
  2. namespace Lc\SovBundle\Field\Filter;
  3. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  4. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  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(RepositoryQueryInterface $repositoryQuery, FieldDto $fieldDto, \DateTime $dateStart = null, \DateTime $dateEnd = null)
  41. {
  42. $fieldProperty = $this->getFieldProperty($fieldDto);
  43. if ($dateStart) {
  44. $repositoryQuery->andWhere(
  45. '.' . $fieldProperty . ' >= :dateStart'
  46. )->setParameter('dateStart', $dateStart);
  47. }
  48. if ($dateEnd) {
  49. $dateEnd->setTime(23, 59, 59);
  50. $repositoryQuery->andWhere(
  51. '.' . $fieldProperty . ' <= :dateEnd'
  52. )->setParameter('dateEnd', $dateEnd);
  53. }
  54. }
  55. }