Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
StorageRetrievalRequestsTrait
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 1
420
0.00% covered (danger)
0.00%
0 / 1
 storageRetrievalRequestAction
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 1
420
1<?php
2
3/**
4 * Storage retrieval requests trait (for subclasses of AbstractRecord)
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  Controller
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 Main Site
28 */
29
30namespace VuFind\Controller;
31
32use function in_array;
33use function is_array;
34
35/**
36 * Storage retrieval requests trait (for subclasses of AbstractRecord)
37 *
38 * @category VuFind
39 * @package  Controller
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org Main Site
43 */
44trait StorageRetrievalRequestsTrait
45{
46    /**
47     * Action for dealing with storage retrieval requests.
48     *
49     * @return mixed
50     */
51    public function storageRetrievalRequestAction()
52    {
53        $driver = $this->loadRecord();
54
55        // Stop now if the user does not have valid catalog credentials available:
56        if (!is_array($patron = $this->catalogLogin())) {
57            return $patron;
58        }
59
60        // If we're not supposed to be here, give up now!
61        $catalog = $this->getILS();
62        $checkRequests = $catalog->checkFunction(
63            'StorageRetrievalRequests',
64            [
65                'id' => $driver->getUniqueID(),
66                'patron' => $patron,
67            ]
68        );
69        if (!$checkRequests) {
70            return $this->redirectToRecord();
71        }
72
73        // Do we have valid information?
74        // Sets $this->logonURL and $this->gatheredDetails
75        $gatheredDetails = $this->storageRetrievalRequests()->validateRequest(
76            $checkRequests['HMACKeys']
77        );
78        if (!$gatheredDetails) {
79            return $this->redirectToRecord();
80        }
81
82        // Block invalid requests:
83        $validRequest = $catalog->checkStorageRetrievalRequestIsValid(
84            $driver->getUniqueID(),
85            $gatheredDetails,
86            $patron
87        );
88        if ((is_array($validRequest) && !$validRequest['valid']) || !$validRequest) {
89            $this->flashMessenger()->addErrorMessage(
90                is_array($validRequest)
91                    ? $validRequest['status']
92                    : 'storage_retrieval_request_error_blocked'
93            );
94            return $this->redirectToRecord('#top');
95        }
96
97        // Send various values to the view so we can build the form:
98        $pickup = $catalog->getPickUpLocations($patron, $gatheredDetails);
99        $extraFields = isset($checkRequests['extraFields'])
100            ? explode(':', $checkRequests['extraFields']) : [];
101
102        // Check that there are pick up locations to choose from if the field is
103        // required:
104        if (in_array('pickUpLocation', $extraFields) && !$pickup) {
105            $this->flashMessenger()
106                ->addErrorMessage('No pickup locations available');
107            return $this->redirectToRecord('#top');
108        }
109
110        // Process form submissions if necessary:
111        if (null !== $this->params()->fromPost('placeStorageRetrievalRequest')) {
112            // If we made it this far, we're ready to place the hold;
113            // if successful, we will redirect and can stop here.
114
115            // Check that any pick up location is valid:
116            $validPickup = $this->storageRetrievalRequests()->validatePickUpInput(
117                $gatheredDetails['pickUpLocation'] ?? null,
118                $extraFields,
119                $pickup
120            );
121            if (!$validPickup) {
122                $this->flashMessenger()
123                    ->addErrorMessage('storage_retrieval_request_invalid_pickup');
124            } else {
125                // Add Patron Data to Submitted Data
126                $details = $gatheredDetails + ['patron' => $patron];
127
128                // Attempt to place the hold:
129                $function = (string)$checkRequests['function'];
130                $results = $catalog->$function($details);
131
132                // Success: Go to Display Storage Retrieval Requests
133                if (isset($results['success']) && $results['success'] == true) {
134                    $msg = [
135                        'html' => true,
136                        'msg' => 'storage_retrieval_request_place_success_html',
137                        'tokens' => [
138                            '%%url%%' => $this->url()
139                                ->fromRoute('myresearch-storageretrievalrequests'),
140                        ],
141                    ];
142                    $this->flashMessenger()->addMessage($msg, 'success');
143                    $this->getViewRenderer()->plugin('session')->put('reset_account_status', true);
144                    return $this->redirectToRecord($this->inLightbox() ? '?layout=lightbox' : '');
145                } else {
146                    // Failure: use flash messenger to display messages, stay on
147                    // the current form.
148                    if (isset($results['status'])) {
149                        $this->flashMessenger()->addErrorMessage($results['status']);
150                    }
151                    if (isset($results['sysMessage'])) {
152                        $this->flashMessenger()
153                            ->addMessage($results['sysMessage'], 'error');
154                    }
155                }
156            }
157        }
158
159        // Find and format the default required date:
160        $defaultRequiredDate = $this->storageRetrievalRequests()
161            ->getDefaultRequiredDate($checkRequests);
162        $defaultRequiredDate = $this->getService(\VuFind\Date\Converter::class)
163            ->convertToDisplayDate('U', $defaultRequiredDate);
164        try {
165            $defaultPickup
166                = $catalog->getDefaultPickUpLocation($patron, $gatheredDetails);
167        } catch (\Exception $e) {
168            $defaultPickup = false;
169        }
170
171        $config = $this->getConfig();
172        $homeLibrary = ($config->Account->set_home_library ?? true)
173            ? $this->getUser()->getHomeLibrary() : '';
174        // helpText is only for backward compatibility:
175        $helpText = $helpTextHtml = $checkRequests['helpText'];
176
177        $view = $this->createViewModel(
178            compact(
179                'gatheredDetails',
180                'pickup',
181                'defaultPickup',
182                'homeLibrary',
183                'extraFields',
184                'defaultRequiredDate',
185                'helpText',
186                'helpTextHtml'
187            )
188        );
189        $view->setTemplate('record/storageretrievalrequest');
190        return $view;
191    }
192}