Issues (3936)

Controller/AbstractDocumentFormController.php (9 issues)

1
<?php
2
namespace EWW\Dpf\Controller;
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\Helper\DocumentMapper;
21
use EWW\Dpf\Helper\FormDataReader;
22
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
23
use EWW\Dpf\Services\Email\Notifier;
24
use EWW\Dpf\Domain\Model\DepositLicenseLog;
25
use Exception;
26
27
/**
28
 * DocumentFormController
29
 */
30
abstract class AbstractDocumentFormController extends AbstractController
31
{
32
33
    /**
34
     * documentRepository
35
     *
36
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
37
     * @TYPO3\CMS\Extbase\Annotation\Inject
38
     */
39
    protected $documentRepository = null;
40
41
    /**
42
     * fileRepository
43
     *
44
     * @var \EWW\Dpf\Domain\Repository\FileRepository
45
     * @TYPO3\CMS\Extbase\Annotation\Inject
46
     */
47
    protected $fileRepository = null;
48
49
    /**
50
     * documentTypeRepository
51
     *
52
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
53
     * @TYPO3\CMS\Extbase\Annotation\Inject
54
     */
55
    protected $documentTypeRepository = null;
56
57
    /**
58
     * metadataGroupRepository
59
     *
60
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
61
     * @TYPO3\CMS\Extbase\Annotation\Inject
62
     */
63
    protected $metadataGroupRepository = null;
64
65
    /**
66
     * metadataObjectRepository
67
     *
68
     * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository
69
     * @TYPO3\CMS\Extbase\Annotation\Inject
70
     */
71
    protected $metadataObjectRepository = null;
72
73
    /**
74
     * depositLicenseLogRepository
75
     *
76
     * @var \EWW\Dpf\Domain\Repository\DepositLicenseLogRepository
77
     * @TYPO3\CMS\Extbase\Annotation\Inject
78
     */
79
    protected $depositLicenseLogRepository = null;
80
81
    /**
82
     * persistence manager
83
     *
84
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
85
     * @TYPO3\CMS\Extbase\Annotation\Inject
86
     */
87
    protected $persistenceManager;
88
89
    /**
90
     * fisDataService
91
     *
92
     * @var \EWW\Dpf\Services\FeUser\FisDataService
93
     * @TYPO3\CMS\Extbase\Annotation\Inject
94
     */
95
    protected $fisDataService = null;
96
97
    /**
98
     * action list
99
     *
100
     * @return void
101
     */
102
    public function listAction()
103
    {
104
        $documents = $this->documentRepository->findAll();
105
        $docTypes = $this->documentTypeRepository->getDocumentTypesAlphabetically();
106
107
        if ($this->request->hasArgument('message')) {
108
            $this->view->assign('message', $this->request->getArgument('message'));
109
        }
110
111
        if ($this->request->hasArgument('errorFiles')) {
112
            $this->view->assign('errorFiles', $this->request->getArgument('errorFiles'));
113
        }
114
115
        $this->view->assign('documentTypes', $docTypes);
116
        $this->view->assign('documents', $documents);
117
    }
118
119
    /**
120
     * initialize newAction
121
     *
122
     * @return void
123
     */
124
    public function initializeNewAction()
125
    {
126
127
        $requestArguments = $this->request->getArguments();
128
129
        if (array_key_exists('documentData', $requestArguments)) {
130
            die('Error: initializeNewAction');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
131
        } elseif (array_key_exists('documentType', $requestArguments)) {
132
            $docTypeUid   = $this->request->getArgument('documentType');
133
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
0 ignored issues
show
$docTypeUid of type array|string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Extbase\Persis...Repository::findByUid(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
            $documentType = $this->documentTypeRepository->findByUid(/** @scrutinizer ignore-type */ $docTypeUid);
Loading history...
134
            $document     = $this->objectManager->get(Document::class);
135
            $document->setDocumentType($documentType);
136
            $mapper  = $this->objectManager->get(DocumentMapper::class);
137
            $docForm = $mapper->getDocumentForm($document);
138
        } elseif (array_key_exists('newDocumentForm', $requestArguments)) {
139
            $docForm = $this->request->getArgument('newDocumentForm');
140
        }
141
142
        $requestArguments['newDocumentForm'] = $docForm;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $docForm does not seem to be defined for all execution paths leading up to this point.
Loading history...
143
        $this->request->setArguments($requestArguments);
144
    }
145
146
    /**
147
     * action new
148
     *
149
     * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm
150
     * @param int $returnDocumentId
151
     * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("newDocumentForm")
152
     * @return void
153
     */
154
    public function newAction(DocumentForm $newDocumentForm = null, $returnDocumentId = 0)
155
    {
156
        $this->view->assign('returnDocumentId', $returnDocumentId);
157
        $this->view->assign('documentForm', $newDocumentForm);
158
159
        if (!empty($this->security->getUserAccessToGroups())) {
160
            $this->view->assign('currentUserAccessToGroup', $this->security->getUserAccessToGroups());
161
        }
162
163
        if ($this->fisDataService->getPersonData($this->security->getFisPersId())) {
164
            $this->view->assign('fisPersId', $this->security->getFisPersId());
165
        }
166
    }
167
168
    public function initializeCreateAction()
169
    {
170
171
        $requestArguments = $this->request->getArguments();
172
173
        if ($this->request->hasArgument('documentData')) {
174
            $documentData = $this->request->getArgument('documentData');
175
176
            $formDataReader = $this->objectManager->get(FormDataReader::class);
177
            $formDataReader->setFormData($documentData);
178
179
            $docForm = $formDataReader->getDocumentForm();
180
181
            if (!$docForm->hasValidCsrfToken()) {
182
                throw new Exception("Invalid CSRF Token");
183
            }
184
185
            $requestArguments['newDocumentForm'] = $docForm;
186
187
            $docTypeUid = $documentData['type'];
188
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
189
            $virtualType = $documentType->getVirtualType();
190
191
            if (!$formDataReader->uploadError() || $virtualType === true) {
192
                $this->request->setArguments($requestArguments);
193
            } else {
194
                $t = $docForm->getFileNames();
195
                $this->redirect('list', 'DocumentForm', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t));
196
            }
197
        } else {
198
            $this->redirectToList("UPLOAD_POST_SIZE_ERROR");
199
        }
200
    }
201
202
    /**
203
     * action create
204
     *
205
     * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm
206
     * @return void
207
     */
208
    public function createAction(DocumentForm $newDocumentForm)
209
    {
210
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
211
212
        /* @var $newDocument \EWW\Dpf\Domain\Model\Document */
213
        $newDocument    = $documentMapper->getDocument($newDocumentForm);
214
215
        $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow();
216
217
        if ($this->request->getPluginName() === "Backoffice") {
218
            $newDocument->setCreator($this->security->getUser()->getUid());
219
            $workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE);
220
        } else {
221
            $workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE_REGISTER);
222
        }
