forked from kitodo/kitodo-presentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentTypeFunctionProvider.php
226 lines (198 loc) · 8.07 KB
/
DocumentTypeFunctionProvider.php
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
<?php
/**
* (c) Kitodo. Key to digital objects e.V. <[email protected]>
*
* 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\ExpressionLanguage;
use Kitodo\Dlf\Common\AbstractDocument;
use Kitodo\Dlf\Common\IiifManifest;
use Kitodo\Dlf\Domain\Model\Document;
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;
/**
* Provider class for additional "getDocumentType" function to the ExpressionLanguage.
*
* @package TYPO3
* @subpackage dlf
*
* @access public
*/
class DocumentTypeFunctionProvider implements ExpressionFunctionProviderInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @access public
*
* @return ExpressionFunction[] An array of Function instances
*/
public function getFunctions(): array
{
return [
$this->getDocumentTypeFunction(),
];
}
/**
* This holds the current document
*
* @var Document|null
* @access protected
*/
protected ?Document $document;
/**
* @var ConfigurationManager
*/
protected $configurationManager;
public function injectConfigurationManager(ConfigurationManager $configurationManager): void
{
$this->configurationManager = $configurationManager;
}
/**
* @var DocumentRepository
*/
protected $documentRepository;
/**
* @param DocumentRepository $documentRepository
*/
public function injectDocumentRepository(DocumentRepository $documentRepository): void
{
$this->documentRepository = $documentRepository;
}
/**
* Initialize the extbase repositories
*
* @access protected
*
* @param int $storagePid The storage pid
*
* @return void
*/
protected function initializeRepositories(int $storagePid): void
{
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
// TODO: change it as it is deprecated since 10.4 and will be removed in 12.x
// TODO: necessary to test calendar view after updating this code
$configurationManager = $objectManager->get(ConfigurationManager::class);
$this->injectConfigurationManager($configurationManager);
$frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$frameworkConfiguration['persistence']['storagePid'] = MathUtility::forceIntegerInRange((int) $storagePid, 0);
$this->configurationManager->setConfiguration($frameworkConfiguration);
$this->documentRepository = GeneralUtility::makeInstance(DocumentRepository::class);
}
/**
* Shortcut function to access field values
*
* @access protected
*
* @return ExpressionFunction
*/
protected function getDocumentTypeFunction(): ExpressionFunction
{
return new ExpressionFunction(
'getDocumentType',
function()
{
// Not implemented, we only use the evaluator
},
function($arguments, $cPid)
{
/** @var RequestWrapper $requestWrapper */
$requestWrapper = $arguments['request'];
$queryParams = $requestWrapper->getQueryParams();
$type = 'undefined';
// It happens that $queryParams is an empty array or does not contain a key 'tx_dlf'
// in case of other contexts. In this case we have to return here to avoid log messages.
if (empty($queryParams) || !isset($queryParams['tx_dlf'])) {
return $type;
}
// Load document with current plugin parameters.
$this->loadDocument($queryParams['tx_dlf'], $cPid);
if ($this->document === null || $this->document->getCurrentDocument() === null) {
return $type;
}
// Set PID for metadata definitions.
$this->document->getCurrentDocument()->cPid = $cPid;
$metadata = $this->document->getCurrentDocument()->getToplevelMetadata($cPid);
if (!empty($metadata['type'][0])) {
// Calendar plugin does not support IIIF (yet). Abort for all newspaper related types.
if (
$this->document->getCurrentDocument() instanceof IiifManifest
&& array_search($metadata['type'][0], ['newspaper', 'ephemera', 'year', 'issue']) !== false
) {
return $type;
}
$type = $metadata['type'][0];
}
return $type;
});
}
/**
* Loads the current document into $this->document
*
* @access protected
*
* @param array $requestData The request data
* @param int $pid Storage Pid
*
* @return void
*/
protected function loadDocument(array $requestData, int $pid): void
{
// Try to get document format from database
if (!empty($requestData['id'])) {
$this->initializeRepositories($pid);
$doc = null;
if (MathUtility::canBeInterpretedAsInteger($requestData['id'])) {
// find document from repository by uid
$this->document = $this->documentRepository->findOneByIdAndSettings((int) $requestData['id'], ['storagePid' => $pid]);
if ($this->document) {
$doc = AbstractDocument::getInstance($this->document->getLocation(), ['storagePid' => $pid], true);
} else {
$this->logger->error('Invalid UID "' . $requestData['id'] . '" or PID "' . $pid . '" for document loading');
}
} else if (GeneralUtility::isValidUrl($requestData['id'])) {
$doc = AbstractDocument::getInstance($requestData['id'], ['storagePid' => $pid], 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);
}
$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->setCurrentDocument($doc);
}
} elseif (!empty($requestData['recordId'])) {
$this->document = $this->documentRepository->findOneByRecordId($requestData['recordId']);
if ($this->document !== null) {
$doc = AbstractDocument::getInstance($this->document->getLocation(), ['storagePid' => $pid], true);
if ($doc !== null) {
$this->document->setCurrentDocument($doc);
} else {
$this->logger->error('Failed to load document with record ID "' . $requestData['recordId'] . '"');
}
}
} else {
$this->logger->error('Empty UID or invalid PID "' . $pid . '" for document loading');
}
}
}