Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetUserTransactions
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 handleRequest
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
30
 setPaginationHelper
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPaginationHelper
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * "Get User Transactions" 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
30namespace VuFind\AjaxHandler;
31
32use Laminas\Mvc\Controller\Plugin\Params;
33
34/**
35 * "Get User Transactions" AJAX handler
36 *
37 * @category VuFind
38 * @package  AJAX
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class GetUserTransactions extends AbstractIlsAndUserAction
44{
45    use \VuFind\ILS\Logic\SummaryTrait;
46
47    /**
48     * Paginator
49     *
50     * @var \VuFind\ILS\PaginationHelper
51     */
52    protected $paginationHelper = null;
53
54    /**
55     * Handle a request.
56     *
57     * @param Params $params Parameter helper from controller
58     *
59     * @return array [response data, internal status code, HTTP status code]
60     */
61    public function handleRequest(Params $params)
62    {
63        $this->disableSessionWrites();  // avoid session write timing bug
64        $patron = $this->ilsAuthenticator->storedCatalogLogin();
65        if (!$patron) {
66            return $this->formatResponse('', self::STATUS_HTTP_NEED_AUTH);
67        }
68        if (!$this->ils->checkCapability('getMyTransactions')) {
69            return $this->formatResponse('', self::STATUS_HTTP_ERROR);
70        }
71
72        $counts = [];
73        $functionConfig = $this->ils->checkFunction('getMyTransactions', $patron);
74        $page = 1;
75        do {
76            // Try to use large page size, but take ILS limits into account
77            $pageOptions = $this->getPaginationHelper()
78                ->getOptions($page, null, 1000, $functionConfig);
79            $result = $this->ils
80                ->getMyTransactions($patron, $pageOptions['ilsParams']);
81
82            $summary = $this->getTransactionSummary($result['records']);
83            foreach ($summary as $key => $value) {
84                $counts[$key] = ($counts[$key] ?? 0) + $value;
85            }
86            $pageEnd = $pageOptions['ilsPaging']
87                ? ceil($result['count'] / $pageOptions['limit'])
88                : 1;
89            $page++;
90        } while ($page <= $pageEnd);
91
92        return $this->formatResponse($counts);
93    }
94
95    /**
96     * Set the ILS pagination helper
97     *
98     * @param \VuFind\ILS\PaginationHelper $helper Pagination helper
99     *
100     * @return void
101     */
102    protected function setPaginationHelper($helper)
103    {
104        $this->paginationHelper = $helper;
105    }
106
107    /**
108     * Get the ILS pagination helper
109     *
110     * @return \VuFind\ILS\PaginationHelper
111     */
112    protected function getPaginationHelper()
113    {
114        if (null === $this->paginationHelper) {
115            $this->paginationHelper = new \VuFind\ILS\PaginationHelper();
116        }
117        return $this->paginationHelper;
118    }
119}