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