223
224
        // xml data fields are limited to 64 KB
225
        if (strlen($newDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) {
226
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
227
        }
228
229
        $this->documentRepository->add($newDocument);
230
        $this->persistenceManager->persistAll();
231
232
        $newDocument = $this->documentRepository->findByUid($newDocument->getUid());
233
        $this->persistenceManager->persistAll();
234
235
        $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($newDocument->getProcessNumber());
0 ignored issues
show
The method findOneByProcessNumber() does not exist on EWW\Dpf\Domain\Repositor...sitLicenseLogRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

235
        /** @scrutinizer ignore-call */ 
236
        $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($newDocument->getProcessNumber());
Loading history...
236
        if (empty($depositLicenseLog) && $newDocument->getDepositLicense()) {
237
            // Only if there was no deposit license a notification may be sent
238
239
            /** @var DepositLicenseLog $depositLicenseLog */
240
            $depositLicenseLog = $this->objectManager->get(DepositLicenseLog::class);
241
            $depositLicenseLog->setUsername($this->security->getUsername());
242
            $depositLicenseLog->setObjectIdentifier($newDocument->getObjectIdentifier());
243
            $depositLicenseLog->setProcessNumber($newDocument->getProcessNumber());
244
            $depositLicenseLog->setTitle($newDocument->getTitle());
245
            $depositLicenseLog->setUrn($newDocument->getPrimaryUrn());
246
            $depositLicenseLog->setLicenceUri($newDocument->getDepositLicense());
247
248
            if ($newDocument->hasFiles()) {
249
                $fileList = [];
250
                foreach ($newDocument->getFile() as $file) {
251
                    if (!$file->isFileGroupDeleted()) {
252
                        $fileList[] = $file->getTitle();
253
                    }
254
                }
255
                $depositLicenseLog->setFileNames(implode(", ", $fileList));
256
            }
257
258
            $this->depositLicenseLogRepository->add($depositLicenseLog);
259
260
            /** @var Notifier $notifier */
261
            $notifier = $this->objectManager->get(Notifier::class);
262
            $notifier->sendDepositLicenseNotification($newDocument);
263
        }
264
265
        // Add or update files
266
        $files = $newDocumentForm->getFiles();
267
        // TODO: Is this still necessary?
268
        if (is_array($files)) {
0 ignored issues
show
The condition is_array($files) is always true.
Loading history...
269
            foreach ($files as $file) {
270
                // TODO: Is this still necessary?
271
                if ($file->getUID()) {
272
                    $this->fileRepository->update($file);
273
                } else {
274
                    $file->setDocument($newDocument);
275
                    $this->fileRepository->add($file);
276
                    $newDocument->addFile($file);
277
                    $this->documentRepository->update($newDocument);
278
                }
279
            }
280
        }
281
        $this->persistenceManager->persistAll();
282
283
        // index the document
284
        $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$newDocument]);
