1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
6
|
|
|
* |
7
|
|
|
* @license GNU General Public License version 3 or later. |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kitodo\Dlf\Controller; |
13
|
|
|
|
14
|
|
|
use Kitodo\Dlf\Common\Doc; |
15
|
|
|
use Kitodo\Dlf\Common\Helper; |
16
|
|
|
use Kitodo\Dlf\Domain\Model\Document; |
17
|
|
|
use Kitodo\Dlf\Domain\Repository\DocumentRepository; |
18
|
|
|
use Psr\Log\LoggerAwareInterface; |
19
|
|
|
use Psr\Log\LoggerAwareTrait; |
20
|
|
|
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; |
21
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
22
|
|
|
use TYPO3\CMS\Core\Localization\LanguageService; |
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
24
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Abstract controller class for most of the plugin controller. |
29
|
|
|
* |
30
|
|
|
* @author Sebastian Meyer <[email protected]> |
31
|
|
|
* @package TYPO3 |
32
|
|
|
* @subpackage dlf |
33
|
|
|
* @access public |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController implements LoggerAwareInterface |
36
|
|
|
{ |
37
|
|
|
use LoggerAwareTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var DocumentRepository |
41
|
|
|
*/ |
42
|
|
|
protected $documentRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param DocumentRepository $documentRepository |
46
|
|
|
*/ |
47
|
|
|
public function injectDocumentRepository(DocumentRepository $documentRepository) |
48
|
|
|
{ |
49
|
|
|
$this->documentRepository = $documentRepository; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* This holds the current document |
54
|
|
|
* |
55
|
|
|
* @var \Kitodo\Dlf\Domain\Model\Document |
56
|
|
|
* @access protected |
57
|
|
|
*/ |
58
|
|
|
protected $document; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
* @access protected |
63
|
|
|
*/ |
64
|
|
|
protected $extConf; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* This holds the request parameter |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
* @access protected |
71
|
|
|
*/ |
72
|
|
|
protected $requestData; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* This holds some common data for the fluid view |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
* @access protected |
79
|
|
|
*/ |
80
|
|
|
protected $viewData; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Initialize the plugin controller |
84
|
|
|
* |
85
|
|
|
* @access protected |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
protected function initialize() |
89
|
|
|
{ |
90
|
|
|
$this->requestData = GeneralUtility::_GPmerged('tx_dlf'); |
91
|
|
|
if (empty($this->requestData['page'])) { |
92
|
|
|
$this->requestData['page'] = 1; |
93
|
|
|
} |
94
|
|
|
$this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0); |
95
|
|
|
|
96
|
|
|
// Get extension configuration. |
97
|
|
|
$this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
98
|
|
|
|
99
|
|
|
$this->viewData = [ |
100
|
|
|
'pageUid' => $GLOBALS['TSFE']->id, |
101
|
|
|
'uniqueId'=> uniqid(), |
102
|
|
|
'requestData' => $this->requestData |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Loads the current document into $this->document |
108
|
|
|
* |
109
|
|
|
* @access protected |
110
|
|
|
* |
111
|
|
|
* @param array $requestData: The request data |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
protected function loadDocument($requestData) |
116
|
|
|
{ |
117
|
|
|
// Try to get document format from database |
118
|
|
|
if (!empty($requestData['id'])) { |
119
|
|
|
|
120
|
|
|
$doc = null; |
121
|
|
|
|
122
|
|
|
if (MathUtility::canBeInterpretedAsInteger($requestData['id'])) { |
123
|
|
|
// find document from repository by uid |
124
|
|
|
$this->document = $this->documentRepository->findOneByIdAndSettings((int) $requestData['id']); |
125
|
|
|
if ($this->document) { |
126
|
|
|
$doc = Doc::getInstance($this->document->getLocation(), $this->settings, true); |
127
|
|
|
} else { |
128
|
|
|
$this->logger->error('Invalid UID "' . $requestData['id'] . '" or PID "' . $this->settings['storagePid'] . '" for document loading'); |
129
|
|
|
} |
130
|
|
|
} else if (GeneralUtility::isValidUrl($requestData['id'])) { |
131
|
|
|
|
132
|
|
|
$doc = Doc::getInstance($requestData['id'], $this->settings, true); |
133
|
|
|
|
134
|
|
|
if ($doc !== null) { |
135
|
|
|
if ($doc->recordId) { |
136
|
|
|
$this->document = $this->documentRepository->findOneByRecordId($doc->recordId); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($this->document === null) { |
140
|
|
|
// create new dummy Document object |
141
|
|
|
$this->document = GeneralUtility::makeInstance(Document::class); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Make sure configuration PID is set when applicable |
145
|
|
|
if ($doc->cPid == 0) { |
146
|
|
|
$doc->cPid = max(intval($this->settings['storagePid']), 0); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->document->setLocation($requestData['id']); |
150
|
|
|
} else { |
151
|
|
|
$this->logger->error('Invalid location given "' . $requestData['id'] . '" for document loading'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($this->document !== null && $doc !== null) { |
156
|
|
|
$this->document->setDoc($doc); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} elseif (!empty($requestData['recordId'])) { |
160
|
|
|
|
161
|
|
|
$this->document = $this->documentRepository->findOneByRecordId($requestData['recordId']); |
162
|
|
|
|
163
|
|
|
if ($this->document !== null) { |
164
|
|
|
$doc = Doc::getInstance($this->document->getLocation(), $this->settings, true); |
|
|
|
|
165
|
|
|
if ($this->document !== null && $doc !== null) { |
166
|
|
|
$this->document->setDoc($doc); |
|
|
|
|
167
|
|
|
} else { |
168
|
|
|
$this->logger->error('Failed to load document with record ID "' . $requestData['recordId'] . '"'); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} else { |
172
|
|
|
$this->logger->error('Invalid ID "' . $requestData['id'] . '" or PID "' . $this->settings['storagePid'] . '" for document loading'); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Checks if doc is missing or is empty (no pages) |
178
|
|
|
* |
179
|
|
|
* @return boolean |
180
|
|
|
*/ |
181
|
|
|
protected function isDocMissingOrEmpty() { |
182
|
|
|
return $this->isDocMissing() || $this->document->getDoc()->numPages < 1; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Checks if doc is missing |
187
|
|
|
* |
188
|
|
|
* @return boolean |
189
|
|
|
*/ |
190
|
|
|
protected function isDocMissing() { |
191
|
|
|
return $this->document === null || $this->document->getDoc() === null; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns the LanguageService |
196
|
|
|
* |
197
|
|
|
* @return LanguageService |
198
|
|
|
*/ |
199
|
|
|
protected function getLanguageService(): LanguageService |
200
|
|
|
{ |
201
|
|
|
return $GLOBALS['LANG']; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Safely gets Parameters from request |
206
|
|
|
* if they exist |
207
|
|
|
* |
208
|
|
|
* @param string $parameterName |
209
|
|
|
* |
210
|
|
|
* @return null|string|array |
211
|
|
|
*/ |
212
|
|
|
protected function getParametersSafely($parameterName) |
213
|
|
|
{ |
214
|
|
|
if ($this->request->hasArgument($parameterName)) { |
215
|
|
|
return $this->request->getArgument($parameterName); |
216
|
|
|
} |
217
|
|
|
return null; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* This is the constructor |
222
|
|
|
* |
223
|
|
|
* @access public |
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
public function __construct() |
228
|
|
|
{ |
229
|
|
|
$this->initialize(); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|