Skip to content

Commit

Permalink
Merge pull request #190 from einorler/null_choices
Browse files Browse the repository at this point in the history
Show zero choices
  • Loading branch information
saimaz authored Sep 20, 2016
2 parents 3a2e0f9 + bc397d7 commit acc7aa1
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ private function buildFilterTree($filterName)
->integerNode('size')
->info('Result size to return.')
->end()
->booleanNode('show_zero_choices')->defaultFalse()->end()
->arrayNode('sort')
->children()
->enumNode('type')
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Filter/ChoiceFilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]);
Expand Down
47 changes: 47 additions & 0 deletions Filter/Widget/Choice/SingleTermChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class SingleTermChoice extends AbstractSingleRequestValueFilter implements Field
*/
private $sortType;

/**
* @var bool
*/
private $showZeroChoices;

/**
* @param array $sortType
*/
Expand All @@ -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}
*/
Expand Down Expand Up @@ -86,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);
}
Expand All @@ -107,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);
Expand All @@ -120,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.
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/filter/choice.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/filter/multi_choice.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down
35 changes: 35 additions & 0 deletions Tests/Functional/Filter/Widget/Choice/SingleTermChoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
Expand Down Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions Tests/Unit/Filter/Widget/Choice/SingleTermChoiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ONGR\FilterManagerBundle\Tests\Unit\Filter\Widget\Choice;

use ONGR\FilterManagerBundle\Filter\Widget\Choice\SingleTermChoice;

class SingleTermChoiceTest extends \PHPUnit_Framework_TestCase
{
public function testShowZeroChoicesGetterAndSetter()
{
$filter = new SingleTermChoice();
$filter->setShowZeroChoices(true);
$this->assertTrue($filter->getShowZeroChoices());
}
}
6 changes: 5 additions & 1 deletion Tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -108,6 +108,10 @@ ongr_filter_manager:
order: asc
priorities:
- red
zero_choices:
request_field: 'zero'
field: 'sku'
show_zero_choices: true
fuzzy:
fuzzy:
request_field: 'fuzzy'
Expand Down

0 comments on commit acc7aa1

Please sign in to comment.