285
286
    }
287
288
    public function initializeEditAction()
289
    {
290
        $requestArguments = $this->request->getArguments();
291
292
        if (array_key_exists('document', $requestArguments)) {
293
294
            $document = $this->documentManager->read(
295
                $this->request->getArgument('document'),
296
                $this->security->getUser()->getUID()
297
            );
298
299
            if ($document) {
300
                $mapper = $this->objectManager->get(DocumentMapper::class);
301
                $documentForm = $mapper->getDocumentForm($document);
302
            }
303
304
        } elseif (array_key_exists('documentForm', $requestArguments)) {
305
            $documentForm = $this->request->getArgument('documentForm');
306
        }
307
308
        $requestArguments['documentForm'] = $documentForm;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $documentForm does not seem to be defined for all execution paths leading up to this point.
Loading history...
309
        $this->request->setArguments($requestArguments);
310
    }
311
312
    /**
313
     * action edit
314
     *
315
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
316
     * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("documentForm")
317
     * @return void
318
     */
319
    public function editAction(DocumentForm $documentForm)
320
    {
321
        $this->view->assign('documentForm', $documentForm);
322
323
        if (!empty($this->security->getUserAccessToGroups())) {
324
            $this->view->assign('currentUserAccessToGroup', $this->security->getUserAccessToGroups());
325
        }
326
327
        if ($this->fisDataService->getPersonData($this->security->getFisPersId())) {
328
            $this->view->assign('fisPersId', $this->security->getFisPersId());
329
        }
330
    }
331
332
    public function initializeUpdateAction()
333
    {
334
        $requestArguments = $this->request->getArguments();
335
336
        if ($this->request->hasArgument('documentData')) {
337
            $documentData = $this->request->getArgument('documentData');
338
339
            $formDataReader = $this->objectManager->get(FormDataReader::class);
340
            $formDataReader->setFormData($documentData);
341
            $docForm = $formDataReader->getDocumentForm();
342
343
            if (!$docForm->hasValidCsrfToken()) {
344
                throw new Exception("Invalid CSRF Token");
345
            }
346
347
            $requestArguments['documentForm'] = $docForm;
348
349
            $docTypeUid = $documentData['type'];
350
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
351
            $virtualType = $documentType->getVirtualType();
352
353
354
            if (!$formDataReader->uploadError() || $virtualType === true) {
355
                $this->request->setArguments($requestArguments);
356
            } else {
357
                $t = $docForm->getFileNames();
358
                $this->redirect('list', 'Document', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t));
359
            }
360
        } else {
361
            $this->redirectToList("UPLOAD_POST_SIZE_ERROR");
362
        }
363
    }
364
365
    /**
366
     * action update
367
     *
368
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
369
     * @return void
370
     */
371
    public function updateAction(DocumentForm $documentForm)
372
    {
373
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
374
375
        /* @var $updateDocument \EWW\Dpf\Domain\Model\Document */
376
        $updateDocument = $documentMapper->getDocument($documentForm);
377
378
        // xml data fields are limited to 64 KB
379
        if (strlen($updateDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) {
380
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
381
        }
382
383
        // add document to local es index
384
        $elasticsearchMapper = $this->objectManager->get(ElasticsearchMapper::class);
0 ignored issues
show
The type EWW\Dpf\Controller\ElasticsearchMapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
385
        $json                = $elasticsearchMapper->getElasticsearchJson($updateDocument);
386
387
        $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class);
0 ignored issues
show
The type EWW\Dpf\Controller\ElasticsearchRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
388
        // send document to index
389
        $elasticsearchRepository->add($updateDocument, $json);
390
391
        $updateDocument->setChanged(true);
392
        $this->documentRepository->update($updateDocument);
393
394
        // index the document
395
        $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$updateDocument]);
396
    }
397
398
    /**
399
     * action cancel
400
     *
401
     * @return void
402
     */
403
    public function cancelAction()
404
    {
405
        $this->redirectToList();
406
    }
407
408
    protected function redirectAfterUpdate()
409
    {
410
        $this->redirect('list');
411
    }
412
413
    protected function redirectToList($message = null)
0 ignored issues
show
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

413
    protected function redirectToList(/** @scrutinizer ignore-unused */ $message = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
414
    {
415
        $this->redirect('list');
416
    }
417
418
}
419