Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
39 / 39 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
SearchTabsHelper | |
100.00% |
39 / 39 |
|
100.00% |
10 / 10 |
22 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
getHiddenFilters | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
getTabConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTabFilterConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTabPermissionConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSettings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
extractClassName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
filtersMatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultTabHiddenFilters | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
8 | |||
parseFilters | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * "Search tabs" helper |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * Copyright (C) The National Library of Finland 2015-2016. |
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 View_Helpers |
26 | * @author Demian Katz <demian.katz@villanova.edu> |
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/wiki/development Wiki |
30 | */ |
31 | |
32 | namespace VuFind\Search; |
33 | |
34 | use Laminas\Http\Request; |
35 | use VuFind\Search\Results\PluginManager; |
36 | |
37 | /** |
38 | * "Search tabs" helper |
39 | * |
40 | * @category VuFind |
41 | * @package View_Helpers |
42 | * @author Demian Katz <demian.katz@villanova.edu> |
43 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
44 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
45 | * @link https://vufind.org/wiki/development Wiki |
46 | */ |
47 | class SearchTabsHelper extends \Laminas\View\Helper\AbstractHelper |
48 | { |
49 | /** |
50 | * Search manager |
51 | * |
52 | * @var PluginManager |
53 | */ |
54 | protected $results; |
55 | |
56 | /** |
57 | * Tab configuration |
58 | * |
59 | * @var array |
60 | */ |
61 | protected $tabConfig; |
62 | |
63 | /** |
64 | * Tab filter configuration |
65 | * |
66 | * @var array |
67 | */ |
68 | protected $filterConfig; |
69 | |
70 | /** |
71 | * Tab permission configuration |
72 | * |
73 | * @var array |
74 | */ |
75 | protected $permissionConfig; |
76 | |
77 | /** |
78 | * Tab settings |
79 | * |
80 | * @var array |
81 | */ |
82 | protected $settings; |
83 | |
84 | /** |
85 | * Request |
86 | * |
87 | * @var Request |
88 | */ |
89 | protected $request; |
90 | |
91 | /** |
92 | * Constructor |
93 | * |
94 | * @param PluginManager $results Search results plugin manager |
95 | * @param array $tabConfig Tab configuration |
96 | * @param array $filterConfig Tab filter configuration |
97 | * @param Request $request Request |
98 | * @param array $permConfig Tab permission configuration |
99 | * @param array $settings Tab settings |
100 | */ |
101 | public function __construct( |
102 | PluginManager $results, |
103 | array $tabConfig, |
104 | array $filterConfig, |
105 | Request $request, |
106 | array $permConfig = [], |
107 | array $settings = [] |
108 | ) { |
109 | $this->results = $results; |
110 | $this->tabConfig = $tabConfig; |
111 | $this->filterConfig = $filterConfig; |
112 | $this->request = $request; |
113 | $this->permissionConfig = $permConfig; |
114 | $this->settings = $settings; |
115 | } |
116 | |
117 | /** |
118 | * Get an array of hidden filters |
119 | * |
120 | * @param string $searchClassId Active search class |
121 | * @param bool $returnDefaultsIfEmpty Whether to return default tab filters if |
122 | * no filters are currently active |
123 | * @param bool $ignoreCurrentRequest Whether to ignore hidden filters in |
124 | * the current request |
125 | * |
126 | * @return array |
127 | */ |
128 | public function getHiddenFilters( |
129 | $searchClassId, |
130 | $returnDefaultsIfEmpty = true, |
131 | $ignoreCurrentRequest = false |
132 | ) { |
133 | $filters = $ignoreCurrentRequest |
134 | ? null : $this->request->getQuery('hiddenFilters'); |
135 | if (null === $filters && $returnDefaultsIfEmpty) { |
136 | $filters = $this->getDefaultTabHiddenFilters($searchClassId); |
137 | } |
138 | return null === $filters |
139 | ? [] : $this->parseFilters($searchClassId, (array)$filters); |
140 | } |
141 | |
142 | /** |
143 | * Get the tab configuration |
144 | * |
145 | * @return array |
146 | */ |
147 | public function getTabConfig() |
148 | { |
149 | return $this->tabConfig; |
150 | } |
151 | |
152 | /** |
153 | * Get the tab filters |
154 | * |
155 | * @return array |
156 | */ |
157 | public function getTabFilterConfig() |
158 | { |
159 | return $this->filterConfig; |
160 | } |
161 | |
162 | /** |
163 | * Get the tab permissions |
164 | * |
165 | * @return array |
166 | */ |
167 | public function getTabPermissionConfig() |
168 | { |
169 | return $this->permissionConfig; |
170 | } |
171 | |
172 | /** |
173 | * Get the tab details |
174 | * |
175 | * @return array |
176 | */ |
177 | public function getSettings() |
178 | { |
179 | return $this->settings; |
180 | } |
181 | |
182 | /** |
183 | * Extract search class name from a tab id |
184 | * |
185 | * @param string $tabId Tab id as defined in config.ini |
186 | * |
187 | * @return string |
188 | */ |
189 | public function extractClassName($tabId) |
190 | { |
191 | [$class] = explode(':', $tabId, 2); |
192 | return $class; |
193 | } |
194 | |
195 | /** |
196 | * Check if given hidden filters match with the hidden filters from configuration |
197 | * |
198 | * @param string $class Search class ID |
199 | * @param array $hiddenFilters Hidden filters |
200 | * @param array $configFilters Filters from filter configuration |
201 | * |
202 | * @return bool |
203 | */ |
204 | public function filtersMatch($class, $hiddenFilters, $configFilters) |
205 | { |
206 | return $hiddenFilters == $this->parseFilters($class, $configFilters); |
207 | } |
208 | |
209 | /** |
210 | * Get an array of hidden filters for the default tab of the given search class |
211 | * |
212 | * @param string $searchClassId Search class |
213 | * |
214 | * @return null|array |
215 | */ |
216 | protected function getDefaultTabHiddenFilters($searchClassId) |
217 | { |
218 | if (empty($this->tabConfig)) { |
219 | return null; |
220 | } |
221 | |
222 | $firstTab = null; |
223 | foreach (array_keys($this->tabConfig) as $key) { |
224 | $class = $this->extractClassName($key); |
225 | if ($class == $searchClassId) { |
226 | if (null === $firstTab) { |
227 | $firstTab = $key; |
228 | } |
229 | if (empty($this->filterConfig[$key])) { |
230 | return null; |
231 | } |
232 | } |
233 | } |
234 | if (null === $firstTab || empty($this->filterConfig[$firstTab])) { |
235 | return null; |
236 | } |
237 | |
238 | return (array)$this->filterConfig[$firstTab]; |
239 | } |
240 | |
241 | /** |
242 | * Parse a simple filter array to a keyed array |
243 | * |
244 | * @param string $class Search class ID |
245 | * @param array $filters Filters to parse |
246 | * |
247 | * @return array |
248 | */ |
249 | protected function parseFilters($class, $filters) |
250 | { |
251 | $results = $this->results->get($class); |
252 | $params = $results->getParams(); |
253 | $result = []; |
254 | foreach ($filters as $filter) { |
255 | [$field, $value] = $params->parseFilter($filter); |
256 | $result[$field][] = $value; |
257 | } |
258 | return $result; |
259 | } |
260 | } |