Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.82% |
36 / 44 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CheckRequestIsValid | |
81.82% |
36 / 44 |
|
0.00% |
0 / 2 |
12.87 | |
0.00% |
0 / 1 |
getStatusMessage | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
handleRequest | |
82.93% |
34 / 41 |
|
0.00% |
0 / 1 |
10.50 |
1 | <?php |
2 | |
3 | /** |
4 | * "Check Request is Valid" AJAX handler |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2018. |
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 AJAX |
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/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\AjaxHandler; |
31 | |
32 | use Laminas\Mvc\Controller\Plugin\Params; |
33 | |
34 | use function is_array; |
35 | |
36 | /** |
37 | * "Check Request is Valid" AJAX handler |
38 | * |
39 | * @category VuFind |
40 | * @package AJAX |
41 | * @author Demian Katz <demian.katz@villanova.edu> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org/wiki/development Wiki |
44 | */ |
45 | class CheckRequestIsValid extends AbstractIlsAndUserAction |
46 | { |
47 | /** |
48 | * Status messages |
49 | * |
50 | * @var array |
51 | */ |
52 | protected $statuses = [ |
53 | 'ILLRequest' => [ |
54 | 'success' => 'ill_request_place_text', |
55 | 'failure' => 'ill_request_error_blocked', |
56 | ], |
57 | 'StorageRetrievalRequest' => [ |
58 | 'success' => 'storage_retrieval_request_place_text', |
59 | 'failure' => 'storage_retrieval_request_error_blocked', |
60 | ], |
61 | ]; |
62 | |
63 | /** |
64 | * Given a request type and a boolean success status, return an appropriate |
65 | * message. |
66 | * |
67 | * @param string $requestType Type of request being made |
68 | * @param bool $results Result status |
69 | * |
70 | * @return string |
71 | */ |
72 | protected function getStatusMessage($requestType, $results) |
73 | { |
74 | // If successful, return success message: |
75 | if ($results) { |
76 | return $this->statuses[$requestType]['success'] ?? 'request_place_text'; |
77 | } |
78 | // If unsuccessful, return failure message: |
79 | return $this->statuses[$requestType]['failure'] ?? 'hold_error_blocked'; |
80 | } |
81 | |
82 | /** |
83 | * Handle a request. |
84 | * |
85 | * @param Params $params Parameter helper from controller |
86 | * |
87 | * @return array [response data, HTTP status code] |
88 | */ |
89 | public function handleRequest(Params $params) |
90 | { |
91 | $this->disableSessionWrites(); // avoid session write timing bug |
92 | $id = $params->fromQuery('id'); |
93 | $data = $params->fromQuery('data'); |
94 | $requestType = $params->fromQuery('requestType'); |
95 | if (empty($id) || empty($data)) { |
96 | return $this->formatResponse( |
97 | $this->translate('bulk_error_missing'), |
98 | self::STATUS_HTTP_BAD_REQUEST |
99 | ); |
100 | } |
101 | // check if user is logged in |
102 | if (!$this->user) { |
103 | return $this->formatResponse( |
104 | $this->translate('You must be logged in first'), |
105 | self::STATUS_HTTP_NEED_AUTH |
106 | ); |
107 | } |
108 | |
109 | try { |
110 | $patron = $this->ilsAuthenticator->storedCatalogLogin(); |
111 | if ($patron) { |
112 | switch ($requestType) { |
113 | case 'ILLRequest': |
114 | $results = $this->ils |
115 | ->checkILLRequestIsValid($id, $data, $patron); |
116 | break; |
117 | case 'StorageRetrievalRequest': |
118 | $results = $this->ils->checkStorageRetrievalRequestIsValid( |
119 | $id, |
120 | $data, |
121 | $patron |
122 | ); |
123 | break; |
124 | default: |
125 | $results = $this->ils |
126 | ->checkRequestIsValid($id, $data, $patron); |
127 | break; |
128 | } |
129 | if (is_array($results)) { |
130 | $msg = $results['status']; |
131 | $results = $results['valid']; |
132 | } else { |
133 | $msg = $this->getStatusMessage($requestType, $results); |
134 | } |
135 | return $this->formatResponse( |
136 | ['status' => $results, 'msg' => $this->translate($msg)] |
137 | ); |
138 | } |
139 | } catch (\Exception $e) { |
140 | // Do nothing -- just fail through to the error message below. |
141 | } |
142 | |
143 | return $this->formatResponse( |
144 | $this->translate('An error has occurred'), |
145 | self::STATUS_HTTP_ERROR |
146 | ); |
147 | } |
148 | } |