]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Handle only upper or only lower reading filter
authorJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 17 Jun 2016 21:48:51 +0000 (23:48 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Thu, 23 Jun 2016 07:41:30 +0000 (09:41 +0200)
When we select only one value in the reading time filter, we need to perform a query with only one value (greater than OR lower than).

src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php

index bc59c9a1461ac86bb095246b78d1637fa671bf9f..9d63a8a92fa2ee2969072bd5a7950e3f1d51ef7c 100644 (file)
@@ -36,17 +36,26 @@ class EntryFilterType extends AbstractType
         $builder
             ->add('readingTime', NumberRangeFilterType::class, [
                 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
-                    $value = $values['value'];
+                    $lower = $values['value']['left_number'][0];
+                    $upper = $values['value']['right_number'][0];
+
+                    $min = (int) ($lower * $this->user->getConfig()->getReadingSpeed());
+                    $max = (int) ($upper * $this->user->getConfig()->getReadingSpeed());
 
-                    if (null === $value['left_number'][0] || null === $value['right_number'][0]) {
+                    if (null === $lower && null === $upper) {
+                        // no value? no filter
                         return;
+                    } else if (null === $lower && null !== $upper) {
+                        // only lower value is defined: query all entries with reading LOWER THAN this value
+                        $expression = $filterQuery->getExpr()->lte($field, $max);
+                    } else if (null !== $lower && null === $upper) {
+                        // only upper value is defined: query all entries with reading GREATER THAN this value
+                        $expression = $filterQuery->getExpr()->gte($field, $min);
+                    } else {
+                        // both value are defined, perform a between
+                        $expression = $filterQuery->getExpr()->between($field, $min, $max);
                     }
 
-                    $min = (int) ($value['left_number'][0] * $this->user->getConfig()->getReadingSpeed());
-                    $max = (int) ($value['right_number'][0] * $this->user->getConfig()->getReadingSpeed());
-
-                    $expression = $filterQuery->getExpr()->between($field, $min, $max);
-
                     return $filterQuery->createCondition($expression);
                 },
                 'label' => 'entry.filters.reading_time.label',