1 | <?php |
||
36 | class Facet |
||
37 | { |
||
38 | const TYPE_FIELD = 'field'; |
||
39 | const TYPE_QUERY = 'query'; |
||
40 | const TYPE_RANGE = 'range'; |
||
41 | |||
42 | /** |
||
43 | * @var Search |
||
44 | */ |
||
45 | protected $search; |
||
46 | |||
47 | /** |
||
48 | * The facet's name as configured on TypoScript |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $name; |
||
53 | |||
54 | /** |
||
55 | * Facet type, defaults to field facet. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $type = self::TYPE_FIELD; |
||
60 | |||
61 | /** |
||
62 | * The index field the facet is built from. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $field; |
||
67 | |||
68 | /** |
||
69 | * Facet configuration |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $configuration; |
||
74 | |||
75 | /** |
||
76 | * Constructor. |
||
77 | * |
||
78 | * @param string $facetName The facet's name |
||
79 | * @param string $facetType The facet's internal type. field, range, or query |
||
80 | */ |
||
81 | 18 | public function __construct($facetName, $facetType = self::TYPE_FIELD) |
|
89 | |||
90 | /** |
||
91 | * Initializes/loads the facet configuration |
||
92 | * |
||
93 | */ |
||
94 | 18 | protected function initializeConfiguration() |
|
101 | |||
102 | /** |
||
103 | * Checks whether an option of the facet has been selected by the user by |
||
104 | * checking the URL GET parameters. |
||
105 | * |
||
106 | * @return bool TRUE if any option of the facet is applied, FALSE otherwise |
||
107 | */ |
||
108 | 18 | public function isActive() |
|
109 | { |
||
110 | 18 | $isActive = false; |
|
111 | |||
112 | 18 | $selectedOptions = $this->getSelectedOptions(); |
|
113 | 18 | if (!empty($selectedOptions)) { |
|
114 | 1 | $isActive = true; |
|
115 | } |
||
116 | |||
117 | 18 | return $isActive; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Gets the facet's currently user-selected options |
||
122 | * |
||
123 | * @return array An array with user-selected facet options. |
||
124 | */ |
||
125 | 18 | public function getSelectedOptions() |
|
126 | { |
||
127 | 18 | $selectedOptions = array(); |
|
128 | |||
129 | 18 | $resultParameters = GeneralUtility::_GET('tx_solr'); |
|
130 | 18 | $filterParameters = array(); |
|
131 | 18 | if (isset($resultParameters['filter'])) { |
|
132 | 1 | $filterParameters = (array)array_map('urldecode', |
|
133 | 1 | $resultParameters['filter']); |
|
134 | } |
||
135 | |||
136 | 18 | foreach ($filterParameters as $filter) { |
|
137 | 1 | list($facetName, $filterValue) = explode(':', $filter); |
|
138 | |||
139 | 1 | if ($facetName == $this->name) { |
|
140 | 1 | $selectedOptions[] = $filterValue; |
|
141 | } |
||
142 | } |
||
143 | |||
144 | 18 | return $selectedOptions; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Determines if a facet has any options. |
||
149 | * |
||
150 | * @return bool TRUE if no facet options are given, FALSE if facet options are given |
||
151 | */ |
||
152 | 18 | public function isEmpty() |
|
153 | { |
||
154 | 18 | $isEmpty = false; |
|
155 | |||
156 | 18 | $options = $this->getOptionsRaw(); |
|
157 | 18 | $optionsCount = count($options); |
|
158 | |||
159 | // facet options include '_empty_', if no options are given |
||
160 | 18 | if ($optionsCount == 0 |
|
161 | 18 | || ($optionsCount == 1 && array_key_exists('_empty_', $options)) |
|
162 | ) { |
||
163 | 1 | $isEmpty = true; |
|
164 | } |
||
165 | |||
166 | 18 | return $isEmpty; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Checks whether requirements are fulfilled |
||
171 | * |
||
172 | * @return bool TRUE if conditions required to render this facet are met, FALSE otherwise |
||
173 | */ |
||
174 | 18 | public function isRenderingAllowed() |
|
175 | { |
||
176 | 18 | $renderingAllowed = true; |
|
177 | |||
178 | 18 | $requirements = $this->getRequirements(); |
|
179 | 18 | foreach ($requirements as $requirement) { |
|
180 | if (!$this->isRequirementMet($requirement)) { |
||
181 | $renderingAllowed = false; |
||
182 | break; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | 18 | return $renderingAllowed; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Gets the configured requirements to allow rendering of the facet. |
||
191 | * |
||
192 | * @return array Requirements with keys "name", "facet", and "value". |
||
193 | */ |
||
194 | 18 | protected function getRequirements() |
|
211 | |||
212 | /** |
||
213 | * Evaluates a single facet rendering requirement. |
||
214 | * |
||
215 | * @param array $requirement A requirement with keys "name", "facet", and "value". |
||
216 | * @return bool TRUE if the requirement is met, FALSE otherwise. |
||
217 | */ |
||
218 | protected function isRequirementMet(array $requirement) |
||
242 | |||
243 | /** |
||
244 | * Gets the facet's options |
||
245 | * |
||
246 | * @return array An array with facet options. |
||
247 | */ |
||
248 | 18 | public function getOptionsRaw() |
|
266 | |||
267 | /** |
||
268 | * Gets the number of options for a facet. |
||
269 | * |
||
270 | * @return int Number of facet options for the current facet. |
||
271 | */ |
||
272 | 18 | public function getOptionsCount() |
|
278 | |||
279 | /** |
||
280 | * Gets the facet's name |
||
281 | * |
||
282 | * @return string The facet's name |
||
283 | */ |
||
284 | 18 | public function getName() |
|
288 | |||
289 | /** |
||
290 | * Gets the field name the facet is operating on. |
||
291 | * |
||
292 | * @return string The name of the field the facet is operating on. |
||
293 | */ |
||
294 | public function getField() |
||
298 | |||
299 | /** |
||
300 | * Gets the facet's configuration. |
||
301 | * |
||
302 | * @return array The facet's configuration as an array. |
||
303 | */ |
||
304 | public function getConfiguration() |
||
308 | |||
309 | /** |
||
310 | * Gets the facet's internal type. One of field, range, or query. |
||
311 | * |
||
312 | * @return string Facet type. |
||
313 | */ |
||
314 | public function getType() |
||
318 | } |
||
319 |