Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
HierarchyController | |
0.00% |
0 / 63 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
outputJSON | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
gettreeAction | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
searchtreeAction | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
20 | |||
getrecordAction | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * Hierarchy Controller |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010-2023. |
9 | * Copyright (C) The National Library of Finland 2024. |
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 Controller |
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:plugins:controllers Wiki |
30 | */ |
31 | |
32 | namespace VuFind\Controller; |
33 | |
34 | use Laminas\Stdlib\ResponseInterface; |
35 | |
36 | use function array_slice; |
37 | use function count; |
38 | use function is_object; |
39 | |
40 | /** |
41 | * Hierarchy Controller |
42 | * |
43 | * @category VuFind |
44 | * @package Controller |
45 | * @author Demian Katz <demian.katz@villanova.edu> |
46 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
47 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
48 | * @link https://vufind.org/wiki/development:plugins:controllers Wiki |
49 | */ |
50 | class HierarchyController extends AbstractBase |
51 | { |
52 | /** |
53 | * Output JSON |
54 | * |
55 | * @param array $result Result to be encoded as JSON |
56 | * @param int $status Response status code |
57 | * |
58 | * @return ResponseInterface |
59 | */ |
60 | protected function outputJSON(array $result, int $status = 200): ResponseInterface |
61 | { |
62 | $response = $this->getResponse(); |
63 | $headers = $response->getHeaders(); |
64 | $headers->addHeaderLine('Content-type', 'application/json'); |
65 | $response->setContent(json_encode($result)); |
66 | $response->setStatusCode($status); |
67 | return $response; |
68 | } |
69 | |
70 | /** |
71 | * Gets a Hierarchy Tree |
72 | * |
73 | * @return mixed |
74 | */ |
75 | public function gettreeAction() |
76 | { |
77 | $this->disableSessionWrites(); // avoid session write timing bug |
78 | |
79 | $id = $this->params()->fromQuery('id'); |
80 | $source = $this->params()->fromQuery('sourceId', DEFAULT_SEARCH_BACKEND); |
81 | $loader = $this->getRecordLoader(); |
82 | $message = 'Service Unavailable'; // default error message |
83 | try { |
84 | $recordDriver = $loader->load($id, $source); |
85 | $hierarchyDriver = $recordDriver->tryMethod('getHierarchyDriver'); |
86 | if (is_object($hierarchyDriver)) { |
87 | return $this->outputJSON([ |
88 | 'html' => $hierarchyDriver->render( |
89 | $recordDriver, |
90 | $this->params()->fromQuery('context', 'Record'), |
91 | 'List', |
92 | $this->params()->fromQuery('hierarchyId', ''), |
93 | $this->params()->fromQuery(), |
94 | ), |
95 | ]); |
96 | } |
97 | } catch (\Exception $e) { |
98 | // Let exceptions fall through to error condition below: |
99 | $message = APPLICATION_ENV === 'development' ? (string)$e : 'Unexpected exception'; |
100 | } |
101 | |
102 | // If we got this far, something went wrong: |
103 | $code = 503; |
104 | $response = ['error' => compact('code', 'message')]; |
105 | return $this->outputJSON($response, $code); |
106 | } |
107 | |
108 | /** |
109 | * Search the tree and output a JSON result of items that matched the keywords. |
110 | * |
111 | * @return ResponseInterface |
112 | */ |
113 | public function searchtreeAction(): ResponseInterface |
114 | { |
115 | $this->disableSessionWrites(); // avoid session write timing bug |
116 | $config = $this->getConfig(); |
117 | $limit = $config->Hierarchy->treeSearchLimit; |
118 | $resultIDs = []; |
119 | $hierarchyID = $this->params()->fromQuery('hierarchyID'); |
120 | $source = $this->params() |
121 | ->fromQuery('hierarchySource', DEFAULT_SEARCH_BACKEND); |
122 | $lookfor = $this->params()->fromQuery('lookfor', ''); |
123 | $searchType = $this->params()->fromQuery('type', 'AllFields'); |
124 | |
125 | $results = $this->serviceLocator |
126 | ->get(\VuFind\Search\Results\PluginManager::class)->get($source); |
127 | $results->getParams()->setBasicSearch($lookfor, $searchType); |
128 | $results->getParams()->addFilter('hierarchy_top_id:' . $hierarchyID); |
129 | $facets = $results->getFullFieldFacets(['id'], false, null === $limit ? -1 : $limit + 1); |
130 | |
131 | $callback = function ($data) { |
132 | return $data['value']; |
133 | }; |
134 | $resultIDs = isset($facets['id']['data']['list']) |
135 | ? array_map($callback, $facets['id']['data']['list']) : []; |
136 | |
137 | $limitReached = ($limit > 0 && count($resultIDs) > $limit); |
138 | |
139 | $returnArray = [ |
140 | 'limitReached' => $limitReached, |
141 | 'results' => array_slice($resultIDs, 0, $limit), |
142 | ]; |
143 | return $this->outputJSON($returnArray); |
144 | } |
145 | |
146 | /** |
147 | * Get a record for display within a tree |
148 | * |
149 | * @return ResponseInterface |
150 | */ |
151 | public function getrecordAction(): ResponseInterface |
152 | { |
153 | $id = $this->params()->fromQuery('id'); |
154 | $source = $this->params()->fromQuery('source', DEFAULT_SEARCH_BACKEND); |
155 | $loader = $this->getRecordLoader(); |
156 | try { |
157 | $record = $loader->load($id, $source); |
158 | $result = $this->getViewRenderer()->record($record)->getCollectionBriefRecord(); |
159 | } catch (\VuFind\Exception\RecordMissing $e) { |
160 | $result = $this->getViewRenderer()->render('collection/collection-record-error.phtml'); |
161 | } |
162 | $response = $this->getResponse(); |
163 | $response->setContent($result); |
164 | return $response; |
165 | } |
166 | } |