Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
GetRecordCover | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
handleRequest | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | /** |
4 | * GetRecordCover 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 Josef Moravec <moravec@mzk.cz> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\AjaxHandler; |
31 | |
32 | use Laminas\Mvc\Controller\Plugin\Params; |
33 | use Laminas\View\Renderer\PhpRenderer; |
34 | use VuFind\Cache\CacheTrait; |
35 | use VuFind\Cover\Router as CoverRouter; |
36 | use VuFind\Record\Loader as RecordLoader; |
37 | use VuFind\Session\Settings as SessionSettings; |
38 | |
39 | use function in_array; |
40 | |
41 | /** |
42 | * GetRecordCover AJAX handler. |
43 | * |
44 | * @category VuFind |
45 | * @package AJAX |
46 | * @author Josef Moravec <moravec@mzk.cz> |
47 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
48 | * @link https://vufind.org/wiki/development Wiki |
49 | */ |
50 | class GetRecordCover extends AbstractBase implements AjaxHandlerInterface |
51 | { |
52 | use CacheTrait; |
53 | |
54 | /** |
55 | * Record loader |
56 | * |
57 | * @var RecordLoader |
58 | */ |
59 | protected $recordLoader; |
60 | |
61 | /** |
62 | * Cover router |
63 | * |
64 | * @var CoverRouter |
65 | */ |
66 | protected $coverRouter; |
67 | |
68 | /** |
69 | * PHP renderer |
70 | * |
71 | * @var PhpRenderer |
72 | */ |
73 | protected $renderer; |
74 | |
75 | /** |
76 | * If true we will render a fallback html template in case no image could be |
77 | * loaded |
78 | * |
79 | * @var bool |
80 | */ |
81 | protected $useCoverFallbacksOnFail = false; |
82 | |
83 | /** |
84 | * GetRecordCover constructor. |
85 | * |
86 | * @param SessionSettings $ss Session settings |
87 | * @param RecordLoader $recordLoader Record loader |
88 | * @param CoverRouter $coverRouter Cover router |
89 | * @param PhpRenderer $renderer PHP renderer (only |
90 | * required if $userCoverFallbacksOnFail is set to true) |
91 | * @param bool $useCoverFallbacksOnFail If true we will render a |
92 | * fallback html template in case no image could be loaded |
93 | */ |
94 | public function __construct( |
95 | SessionSettings $ss, |
96 | RecordLoader $recordLoader, |
97 | CoverRouter $coverRouter, |
98 | ?PhpRenderer $renderer = null, |
99 | $useCoverFallbacksOnFail = false |
100 | ) { |
101 | $this->sessionSettings = $ss; |
102 | $this->recordLoader = $recordLoader; |
103 | $this->coverRouter = $coverRouter; |
104 | $this->renderer = $renderer; |
105 | $this->useCoverFallbacksOnFail = $useCoverFallbacksOnFail; |
106 | } |
107 | |
108 | /** |
109 | * Handle request |
110 | * |
111 | * @param Params $params Request parameters |
112 | * |
113 | * @return array |
114 | * @throws \Exception |
115 | */ |
116 | public function handleRequest(Params $params) |
117 | { |
118 | $this->disableSessionWrites(); |
119 | |
120 | $recordId = $params->fromQuery('recordId'); |
121 | $recordSource = $params->fromQuery('source', DEFAULT_SEARCH_BACKEND); |
122 | $size = $params->fromQuery('size', 'small'); |
123 | if (!in_array($size, ['small', 'medium', 'large'])) { |
124 | $size = 'small'; |
125 | } |
126 | $record = $this->recordLoader->load($recordId, $recordSource, true); |
127 | $metadata = $this->coverRouter->getMetadata( |
128 | $record, |
129 | $size ?? 'small', |
130 | true, |
131 | $this->useCoverFallbacksOnFail, |
132 | true |
133 | ); |
134 | |
135 | return ($metadata || !$this->renderer || !$this->useCoverFallbacksOnFail) |
136 | ? $this->formatResponse(array_merge($metadata, compact('size'))) |
137 | : $this->formatResponse( |
138 | [ |
139 | 'html' => $this->renderer->render( |
140 | 'record/coverReplacement', |
141 | ['driver' => $record] |
142 | ), |
143 | ] |
144 | ); |
145 | } |
146 | } |