-
Notifications
You must be signed in to change notification settings - Fork 43
/
SingleTermChoice.php
218 lines (190 loc) · 6.12 KB
/
SingleTermChoice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\FilterManagerBundle\Filters\Widget\Choice;
use ONGR\ElasticsearchBundle\DSL\Aggregation\FilterAggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation;
use ONGR\ElasticsearchBundle\DSL\Filter\TermFilter;
use ONGR\ElasticsearchBundle\DSL\Search;
use ONGR\ElasticsearchBundle\Result\Aggregation\ValueAggregation;
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
use ONGR\FilterManagerBundle\Filters\FilterState;
use ONGR\FilterManagerBundle\Filters\Helper\ViewDataFactoryInterface;
use ONGR\FilterManagerBundle\Filters\ViewData\ChoicesAwareViewData;
use ONGR\FilterManagerBundle\Filters\ViewData;
use ONGR\FilterManagerBundle\Filters\Widget\AbstractSingleRequestValueFilter;
use ONGR\FilterManagerBundle\Filters\Helper\FieldAwareInterface;
use ONGR\FilterManagerBundle\Filters\Helper\FieldAwareTrait;
use ONGR\FilterManagerBundle\Search\SearchRequest;
/**
* This class provides single terms choice.
*/
class SingleTermChoice extends AbstractSingleRequestValueFilter implements FieldAwareInterface, ViewDataFactoryInterface
{
use FieldAwareTrait;
/**
* @var array
*/
private $sortType;
/**
* @param array $sortType
*/
public function setSortType($sortType)
{
$this->sortType = $sortType;
}
/**
* @return array
*/
public function getSortType()
{
return $this->sortType;
}
/**
* {@inheritdoc}
*/
public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
{
if ($state && $state->isActive()) {
$search->addPostFilter(new TermFilter($this->getField(), $state->getValue()));
}
}
/**
* {@inheritdoc}
*/
public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
{
$name = $state ? $state->getName() : $this->getField();
$agg = new TermsAggregation($name);
$agg->setField($this->getField());
if ($this->getSortType()) {
$agg->setOrder($this->sortType['type'], $this->sortType['order']);
}
if ($relatedSearch->getPostFilters() && $relatedSearch->getPostFilters()->isRelevant()) {
$filterAgg = new FilterAggregation($name . '-filter');
$filterAgg->setFilter($relatedSearch->getPostFilters());
$filterAgg->addAggregation($agg);
$search->addAggregation($filterAgg);
} else {
$search->addAggregation($agg);
}
}
/**
* {@inheritdoc}
*/
public function createViewData()
{
return new ChoicesAwareViewData();
}
/**
* {@inheritdoc}
*/
public function getViewData(DocumentIterator $result, ViewData $data)
{
/** @var ChoicesAwareViewData $data */
$unsortedChoices = [];
/** @var ValueAggregation $bucket */
foreach ($this->fetchAggregation($result, $data->getName()) as $bucket) {
$bucket = $bucket->getValue();
$active = $this->isChoiceActive($bucket['key'], $data);
$choice = new ViewData\Choice();
$choice->setLabel($bucket['key']);
$choice->setCount($bucket['doc_count']);
$choice->setActive($active);
if ($active) {
$choice->setUrlParameters($this->getUnsetUrlParameters($bucket['key'], $data));
} else {
$choice->setUrlParameters($this->getOptionUrlParameters($bucket['key'], $data));
}
$unsortedChoices[$bucket['key']] = $choice;
}
// Add the prioritized choices first.
if ($this->getSortType()) {
$unsortedChoices = $this->addPriorityChoices($unsortedChoices, $data);
}
foreach ($unsortedChoices as $choice) {
$data->addChoice($choice);
}
return $data;
}
/**
* Adds prioritized choices.
*
* @param array $unsortedChoices
* @param ChoicesAwareViewData $data
*
* @return array
*/
protected function addPriorityChoices(array $unsortedChoices, ChoicesAwareViewData $data)
{
foreach ($this->getSortType()['priorities'] as $name) {
if (array_key_exists($name, $unsortedChoices)) {
$data->addChoice($unsortedChoices[$name]);
unset($unsortedChoices[$name]);
}
}
return $unsortedChoices;
}
/**
* Fetches buckets from search results.
*
* @param DocumentIterator $result Search results.
* @param string $name Filter name.
*
* @return array Buckets
*/
protected function fetchAggregation(DocumentIterator $result, $name)
{
$aggregations = $result->getAggregations();
if (isset($aggregations[$name])) {
return $aggregations[$name];
}
$buckets = $aggregations->find(sprintf('%s-filter.%s', $name, $name));
if (isset($buckets)) {
return $buckets;
}
return [];
}
/**
* @param string $key
* @param ViewData $data
*
* @return array
*/
protected function getOptionUrlParameters($key, ViewData $data)
{
$parameters = $data->getResetUrlParameters();
$parameters[$this->getRequestField()] = $key;
return $parameters;
}
/**
* Returns url with selected term disabled.
*
* @param string $key
* @param ViewData $data
*
* @return array
*/
protected function getUnsetUrlParameters($key, ViewData $data)
{
return $data->getResetUrlParameters();
}
/**
* Returns whether choice with the specified key is active.
*
* @param string $key
* @param ViewData $data
*
* @return bool
*/
protected function isChoiceActive($key, ViewData $data)
{
return $data->getState()->isActive() && $data->getState()->getValue() == $key;
}
}