Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
AbstractSearchObjectDeferred | |
100.00% |
15 / 15 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
getAjaxModule | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
setConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
init | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
initLookFor | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
process | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUrlParams | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * Abstract SearchObjectDeferred Recommendations Module (needs to be extended to use |
5 | * a particular search object). |
6 | * |
7 | * PHP version 8 |
8 | * |
9 | * Copyright (C) Villanova University 2023. |
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 Recommendations |
26 | * @author Demian Katz <demian.katz@villanova.edu> |
27 | * @author Maccabee Levine <msl321@lehigh.edu> |
28 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
29 | * @link https://vufind.org/wiki/development:plugins:recommendation_modules Wiki |
30 | */ |
31 | |
32 | namespace VuFind\Recommend; |
33 | |
34 | use function is_array; |
35 | |
36 | /** |
37 | * Abstract SearchObjectDeferred Recommendations Module (needs to be extended to use |
38 | * a particular search object). |
39 | * |
40 | * This class sets up an AJAX call to trigger a call to some SearchObject implementation. |
41 | * |
42 | * @category VuFind |
43 | * @package Recommendations |
44 | * @author Demian Katz <demian.katz@villanova.edu> |
45 | * @author Maccabee Levine <msl321@lehigh.edu> |
46 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
47 | * @link https://vufind.org/wiki/development:plugins:recommendation_modules Wiki |
48 | */ |
49 | abstract class AbstractSearchObjectDeferred implements RecommendInterface |
50 | { |
51 | /** |
52 | * Raw configuration parameters |
53 | * |
54 | * @var string |
55 | */ |
56 | protected $rawParams; |
57 | |
58 | /** |
59 | * Current search query |
60 | * |
61 | * @var string |
62 | */ |
63 | protected $lookfor; |
64 | |
65 | /** |
66 | * Configuration parameters processed for submission via AJAX |
67 | * |
68 | * @var string |
69 | */ |
70 | protected $processedParams; |
71 | |
72 | /** |
73 | * Number of expected module parameters (from .ini config) |
74 | * |
75 | * @var int |
76 | */ |
77 | protected $paramCount = 2; |
78 | |
79 | /** |
80 | * Store the configuration of the recommendation module. |
81 | * |
82 | * @return string Module name in call to AjaxHandler |
83 | */ |
84 | abstract protected function getAjaxModule(); |
85 | |
86 | /** |
87 | * Store the configuration of the recommendation module. |
88 | * |
89 | * @param string $settings Settings from searches.ini. |
90 | * |
91 | * @return void |
92 | */ |
93 | public function setConfig($settings) |
94 | { |
95 | $this->rawParams = $settings; |
96 | } |
97 | |
98 | /** |
99 | * Called before the Search Results object performs its main search |
100 | * (specifically, in response to \VuFind\Search\SearchRunner::EVENT_CONFIGURED). |
101 | * This method is responsible for setting search parameters needed by the |
102 | * recommendation module and for reading any existing search parameters that may |
103 | * be needed. |
104 | * |
105 | * @param \VuFind\Search\Base\Params $params Search parameter object |
106 | * @param \Laminas\Stdlib\Parameters $request Parameter object representing user |
107 | * request. |
108 | * |
109 | * @return void |
110 | */ |
111 | public function init($params, $request) |
112 | { |
113 | // Parse out parameters: |
114 | $settings = explode(':', $this->rawParams); |
115 | |
116 | // Make sure all elements of the params array are filled in, even if just |
117 | // with a blank string, so we can rebuild the parameters to pass through |
118 | // AJAX later on! |
119 | for ($i = 0; $i < $this->paramCount; $i++) { |
120 | $settings[$i] ??= ''; |
121 | } |
122 | |
123 | $this->initLookFor($params, $request, $settings); |
124 | |
125 | $this->processedParams = implode(':', $settings); |
126 | } |
127 | |
128 | /** |
129 | * Initialize the lookFor query parameter. Called from init(). |
130 | * |
131 | * @param \VuFind\Search\Base\Params $params Search parameter object |
132 | * @param \Laminas\Stdlib\Parameters $request Parameter object representing user |
133 | * request. |
134 | * @param array $settings Parameter array (passed by reference) |
135 | * |
136 | * @return void |
137 | */ |
138 | protected function initLookFor($params, $request, &$settings) |
139 | { |
140 | // Map the user-specified query field to 'lookfor' for simplicity: |
141 | $this->lookfor |
142 | = $request->get(empty($settings[0]) ? 'lookfor' : $settings[0], ''); |
143 | $settings[0] = 'lookfor'; |
144 | |
145 | // If lookfor has somehow been set as an array, collapse it into a string: |
146 | if (is_array($this->lookfor)) { |
147 | $this->lookfor = implode(' ', $this->lookfor); |
148 | } |
149 | } |
150 | |
151 | /** |
152 | * Called after the Search Results object has performed its main search. This |
153 | * may be used to extract necessary information from the Search Results object |
154 | * or to perform completely unrelated processing. |
155 | * |
156 | * @param \VuFind\Search\Base\Results $results Search results object |
157 | * |
158 | * @return void |
159 | */ |
160 | public function process($results) |
161 | { |
162 | // No action needed |
163 | } |
164 | |
165 | /** |
166 | * Get the URL parameters needed to make the AJAX recommendation request. |
167 | * |
168 | * @return string |
169 | */ |
170 | public function getUrlParams() |
171 | { |
172 | return 'mod=' . urlencode($this->getAjaxModule()) |
173 | . '¶ms=' . urlencode($this->processedParams) |
174 | . '&lookfor=' . urlencode($this->lookfor); |
175 | } |
176 | } |