From 69ae896fbb05b2bf545f9653720c3dba2ded304f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Marcinkevi=C4=8Dius?= Date: Mon, 12 Sep 2016 09:51:45 +0300 Subject: [PATCH 1/5] added parameter to choice filters --- DependencyInjection/Configuration.php | 1 + .../Filter/ChoiceFilterFactory.php | 1 + Filter/Widget/Choice/SingleTermChoice.php | 21 +++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 6a3600bf..29d4c358 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -178,6 +178,7 @@ private function buildFilterTree($filterName) ->integerNode('size') ->info('Result size to return.') ->end() + ->booleanNode('show_zero_choices')->defaultFalse()->end() ->arrayNode('sort') ->children() ->enumNode('type') diff --git a/DependencyInjection/Filter/ChoiceFilterFactory.php b/DependencyInjection/Filter/ChoiceFilterFactory.php index c1c61b1c..53e2adb7 100644 --- a/DependencyInjection/Filter/ChoiceFilterFactory.php +++ b/DependencyInjection/Filter/ChoiceFilterFactory.php @@ -26,6 +26,7 @@ protected function configure(Definition $definition, array $configuration) parent::configure($definition, $configuration); $definition->addMethodCall('setField', [$configuration['field']]); + $definition->addMethodCall('setShowZeroChoices', [$configuration['show_zero_choices']]); if (isset($configuration['size'])) { $definition->addMethodCall('setSize', [$configuration['size']]); diff --git a/Filter/Widget/Choice/SingleTermChoice.php b/Filter/Widget/Choice/SingleTermChoice.php index 75562bae..747e7a73 100644 --- a/Filter/Widget/Choice/SingleTermChoice.php +++ b/Filter/Widget/Choice/SingleTermChoice.php @@ -38,6 +38,11 @@ class SingleTermChoice extends AbstractSingleRequestValueFilter implements Field */ private $sortType; + /** + * @var bool + */ + private $showZeroChoices; + /** * @param array $sortType */ @@ -54,6 +59,22 @@ public function getSortType() return $this->sortType; } + /** + * @param bool $showZeroChoices + */ + public function setShowZeroChoices($showZeroChoices) + { + $this->showZeroChoices = $showZeroChoices; + } + + /** + * @return bool + */ + public function getShowZeroChoices() + { + return $this->showZeroChoices; + } + /** * {@inheritdoc} */ From e6646ed2d6910cbe7f91b6ea60fba30703d84604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Marcinkevi=C4=8Dius?= Date: Mon, 12 Sep 2016 10:27:58 +0300 Subject: [PATCH 2/5] added zero choice functionality --- Filter/Widget/Choice/SingleTermChoice.php | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Filter/Widget/Choice/SingleTermChoice.php b/Filter/Widget/Choice/SingleTermChoice.php index 747e7a73..0f61d126 100644 --- a/Filter/Widget/Choice/SingleTermChoice.php +++ b/Filter/Widget/Choice/SingleTermChoice.php @@ -107,6 +107,12 @@ public function preProcessSearch(Search $search, Search $relatedSearch, FilterSt $filterAggregation->setFilter($relatedSearch->getPostFilters()); $filterAggregation->addAggregation($aggregation); $search->addAggregation($filterAggregation); + + if ($this->showZeroChoices) { + $unfilteredAggregation = clone $aggregation; + $unfilteredAggregation->setName($name . '-unfiltered'); + $search->addAggregation($unfilteredAggregation); + } } else { $search->addAggregation($aggregation); } @@ -128,6 +134,13 @@ public function getViewData(DocumentIterator $result, ViewData $data) /** @var ChoicesAwareViewData $data */ $unsortedChoices = []; + $zeroValueChoices = []; + + if ($this->showZeroChoices && $agg = $result->getAggregation($data->getName() . '-unfiltered')) { + foreach ($agg as $bucket) { + $zeroValueChoices[$bucket['key']] = $bucket['doc_count']; + } + } foreach ($this->fetchAggregation($result, $data->getName()) as $bucket) { $active = $this->isChoiceActive($bucket['key'], $data); @@ -141,6 +154,19 @@ public function getViewData(DocumentIterator $result, ViewData $data) $choice->setUrlParameters($this->getOptionUrlParameters($bucket['key'], $data)); } $unsortedChoices[$bucket['key']] = $choice; + + if (!empty($zeroValueChoices)) { + unset($zeroValueChoices[$bucket['key']]); + } + } + + foreach ($zeroValueChoices as $choiceLabel => $value) { + $choice = new ViewData\Choice(); + $choice->setLabel($choiceLabel); + $choice->setCount(0); + $choice->setActive(false); + $choice->setUrlParameters($data->getResetUrlParameters()); + $unsortedChoices[$choiceLabel] = $choice; } // Add the prioritized choices first. From e5184176c42643d7c8bef5c252d95ec2bd682184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Marcinkevi=C4=8Dius?= Date: Mon, 12 Sep 2016 13:13:39 +0300 Subject: [PATCH 3/5] added a test --- .../Widget/Choice/SingleTermChoiceTest.php | 35 +++++++++++++++++++ Tests/app/config/config_test.yml | 6 +++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Tests/Functional/Filter/Widget/Choice/SingleTermChoiceTest.php b/Tests/Functional/Filter/Widget/Choice/SingleTermChoiceTest.php index b22715ff..fd6a835e 100644 --- a/Tests/Functional/Filter/Widget/Choice/SingleTermChoiceTest.php +++ b/Tests/Functional/Filter/Widget/Choice/SingleTermChoiceTest.php @@ -34,66 +34,77 @@ protected function getDataArray() '_id' => 1, 'color' => 'red', 'manufacturer' => 'a', + 'sku' => 'foo', 'title' => 'm1', ], [ '_id' => 2, 'color' => 'blue', 'manufacturer' => 'a', + 'sku' => 'foo', 'title' => 'm2', ], [ '_id' => 3, 'color' => 'red', 'manufacturer' => 'b', + 'sku' => 'foo', 'title' => 'm3', ], [ '_id' => 4, 'color' => 'blue', 'manufacturer' => 'b', + 'sku' => 'foo', 'title' => 'm4', ], [ '_id' => 5, 'color' => 'green', 'manufacturer' => 'b', + 'sku' => 'acme', 'title' => 'm5', ], [ '_id' => 6, 'color' => 'blue', 'manufacturer' => 'a', + 'sku' => 'acme', 'title' => 'm6', ], [ '_id' => 7, 'color' => 'yellow', 'manufacturer' => 'a', + 'sku' => 'bar', 'title' => 'm7', ], [ '_id' => 8, 'color' => 'red', 'manufacturer' => 'a', + 'sku' => 'bar', 'title' => 'm8', ], [ '_id' => 9, 'color' => 'blue', 'manufacturer' => 'a', + 'sku' => 'bar', 'title' => 'm9', ], [ '_id' => 10, 'color' => 'red', 'manufacturer' => 'a', + 'sku' => 'foo', 'title' => 'm10', ], [ '_id' => 11, 'color' => 'blue', 'manufacturer' => 'a', + 'sku' => 'bar', 'title' => 'm11', ], ], @@ -220,4 +231,28 @@ public function testChoicesSize() $this->assertEquals(11, count($result->getChoices())); } + + /** + * Check if fetches choices that represent 0 documents + */ + public function testZeroChoicesSize() + { + /** @var ChoicesAwareViewData $result */ + $result = $this->getContainer()->get('ongr_filter_manager.foo_filters') + ->handleRequest(new Request(['single_choice' => 'red']))->getFilters()['zero_choices']; + + $expectedChoices = [ + 'foo' => 3, + 'bar' => 1, + 'acme' => 0, + ]; + + $actualChoices = []; + + foreach ($result->getChoices() as $choice) { + $actualChoices[$choice->getLabel()] = $choice->getCount(); + } + + $this->assertEquals($expectedChoices, $actualChoices); + } } diff --git a/Tests/app/config/config_test.yml b/Tests/app/config/config_test.yml index 4a46bd43..51a8521b 100644 --- a/Tests/app/config/config_test.yml +++ b/Tests/app/config/config_test.yml @@ -54,7 +54,7 @@ ongr_elasticsearch: ongr_filter_manager: managers: foo_filters: - filters: ['phrase', 'single_choice', 'foo_range'] + filters: ['phrase', 'single_choice', 'foo_range', 'zero_choices'] repository: 'es.manager.default.product' bar_filters: filters: ['sort', 'inclusive_range', 'bar_range'] @@ -105,6 +105,10 @@ ongr_filter_manager: order: asc priorities: - red + zero_choices: + request_field: 'zero' + field: 'sku' + show_zero_choices: true fuzzy: fuzzy: request_field: 'fuzzy' From 578932b818997102df9def2eb2ba8e5924ee1f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Marcinkevi=C4=8Dius?= Date: Mon, 12 Sep 2016 13:13:55 +0300 Subject: [PATCH 4/5] updated documentation --- Resources/doc/filter/choice.md | 1 + Resources/doc/filter/multi_choice.md | 1 + 2 files changed, 2 insertions(+) diff --git a/Resources/doc/filter/choice.md b/Resources/doc/filter/choice.md index 88c42698..9afa9b32 100644 --- a/Resources/doc/filter/choice.md +++ b/Resources/doc/filter/choice.md @@ -10,6 +10,7 @@ If you select one of the options, *choice filter* will return item list filtered | `request_field` | Request field used to view the selected page. (e.g. `www.page.com/?request_field=4`) | | `field` | Specifies the field in repository to apply this filter on. (e.g. `item_color`) | | `sort` | Sorts the choices based on your configuration. | +| `show_zero_choices` | Includes choices that have 0 documents in the choice array (defaults to `false`) | | `tags` | Array of filter specific tags that will be accessible at Twig view data. | Sorting configuration diff --git a/Resources/doc/filter/multi_choice.md b/Resources/doc/filter/multi_choice.md index e4e7daa8..0dedaf52 100644 --- a/Resources/doc/filter/multi_choice.md +++ b/Resources/doc/filter/multi_choice.md @@ -10,6 +10,7 @@ If you select one or more of the options, *multi choice filter* will return item |------------------------|--------------------------------------------------------------------------------------------------| | `request_field` | Request field used to view the selected page. (e.g. `www.page.com/?request_field=4`) | | `field` | Specifies the field in repository to apply this filter on. (e.g. `item_color`) | +| `show_zero_choices` | Includes choices that have 0 documents in the choice array (defaults to `false`) | | `sort` | Choices can also be sorted. You can read more about this [here](choice.md#sorting-configuration).| | `tags` | Array of filter specific tags that will be accessible at Twig view data. | From bc397d7e49233ca34be978e86a9fb0e995c397f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Marcinkevi=C4=8Dius?= Date: Tue, 20 Sep 2016 11:04:51 +0300 Subject: [PATCH 5/5] added a unit test for getter --- .../Filter/Widget/Choice/SingleTermChoiceTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Tests/Unit/Filter/Widget/Choice/SingleTermChoiceTest.php diff --git a/Tests/Unit/Filter/Widget/Choice/SingleTermChoiceTest.php b/Tests/Unit/Filter/Widget/Choice/SingleTermChoiceTest.php new file mode 100644 index 00000000..ffe584c6 --- /dev/null +++ b/Tests/Unit/Filter/Widget/Choice/SingleTermChoiceTest.php @@ -0,0 +1,15 @@ +setShowZeroChoices(true); + $this->assertTrue($filter->getShowZeroChoices()); + } +}