1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ONGR\FilterManagerBundle\Filter\Widget\Dynamic; |
4
|
|
|
|
5
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\FilterAggregation; |
6
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\NestedAggregation; |
7
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
8
|
|
|
use ONGR\ElasticsearchDSL\Query\BoolQuery; |
9
|
|
|
use ONGR\ElasticsearchDSL\Query\MatchAllQuery; |
10
|
|
|
use ONGR\ElasticsearchDSL\Query\NestedQuery; |
11
|
|
|
use ONGR\ElasticsearchDSL\Query\TermQuery; |
12
|
|
|
use ONGR\ElasticsearchDSL\Query\TermsQuery; |
13
|
|
|
use ONGR\ElasticsearchDSL\Search; |
14
|
|
|
use ONGR\FilterManagerBundle\Filter\FilterState; |
15
|
|
|
use ONGR\FilterManagerBundle\Filter\ViewData; |
16
|
|
|
use ONGR\FilterManagerBundle\Search\SearchRequest; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
class MultiDynamicAggregate extends DynamicAggregate |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function getState(Request $request) |
25
|
|
|
{ |
26
|
|
|
$state = new FilterState(); |
27
|
|
|
$value = $request->get($this->getRequestField()); |
28
|
|
|
|
29
|
|
|
if (isset($value) && is_array($value) && reset($value) && is_array(reset($value))) { |
30
|
|
|
$state->setActive(true); |
31
|
|
|
$state->setValue($value); |
32
|
|
|
$state->setUrlParameters([$this->getRequestField() => $value]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $state; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null) |
42
|
|
|
{ |
43
|
|
|
if ($state && $state->isActive()) { |
44
|
|
|
$search->addPostFilter($this->getFilterQuery($state->getValue())); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* A method used to add an additional filter to the aggregations |
50
|
|
|
* in preProcessSearch |
51
|
|
|
* |
52
|
|
|
* @param FilterAggregation $filterAggregation |
53
|
|
|
* @param NestedAggregation $deepLevelAggregation |
54
|
|
|
* @param array $terms Terms of additional filter |
55
|
|
|
* @param string $aggName |
56
|
|
|
* |
57
|
|
|
* @return BuilderInterface |
58
|
|
|
*/ |
59
|
|
|
protected function addSubFilterAggregation( |
60
|
|
|
$filterAggregation, |
61
|
|
|
&$deepLevelAggregation, |
62
|
|
|
$terms, |
63
|
|
|
$aggName |
64
|
|
|
) { |
65
|
|
|
$boolQuery = $this->getFilterQuery($terms); |
66
|
|
|
|
67
|
|
|
if ($boolQuery->getQueries() == []) { |
68
|
|
|
$boolQuery->add(new MatchAllQuery()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$innerFilterAggregation = new FilterAggregation( |
|
|
|
|
72
|
|
|
$aggName, |
73
|
|
|
$boolQuery |
74
|
|
|
); |
75
|
|
|
$innerFilterAggregation->addAggregation($deepLevelAggregation); |
76
|
|
|
$filterAggregation->addAggregation($innerFilterAggregation); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $key |
81
|
|
|
* @param string $name |
82
|
|
|
* @param ViewData $data |
83
|
|
|
* @param bool $active True when the choice is active |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
protected function getOptionUrlParameters($key, $name, ViewData $data, $active) |
88
|
|
|
{ |
89
|
|
|
$value = $data->getState()->getValue(); |
90
|
|
|
$parameters = $data->getResetUrlParameters(); |
91
|
|
|
|
92
|
|
View Code Duplication |
if (!empty($value)) { |
|
|
|
|
93
|
|
|
if ($active) { |
94
|
|
|
unset($value[$name][array_search($key, $value[$name])]); |
95
|
|
|
$parameters[$this->getRequestField()] = $value; |
96
|
|
|
|
97
|
|
|
return $parameters; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$parameters[$this->getRequestField()] = $value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$parameters[$this->getRequestField()][$name][] = $key; |
104
|
|
|
|
105
|
|
|
return $parameters; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns whether choice with the specified key is active. |
110
|
|
|
* |
111
|
|
|
* @param string $key |
112
|
|
|
* @param ViewData $data |
113
|
|
|
* @param string $activeName |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
protected function isChoiceActive($key, ViewData $data, $activeName) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if ($data->getState()->isActive()) { |
120
|
|
|
$value = $data->getState()->getValue(); |
121
|
|
|
|
122
|
|
|
if (isset($value[$activeName]) && in_array($key, $value[$activeName])) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $terms |
132
|
|
|
* |
133
|
|
|
* @return BoolQuery |
134
|
|
|
*/ |
135
|
|
|
private function getFilterQuery($terms) |
136
|
|
|
{ |
137
|
|
|
list($path, $field) = explode('>', $this->getDocumentField()); |
138
|
|
|
$boolQuery = new BoolQuery(); |
139
|
|
|
|
140
|
|
|
foreach ($terms as $groupName => $values) { |
141
|
|
|
$innerBoolQuery = new BoolQuery(); |
142
|
|
|
|
143
|
|
View Code Duplication |
foreach ($values as $value) { |
|
|
|
|
144
|
|
|
$nestedBoolQuery = new BoolQuery(); |
145
|
|
|
$nestedBoolQuery->add(new TermQuery($field, $value)); |
146
|
|
|
$nestedBoolQuery->add(new TermQuery($this->getNameField(), $groupName)); |
147
|
|
|
$innerBoolQuery->add( |
148
|
|
|
new NestedQuery( |
149
|
|
|
$path, |
150
|
|
|
$nestedBoolQuery |
151
|
|
|
), |
152
|
|
|
BoolQuery::SHOULD |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$boolQuery->add($innerBoolQuery); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $boolQuery; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.