Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.00% covered (warning)
85.00%
17 / 20
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
OpenLibrarySubjectsDeferred
85.00% covered (warning)
85.00%
17 / 20
75.00% covered (warning)
75.00%
3 / 4
12.49
0.00% covered (danger)
0.00%
0 / 1
 setConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
81.25% covered (warning)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
9.53
 process
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUrlParams
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * OpenLibrarySubjects Recommendations Module
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  Recommendations
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Eoghan Ó Carragáin <eoghan.ocarragain@gmail.com>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development:plugins:recommendation_modules Wiki
29 */
30
31namespace VuFind\Recommend;
32
33use function is_object;
34
35/**
36 * OpenLibrarySubjects Recommendations Module
37 *
38 * This class provides recommendations by doing a search of the catalog; useful
39 * for displaying catalog recommendations in other modules (i.e. Summon, Web, etc.)
40 *
41 * @category VuFind
42 * @package  Recommendations
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @author   Eoghan Ó Carragáin <eoghan.ocarragain@gmail.com>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org/wiki/development:plugins:recommendation_modules Wiki
47 */
48class OpenLibrarySubjectsDeferred extends OpenLibrarySubjects
49{
50    /**
51     * Raw configuration string
52     *
53     * @var string
54     */
55    protected $rawParams;
56
57    /**
58     * Processed configuration string
59     *
60     * @var array
61     */
62    protected $processedParams;
63
64    /**
65     * Store the configuration of the recommendation module.
66     *
67     * @param string $settings Settings from searches.ini.
68     *
69     * @return void
70     */
71    public function setConfig($settings)
72    {
73        $this->rawParams = $settings;
74    }
75
76    /**
77     * Called before the Search Results object performs its main search
78     * (specifically, in response to \VuFind\Search\SearchRunner::EVENT_CONFIGURED).
79     * This method is responsible for setting search parameters needed by the
80     * recommendation module and for reading any existing search parameters that may
81     * be needed.
82     *
83     * @param \VuFind\Search\Base\Params $params  Search parameter object
84     * @param \Laminas\Stdlib\Parameters $request Parameter object representing user
85     * request.
86     *
87     * @return void
88     */
89    public function init($params, $request)
90    {
91        // Parse out parameters:
92        $settings = explode(':', $this->rawParams);
93        $this->requestParam = empty($settings[0]) ? 'lookfor' : $settings[0];
94        $settings[0] = $this->requestParam;
95
96        // Make sure all elements of the params array are filled in, even if just
97        // with a blank string, so we can rebuild the parameters to pass through
98        // AJAX later on!
99        $settings[1] ??= '';
100
101        // If Publication Date filter is to be applied, get the range and add it to
102        //    $settings since the $searchObject will not be available after the AJAX
103        //    call
104        if (!isset($settings[2]) || empty($settings[2])) {
105            $settings[2] = 'publishDate';
106        }
107        $pubDateRange = strtolower($settings[2]) == 'false' ?
108            [] : $this->getPublishedDates($settings[2], $params, $request);
109        if (!empty($pubDateRange)) {
110            // Check if [Subject types] parameter has been supplied in searches.ini
111            if (!isset($settings[3])) {
112                $settings[3] = '';
113            }
114            $settings[4] = $pubDateRange;
115        }
116
117        $this->processedParams = implode(':', $settings);
118
119        // Collect the best possible search term(s):
120        $this->subject = $request->get($this->requestParam);
121        if (empty($this->subject) && is_object($params)) {
122            $this->subject = $params->getQuery()->getAllTerms();
123        }
124    }
125
126    /**
127     * Called after the Search Results object has performed its main search. This
128     * may be used to extract necessary information from the Search Results object
129     * or to perform completely unrelated processing.
130     *
131     * @param \VuFind\Search\Base\Results $results Search results object
132     *
133     * @return void
134     */
135    public function process($results)
136    {
137        // No action needed
138    }
139
140    /**
141     * Get the URL parameters needed to make the AJAX recommendation request.
142     *
143     * @return string
144     */
145    public function getUrlParams()
146    {
147        return 'mod=OpenLibrarySubjects&params=' . urlencode($this->processedParams)
148            . '&' . urlencode($this->requestParam) . '=' . urlencode($this->subject);
149    }
150}