Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Renewals
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 2
462
0.00% covered (danger)
0.00%
0 / 1
 addRenewDetails
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 processRenewals
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
342
1<?php
2
3/**
4 * VuFind Action Helper - Renewals Support Methods
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_Plugins
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 Page
28 */
29
30namespace VuFind\Controller\Plugin;
31
32use Laminas\Mvc\Controller\Plugin\AbstractPlugin;
33use VuFind\Validator\CsrfInterface;
34
35use function is_array;
36
37/**
38 * Action helper to perform renewal-related actions
39 *
40 * @category VuFind
41 * @package  Controller_Plugins
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Page
45 */
46class Renewals extends AbstractPlugin
47{
48    /**
49     * Update ILS details with renewal-specific information, if appropriate.
50     *
51     * @param \VuFind\ILS\Connection $catalog     ILS connection object
52     * @param array                  $ilsDetails  Transaction details from ILS
53     * driver's getMyTransactions() method
54     * @param array                  $renewStatus Renewal settings from ILS driver's
55     * checkFunction() method
56     *
57     * @return array $ilsDetails with renewal info added
58     */
59    public function addRenewDetails($catalog, $ilsDetails, $renewStatus)
60    {
61        // Only add renewal information if enabled:
62        if ($renewStatus) {
63            if ($renewStatus['function'] == 'renewMyItemsLink') {
64                // Build OPAC URL
65                $ilsDetails['renew_link'] = $catalog->renewMyItemsLink($ilsDetails);
66            } else {
67                // Form Details
68                $ilsDetails['renew_details']
69                    = $catalog->getRenewDetails($ilsDetails);
70            }
71        }
72
73        // Send back the modified array:
74        return $ilsDetails;
75    }
76
77    /**
78     * Process renewal requests.
79     *
80     * @param \Laminas\Stdlib\Parameters $request       Request object
81     * @param \VuFind\ILS\Connection     $catalog       ILS connection object
82     * @param array                      $patron        Current logged in patron
83     * @param CsrfInterface              $csrfValidator CSRF validator
84     *
85     * @return array                  The result of the renewal, an
86     * associative array keyed by item ID (empty if no renewals performed)
87     */
88    public function processRenewals(
89        $request,
90        $catalog,
91        $patron,
92        $csrfValidator = null
93    ) {
94        // Pick IDs to renew based on which button was pressed:
95        $all = $request->get('renewAll');
96        $selected = $request->get('renewSelected');
97        if (!empty($all)) {
98            $ids = $request->get('renewAllIDS');
99        } elseif (!empty($selected)) {
100            $ids = $request->get('selectAll')
101                ? $request->get('selectAllIDS')
102                : $request->get('renewSelectedIDS');
103        } else {
104            $ids = [];
105        }
106
107        // Retrieve the flashMessenger helper:
108        $flashMsg = $this->getController()->flashMessenger();
109
110        // If there is actually something to renew, attempt the renewal action:
111        if (is_array($ids) && !empty($ids)) {
112            if (null !== $csrfValidator) {
113                if (!$csrfValidator->isValid($request->get('csrf'))) {
114                    $flashMsg->addErrorMessage('csrf_validation_failed');
115                    return [];
116                }
117                // After successful token verification, clear list to shrink session
118                // and prevent double submit:
119                $csrfValidator->trimTokenList(0);
120            }
121
122            $renewResult = $catalog->renewMyItems(
123                ['details' => $ids, 'patron' => $patron]
124            );
125            if ($renewResult !== false) {
126                // Assign Blocks to the Template
127                if (is_array($renewResult['blocks'] ?? null)) {
128                    foreach ($renewResult['blocks'] as $block) {
129                        $flashMsg->addMessage($block, 'info');
130                    }
131                } elseif (is_array($renewResult['details'] ?? null)) {
132                    $bad = $good = 0;
133                    foreach ($renewResult['details'] as $next) {
134                        if ($next['success'] ?? false) {
135                            $good++;
136                        } else {
137                            $bad++;
138                        }
139                    }
140                    if ($good > 0) {
141                        $flashMsg->addMessage(
142                            ['msg' => 'renew_success_summary', 'tokens' => ['count' => $good], 'icu' => true],
143                            'success'
144                        );
145                    }
146                    if ($bad > 0) {
147                        $flashMsg->addMessage(
148                            ['msg' => 'renew_error_summary', 'tokens' => ['count' => $bad], 'icu' => true],
149                            'error'
150                        );
151                    }
152                }
153
154                // Send back result details:
155                return $renewResult['details'];
156            } else {
157                // System failure:
158                $flashMsg->addMessage('renew_error', 'error');
159            }
160        } elseif (!empty($all) || !empty($selected)) {
161            // Button was clicked but no items were selected:
162            $flashMsg->addMessage('renew_empty_selection', 'error');
163        }
164
165        return [];
166    }
167}