Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractSolrSearch
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 6
552
0.00% covered (danger)
0.00%
0 / 1
 addFacetDetailsToView
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 advancedAction
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 getIllustrationSettings
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
 processAdvancedFacets
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
90
 getHierarchicalFacets
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getAdvancedHierarchicalFacetsSortOptions
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * AbstractSearch with Solr-specific features added.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Controller
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Juha Luoma <juha.luoma@helsinki.fi>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org Main Site
29 */
30
31namespace VuFind\Controller;
32
33use Laminas\View\Model\ViewModel;
34
35use function in_array;
36
37/**
38 * AbstractSearch with Solr-specific features added.
39 *
40 * @category VuFind
41 * @package  Controller
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @author   Juha Luoma <juha.luoma@helsinki.fi>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org Main Site
46 */
47class AbstractSolrSearch extends AbstractSearch
48{
49    use Feature\RecordVersionsSearchTrait;
50
51    /**
52     * Set up facet details in the view (for use in advanced search and similar).
53     *
54     * @param ViewModel $view View model to update
55     * @param string    $list Name of facet list to retrieve
56     *
57     * @return void
58     */
59    protected function addFacetDetailsToView(ViewModel $view, $list = 'Advanced'): void
60    {
61        $facets = $this->serviceLocator
62            ->get(\VuFind\Search\FacetCache\PluginManager::class)
63            ->get($this->searchClassId)
64            ->getList($list);
65        $view->hierarchicalFacets
66            = $this->getHierarchicalFacets($view->options->getFacetsIni());
67        $view->hierarchicalFacetsSortOptions
68            = $this->getAdvancedHierarchicalFacetsSortOptions(
69                $view->options->getFacetsIni()
70            );
71        $view->facetList = $this->processAdvancedFacets(
72            $facets,
73            $view->saved ?? false,
74            $view->hierarchicalFacets,
75            $view->hierarchicalFacetsSortOptions
76        );
77    }
78
79    /**
80     * Handle an advanced search
81     *
82     * @return mixed
83     */
84    public function advancedAction()
85    {
86        // Standard setup from base class:
87        $view = parent::advancedAction();
88
89        // Set up facet information:
90        $this->addFacetDetailsToView($view);
91        $specialFacets = $this->parseSpecialFacetsSetting(
92            $view->options->getSpecialAdvancedFacets()
93        );
94        if (isset($specialFacets['illustrated'])) {
95            $view->illustratedLimit
96                = $this->getIllustrationSettings($view->saved);
97        }
98        if (isset($specialFacets['checkboxes'])) {
99            $view->checkboxFacets = $this->processAdvancedCheckboxes(
100                $specialFacets['checkboxes'],
101                $view->saved
102            );
103        }
104        $view->ranges = $this->getAllRangeSettings($specialFacets, $view->saved);
105
106        return $view;
107    }
108
109    /**
110     * Get the possible legal values for the illustration limit radio buttons.
111     *
112     * @param object $savedSearch Saved search object (false if none)
113     *
114     * @return array              Legal options, with selected value flagged.
115     */
116    protected function getIllustrationSettings($savedSearch = false)
117    {
118        $illYes = [
119            'text' => 'Has Illustrations', 'value' => 1, 'selected' => false,
120        ];
121        $illNo = [
122            'text' => 'Not Illustrated', 'value' => 0, 'selected' => false,
123        ];
124        $illAny = [
125            'text' => 'No Preference', 'value' => -1, 'selected' => false,
126        ];
127
128        // Find the selected value by analyzing facets -- if we find match, remove
129        // the offending facet to avoid inappropriate items appearing in the
130        // "applied filters" sidebar!
131        if (
132            $savedSearch
133            && $savedSearch->getParams()->hasFilter('illustrated:Illustrated')
134        ) {
135            $illYes['selected'] = true;
136            $savedSearch->getParams()->removeFilter('illustrated:Illustrated');
137        } elseif (
138            $savedSearch
139            && $savedSearch->getParams()->hasFilter('illustrated:"Not Illustrated"')
140        ) {
141            $illNo['selected'] = true;
142            $savedSearch->getParams()->removeFilter('illustrated:"Not Illustrated"');
143        } else {
144            $illAny['selected'] = true;
145        }
146        return [$illYes, $illNo, $illAny];
147    }
148
149    /**
150     * Process the facets to be used as limits on the Advanced Search screen.
151     *
152     * @param array  $facetList                     The advanced facet values
153     * @param object $searchObject                  Saved search object
154     * (false if none)
155     * @param array  $hierarchicalFacets            Hierarchical facet list (if any)
156     * @param array  $hierarchicalFacetsSortOptions Hierarchical facet sort options
157     * (if any)
158     *
159     * @return array Sorted facets, with selected values flagged.
160     */
161    protected function processAdvancedFacets(
162        $facetList,
163        $searchObject = false,
164        $hierarchicalFacets = [],
165        $hierarchicalFacetsSortOptions = []
166    ) {
167        $facetHelper = null;
168        $options = null;
169        foreach ($facetList as $facet => &$list) {
170            // Hierarchical facets: format display texts and sort facets
171            // to a flat array according to the hierarchy
172            if (in_array($facet, $hierarchicalFacets)) {
173                // Process the facets
174                if (!$facetHelper) {
175                    $facetHelper = $this->serviceLocator
176                        ->get(\VuFind\Search\Solr\HierarchicalFacetHelper::class);
177                    $options = $this->getOptionsForClass();
178                }
179
180                $tmpList = $list['list'];
181                if ($options->getFilterHierarchicalFacetsInAdvanced()) {
182                    $tmpList = $facetHelper->filterFacets(
183                        $facet,
184                        $tmpList,
185                        $options
186                    );
187                }
188                $list['list'] = $facetHelper->flattenFacetHierarchy($tmpList);
189            }
190
191            foreach ($list['list'] as $key => $value) {
192                // Build the filter string for the URL:
193                $fullFilter = ($value['operator'] == 'OR' ? '~' : '')
194                    . $facet . ':"' . $value['value'] . '"';
195
196                // If we haven't already found a selected facet and the current
197                // facet has been applied to the search, we should store it as
198                // the selected facet for the current control.
199                if (
200                    $searchObject
201                    && $searchObject->getParams()->hasFilter($fullFilter)
202                ) {
203                    $list['list'][$key]['selected'] = true;
204                    // Remove the filter from the search object -- we don't want
205                    // it to show up in the "applied filters" sidebar since it
206                    // will already be accounted for by being selected in the
207                    // filter select list!
208                    $searchObject->getParams()->removeFilter($fullFilter);
209                }
210            }
211        }
212        return $facetList;
213    }
214
215    /**
216     * Get an array of hierarchical facets
217     *
218     * @param string $config Name of facet configuration file to load.
219     *
220     * @return array Facets
221     */
222    protected function getHierarchicalFacets($config)
223    {
224        $facetConfig = $this->getConfig($config);
225        return isset($facetConfig->SpecialFacets->hierarchical)
226            ? $facetConfig->SpecialFacets->hierarchical->toArray()
227            : [];
228    }
229
230    /**
231     * Get an array of hierarchical facet sort options for Advanced search
232     *
233     * @param string $config Name of facet configuration file to load.
234     *
235     * @return array
236     */
237    protected function getAdvancedHierarchicalFacetsSortOptions($config)
238    {
239        $facetConfig = $this->getConfig($config);
240        $baseConfig
241            = isset($facetConfig->SpecialFacets->hierarchicalFacetSortOptions)
242            ? $facetConfig->SpecialFacets->hierarchicalFacetSortOptions->toArray()
243            : [];
244        $advancedConfig
245            = isset($facetConfig->Advanced_Settings->hierarchicalFacetSortOptions)
246            ? $facetConfig->Advanced_Settings->hierarchicalFacetSortOptions
247                ->toArray()
248            : [];
249
250        return array_merge($baseConfig, $advancedConfig);
251    }
252}