]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #1393 from wallabag/fix-filter-same-day
authorNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 24 Aug 2015 08:39:24 +0000 (10:39 +0200)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 24 Aug 2015 08:39:24 +0000 (10:39 +0200)
Fix date filter on same day

app/config/services.yml
src/Wallabag/CoreBundle/Event/Subscriber/CustomDoctrineORMSubscriber.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Filter/EntryFilterType.php
src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php

index af22d3818cd7880c02c7ac8f67cbd610992b9242..b3ec7c513317e690f0899ab613e975fa4c2bd4c6 100644 (file)
@@ -4,6 +4,7 @@ parameters:
     security.authentication.provider.dao.class: Wallabag\CoreBundle\Security\Authentication\Provider\WallabagAuthenticationProvider
     security.encoder.digest.class: Wallabag\CoreBundle\Security\Authentication\Encoder\WallabagPasswordEncoder
     security.validator.user_password.class: Wallabag\CoreBundle\Security\Validator\WallabagUserPasswordValidator
+    lexik_form_filter.get_filter.doctrine_orm.class: Wallabag\CoreBundle\Event\Subscriber\CustomDoctrineORMSubscriber
 
 services:
     # used for tests
diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/CustomDoctrineORMSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/CustomDoctrineORMSubscriber.php
new file mode 100644 (file)
index 0000000..cfdbfe9
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace Wallabag\CoreBundle\Event\Subscriber;
+
+use Lexik\Bundle\FormFilterBundle\Event\Subscriber\DoctrineORMSubscriber;
+use Lexik\Bundle\FormFilterBundle\Event\GetFilterConditionEvent;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * This custom class override the default behavior of LexikFilterBundle on `filter_date_range`
+ * 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).
+ */
+class CustomDoctrineORMSubscriber extends DoctrineORMSubscriber implements EventSubscriberInterface
+{
+    /**
+     * @param GetFilterConditionEvent $event
+     */
+    public function filterDateRange(GetFilterConditionEvent $event)
+    {
+        $expr = $event->getFilterQuery()->getExpressionBuilder();
+        $values = $event->getValues();
+        $value = $values['value'];
+
+        // left date should start at midnight
+        if (isset($value['left_date'][0]) && $value['left_date'][0] instanceof \DateTime) {
+            $value['left_date'][0]->setTime(0, 0, 0);
+        }
+
+        // right adte should end one second before midnight
+        if (isset($value['right_date'][0]) && $value['right_date'][0] instanceof \DateTime) {
+            $value['right_date'][0]->setTime(23, 59, 59);
+        }
+
+        if (isset($value['left_date'][0]) || isset($value['right_date'][0])) {
+            $event->setCondition($expr->dateTimeInRange($event->getField(), $value['left_date'][0], $value['right_date'][0]));
+        }
+    }
+}
index ff51785b8411ca733def9bdc0cdfe12b84fd902e..85d1a0610c1e576fc40947bbf336c44695c0a922 100644 (file)
@@ -27,8 +27,9 @@ class EntryFilterType extends AbstractType
                         ),
                         'format' => 'dd/MM/yyyy',
                         'widget' => 'single_text',
-                ),
-            ))
+                    ),
+                )
+            )
             ->add('domainName', 'filter_text', array(
                 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
                         $value = $values['value'];
index 5f0a6076388535821460c9c5eb83f0561c11d032..a096628582cc4a6209e48ecc3f1f7e5a2dc57775 100644 (file)
@@ -278,6 +278,15 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $this->assertCount(5, $crawler->filter('div[class=entry]'));
 
+        $data = array(
+            'entry_filter[createdAt][left_date]' => date('d/m/Y'),
+            'entry_filter[createdAt][right_date]' => date('d/m/Y'),
+        );
+
+        $crawler = $client->submit($form, $data);
+
+        $this->assertCount(5, $crawler->filter('div[class=entry]'));
+
         $data = array(
             'entry_filter[createdAt][left_date]' => '01/01/1970',
             'entry_filter[createdAt][right_date]' => '01/01/1970',