Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetRecordVersions
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getVersionsLinkForRecord
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 handleRequest
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3/**
4 * AJAX handler for fetching versions link
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2019.
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   Ere Maijala <ere.maijala@helsinki.fi>
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;
33use VuFind\Record\Loader;
34use VuFind\RecordTab\TabManager;
35use VuFind\Session\Settings as SessionSettings;
36use VuFind\View\Helper\Root\Record;
37
38use function count;
39use function is_array;
40
41/**
42 * AJAX handler for fetching versions link
43 *
44 * @category VuFind
45 * @package  AJAX
46 * @author   Ere Maijala <ere.maijala@helsinki.fi>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development Wiki
49 */
50class GetRecordVersions extends \VuFind\AjaxHandler\AbstractBase
51{
52    /**
53     * Record loader
54     *
55     * @var Loader
56     */
57    protected $recordLoader;
58
59    /**
60     * Record plugin
61     *
62     * @var Record
63     */
64    protected $recordPlugin;
65
66    /**
67     * Tab manager
68     *
69     * @var TabManager
70     */
71    protected $tabManager;
72
73    /**
74     * Constructor
75     *
76     * @param SessionSettings $ss     Session settings
77     * @param Loader          $loader Record loader
78     * @param Record          $rp     Record plugin
79     * @param TabManager      $tm     Tab manager
80     */
81    public function __construct(
82        SessionSettings $ss,
83        Loader $loader,
84        Record $rp,
85        TabManager $tm
86    ) {
87        $this->sessionSettings = $ss;
88        $this->recordLoader = $loader;
89        $this->recordPlugin = $rp;
90        $this->tabManager = $tm;
91    }
92
93    /**
94     * Load a single record and render the link template
95     *
96     * @param string $id       Record id
97     * @param string $source   Record source
98     * @param string $searchId Search ID
99     *
100     * @return string
101     */
102    protected function getVersionsLinkForRecord($id, $source, $searchId)
103    {
104        $driver = $this->recordLoader->load($id, $source, $searchId);
105        $tabs = $this->tabManager->getTabsForRecord($driver);
106        $full = true;
107
108        return ($this->recordPlugin)($driver)->renderTemplate(
109            'versions-link.phtml',
110            compact('driver', 'tabs', 'full', 'searchId')
111        );
112    }
113
114    /**
115     * Handle a request.
116     *
117     * @param Params $params Parameter helper from controller
118     *
119     * @return array [response data, HTTP status code]
120     */
121    public function handleRequest(Params $params)
122    {
123        $this->disableSessionWrites(); // avoid session write timing bug
124
125        $id = $params->fromPost('id') ?: $params->fromQuery('id');
126        $source = $params->fromPost('source') ?: $params->fromQuery('source');
127        $searchId = $params->fromPost('sid') ?: $params->fromQuery('sid');
128
129        if (!is_array($id)) {
130            return $this->formatResponse(
131                $this->getVersionsLinkForRecord($id, $source, $searchId)
132            );
133        }
134
135        $htmlByRecord = [];
136        for ($i = 0; $i < count($id); $i++) {
137            $key = $source[$i] . '|' . $id[$i];
138
139            $htmlByRecord[$key] = $this->getVersionsLinkForRecord(
140                $id[$i],
141                $source[$i],
142                $searchId
143            );
144        }
145
146        return $this->formatResponse(['records' => $htmlByRecord]);
147    }
148}