Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 78
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ILLRequests
0.00% covered (danger)
0.00%
0 / 78
0.00% covered (danger)
0.00%
0 / 2
272
0.00% covered (danger)
0.00%
0 / 1
 addCancelDetails
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 cancelILLRequests
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 1
182
1<?php
2
3/**
4 * VuFind Action Helper - ILL Requests Support Methods
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) The National Library of Finland 2014.
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_Plugins
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 Main Page
30 */
31
32namespace VuFind\Controller\Plugin;
33
34use function in_array;
35
36/**
37 * Action helper to perform ILL request related actions
38 *
39 * @category VuFind
40 * @package  Controller_Plugins
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @author   Ere Maijala <ere.maijala@helsinki.fi>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Page
45 */
46class ILLRequests extends AbstractRequestBase
47{
48    /**
49     * Update ILS details with cancellation-specific information, if appropriate.
50     *
51     * @param \VuFind\ILS\Connection $catalog      ILS connection object
52     * @param array                  $ilsDetails   Details from ILS driver's
53     * getMyILLRequests() method
54     * @param array                  $cancelStatus Cancellation settings from ILS
55     * driver's checkFunction() method
56     * @param array                  $patron       ILS patron
57     *
58     * @return array $ilsDetails with cancellation info added
59     */
60    public function addCancelDetails($catalog, $ilsDetails, $cancelStatus, $patron)
61    {
62        // Generate form details for cancelling requests if enabled
63        if ($cancelStatus) {
64            if (
65                $cancelStatus['function'] == 'getCancelILLRequestsLink'
66            ) {
67                // Build OPAC URL
68                $ilsDetails['cancel_link']
69                    = $catalog->getCancelILLRequestLink(
70                        $ilsDetails,
71                        $patron
72                    );
73            } else {
74                // Form Details
75                $ilsDetails['cancel_details']
76                    = $catalog->getCancelILLRequestDetails(
77                        $ilsDetails,
78                        $patron
79                    );
80                $this->rememberValidId(
81                    $ilsDetails['cancel_details']
82                );
83            }
84        }
85
86        return $ilsDetails;
87    }
88
89    /**
90     * Process cancel request.
91     *
92     * @param \VuFind\ILS\Connection $catalog ILS connection object
93     * @param array                  $patron  Current logged in patron
94     *
95     * @return array                          The result of the cancellation, an
96     * associative array keyed by item ID (empty if no cancellations performed)
97     */
98    public function cancelILLRequests($catalog, $patron)
99    {
100        // Retrieve the flashMessenger helper:
101        $flashMsg = $this->getController()->flashMessenger();
102        $params = $this->getController()->params();
103
104        // Pick IDs to cancel based on which button was pressed:
105        $all = $params->fromPost('cancelAll');
106        $selected = $params->fromPost('cancelSelected');
107        if (!empty($all)) {
108            $details = $params->fromPost('cancelAllIDS');
109        } elseif (!empty($selected)) {
110            $details = $params->fromPost('cancelSelectedIDS');
111        } else {
112            // No button pushed -- no action needed
113            return [];
114        }
115
116        if (!empty($details)) {
117            // Confirm?
118            if ($params->fromPost('confirm') === '0') {
119                $url = $this->getController()->url()
120                    ->fromRoute('myresearch-illrequests');
121                if ($params->fromPost('cancelAll') !== null) {
122                    return $this->getController()->confirm(
123                        'ill_request_cancel_all',
124                        $url,
125                        $url,
126                        'confirm_ill_request_cancel_all_text',
127                        [
128                            'cancelAll' => 1,
129                            'cancelAllIDS' => $params->fromPost('cancelAllIDS'),
130                        ]
131                    );
132                } else {
133                    return $this->getController()->confirm(
134                        'ill_request_cancel_selected',
135                        $url,
136                        $url,
137                        'confirm_ill_request_cancel_selected_text',
138                        [
139                            'cancelSelected' => 1,
140                            'cancelSelectedIDS' =>
141                                $params->fromPost('cancelSelectedIDS'),
142                        ]
143                    );
144                }
145            }
146
147            foreach ($details as $info) {
148                // If the user input contains a value not found in the session
149                // legal list, something has been tampered with -- abort the process.
150                if (!in_array($info, $this->getSession()->validIds)) {
151                    $flashMsg->addMessage('error_inconsistent_parameters', 'error');
152                    return [];
153                }
154            }
155
156            // Add Patron Data to Submitted Data
157            $cancelResults = $catalog->cancelILLRequests(
158                ['details' => $details, 'patron' => $patron]
159            );
160            if ($cancelResults == false) {
161                $flashMsg->addMessage('ill_request_cancel_fail', 'error');
162            } else {
163                $failed = 0;
164                foreach ($cancelResults['items'] ?? [] as $item) {
165                    if (!$item['success']) {
166                        ++$failed;
167                    }
168                }
169                if ($failed) {
170                    $flashMsg->addErrorMessage(
171                        ['msg' => 'ill_request_cancel_fail_items', 'tokens' => ['%%count%%' => $failed]]
172                    );
173                }
174                if ($cancelResults['count'] > 0) {
175                    $flashMsg->addSuccessMessage(
176                        [
177                            'msg' => 'ill_request_cancel_success_items',
178                            'tokens' => ['%%count%%' => $cancelResults['count']],
179                        ]
180                    );
181                }
182                return $cancelResults;
183            }
184        } else {
185            $flashMsg->addMessage('ill_request_empty_selection', 'error');
186        }
187        return [];
188    }
189}