Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
EdsController
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 9
870
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 resultScrollerActive
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 advancedAction
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 searchAction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAdvancedFacets
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 processAdvancedFacets
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
 processAdvancedExpanders
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 processPublicationDateRange
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
56
 processAdvancedSearchModes
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * Eds Controller
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 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Controller;
31
32use Laminas\ServiceManager\ServiceLocatorInterface;
33use VuFind\Solr\Utils as SolrUtils;
34
35use function in_array;
36
37/**
38 * EDS Controller
39 *
40 * @category VuFind
41 * @package  Controller
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Site
45 */
46class EdsController extends AbstractSearch
47{
48    /**
49     * Constructor
50     *
51     * @param ServiceLocatorInterface $sm Service locator
52     */
53    public function __construct(ServiceLocatorInterface $sm)
54    {
55        $this->searchClassId = 'EDS';
56        parent::__construct($sm);
57    }
58
59    /**
60     * Is the result scroller active?
61     *
62     * @return bool
63     */
64    protected function resultScrollerActive()
65    {
66        $config = $this->serviceLocator->get(\VuFind\Config\PluginManager::class)
67            ->get('EDS');
68        return $config->Record->next_prev_navigation ?? false;
69    }
70
71    /**
72     * Handle an advanced search
73     *
74     * @return mixed
75     */
76    public function advancedAction()
77    {
78        // Standard setup from base class:
79        $view = parent::advancedAction();
80        // Set up facet information:
81        $view->limiterList = $this->processAdvancedFacets(
82            $this->getAdvancedFacets(),
83            $view->saved
84        );
85        $view->expanderList = $this->processAdvancedExpanders($view->saved);
86        $view->searchModes = $this->processAdvancedSearchModes($view->saved);
87        $view->dateRangeLimit = $this->processPublicationDateRange($view->saved);
88        return $view;
89    }
90
91    /**
92     * Search action -- call standard results action
93     *
94     * @return mixed
95     */
96    public function searchAction()
97    {
98        return $this->resultsAction();
99    }
100
101    /**
102     * Return a Search Results object containing advanced facet information. This
103     * data may come from the cache.
104     *
105     * @return array
106     */
107    protected function getAdvancedFacets()
108    {
109        // VuFind facets are what the EDS API calls limiters. Available limiters
110        // are returned with a call to the EDS API Info method and are cached.
111        // Since they are obtained from a separate call, there is no need to call
112        // search.
113
114        // Check if we have facet results stored in session. Build them if we don't.
115        // pull them from the session cache
116        $results = $this->getResultsManager()->get('EDS');
117        $params = $results->getParams();
118        $options = $params->getOptions();
119        return $options->getAdvancedLimiters();
120    }
121
122    /**
123     * Process the facets to be used as limits on the Advanced Search screen.
124     *
125     * @param array  $facetList    The advanced facet values
126     * @param object $searchObject Saved search object (false if none)
127     *
128     * @return array               Sorted facets, with selected values flagged.
129     */
130    protected function processAdvancedFacets($facetList, $searchObject = false)
131    {
132        // Process the facets, assuming they came back
133        foreach ($facetList as $facet => $list) {
134            if (isset($list['LimiterValues'])) {
135                foreach ($list['LimiterValues'] as $key => $value) {
136                    // Build the filter string for the URL:
137                    $fullFilter = $facet . ':' . $value['Value'];
138
139                    // If we haven't already found a selected facet and the current
140                    // facet has been applied to the search, we should store it as
141                    // the selected facet for the current control.
142                    if ($searchObject) {
143                        $limitFilt = 'LIMIT|' . $fullFilter;
144                        if ($searchObject->getParams()->hasFilter($limitFilt)) {
145                            $facetList[$facet]['LimiterValues'][$key]['selected']
146                                = true;
147                            // Remove the filter from the search object -- we don't
148                            // want it to show up in the "applied filters" sidebar
149                            // since it will already be accounted for by being
150                            // selected in the filter select list!
151                            $searchObject->getParams()->removeFilter($limitFilt);
152                        }
153                    } else {
154                        if ('y' == $facetList[$facet]['DefaultOn']) {
155                            $facetList[$facet]['selected'] = true;
156                        }
157                    }
158                }
159            }
160        }
161        return $facetList;
162    }
163
164    /**
165     * Process the expanders to be used on the Advanced Search screen.
166     *
167     * @param object $searchObject Saved search object (false if none)
168     *
169     * @return array               Sorted facets, with selected values flagged.
170     */
171    protected function processAdvancedExpanders($searchObject = false)
172    {
173        $results = $this->getResultsManager()->get('EDS');
174        $params = $results->getParams();
175        $options = $params->getOptions();
176        $availableExpanders = $options->getAvailableExpanders();
177        $defaultExpanders = $options->getDefaultExpanders();
178        // Process the expanders, assuming they came back
179        foreach ($availableExpanders as $key => $value) {
180            if ($searchObject) {
181                $expandFilt = 'EXPAND:' . $value['Value'];
182                if ($searchObject->getParams()->hasFilter($expandFilt)) {
183                    $availableExpanders[$key]['selected'] = true;
184                    // Remove the filter from the search object -- we don't want
185                    // it to show up in the "applied filters" sidebar since it
186                    // will already be accounted for by being selected in the
187                    // filter select list!
188                    $searchObject->getParams()->removeFilter($expandFilt);
189                }
190            } else {
191                if (in_array($key, $defaultExpanders)) {
192                    $availableExpanders[$key]['selected'] = true;
193                }
194            }
195        }
196        return $availableExpanders;
197    }
198
199    /**
200     * Process the publication date range limiter widget
201     *
202     * @param object $searchObject Saved search object (false if none)
203     *
204     * @return array               To and from dates
205     */
206    protected function processPublicationDateRange($searchObject = false)
207    {
208        $from = $to = '';
209        if ($searchObject) {
210            $filters = $searchObject->getParams()->getFilterList();
211            foreach ($filters as $key => $value) {
212                if ('PublicationDate' == $key) {
213                    if ($range = SolrUtils::parseRange($value[0]['value'])) {
214                        $from = $range['from'] == '*' ? '11' : $range['from'];
215                        $to = $range['to'] == '*' ? '12' : $range['to'];
216                    }
217                    $searchObject->getParams()
218                        ->removeFilter($key . ':' . $value[0]['value']);
219                    break;
220                }
221            }
222        }
223        return [$from, $to];
224    }
225
226    /**
227     * Process the search modes to be used on the Advanced Search screen.
228     *
229     * @param object $searchObject Saved search object (false if none)
230     *
231     * @return array               search modes with selected values flagged.
232     */
233    protected function processAdvancedSearchModes($searchObject = false)
234    {
235        $results = $this->getResultsManager()->get('EDS');
236        $params = $results->getParams();
237        $options = $params->getOptions();
238        $searchModes = $options->getModeOptions();
239        // Process the facets, assuming they came back
240        foreach ($searchModes as $key => $mode) {
241            if ($searchObject) {
242                $modeFilter = 'SEARCHMODE:' . $mode['Value'];
243                if ($searchObject->getParams()->hasFilter($modeFilter)) {
244                    $searchModes[$key]['selected'] = true;
245                    // Remove the filter from the search object -- we don't want
246                    // it to show up in the "applied filters" sidebar since it
247                    // will already be accounted for by being selected in the
248                    // filter select list!
249                    $searchObject->getParams()->removeFilter($modeFilter);
250                }
251            } else {
252                if ($key == $options->getDefaultMode()) {
253                    $searchModes[$key]['selected'] = true;
254                }
255            }
256        }
257
258        return $searchModes;
259    }
260}