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\Common\IiifManifest; |
17
|
|
|
use Kitodo\Dlf\Domain\Model\Collection; |
18
|
|
|
use Kitodo\Dlf\Domain\Model\Metadata; |
19
|
|
|
use Kitodo\Dlf\Domain\Repository\CollectionRepository; |
20
|
|
|
use Kitodo\Dlf\Domain\Repository\MetadataRepository; |
21
|
|
|
use Kitodo\Dlf\Domain\Repository\StructureRepository; |
22
|
|
|
use Ubl\Iiif\Context\IRI; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Controller class for the plugin 'Metadata'. |
26
|
|
|
* |
27
|
|
|
* @author Sebastian Meyer <[email protected]> |
28
|
|
|
* @package TYPO3 |
29
|
|
|
* @subpackage dlf |
30
|
|
|
* @access public |
31
|
|
|
*/ |
32
|
|
|
class MetadataController extends AbstractController |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var CollectionRepository |
36
|
|
|
*/ |
37
|
|
|
protected $collectionRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param CollectionRepository $collectionRepository |
41
|
|
|
*/ |
42
|
|
|
public function injectCollectionRepository(CollectionRepository $collectionRepository) |
43
|
|
|
{ |
44
|
|
|
$this->collectionRepository = $collectionRepository; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var MetadataRepository |
49
|
|
|
*/ |
50
|
|
|
protected $metadataRepository; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param MetadataRepository $metadataRepository |
54
|
|
|
*/ |
55
|
|
|
public function injectMetadataRepository(MetadataRepository $metadataRepository) |
56
|
|
|
{ |
57
|
|
|
$this->metadataRepository = $metadataRepository; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var StructureRepository |
62
|
|
|
*/ |
63
|
|
|
protected $structureRepository; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param StructureRepository $structureRepository |
67
|
|
|
*/ |
68
|
|
|
public function injectStructureRepository(StructureRepository $structureRepository) |
69
|
|
|
{ |
70
|
|
|
$this->structureRepository = $structureRepository; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string|void |
75
|
|
|
*/ |
76
|
|
|
public function mainAction() |
77
|
|
|
{ |
78
|
|
|
$this->cObj = $this->configurationManager->getContentObject(); |
|
|
|
|
79
|
|
|
|
80
|
|
|
// Load current document. |
81
|
|
|
$this->loadDocument($this->requestData); |
82
|
|
|
if ( |
83
|
|
|
$this->document === null |
84
|
|
|
|| $this->document->getDoc() === null |
85
|
|
|
) { |
86
|
|
|
// Quit without doing anything if required variables are not set. |
87
|
|
|
return ''; |
88
|
|
|
} else { |
89
|
|
|
// Set default values if not set. |
90
|
|
|
if (!isset($this->settings['rootline'])) { |
91
|
|
|
$this->settings['rootline'] = 0; |
92
|
|
|
} |
93
|
|
|
if (!isset($this->settings['originalIiifMetadata'])) { |
94
|
|
|
$this->settings['originalIiifMetadata'] = 0; |
95
|
|
|
} |
96
|
|
|
if (!isset($this->settings['displayIiifDescription'])) { |
97
|
|
|
$this->settings['displayIiifDescription'] = 1; |
98
|
|
|
} |
99
|
|
|
if (!isset($this->settings['displayIiifRights'])) { |
100
|
|
|
$this->settings['displayIiifRights'] = 1; |
101
|
|
|
} |
102
|
|
|
if (!isset($this->settings['displayIiifLinks'])) { |
103
|
|
|
$this->settings['displayIiifLinks'] = 1; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
$useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest; |
107
|
|
|
$metadata = []; |
108
|
|
|
if ($this->settings['rootline'] < 2) { |
109
|
|
|
// Get current structure's @ID. |
110
|
|
|
$ids = []; |
111
|
|
|
if (!empty($this->document->getDoc()->physicalStructure[$this->requestData['page']]) && !empty($this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]])) { |
112
|
|
|
foreach ($this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]] as $logId) { |
113
|
|
|
$count = $this->document->getDoc()->getStructureDepth($logId); |
114
|
|
|
$ids[$count][] = $logId; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
ksort($ids); |
118
|
|
|
reset($ids); |
119
|
|
|
// Check if we should display all metadata up to the root. |
120
|
|
|
if ($this->settings['rootline'] == 1) { |
121
|
|
|
foreach ($ids as $id) { |
122
|
|
|
foreach ($id as $sid) { |
123
|
|
|
if ($useOriginalIiifManifestMetadata) { |
124
|
|
|
$data = $this->document->getDoc()->getManifestMetadata($sid, $this->settings['storagePid']); |
125
|
|
|
} else { |
126
|
|
|
$data = $this->document->getDoc()->getMetadata($sid, $this->settings['storagePid']); |
127
|
|
|
} |
128
|
|
|
if (!empty($data)) { |
129
|
|
|
$data['_id'] = $sid; |
130
|
|
|
$metadata[] = $data; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
$id = array_pop($ids); |
136
|
|
|
if (is_array($id)) { |
137
|
|
|
foreach ($id as $sid) { |
138
|
|
|
if ($useOriginalIiifManifestMetadata) { |
139
|
|
|
$data = $this->document->getDoc()->getManifestMetadata($sid, $this->settings['storagePid']); |
140
|
|
|
} else { |
141
|
|
|
$data = $this->document->getDoc()->getMetadata($sid, $this->settings['storagePid']); |
142
|
|
|
} |
143
|
|
|
if (!empty($data)) { |
144
|
|
|
$data['_id'] = $sid; |
145
|
|
|
$metadata[] = $data; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
// Get titledata? |
152
|
|
|
if (empty($metadata) || ($this->settings['rootline'] == 1 && $metadata[0]['_id'] != $this->document->getDoc()->toplevelId)) { |
153
|
|
|
$data = $useOriginalIiifManifestMetadata ? $this->document->getDoc()->getManifestMetadata($this->document->getDoc()->toplevelId, $this->settings['storagePid']) : $this->document->getDoc()->getTitleData($this->settings['storagePid']); |
|
|
|
|
154
|
|
|
$data['_id'] = $this->document->getDoc()->toplevelId; |
155
|
|
|
array_unshift($metadata, $data); |
156
|
|
|
} |
157
|
|
|
if (empty($metadata)) { |
158
|
|
|
$this->logger->warning('No metadata found for document with UID ' . $this->document->getUid()); |
159
|
|
|
return ''; |
160
|
|
|
} |
161
|
|
|
ksort($metadata); |
162
|
|
|
// Get hook objects. |
163
|
|
|
$this->hookObjects = Helper::getHookObjects($this->scriptRelPath); |
|
|
|
|
164
|
|
|
// Hook for getting a customized title bar (requested by SBB). |
165
|
|
|
foreach ($this->hookObjects as $hookObj) { |
166
|
|
|
if (method_exists($hookObj, 'main_customizeTitleBarGetCustomTemplate')) { |
167
|
|
|
$hookObj->main_customizeTitleBarGetCustomTemplate($this, $metadata); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
$this->printMetadata($metadata, $useOriginalIiifManifestMetadata); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Prepares the metadata array for output |
175
|
|
|
* |
176
|
|
|
* @access protected |
177
|
|
|
* |
178
|
|
|
* @param array $metadataArray: The metadata array |
179
|
|
|
* @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs? |
180
|
|
|
* |
181
|
|
|
* @return string The metadata array ready for output |
182
|
|
|
*/ |
183
|
|
|
protected function printMetadata(array $metadataArray, $useOriginalIiifManifestMetadata = false) |
184
|
|
|
{ |
185
|
|
|
if ($useOriginalIiifManifestMetadata) { |
186
|
|
|
$iiifData = []; |
187
|
|
|
foreach ($metadataArray as $metadata) { |
188
|
|
|
foreach ($metadata as $key => $group) { |
189
|
|
|
if ($key == '_id') { |
190
|
|
|
continue; |
191
|
|
|
} |
192
|
|
|
if (!is_array($group)) { |
193
|
|
|
if ( |
194
|
|
|
IRI::isAbsoluteIri($group) |
195
|
|
|
&& (($scheme = (new IRI($group))->getScheme()) == 'http' || $scheme == 'https') |
196
|
|
|
) { |
197
|
|
|
// Build link |
198
|
|
|
$iiifData[$key] = [ |
199
|
|
|
'label' => $key, |
200
|
|
|
'value' => $group, |
201
|
|
|
'buildUrl' => true, |
202
|
|
|
]; |
203
|
|
|
} else { |
204
|
|
|
// Data output |
205
|
|
|
$iiifData[$key] = [ |
206
|
|
|
'label' => $key, |
207
|
|
|
'value' => $group, |
208
|
|
|
'buildUrl' => false, |
209
|
|
|
]; |
210
|
|
|
} |
211
|
|
|
} else { |
212
|
|
|
foreach ($group as $label => $value) { |
213
|
|
|
if ($label == '_id') { |
214
|
|
|
continue; |
215
|
|
|
} |
216
|
|
|
if (is_array($value)) { |
217
|
|
|
$value = implode($this->settings['separator'], $value); |
218
|
|
|
} |
219
|
|
|
// NOTE: Labels are to be escaped in Fluid template |
220
|
|
|
if (IRI::isAbsoluteIri($value) && (($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https')) { |
221
|
|
|
$nolabel = $value == $label; |
222
|
|
|
$iiifData[$key]['data'][] = [ |
223
|
|
|
'label' => $nolabel ? '' : $label, |
224
|
|
|
'value' => $value, |
225
|
|
|
'buildUrl' => true, |
226
|
|
|
]; |
227
|
|
|
} else { |
228
|
|
|
$iiifData[$key]['data'][] = [ |
229
|
|
|
'label' => $label, |
230
|
|
|
'value' => $value, |
231
|
|
|
'buildUrl' => false, |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
$this->view->assign('useIiif', true); |
237
|
|
|
$this->view->assign('iiifData', $iiifData); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} else { |
241
|
|
|
|
242
|
|
|
// findBySettings also sorts entries by the `sorting` field |
243
|
|
|
$metadataResult = $this->metadataRepository->findBySettings([ |
244
|
|
|
'is_listed' => !$this->settings['showFull'], |
245
|
|
|
]); |
246
|
|
|
|
247
|
|
|
$buildUrl = []; |
248
|
|
|
$i = 0; |
249
|
|
|
foreach ($metadataArray as $metadataSection) { |
250
|
|
|
foreach ($metadataSection as $metadataName => $metadataValue) { |
251
|
|
|
// NOTE: Labels are to be escaped in Fluid template |
252
|
|
|
|
253
|
|
|
if ($metadataName == 'title') { |
254
|
|
|
// Get title of parent document if needed. |
255
|
|
|
if (empty($metadataValue) && $this->settings['getTitle'] && $this->document->getDoc()->parentId) { |
256
|
|
|
$superiorTitle = Doc::getTitle($this->document->getPartof(), true); |
257
|
|
|
if (!empty($superiorTitle)) { |
258
|
|
|
$metadataArray[$i][$metadataName] = ['[' . $superiorTitle . ']']; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
if (!empty($metadataValue)) { |
262
|
|
|
$metadataArray[$i][$metadataName][0] = $metadataArray[$i][$metadataName][0]; |
263
|
|
|
// Link title to pageview. |
264
|
|
|
if ($this->settings['linkTitle'] && $metadataSection['_id']) { |
265
|
|
|
$details = $this->document->getDoc()->getLogicalStructure($metadataSection['_id']); |
266
|
|
|
$buildUrl[$i][$metadataName]['buildUrl'] = [ |
267
|
|
|
'id' => $this->document->getUid(), |
268
|
|
|
'page' => (!empty($details['points']) ? intval($details['points']) : 1), |
269
|
|
|
'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0), |
270
|
|
|
]; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
} elseif ($metadataName == 'owner' && empty($metadataValue)) { |
274
|
|
|
// no owner is found by metadata records --> take the one associated to the document |
275
|
|
|
$library = $this->document->getOwner(); |
276
|
|
|
if ($library) { |
277
|
|
|
$metadataArray[$i][$metadataName][0] = $library->getLabel(); |
278
|
|
|
} |
279
|
|
|
} elseif ($metadataName == 'type' && !empty($metadataValue)) { |
280
|
|
|
// Translate document type. |
281
|
|
|
$structure = $this->structureRepository->findOneByIndexName($metadataArray[$i][$metadataName][0]); |
|
|
|
|
282
|
|
|
$metadataArray[$i][$metadataName][0] = $structure->getLabel(); |
|
|
|
|
283
|
|
|
} elseif ($metadataName == 'collection' && !empty($metadataValue)) { |
284
|
|
|
// Check if collections isn't hidden. |
285
|
|
|
$j = 0; |
286
|
|
|
foreach ($metadataValue as $metadataEntry) { |
287
|
|
|
$collection = $this->collectionRepository->findOneByIndexName($metadataEntry); |
|
|
|
|
288
|
|
|
$metadataArray[$i][$metadataName][$j] = $collection->getLabel() ? : ''; |
289
|
|
|
$j++; |
290
|
|
|
} |
291
|
|
|
} elseif ($metadataName == 'language' && !empty($metadataValue)) { |
292
|
|
|
// Translate ISO 639 language code. |
293
|
|
|
$metadataArray[$i][$metadataName][0] = Helper::getLanguageName($metadataArray[$i][$metadataName][0]); |
294
|
|
|
} elseif (!empty($metadataValue)) { |
295
|
|
|
$metadataArray[$i][$metadataName][0] = $metadataArray[$i][$metadataName][0]; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if (is_array($metadataArray[$i][$metadataName])) { |
299
|
|
|
$metadataArray[$i][$metadataName] = array_values(array_filter($metadataArray[$i][$metadataName], function($value) |
300
|
|
|
{ |
301
|
|
|
return !empty($value); |
302
|
|
|
})); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
$i++; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$this->view->assign('buildUrl', $buildUrl); |
309
|
|
|
$this->view->assign('documentMetadataSections', $metadataArray); |
310
|
|
|
$this->view->assign('configMetadata', $metadataResult); |
311
|
|
|
$this->view->assign('separator', $this->settings['separator']); |
312
|
|
|
|
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|