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 | |||||||
141 | $fields = array(); |
||||||
142 | |||||||
143 | if (is_array($this->formData['metadata'])) { |
||||||
144 | foreach ($this->formData['metadata'] as $key => $value) { |
||||||
145 | $formField = new \EWW\Dpf\Helper\FormField($key, $value); |
||||||
146 | $fields[] = $formField; |
||||||
147 | } |
||||||
148 | } |
||||||
149 | |||||||
150 | return $fields; |
||||||
151 | } |
||||||
152 | |||||||
153 | protected function getDeletedFiles() |
||||||
154 | { |
||||||
155 | $deletedFiles = array(); |
||||||
156 | |||||||
157 | if (is_array($this->formData['deleteFile'])) { |
||||||
158 | foreach ($this->formData['deleteFile'] as $key => $value) { |
||||||
159 | |||||||
160 | $file = $this->fileRepository->findByUid($value); |
||||||
161 | |||||||
162 | // Deleting the primary file is not allowed. |
||||||
163 | // if (!$file->isPrimaryFile()) { |
||||||
164 | // $deletedFiles[] = $file; |
||||||
165 | // } |
||||||
166 | |||||||
167 | $deletedFiles[] = $file; |
||||||
168 | } |
||||||
169 | } |
||||||
170 | |||||||
171 | return $deletedFiles; |
||||||
172 | } |
||||||
173 | |||||||
174 | public function uploadError() |
||||||
175 | { |
||||||
176 | // todo: To be updated or implemented elsewhere |
||||||
177 | return false; |
||||||
178 | |||||||
179 | if ( |
||||||
0 ignored issues
–
show
|
|||||||
180 | $this->formData['primaryFile'] && |
||||||
181 | $this->formData['primaryFile']['error'] != UPLOAD_ERR_OK && |
||||||
182 | $this->formData['primaryFile']['error'] != UPLOAD_ERR_NO_FILE |
||||||
183 | ) { |
||||||
184 | return true; |
||||||
185 | } |
||||||
186 | |||||||
187 | if (is_array($this->formData['secondaryFiles'])) { |
||||||
188 | foreach ($this->formData['secondaryFiles'] as $tmpFile) { |
||||||
189 | if ($tmpFile['error'] != UPLOAD_ERR_OK && $tmpFile['error'] != UPLOAD_ERR_NO_FILE) { |
||||||
190 | return true; |
||||||
191 | } |
||||||
192 | } |
||||||
193 | } |
||||||
194 | |||||||
195 | return false; |
||||||
196 | } |
||||||
197 | |||||||
198 | protected function getUploadedFile($tmpFile, $primary = false, \EWW\Dpf\Domain\Model\File $file = null) |
||||||
199 | { |
||||||
200 | |||||||
201 | if (empty($file)) { |
||||||
202 | $file = $this->objectManager->get(File::class); |
||||||
203 | } |
||||||
204 | |||||||
205 | $fileName = uniqid(time(), true); |
||||||
206 | |||||||
207 | \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($tmpFile['tmp_name'], $this->uploadPath . $fileName); |
||||||
208 | |||||||
209 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||||||
210 | $contentType = finfo_file($finfo, $this->uploadPath . $fileName); |
||||||
211 | finfo_close($finfo); |
||||||
212 | |||||||
213 | $file->setContentType($contentType); |
||||||
214 | |||||||
215 | $file->setTitle($tmpFile['name']); |
||||||
216 | $file->setLink($fileName); |
||||||
217 | $file->setPrimaryFile($primary); |
||||||
218 | $file->setFileIdentifier(uniqid(time(), true)); |
||||||
219 | |||||||
220 | if ($primary) { |
||||||
221 | if ($file->getDatastreamIdentifier()) { |
||||||
222 | $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_CHANGED); |
||||||
223 | } else { |
||||||
224 | $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED); |
||||||
225 | } |
||||||
226 | } else { |
||||||
227 | $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED); |
||||||
228 | } |
||||||
229 | |||||||
230 | return $file; |
||||||
231 | } |
||||||
232 | |||||||
233 | public function getDocumentForm() |
||||||
234 | { |
||||||
235 | $fields = $this->getFields(); |
||||||
236 | |||||||
237 | $documentForm = new \EWW\Dpf\Domain\Model\DocumentForm(); |
||||||
238 | $documentForm->setCsrfToken($this->formData['csrfToken']); |
||||||
239 | $documentForm->setUid($this->documentType->getUid()); |
||||||
240 | $documentForm->setDisplayName($this->documentType->getDisplayName()); |
||||||
241 | $documentForm->setName($this->documentType->getName()); |
||||||
242 | $documentForm->setDocumentUid($this->formData['documentUid']); |
||||||
243 | $documentForm->setFedoraPid($this->formData['fedoraPid']); |
||||||
244 | $documentForm->setValid(!empty($this->formData['validDocument'])); |
||||||
245 | if ($this->formData['comment']) { |
||||||
246 | $documentForm->setComment($this->formData['comment']); |
||||||
247 | } |
||||||
248 | |||||||
249 | $documentData = array(); |
||||||
250 | |||||||
251 | foreach ($fields as $field) { |
||||||
252 | $pageUid = $field->getPageUid(); |
||||||
253 | $groupUid = $field->getGroupUid(); |
||||||
254 | $groupIndex = $field->getGroupIndex(); |
||||||
255 | $fieldUid = $field->getFieldUid(); |
||||||
256 | $fieldIndex = $field->getFieldIndex(); |
||||||
257 | $value = $field->getValue(); |
||||||
258 | |||||||
259 | $documentData[$pageUid][$groupUid][$groupIndex][$fieldUid][$fieldIndex] = $value; |
||||||
260 | } |
||||||
261 | |||||||
262 | foreach ($documentData as $pageUid => $page) { |
||||||
263 | $metadataPage = $this->metadataPageRepository->findByUid($pageUid); |
||||||
264 | $documentFormPage = new \EWW\Dpf\Domain\Model\DocumentFormPage(); |
||||||
265 | $documentFormPage->setUid($metadataPage->getUid()); |
||||||
266 | $documentFormPage->setDisplayName($metadataPage->getDisplayName()); |
||||||
267 | $documentFormPage->setName($metadataPage->getName()); |
||||||
268 | |||||||
269 | $documentFormPage->setAccessRestrictionRoles($metadataPage->getAccessRestrictionRoles()); |
||||||
270 | |||||||
271 | foreach ($page as $groupUid => $groupItem) { |
||||||
272 | foreach ($groupItem as $group) { |
||||||
273 | $metadataGroup = $this->metadataGroupRepository->findByUid($groupUid); |
||||||
274 | $documentFormGroup = new \EWW\Dpf\Domain\Model\DocumentFormGroup(); |
||||||
275 | $documentFormGroup->setUid($metadataGroup->getUid()); |
||||||
276 | $documentFormGroup->setDisplayName($metadataGroup->getDisplayName()); |
||||||
277 | $documentFormGroup->setName($metadataGroup->getName()); |
||||||
278 | $documentFormGroup->setMandatory($metadataGroup->getMandatory()); |
||||||
279 | |||||||
280 | $documentFormGroup->setAccessRestrictionRoles($metadataGroup->getAccessRestrictionRoles()); |
||||||
281 | |||||||
282 | $documentFormGroup->setMaxIteration($metadataGroup->getMaxIteration()); |
||||||
283 | |||||||
284 | $fileLabel = ""; |
||||||
285 | $fileDownload = ""; |
||||||
286 | $fileArchive = ""; |
||||||
287 | |||||||
288 | $fileIdentifier = ''; |
||||||
289 | if (array_key_exists('fileIdentifier', $group)) { |
||||||
290 | $fileIdentifier = array_shift($group['fileIdentifier']); |
||||||
291 | unset($group['fileIdentifier']); |
||||||
292 | } |
||||||
293 | |||||||
294 | foreach ($group as $objectUid => $objectItem) { |
||||||
295 | |||||||
296 | foreach ($objectItem as $objectItem => $object) { |
||||||
0 ignored issues
–
show
|
|||||||
297 | /** @var MetadataObject $metadataObject */ |
||||||
298 | $metadataObject = $this->metadataObjectRepository->findByUid($objectUid); |
||||||
299 | |||||||
300 | /** @var DocumentForm $documentFormField */ |
||||||
301 | $documentFormField = new \EWW\Dpf\Domain\Model\DocumentFormField(); |
||||||
302 | $documentFormField->setUid($metadataObject->getUid()); |
||||||
303 | $documentFormField->setDisplayName($metadataObject->getDisplayName()); |
||||||
304 | $documentFormField->setName($metadataObject->getName()); |
||||||
305 | $documentFormField->setMandatory($metadataObject->getMandatory()); |
||||||
306 | |||||||
307 | $documentFormField->setAccessRestrictionRoles($metadataObject->getAccessRestrictionRoles()); |
||||||
308 | |||||||
309 | $documentFormField->setConsent($metadataObject->getConsent()); |
||||||
0 ignored issues
–
show
The method
setConsent() does not exist on EWW\Dpf\Domain\Model\DocumentForm . Did you maybe mean setComment() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
310 | $documentFormField->setValidation($metadataObject->getValidation()); |
||||||
0 ignored issues
–
show
The method
setValidation() does not exist on EWW\Dpf\Domain\Model\DocumentForm . Did you maybe mean setValid() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
311 | $documentFormField->setDataType($metadataObject->getDataType()); |
||||||
0 ignored issues
–
show
The method
setDataType() does not exist on EWW\Dpf\Domain\Model\DocumentForm .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
312 | $documentFormField->setMaxIteration($metadataObject->getMaxIteration()); |
||||||
313 | $documentFormField->setInputOptions($metadataObject->getInputOptionList()); |
||||||
0 ignored issues
–
show
The method
setInputOptions() does not exist on EWW\Dpf\Domain\Model\DocumentForm .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
314 | $documentFormField->setInputField($metadataObject->getInputField()); |
||||||
0 ignored issues
–
show
The method
setInputField() does not exist on EWW\Dpf\Domain\Model\DocumentForm .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
315 | $documentFormField->setFillOutService($metadataObject->getFillOutService()); |
||||||
0 ignored issues
–
show
The method
setFillOutService() does not exist on EWW\Dpf\Domain\Model\DocumentForm .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
316 | $documentFormField->setGndFieldUid($metadataObject->getGndFieldUid()); |
||||||
0 ignored issues
–
show
The method
setGndFieldUid() does not exist on EWW\Dpf\Domain\Model\DocumentForm .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
317 | $documentFormField->setMaxInputLength($metadataObject->getMaxInputLength()); |
||||||
0 ignored issues
–
show
The method
setMaxInputLength() does not exist on EWW\Dpf\Domain\Model\DocumentForm .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
318 | $documentFormField->setValue($object, $metadataObject->getDefaultValue()); |
||||||
0 ignored issues
–
show
The method
setValue() does not exist on EWW\Dpf\Domain\Model\DocumentForm . Did you maybe mean setValid() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
319 | |||||||
320 | $documentFormGroup->addItem($documentFormField); |
||||||
321 | |||||||
322 | if ($metadataGroup->isFileGroup()) { |
||||||
323 | |||||||
324 | // Use the existing file entry |
||||||
325 | $file = null; |
||||||
326 | $document = $this->documentRepository->findByUid($this->formData['documentUid']); |
||||||
327 | if ($document) { |
||||||
328 | if ($metadataGroup->isPrimaryFileGroup()) { |
||||||
329 | $file = $document->getPrimaryFile(); |
||||||
330 | } else { |
||||||
331 | $file = $document->getFileByFileIdentifier($fileIdentifier); |
||||||
332 | } |
||||||
333 | } |
||||||
334 | |||||||
335 | if ($metadataObject->isUploadField() ) { |
||||||
336 | if ($object && is_array($object) && |
||||||
337 | array_key_exists('error', $object) && |
||||||
338 | $object['error'] != UPLOAD_ERR_NO_FILE) |
||||||
339 | { |
||||||
340 | if (empty($file)) { |
||||||
341 | $file = $this->objectManager->get(File::class); |
||||||
342 | $file = $this->getUploadedFile( |
||||||
343 | $object, |
||||||
344 | $metadataGroup->isPrimaryFileGroup(), |
||||||
345 | $file); |
||||||
346 | $documentFormField->setFile($file); |
||||||
0 ignored issues
–
show
The method
setFile() does not exist on EWW\Dpf\Domain\Model\DocumentForm . Did you maybe mean setFiles() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
347 | } else { |
||||||
348 | $file = $this->getUploadedFile( |
||||||
349 | $object, |
||||||
350 | $metadataGroup->isPrimaryFileGroup(), |
||||||
351 | $file); |
||||||
352 | $documentFormField->setFile($file); |
||||||
353 | } |
||||||
354 | |||||||
355 | $documentFormField->setValue($file->getUrl()); |
||||||
356 | $fileIdentifier = $file->getFileIdentifier(); |
||||||
357 | $documentForm->addFile($file); |
||||||
358 | |||||||
359 | } elseif ($object && !is_array($object)) { |
||||||
360 | $documentFormField->setFile($file); |
||||||
361 | $fileIdentifier = $file->getFileIdentifier(); |
||||||
362 | $documentForm->addFile($file); |
||||||
363 | } |
||||||
364 | } else { |
||||||
365 | if ($metadataObject->isFileLabelField()) { |
||||||
366 | $fileLabel = $object; |
||||||
367 | } |
||||||
368 | |||||||
369 | if ($metadataObject->isFileDownloadField()) { |
||||||
370 | $fileDownload = !empty($object); |
||||||
371 | } |
||||||
372 | |||||||
373 | if ($metadataObject->isFileArchiveField()) { |
||||||
374 | $fileArchive = !empty($object); |
||||||
375 | } |
||||||
376 | |||||||
377 | } |
||||||
378 | |||||||
379 | if ($file) { |
||||||
380 | $file->setLabel($fileLabel); |
||||||
381 | $file->setDownload($fileDownload); |
||||||
382 | $file->setArchive($fileArchive); |
||||||
383 | } |
||||||
384 | } |
||||||
385 | } |
||||||
386 | } |
||||||
387 | |||||||
388 | if (!$metadataGroup->isFileGroup() || $fileIdentifier) { |
||||||
389 | $documentFormPage->addItem($documentFormGroup); |
||||||
390 | } |
||||||
391 | } |
||||||
392 | } |
||||||
393 | |||||||
394 | $documentForm->addItem($documentFormPage); |
||||||
395 | } |
||||||
396 | |||||||
397 | return $documentForm; |
||||||
398 | } |
||||||
399 | } |
||||||
400 |
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.