1
|
|
|
<?php |
2
|
|
|
namespace EWW\Dpf\Helper; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use EWW\Dpf\Domain\Model\Document; |
18
|
|
|
use EWW\Dpf\Domain\Model\DocumentForm; |
19
|
|
|
use EWW\Dpf\Domain\Model\File; |
20
|
|
|
use EWW\Dpf\Domain\Model\MetadataObject; |
21
|
|
|
use TYPO3\CMS\Core\Core\Environment; |
22
|
|
|
|
23
|
|
|
class FormDataReader |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* documentTypeRepository |
28
|
|
|
* |
29
|
|
|
* @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
30
|
|
|
* @TYPO3\CMS\Extbase\Annotation\Inject |
31
|
|
|
*/ |
32
|
|
|
protected $documentTypeRepository = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* metadataPageRepository |
36
|
|
|
* |
37
|
|
|
* @var \EWW\Dpf\Domain\Repository\MetadataPageRepository |
38
|
|
|
* @TYPO3\CMS\Extbase\Annotation\Inject |
39
|
|
|
*/ |
40
|
|
|
protected $metadataPageRepository = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* metadataGroupRepository |
44
|
|
|
* |
45
|
|
|
* @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
46
|
|
|
* @TYPO3\CMS\Extbase\Annotation\Inject |
47
|
|
|
*/ |
48
|
|
|
protected $metadataGroupRepository = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* metadataObjectRepository |
52
|
|
|
* |
53
|
|
|
* @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository |
54
|
|
|
* @TYPO3\CMS\Extbase\Annotation\Inject |
55
|
|
|
*/ |
56
|
|
|
protected $metadataObjectRepository = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* fileRepository |
60
|
|
|
* |
61
|
|
|
* @var \EWW\Dpf\Domain\Repository\FileRepository |
62
|
|
|
* @TYPO3\CMS\Extbase\Annotation\Inject |
63
|
|
|
*/ |
64
|
|
|
protected $fileRepository = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* documentRepository |
68
|
|
|
* |
69
|
|
|
* @var \EWW\Dpf\Domain\Repository\DocumentRepository |
70
|
|
|
* @TYPO3\CMS\Extbase\Annotation\Inject |
71
|
|
|
*/ |
72
|
|
|
protected $documentRepository = null; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* objectManager |
76
|
|
|
* |
77
|
|
|
* @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
78
|
|
|
* @TYPO3\CMS\Extbase\Annotation\Inject |
79
|
|
|
*/ |
80
|
|
|
protected $objectManager; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* |
84
|
|
|
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface |
85
|
|
|
* @TYPO3\CMS\Extbase\Annotation\Inject |
86
|
|
|
*/ |
87
|
|
|
protected $configurationManager; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* formData |
91
|
|
|
* |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
protected $formData; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* documentType |
98
|
|
|
* |
99
|
|
|
* @var |
100
|
|
|
*/ |
101
|
|
|
protected $documentType; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* uploadPath |
105
|
|
|
* |
106
|
|
|
* @var |
107
|
|
|
*/ |
108
|
|
|
protected $uploadPath; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* basePath |
112
|
|
|
* |
113
|
|
|
* @var |
114
|
|
|
*/ |
115
|
|
|
protected $uploadBaseUrl; |
116
|
|
|
|
117
|
|
|
public function __construct() |
118
|
|
|
{ |
119
|
|
|
|
120
|
|
|
$uploadFileUrl = new \EWW\Dpf\Helper\UploadFileUrl; |
121
|
|
|
|
122
|
|
|
$this->uploadBaseUrl = $uploadFileUrl->getUploadUrl() . "/"; |
123
|
|
|
|
124
|
|
|
$this->uploadPath = Environment::getPublicPath() . "/" . $uploadFileUrl->getDirectory() . "/"; |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* @param array $formData |
131
|
|
|
*/ |
132
|
|
|
public function setFormData($formData) |
133
|
|
|
{ |
134
|
|
|
$this->formData = $formData; |
135
|
|
|
$this->documentType = $this->documentTypeRepository->findByUid($formData['type']); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function getFields() |
139
|
|
|
{ |
140
|
|
|
$fields = array(); |
141
|
|
|
|
142
|
|
|
if (is_array($this->formData['metadata'])) { |
143
|
|
|
|
144
|
|
|
foreach ($this->formData['metadata'] as $key => $value) { |
145
|
|
|
if (is_array($value)) { |
146
|
|
|
$formField = new \EWW\Dpf\Helper\FormField($key, array_shift($value)); |
147
|
|
|
$fields[] = $formField; |
148
|
|
|
} else { |
149
|
|
|
$formField = new \EWW\Dpf\Helper\FormField($key, $value); |
150
|
|
|
$fields[] = $formField; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $fields; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function getDeletedFiles() |
159
|
|
|
{ |
160
|
|
|
$deletedFiles = array(); |
161
|
|
|
|
162
|
|
|
if (is_array($this->formData['deleteFile'])) { |
163
|
|
|
foreach ($this->formData['deleteFile'] as $key => $value) { |
164
|
|
|
|
165
|
|
|
$file = $this->fileRepository->findByUid($value); |
166
|
|
|
|
167
|
|
|
// Deleting the primary file is not allowed. |
168
|
|
|
// if (!$file->isPrimaryFile()) { |
169
|
|
|
// $deletedFiles[] = $file; |
170
|
|
|
// } |
171
|
|
|
|
172
|
|
|
$deletedFiles[] = $file; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $deletedFiles; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function uploadError() |
180
|
|
|
{ |
181
|
|
|
// todo: To be updated or implemented elsewhere |
182
|
|
|
return false; |
183
|
|
|
|
184
|
|
|
if ( |
|
|
|
|
185
|
|
|
$this->formData['primaryFile'] && |
186
|
|
|
$this->formData['primaryFile']['error'] != UPLOAD_ERR_OK && |
187
|
|
|
$this->formData['primaryFile']['error'] != UPLOAD_ERR_NO_FILE |
188
|
|
|
) { |
189
|
|
|
return true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (is_array($this->formData['secondaryFiles'])) { |
193
|
|
|
foreach ($this->formData['secondaryFiles'] as $tmpFile) { |
194
|
|
|
if ($tmpFile['error'] != UPLOAD_ERR_OK && $tmpFile['error'] != UPLOAD_ERR_NO_FILE) { |
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
protected function getUrlFile($fileUrl, $primary = false, \EWW\Dpf\Domain\Model\File $file = null) |
204
|
|
|
{ |
205
|
|
|
if (empty($file)) { |
206
|
|
|
$file = $this->objectManager->get(File::class); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$fileName = uniqid(time(), true); |
|
|
|
|
210
|
|
|
|
211
|
|
|
# get remote mimetype |
212
|
|
|
$ch = curl_init($fileUrl); |
213
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
214
|
|
|
curl_exec($ch); |
215
|
|
|
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
216
|
|
|
|
217
|
|
|
$file->setContentType($contentType); |
218
|
|
|
|
219
|
|
|
$path_parts = pathinfo($fileUrl); |
220
|
|
|
$origFilename = $path_parts['filename']? $path_parts['filename'] : 'unknown-file-name'; |
221
|
|
|
$origFilename .= $path_parts['extension']? '.'.$path_parts['extension'] : ''; |
222
|
|
|
$file->setTitle($origFilename); |
223
|
|
|
|
224
|
|
|
$file->setLink($fileUrl); |
225
|
|
|
$file->setPrimaryFile($primary); |
226
|
|
|
$file->setFileIdentifier(uniqid(time(), true)); |
227
|
|
|
|
228
|
|
|
if ($primary) { |
229
|
|
|
if ($file->getDatastreamIdentifier()) { |
230
|
|
|
$file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_CHANGED); |
231
|
|
|
} else { |
232
|
|
|
$file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED); |
233
|
|
|
} |
234
|
|
|
} else { |
235
|
|
|
$file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $file; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
protected function getUploadedFile($tmpFile, $primary = false, \EWW\Dpf\Domain\Model\File $file = null) |
242
|
|
|
{ |
243
|
|
|
|
244
|
|
|
if (empty($file)) { |
245
|
|
|
$file = $this->objectManager->get(File::class); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$fileName = uniqid(time(), true); |
249
|
|
|
|
250
|
|
|
\TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($tmpFile['tmp_name'], $this->uploadPath . $fileName); |
251
|
|
|
|
252
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
253
|
|
|
$contentType = finfo_file($finfo, $this->uploadPath . $fileName); |
254
|
|
|
finfo_close($finfo); |
255
|
|
|
|
256
|
|
|
$file->setContentType($contentType); |
257
|
|
|
|
258
|
|
|
$file->setTitle($tmpFile['name']); |
259
|
|
|
$file->setLink($fileName); |
260
|
|
|
$file->setPrimaryFile($primary); |
261
|
|
|
$file->setFileIdentifier(uniqid(time(), true)); |
262
|
|
|
|
263
|
|
|
if ($primary) { |
264
|
|
|
if ($file->getDatastreamIdentifier()) { |
265
|
|
|
$file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_CHANGED); |
266
|
|
|
} else { |
267
|
|
|
$file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED); |
268
|
|
|
} |
269
|
|
|
} else { |
270
|
|
|
$file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $file; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function getDocumentForm() |
277
|
|
|
{ |
278
|
|
|
$fields = $this->getFields(); |
279
|
|
|
|
280
|
|
|
$documentForm = new \EWW\Dpf\Domain\Model\DocumentForm(); |
281
|
|
|
$documentForm->setCsrfToken($this->formData['csrfToken']); |
282
|
|
|
$documentForm->setUid($this->documentType->getUid()); |
283
|
|
|
$documentForm->setDisplayName($this->documentType->getDisplayName()); |
284
|
|
|
$documentForm->setName($this->documentType->getName()); |
285
|
|
|
$documentForm->setDocumentUid($this->formData['documentUid']); |
286
|
|
|
$documentForm->setFedoraPid($this->formData['fedoraPid']); |
287
|
|
|
$documentForm->setValid(!empty($this->formData['validDocument'])); |
288
|
|
|
if ($this->formData['comment']) { |
289
|
|
|
$documentForm->setComment($this->formData['comment']); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$documentData = array(); |
293
|
|
|
|
294
|
|
|
foreach ($fields as $field) { |
295
|
|
|
$pageUid = $field->getPageUid(); |
296
|
|
|
$groupUid = $field->getGroupUid(); |
297
|
|
|
$groupIndex = $field->getGroupIndex(); |
298
|
|
|
$fieldUid = $field->getFieldUid(); |
299
|
|
|
$fieldIndex = $field->getFieldIndex(); |
300
|
|
|
$value = $field->getValue(); |
301
|
|
|
|
302
|
|
|
$documentData[$pageUid][$groupUid][$groupIndex][$fieldUid][$fieldIndex] = $value; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
foreach ($documentData as $pageUid => $page) { |
306
|
|
|
$metadataPage = $this->metadataPageRepository->findByUid($pageUid); |
307
|
|
|
$documentFormPage = new \EWW\Dpf\Domain\Model\DocumentFormPage(); |
308
|
|
|
$documentFormPage->setUid($metadataPage->getUid()); |
309
|
|
|
$documentFormPage->setDisplayName($metadataPage->getDisplayName()); |
310
|
|
|
$documentFormPage->setName($metadataPage->getName()); |
311
|
|
|
|
312
|
|
|
$documentFormPage->setAccessRestrictionRoles($metadataPage->getAccessRestrictionRoles()); |
313
|
|
|
|
314
|
|
|
foreach ($page as $groupUid => $groupItem) { |
315
|
|
|
foreach ($groupItem as $group) { |
316
|
|
|
$metadataGroup = $this->metadataGroupRepository->findByUid($groupUid); |
317
|
|
|
$documentFormGroup = new \EWW\Dpf\Domain\Model\DocumentFormGroup(); |
318
|
|
|
$documentFormGroup->setUid($metadataGroup->getUid()); |
319
|
|
|
$documentFormGroup->setDisplayName($metadataGroup->getDisplayName()); |
320
|
|
|
$documentFormGroup->setName($metadataGroup->getName()); |
321
|
|
|
$documentFormGroup->setMandatory($metadataGroup->getMandatory()); |
322
|
|
|
|
323
|
|
|
$documentFormGroup->setAccessRestrictionRoles($metadataGroup->getAccessRestrictionRoles()); |
324
|
|
|
|
325
|
|
|
$documentFormGroup->setMaxIteration($metadataGroup->getMaxIteration()); |
326
|
|
|
|
327
|
|
|
$fileLabel = ""; |
328
|
|
|
$fileDownload = ""; |
329
|
|
|
$fileArchive = ""; |
330
|
|
|
|
331
|
|
|
$fileIdentifier = ''; |
332
|
|
|
if (array_key_exists('fileIdentifier', $group)) { |
333
|
|
|
$fileIdentifier = array_shift($group['fileIdentifier']); |
334
|
|
|
unset($group['fileIdentifier']); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
foreach ($group as $objectUid => $objectItem) { |
338
|
|
|
|
339
|
|
|
foreach ($objectItem as $objectItem => $object) { |
|
|
|
|
340
|
|
|
/** @var MetadataObject $metadataObject */ |
341
|
|
|
$metadataObject = $this->metadataObjectRepository->findByUid($objectUid); |
342
|
|
|
|
343
|
|
|
/** @var DocumentForm $documentFormField */ |
344
|
|
|
$documentFormField = new \EWW\Dpf\Domain\Model\DocumentFormField(); |
345
|
|
|
$documentFormField->setUid($metadataObject->getUid()); |
346
|
|
|
$documentFormField->setDisplayName($metadataObject->getDisplayName()); |
347
|
|
|
$documentFormField->setName($metadataObject->getName()); |
348
|
|
|
$documentFormField->setMandatory($metadataObject->getMandatory()); |
349
|
|
|
|
350
|
|
|
$documentFormField->setAccessRestrictionRoles($metadataObject->getAccessRestrictionRoles()); |
351
|
|
|
|
352
|
|
|
$documentFormField->setConsent($metadataObject->getConsent()); |
|
|
|
|
353
|
|
|
$documentFormField->setValidation($metadataObject->getValidation()); |
|
|
|
|
354
|
|
|
$documentFormField->setValidationErrorMessage($metadataObject->getValidationErrorMessage()); |
|
|
|
|
355
|
|
|
$documentFormField->setValidator($metadataObject->getValidator()); |
|
|
|
|
356
|
|
|
$documentFormField->setMaxIteration($metadataObject->getMaxIteration()); |
357
|
|
|
$documentFormField->setInputOptions($metadataObject->getInputOptionList()); |
|
|
|
|
358
|
|
|
$documentFormField->setInputField($metadataObject->getInputField()); |
|
|
|
|
359
|
|
|
$documentFormField->setFillOutService($metadataObject->getFillOutService()); |
|
|
|
|
360
|
|
|
$documentFormField->setGndFieldUid($metadataObject->getGndFieldUid()); |
|
|
|
|
361
|
|
|
$documentFormField->setMaxInputLength($metadataObject->getMaxInputLength()); |
|
|
|
|
362
|
|
|
$documentFormField->setValue($object, $metadataObject->getDefaultValue()); |
|
|
|
|
363
|
|
|
|
364
|
|
|
$documentFormGroup->addItem($documentFormField); |
365
|
|
|
|
366
|
|
|
if ($metadataGroup->isFileGroup()) { |
367
|
|
|
|
368
|
|
|
// Use the existing file entry |
369
|
|
|
$file = null; |
370
|
|
|
$document = $this->documentRepository->findByUid($this->formData['documentUid']); |
371
|
|
|
if ($document) { |
372
|
|
|
if ($metadataGroup->isPrimaryFileGroup()) { |
373
|
|
|
$file = $document->getPrimaryFile(); |
374
|
|
|
} else { |
375
|
|
|
$file = $document->getFileByFileIdentifier($fileIdentifier); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
if ($metadataObject->isUploadField() ) { |
380
|
|
|
if ($object && is_array($object) && |
381
|
|
|
array_key_exists('error', $object) && |
382
|
|
|
$object['error'] != UPLOAD_ERR_NO_FILE) |
383
|
|
|
{ |
384
|
|
|
if (empty($file)) { |
385
|
|
|
$file = $this->objectManager->get(File::class); |
386
|
|
|
$file = $this->getUploadedFile( |
387
|
|
|
$object, |
388
|
|
|
$metadataGroup->isPrimaryFileGroup(), |
389
|
|
|
$file); |
390
|
|
|
$documentFormField->setFile($file); |
|
|
|
|
391
|
|
|
} else { |
392
|
|
|
$file = $this->getUploadedFile( |
393
|
|
|
$object, |
394
|
|
|
$metadataGroup->isPrimaryFileGroup(), |
395
|
|
|
$file); |
396
|
|
|
$documentFormField->setFile($file); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$documentFormField->setValue($file->getLink()); |
400
|
|
|
$fileIdentifier = $file->getFileIdentifier(); |
401
|
|
|
$documentForm->addFile($file); |
402
|
|
|
|
403
|
|
|
} elseif ($object && !is_array($object)) { |
404
|
|
|
$file = $this->getUrlFile( |
405
|
|
|
$object, |
406
|
|
|
$metadataGroup->isPrimaryFileGroup(), |
407
|
|
|
$file |
408
|
|
|
); |
409
|
|
|
$documentFormField->setFile($file); |
410
|
|
|
$fileIdentifier = $file->getFileIdentifier(); |
411
|
|
|
$documentForm->addFile($file); |
412
|
|
|
} |
413
|
|
|
} else { |
414
|
|
|
if ($metadataObject->isFileLabelField()) { |
415
|
|
|
$fileLabel = $object; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
if ($metadataObject->isFileDownloadField()) { |
419
|
|
|
$fileDownload = !empty($object); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
if ($metadataObject->isFileArchiveField()) { |
423
|
|
|
$fileArchive = !empty($object); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
if ($file) { |
429
|
|
|
$file->setLabel($fileLabel); |
430
|
|
|
$file->setDownload($fileDownload); |
431
|
|
|
$file->setArchive($fileArchive); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
if (!$metadataGroup->isFileGroup() || $fileIdentifier) { |
439
|
|
|
$documentFormPage->addItem($documentFormGroup); |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$documentForm->addItem($documentFormPage); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
return $documentForm; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.