]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Event/Subscriber/CustomDoctrineORMSubscriber.php
Merge pull request #1524 from wallabag/sf2.8
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / CustomDoctrineORMSubscriber.php
CommitLineData
7d6c3edc
JB
1<?php
2
3namespace Wallabag\CoreBundle\Event\Subscriber;
4
7d6c3edc 5use Lexik\Bundle\FormFilterBundle\Event\GetFilterConditionEvent;
619cc453 6use Lexik\Bundle\FormFilterBundle\Event\Subscriber\DoctrineORMSubscriber;
7d6c3edc
JB
7use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9/**
10 * This custom class override the default behavior of LexikFilterBundle on `filter_date_range`
f90af145 11 * It converts a date_range to date_time_range to add hour to be able to grab a whole day (from 00:00:00 to 23:59:59).
7d6c3edc
JB
12 */
13class CustomDoctrineORMSubscriber extends DoctrineORMSubscriber implements EventSubscriberInterface
14{
15 /**
16 * @param GetFilterConditionEvent $event
17 */
18 public function filterDateRange(GetFilterConditionEvent $event)
19 {
f90af145 20 $expr = $event->getFilterQuery()->getExpressionBuilder();
7d6c3edc 21 $values = $event->getValues();
f90af145 22 $value = $values['value'];
7d6c3edc
JB
23
24 // left date should start at midnight
f90af145 25 if (isset($value['left_date'][0]) && $value['left_date'][0] instanceof \DateTime) {
7d6c3edc
JB
26 $value['left_date'][0]->setTime(0, 0, 0);
27 }
28
29 // right adte should end one second before midnight
f90af145 30 if (isset($value['right_date'][0]) && $value['right_date'][0] instanceof \DateTime) {
7d6c3edc
JB
31 $value['right_date'][0]->setTime(23, 59, 59);
32 }
33
34 if (isset($value['left_date'][0]) || isset($value['right_date'][0])) {
35 $event->setCondition($expr->dateTimeInRange($event->getField(), $value['left_date'][0], $value['right_date'][0]));
36 }
37 }
38}