Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ILLRequestsTrait
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 1
342
0.00% covered (danger)
0.00%
0 / 1
 illRequestAction
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 1
342
1<?php
2
3/**
4 * ILL 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 * ILL 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 ILLRequestsTrait
45{
46    /**
47     * Action for dealing with ILL requests.
48     *
49     * @return mixed
50     */
51    public function illRequestAction()
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            'ILLRequests',
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->ILLRequests()->validateRequest(
76            $checkRequests['HMACKeys']
77        );
78        if (!$gatheredDetails) {
79            return $this->redirectToRecord();
80        }
81
82        // Block invalid requests:
83        $validRequest = $catalog->checkILLRequestIsValid(
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'] : 'ill_request_error_blocked'
92            );
93            return $this->redirectToRecord('#top');
94        }
95
96        // Send various values to the view so we can build the form:
97
98        $extraFields = isset($checkRequests['extraFields'])
99            ? explode(':', $checkRequests['extraFields']) : [];
100
101        // Process form submissions if necessary:
102        if (null !== $this->params()->fromPost('placeILLRequest')) {
103            // If we made it this far, we're ready to place the hold;
104            // if successful, we will redirect and can stop here.
105
106            // Add Patron Data to Submitted Data
107            $details = $gatheredDetails + ['patron' => $patron];
108
109            // Attempt to place the hold:
110            $function = (string)$checkRequests['function'];
111            $results = $catalog->$function($details);
112
113            // Success: Go to Display ILL Requests
114            if (isset($results['success']) && $results['success'] == true) {
115                $msg = [
116                    'html' => true,
117                    'msg' => 'ill_request_place_success_html',
118                    'tokens' => [
119                        '%%url%%' => $this->url()
120                            ->fromRoute('myresearch-illrequests'),
121                    ],
122                ];
123                $this->flashMessenger()->addMessage($msg, 'success');
124                $this->getViewRenderer()->plugin('session')->put('reset_account_status', true);
125                return $this->redirectToRecord($this->inLightbox() ? '?layout=lightbox' : '');
126            } else {
127                // Failure: use flash messenger to display messages, stay on
128                // the current form.
129                if (isset($results['status'])) {
130                    $this->flashMessenger()
131                        ->addMessage($results['status'], 'error');
132                }
133                if (isset($results['sysMessage'])) {
134                    $this->flashMessenger()
135                        ->addMessage($results['sysMessage'], 'error');
136                }
137            }
138        }
139
140        // Find and format the default required date:
141        $defaultRequiredDate = $this->ILLRequests()
142            ->getDefaultRequiredDate($checkRequests);
143        $defaultRequiredDate
144            = $this->serviceLocator->get(\VuFind\Date\Converter::class)
145            ->convertToDisplayDate('U', $defaultRequiredDate);
146
147        // Get pickup libraries
148        $pickupLibraries = $catalog->getILLPickUpLibraries(
149            $driver->getUniqueID(),
150            $patron,
151            $gatheredDetails
152        );
153
154        // Get pickup locations. Note that these are independent of pickup library,
155        // and library specific locations must be retrieved when a library is
156        // selected.
157        $pickupLocations = $catalog->getPickUpLocations($patron, $gatheredDetails);
158
159        // Check that there are pick up locations to choose from if the field is
160        // required:
161        if (in_array('pickUpLocation', $extraFields) && !$pickupLocations) {
162            $this->flashMessenger()
163                ->addErrorMessage('No pickup locations available');
164            return $this->redirectToRecord('#top');
165        }
166
167        $config = $this->getConfig();
168        $homeLibrary = ($config->Account->set_home_library ?? true)
169            ? $this->getUser()->getHomeLibrary() : '';
170        // helpText is only for backward compatibility:
171        $helpText = $helpTextHtml = $checkRequests['helpText'];
172
173        $view = $this->createViewModel(
174            compact(
175                'gatheredDetails',
176                'pickupLibraries',
177                'pickupLocations',
178                'homeLibrary',
179                'extraFields',
180                'defaultRequiredDate',
181                'helpText',
182                'helpTextHtml'
183            )
184        );
185        $view->setTemplate('record/illrequest');
186        return $view;
187    }
188}