Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
46.90% |
106 / 226 |
|
43.75% |
14 / 32 |
CRAP | |
0.00% |
0 / 1 |
Options | |
46.90% |
106 / 226 |
|
43.75% |
14 / 32 |
1416.74 | |
0.00% |
0 / 1 |
__construct | |
71.43% |
15 / 21 |
|
0.00% |
0 / 1 |
4.37 | |||
getAdvancedHandlers | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getBasicHandlers | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultHandler | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getSortOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultLimit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSearchMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setSearchMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSearchAction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getView | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getModeOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDefaultMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEdsView | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getDefaultExpanders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAdvancedSearchAction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setOptionsFromApi | |
90.62% |
29 / 32 |
|
0.00% |
0 / 1 |
8.05 | |||
filterAndReorderProperty | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
setCommonSettings | |
12.50% |
1 / 8 |
|
0.00% |
0 / 1 |
21.75 | |||
setOptionsFromConfig | |
42.11% |
8 / 19 |
|
0.00% |
0 / 1 |
24.72 | |||
mapSortLabel | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
7.46 | |||
populateSearchCriteria | |
34.69% |
17 / 49 |
|
0.00% |
0 / 1 |
108.24 | |||
populateLimiterValues | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
getApiProperty | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getAvailableLimiters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAdvancedLimiters | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
getAvailableExpanders | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
populateViewSettings | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getLabelForCheckboxFilter | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getSearchScreenLimiters | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
getSearchScreenExpanders | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
getDefaultView | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultFilters | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
8.30 |
1 | <?php |
2 | |
3 | /** |
4 | * EDS API Options |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) EBSCO Industries 2013 |
9 | * Copyright (C) The National Library of Finland 2022 |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License version 2, |
13 | * as published by the Free Software Foundation. |
14 | * |
15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with this program; if not, write to the Free Software |
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | * |
24 | * @category VuFind |
25 | * @package EBSCO |
26 | * @author Michelle Milton <mmilton@epnet.com> |
27 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
28 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
29 | * @link https://vufind.org Main Page |
30 | */ |
31 | |
32 | namespace VuFind\Search\EDS; |
33 | |
34 | use function count; |
35 | use function in_array; |
36 | use function is_callable; |
37 | |
38 | /** |
39 | * EDS API Options |
40 | * |
41 | * @category VuFind |
42 | * @package EBSCO |
43 | * @author Michelle Milton <mmilton@epnet.com> |
44 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://vufind.org Main Page |
47 | */ |
48 | class Options extends \VuFind\Search\Base\Options |
49 | { |
50 | use \VuFind\Config\Feature\ExplodeSettingTrait; |
51 | |
52 | /** |
53 | * Default limit option |
54 | * |
55 | * @var ?int |
56 | */ |
57 | protected $defaultLimit = null; |
58 | |
59 | /** |
60 | * Default view option |
61 | * |
62 | * @var ?string |
63 | */ |
64 | protected $defaultView = null; |
65 | |
66 | /** |
67 | * Available search mode options |
68 | * |
69 | * @var array |
70 | */ |
71 | protected $modeOptions = []; |
72 | |
73 | /** |
74 | * Default search mode options |
75 | * |
76 | * @var string |
77 | */ |
78 | protected $defaultMode = 'all'; |
79 | |
80 | /** |
81 | * The set search mode |
82 | * |
83 | * @var string |
84 | */ |
85 | protected $searchMode; |
86 | |
87 | /** |
88 | * Default expanders to apply |
89 | * |
90 | * @var array |
91 | */ |
92 | protected $defaultExpanders = []; |
93 | |
94 | /** |
95 | * Available expander options |
96 | * |
97 | * @var array |
98 | */ |
99 | protected $expanderOptions = []; |
100 | |
101 | /** |
102 | * Available limiter options |
103 | * |
104 | * @var array |
105 | */ |
106 | protected $limiterOptions = []; |
107 | |
108 | /** |
109 | * Limiters enabled on advanced search screen (empty for all available) |
110 | * |
111 | * @var string[] |
112 | */ |
113 | protected $advancedLimiters = []; |
114 | |
115 | /** |
116 | * Available Search Options from the API or null if not yet initialized |
117 | * |
118 | * @var ?array |
119 | */ |
120 | protected $apiInfo; |
121 | |
122 | /** |
123 | * Callback to get available Search Options from the API |
124 | * |
125 | * @var ?callable |
126 | */ |
127 | protected $apiInfoCallback = null; |
128 | |
129 | /** |
130 | * Whether settings based on API info have been initialized |
131 | * |
132 | * @var bool |
133 | */ |
134 | protected $apiOptionsInitialized = false; |
135 | |
136 | /** |
137 | * Limiters to display on the basic search screen |
138 | * |
139 | * @var array |
140 | */ |
141 | protected $commonLimiters = []; |
142 | |
143 | /** |
144 | * Expanders to display on the basic search screen |
145 | * |
146 | * @var array |
147 | */ |
148 | protected $commonExpanders = []; |
149 | |
150 | /** |
151 | * Search configuration |
152 | * |
153 | * @var \Laminas\Config\Config |
154 | */ |
155 | protected $searchSettings; |
156 | |
157 | /** |
158 | * Constructor |
159 | * |
160 | * @param \VuFind\Config\PluginManager $configLoader Configuration loader |
161 | * @param array|callable $apiInfo API information or callback |
162 | * to retrieve it |
163 | */ |
164 | public function __construct( |
165 | \VuFind\Config\PluginManager $configLoader, |
166 | $apiInfo = null |
167 | ) { |
168 | $this->searchIni = $this->facetsIni = 'EDS'; |
169 | $this->searchSettings = $configLoader->get($this->searchIni); |
170 | parent::__construct($configLoader); |
171 | // 2015-06-30 RF - Changed to unlimited |
172 | //$this->resultLimit = 100; |
173 | $this->viewOptions = [ |
174 | 'list|title' => 'Title View', |
175 | 'list|brief' => 'Brief View', |
176 | 'list|detailed' => 'Detailed View', |
177 | ]; |
178 | // If we get the API info as a callback, defer until it's actually needed to |
179 | // avoid calling the API: |
180 | if (is_callable($apiInfo)) { |
181 | $this->apiInfo = null; |
182 | $this->apiInfoCallback = $apiInfo; |
183 | } else { |
184 | $this->apiInfo = $apiInfo ?? []; |
185 | $this->setOptionsFromApi(); |
186 | } |
187 | $this->setOptionsFromConfig(); |
188 | $facetConf = $configLoader->get($this->facetsIni); |
189 | if ( |
190 | isset($facetConf->Advanced_Facet_Settings->translated_facets) |
191 | && count($facetConf->Advanced_Facet_Settings->translated_facets) > 0 |
192 | ) { |
193 | $this->setTranslatedFacets( |
194 | $facetConf->Advanced_Facet_Settings->translated_facets->toArray() |
195 | ); |
196 | } |
197 | // Make sure first-last navigation is never enabled since we cannot support: |
198 | $this->firstLastNavigationSupported = false; |
199 | } |
200 | |
201 | /** |
202 | * Basic 'getter' for advanced search handlers. |
203 | * |
204 | * @return array |
205 | */ |
206 | public function getAdvancedHandlers() |
207 | { |
208 | return $this->getApiProperty('advancedHandlers'); |
209 | } |
210 | |
211 | /** |
212 | * Basic 'getter' for basic search handlers. |
213 | * |
214 | * @return array |
215 | */ |
216 | public function getBasicHandlers() |
217 | { |
218 | return $this->getApiProperty('basicHandlers'); |
219 | } |
220 | |
221 | /** |
222 | * Get default search handler. |
223 | * |
224 | * @return string |
225 | */ |
226 | public function getDefaultHandler() |
227 | { |
228 | $this->setOptionsFromApi(); |
229 | return parent::getDefaultHandler(); |
230 | } |
231 | |
232 | /** |
233 | * Get an array of sort options. |
234 | * |
235 | * @return array |
236 | */ |
237 | public function getSortOptions() |
238 | { |
239 | return $this->getApiProperty('sortOptions'); |
240 | } |
241 | |
242 | /** |
243 | * Get default limit setting. |
244 | * |
245 | * @return int |
246 | */ |
247 | public function getDefaultLimit() |
248 | { |
249 | return $this->getApiProperty('defaultLimit'); |
250 | } |
251 | |
252 | /** |
253 | * Obtain the set searchmode |
254 | * |
255 | * @return string the search mode |
256 | */ |
257 | public function getSearchMode() |
258 | { |
259 | return $this->searchMode; |
260 | } |
261 | |
262 | /** |
263 | * Set the search mode |
264 | * |
265 | * @param string $mode Mode |
266 | * |
267 | * @return void |
268 | */ |
269 | public function setSearchMode($mode) |
270 | { |
271 | $this->searchMode = $mode; |
272 | } |
273 | |
274 | /** |
275 | * Return the route name for the search results action. |
276 | * |
277 | * @return string |
278 | */ |
279 | public function getSearchAction() |
280 | { |
281 | return 'eds-search'; |
282 | } |
283 | |
284 | /** |
285 | * Return the view associated with this configuration |
286 | * |
287 | * @return string |
288 | */ |
289 | public function getView() |
290 | { |
291 | return $this->getApiProperty('defaultView'); |
292 | } |
293 | |
294 | /** |
295 | * Get an array of search mode options |
296 | * |
297 | * @return array |
298 | */ |
299 | public function getModeOptions() |
300 | { |
301 | return $this->getApiProperty('modeOptions'); |
302 | } |
303 | |
304 | /** |
305 | * Get the default search mode |
306 | * |
307 | * @return string |
308 | */ |
309 | public function getDefaultMode() |
310 | { |
311 | return $this->getApiProperty('defaultMode'); |
312 | } |
313 | |
314 | /** |
315 | * Return the view associated with this configuration |
316 | * |
317 | * @return string |
318 | */ |
319 | public function getEdsView() |
320 | { |
321 | $viewArr = explode('|', $this->getApiProperty('defaultView')); |
322 | return (1 < count($viewArr)) ? $viewArr[1] : $this->defaultView; |
323 | } |
324 | |
325 | /** |
326 | * Return the expander ids that have the default on flag set in admin |
327 | * |
328 | * @return array |
329 | */ |
330 | public function getDefaultExpanders() |
331 | { |
332 | return $this->getApiProperty('defaultExpanders'); |
333 | } |
334 | |
335 | /** |
336 | * Return the route name of the action used for performing advanced searches. |
337 | * Returns false if the feature is not supported. |
338 | * |
339 | * @return string|bool |
340 | */ |
341 | public function getAdvancedSearchAction() |
342 | { |
343 | return 'eds-advanced'; |
344 | } |
345 | |
346 | /** |
347 | * Set the search options from the Eds API Info methods results |
348 | * |
349 | * @return void |
350 | */ |
351 | public function setOptionsFromApi() |
352 | { |
353 | if ($this->apiOptionsInitialized) { |
354 | return; |
355 | } |
356 | $this->apiOptionsInitialized = true; |
357 | |
358 | // If we don't have API options yet, try to fetch them: |
359 | if (null === $this->apiInfo && $this->apiInfoCallback) { |
360 | $this->apiInfo = ($this->apiInfoCallback)(); |
361 | } |
362 | // Set options from the INFO method first. If settings are set in the config |
363 | // file, use them as 'overrides', but only if they are available (ie. are |
364 | // returned in the INFO method) |
365 | $this->populateViewSettings(); |
366 | $this->populateSearchCriteria(); |
367 | |
368 | // Search handler setup. Only valid values set in the config files are used. |
369 | $this->filterAndReorderProperty( |
370 | 'Basic_Searches', |
371 | 'basicHandlers' |
372 | ); |
373 | $this->filterAndReorderProperty( |
374 | 'Advanced_Searches', |
375 | 'advancedHandlers' |
376 | ); |
377 | |
378 | // Sort preferences: |
379 | $this->filterAndReorderProperty('Sorting', 'sortOptions'); |
380 | |
381 | // Apply overrides from configuration: |
382 | $defaultMode = $this->searchSettings->General->default_mode ?? null; |
383 | if (null !== $defaultMode && isset($this->modeOptions[$defaultMode])) { |
384 | $this->defaultMode = $defaultMode; |
385 | } |
386 | |
387 | $defaultSort = $this->searchSettings->General->default_sort ?? null; |
388 | if (null !== $defaultSort && isset($this->sortOptions[$defaultSort])) { |
389 | $this->defaultSort = $defaultSort; |
390 | } |
391 | |
392 | // Set common limiters and expanders. |
393 | // Only the values that are valid for this profile will be used. |
394 | $this->setCommonSettings( |
395 | 'common_limiters', |
396 | 'limiterOptions', |
397 | 'commonLimiters' |
398 | ); |
399 | $this->setCommonSettings( |
400 | 'common_expanders', |
401 | 'expanderOptions', |
402 | 'commonExpanders' |
403 | ); |
404 | } |
405 | |
406 | /** |
407 | * Apply user settings. All legal values have already been loaded from the API |
408 | * at the time this method is called, so we just need to check if the |
409 | * user-supplied values are valid, and if so, filter/reorder accordingly. |
410 | * |
411 | * @param string $section Configuration section to read |
412 | * @param string $property Property of this object to read and/or modify. |
413 | * |
414 | * @return void |
415 | */ |
416 | protected function filterAndReorderProperty( |
417 | string $section, |
418 | string $property |
419 | ): void { |
420 | if (!isset($this->searchSettings->$section)) { |
421 | return; |
422 | } |
423 | |
424 | // Use a reference to access $this->$property to avoid the awkward/ambiguous |
425 | // expression $this->$property[$key] below. |
426 | $propertyRef = & $this->$property; |
427 | |
428 | $newPropertyValues = []; |
429 | foreach ($this->searchSettings->$section as $key => $value) { |
430 | if (isset($propertyRef[$key])) { |
431 | $newPropertyValues[$key] = $value; |
432 | } |
433 | } |
434 | if (!empty($newPropertyValues)) { |
435 | $this->$property = $newPropertyValues; |
436 | } |
437 | } |
438 | |
439 | /** |
440 | * Apply user-requested "common" settings. |
441 | * |
442 | * @param string $setting Name of common setting |
443 | * @param string $list Name of property containing valid values |
444 | * @param string $target Name of property to populate |
445 | * |
446 | * @return void |
447 | */ |
448 | protected function setCommonSettings( |
449 | string $setting, |
450 | string $list, |
451 | string $target |
452 | ): void { |
453 | if (!empty($this->searchSettings->General->$setting)) { |
454 | $userValues = explode(',', $this->searchSettings->General->$setting); |
455 | |
456 | if (!empty($this->$list)) { |
457 | // Reference to property containing API-provided list of legal values |
458 | $listRef = & $this->$list; |
459 | // Reference to property containing final common settings |
460 | $targetRef = & $this->$target; |
461 | foreach ($userValues as $current) { |
462 | // Only add values that are valid according to the API's list |
463 | if (isset($listRef[$current])) { |
464 | $targetRef[] = $current; |
465 | } |
466 | } |
467 | } |
468 | } |
469 | } |
470 | |
471 | /** |
472 | * Load options from the configuration file. |
473 | * |
474 | * @return void |
475 | */ |
476 | protected function setOptionsFromConfig() |
477 | { |
478 | if (isset($this->searchSettings->General->default_limit)) { |
479 | $this->defaultLimit = $this->searchSettings->General->default_limit; |
480 | } |
481 | if (isset($this->searchSettings->General->limit_options)) { |
482 | $this->limitOptions = $this->explodeListSetting($this->searchSettings->General->limit_options); |
483 | } |
484 | |
485 | // Set up highlighting preference |
486 | if (isset($this->searchSettings->General->highlighting)) { |
487 | // For legacy config compatibility, support the "n" value to disable highlighting: |
488 | $falsyStrings = ['n', 'false']; |
489 | $this->highlight = in_array(strtolower($this->searchSettings->General->highlighting), $falsyStrings) |
490 | ? false |
491 | : (bool)$this->searchSettings->General->highlighting; |
492 | } |
493 | |
494 | // View preferences |
495 | if (isset($this->searchSettings->General->default_view)) { |
496 | $this->defaultView |
497 | = 'list|' . $this->searchSettings->General->default_view; |
498 | } |
499 | |
500 | // Load list view for result (controls AJAX embedding vs. linking) |
501 | if (isset($this->searchSettings->List->view)) { |
502 | $this->listviewOption = $this->searchSettings->List->view; |
503 | } |
504 | |
505 | if (isset($this->searchSettings->Advanced_Facet_Settings->special_facets)) { |
506 | $this->specialAdvancedFacets |
507 | = $this->searchSettings->Advanced_Facet_Settings->special_facets; |
508 | } |
509 | |
510 | // Load autocomplete preferences: |
511 | $this->configureAutocomplete($this->searchSettings); |
512 | |
513 | if (isset($this->searchSettings->General->advanced_limiters)) { |
514 | $this->advancedLimiters = $this->explodeListSetting($this->searchSettings->General->advanced_limiters); |
515 | } |
516 | } |
517 | |
518 | /** |
519 | * Map EBSCO sort labels to standard VuFind text. |
520 | * |
521 | * @param string $label Label to transform |
522 | * |
523 | * @return string |
524 | */ |
525 | protected function mapSortLabel($label) |
526 | { |
527 | switch ($label) { |
528 | case 'Date Newest': |
529 | return 'sort_year'; |
530 | case 'Date Oldest': |
531 | return 'sort_year asc'; |
532 | default: |
533 | return 'sort_' . strtolower($label); |
534 | } |
535 | } |
536 | |
537 | /** |
538 | * Populate available search criteria from the EDS API Info method |
539 | * |
540 | * @return void |
541 | */ |
542 | protected function populateSearchCriteria() |
543 | { |
544 | if (isset($this->apiInfo['AvailableSearchCriteria'])) { |
545 | // Reference for readability: |
546 | $availCriteria = & $this->apiInfo['AvailableSearchCriteria']; |
547 | |
548 | // Sort preferences |
549 | $this->sortOptions = []; |
550 | if (isset($availCriteria['AvailableSorts'])) { |
551 | foreach ($availCriteria['AvailableSorts'] as $sort) { |
552 | $this->sortOptions[$sort['Id']] |
553 | = $this->mapSortLabel($sort['Label']); |
554 | } |
555 | } |
556 | |
557 | // By default, use all of the available search fields for both |
558 | // advanced and basic. Use the values in the config files to filter. |
559 | $this->basicHandlers = ['AllFields' => 'All Fields']; |
560 | if (isset($availCriteria['AvailableSearchFields'])) { |
561 | foreach ($availCriteria['AvailableSearchFields'] as $searchField) { |
562 | $this->basicHandlers[$searchField['FieldCode']] |
563 | = $searchField['Label']; |
564 | } |
565 | } |
566 | $this->advancedHandlers = $this->basicHandlers; |
567 | |
568 | // Search Mode preferences |
569 | $this->modeOptions = []; |
570 | if (isset($availCriteria['AvailableSearchModes'])) { |
571 | foreach ($availCriteria['AvailableSearchModes'] as $mode) { |
572 | $this->modeOptions[$mode['Mode']] = [ |
573 | 'Label' => $mode['Label'], 'Value' => $mode['Mode'], |
574 | ]; |
575 | if ( |
576 | isset($mode['DefaultOn']) |
577 | && 'y' == $mode['DefaultOn'] |
578 | ) { |
579 | $this->defaultMode = $mode['Mode']; |
580 | } |
581 | } |
582 | } |
583 | |
584 | // Expanders |
585 | $this->expanderOptions = []; |
586 | $this->defaultExpanders = []; |
587 | if (isset($availCriteria['AvailableExpanders'])) { |
588 | foreach ($availCriteria['AvailableExpanders'] as $expander) { |
589 | $this->expanderOptions[$expander['Id']] = [ |
590 | 'Label' => $expander['Label'], 'Value' => $expander['Id'], |
591 | ]; |
592 | if ( |
593 | isset($expander['DefaultOn']) |
594 | && 'y' == $expander['DefaultOn'] |
595 | ) { |
596 | $this->defaultExpanders[] = $expander['Id']; |
597 | } |
598 | } |
599 | } |
600 | |
601 | // Limiters |
602 | $this->limiterOptions = []; |
603 | if (isset($availCriteria['AvailableLimiters'])) { |
604 | foreach ($availCriteria['AvailableLimiters'] as $limiter) { |
605 | $val = ''; |
606 | if ('select' == $limiter['Type']) { |
607 | $val = 'y'; |
608 | } |
609 | $this->limiterOptions[$limiter['Id']] = [ |
610 | 'Id' => $limiter['Id'], |
611 | 'Label' => $limiter['Label'], |
612 | 'Type' => $limiter['Type'], |
613 | 'LimiterValues' => isset($limiter['LimiterValues']) |
614 | ? $this->populateLimiterValues( |
615 | $limiter['LimiterValues'] |
616 | ) |
617 | : [['Value' => $val]], |
618 | 'DefaultOn' => $limiter['DefaultOn'] ?? 'n', |
619 | ]; |
620 | } |
621 | } |
622 | } |
623 | } |
624 | |
625 | /** |
626 | * Populate limiter values from the EDS API INFO method data |
627 | * |
628 | * @param array $limiterValues Limiter values from the API |
629 | * |
630 | * @return array |
631 | */ |
632 | protected function populateLimiterValues(array $limiterValues) |
633 | { |
634 | $availableLimiterValues = []; |
635 | foreach ($limiterValues as $limiterValue) { |
636 | $availableLimiterValues[] = [ |
637 | 'Value' => $limiterValue['Value'], |
638 | 'LimiterValues' => isset($limiterValue['LimiterValues']) |
639 | ? $this |
640 | ->populateLimiterValues($limiterValue['LimiterValues']) |
641 | : null, |
642 | ]; |
643 | } |
644 | return empty($availableLimiterValues) ? null : $availableLimiterValues; |
645 | } |
646 | |
647 | /** |
648 | * Get the value of a property that is retrieved via the Info method and stored |
649 | * in a member property. |
650 | * |
651 | * @param string $propertyName Name of the member property |
652 | * |
653 | * @return mixed |
654 | */ |
655 | protected function getApiProperty(string $propertyName) |
656 | { |
657 | $this->setOptionsFromApi(); |
658 | return $this->$propertyName; |
659 | } |
660 | |
661 | /** |
662 | * Returns the available limiters |
663 | * |
664 | * @return array |
665 | */ |
666 | public function getAvailableLimiters() |
667 | { |
668 | return $this->getApiProperty('limiterOptions'); |
669 | } |
670 | |
671 | /** |
672 | * Returns the enabled limiters for the advanced search |
673 | * |
674 | * @return array |
675 | */ |
676 | public function getAdvancedLimiters() |
677 | { |
678 | // Make sure that everything is labeled with an appropriate translation |
679 | // string: |
680 | $labeledLimiters = array_map( |
681 | function ($limiter) { |
682 | $limiter['Label'] = $this->getLabelForCheckboxFilter( |
683 | 'eds_limiter_' . $limiter['Id'], |
684 | $limiter['Label'] |
685 | ); |
686 | return $limiter; |
687 | }, |
688 | $this->getApiProperty('limiterOptions') |
689 | ); |
690 | // No setting = use all available values |
691 | if (!$this->advancedLimiters) { |
692 | return $labeledLimiters; |
693 | } |
694 | // If we got this far, let's create a list of enabled limiters in the |
695 | // requested order: |
696 | $enabledLimiters = []; |
697 | foreach ($this->advancedLimiters as $limiter) { |
698 | if (isset($labeledLimiters[$limiter])) { |
699 | $enabledLimiters[$limiter] = $labeledLimiters[$limiter]; |
700 | } |
701 | } |
702 | return $enabledLimiters; |
703 | } |
704 | |
705 | /** |
706 | * Returns the available expanders |
707 | * |
708 | * @return array |
709 | */ |
710 | public function getAvailableExpanders() |
711 | { |
712 | return $this->getApiProperty('expanderOptions'); |
713 | } |
714 | |
715 | /** |
716 | * Sets the view settings from EDS API info method call data |
717 | * |
718 | * @return void |
719 | */ |
720 | protected function populateViewSettings() |
721 | { |
722 | $settings = $this->apiInfo['ViewResultSettings'] ?? []; |
723 | // default result Limit |
724 | $this->defaultLimit ??= $settings['ResultsPerPage'] ?? 20; |
725 | |
726 | // default view |
727 | $this->defaultView ??= 'list|' . ($settings['ResultListView'] ?? 'brief'); |
728 | } |
729 | |
730 | /** |
731 | * Get a translation string (if available) or else use a default |
732 | * |
733 | * @param string $label Translation string to look up |
734 | * @param string $default Default to use if no translation found |
735 | * |
736 | * @return string |
737 | */ |
738 | protected function getLabelForCheckboxFilter($label, $default) |
739 | { |
740 | // If translation doesn't change the label, we don't want to |
741 | // display the non-human-readable version so we should instead |
742 | // return the EDS-provided English default. |
743 | return ($label == $this->translate($label)) |
744 | ? $default : $label; |
745 | } |
746 | |
747 | /** |
748 | * Obtain limiters to display on the basic search screen |
749 | * |
750 | * @return array |
751 | */ |
752 | public function getSearchScreenLimiters() |
753 | { |
754 | $ssLimiterOptions = []; |
755 | $limiterOptions = $this->getApiProperty('limiterOptions'); |
756 | foreach ($this->getApiProperty('commonLimiters') as $key) { |
757 | $limiter = $limiterOptions[$key]; |
758 | $ssLimiterOptions[$key] = [ |
759 | 'selectedvalue' => 'LIMIT|' . $key . ':y', |
760 | 'description' => $this->getLabelForCheckboxFilter( |
761 | 'eds_limiter_' . $key, |
762 | $limiter['Label'] |
763 | ), |
764 | 'selected' => ('y' == $limiter['DefaultOn']) ? true : false, |
765 | ]; |
766 | } |
767 | return $ssLimiterOptions; |
768 | } |
769 | |
770 | /** |
771 | * Obtain expanders to display on the basic search screen |
772 | * |
773 | * @return array |
774 | */ |
775 | public function getSearchScreenExpanders() |
776 | { |
777 | $ssExpanderOptions = []; |
778 | $expanderOptions = $this->getApiProperty('expanderOptions'); |
779 | foreach ($this->getApiProperty('commonExpanders') as $key) { |
780 | $expander = $expanderOptions[$key]; |
781 | $ssExpanderOptions[$key] = [ |
782 | 'selectedvalue' => 'EXPAND:' . $key, |
783 | 'description' => $this->getLabelForCheckboxFilter( |
784 | 'eds_expander_' . $key, |
785 | $expander['Label'] |
786 | ), |
787 | ]; |
788 | } |
789 | return $ssExpanderOptions; |
790 | } |
791 | |
792 | /** |
793 | * Get default view setting. |
794 | * |
795 | * @return int |
796 | */ |
797 | public function getDefaultView() |
798 | { |
799 | $viewArr = explode('|', $this->getApiProperty('defaultView')); |
800 | return $viewArr[0]; |
801 | } |
802 | |
803 | /** |
804 | * Get default filters to apply to an empty search. |
805 | * |
806 | * @return array |
807 | */ |
808 | public function getDefaultFilters() |
809 | { |
810 | // Populate defaults if not already set: |
811 | if (!$this->defaultFilters) { |
812 | //expanders |
813 | $expanders = $this->getDefaultExpanders(); |
814 | foreach ($expanders as $expander) { |
815 | $this->defaultFilters[] = 'EXPAND:' . $expander; |
816 | } |
817 | |
818 | //limiters |
819 | $limiters = $this->getAvailableLimiters(); |
820 | foreach ($limiters as $key => $value) { |
821 | if ('select' == $value['Type'] && 'y' == $value['DefaultOn']) { |
822 | // only select limiters can be defaulted on limiters can be |
823 | // defaulted |
824 | $val = $value['LimiterValues'][0]['Value']; |
825 | $this->defaultFilters[] = 'LIMIT|' . $key . ':' . $val; |
826 | } |
827 | } |
828 | } |
829 | return $this->defaultFilters; |
830 | } |
831 | } |