Passed
Pull Request — master (#214)
by Ralf
11:24
created

documentFormMapping()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 35
rs 9.2728
c 0
b 0
f 0
cc 5
nc 4
nop 0
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
     * documentManager
34
     *
35
     * @var \EWW\Dpf\Services\Document\DocumentManager
36
     * @TYPO3\CMS\Extbase\Annotation\Inject
37
     */
38
    protected $documentManager = null;
39
40
    /**
41
     * documentRepository
42
     *
43
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
44
     * @TYPO3\CMS\Extbase\Annotation\Inject
45
     */
46
    protected $documentRepository = null;
47
48
    /**
49
     * fileRepository
50
     *
51
     * @var \EWW\Dpf\Domain\Repository\FileRepository
52
     * @TYPO3\CMS\Extbase\Annotation\Inject
53
     */
54
    protected $fileRepository = null;
55
56
    /**
57
     * documentTypeRepository
58
     *
59
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
60
     * @TYPO3\CMS\Extbase\Annotation\Inject
61
     */
62
    protected $documentTypeRepository = null;
63
64
    /**
65
     * metadataGroupRepository
66
     *
67
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
68
     * @TYPO3\CMS\Extbase\Annotation\Inject
69
     */
70
    protected $metadataGroupRepository = null;
71
72
    /**
73
     * metadataObjectRepository
74
     *
75
     * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository
76
     * @TYPO3\CMS\Extbase\Annotation\Inject
77
     */
78
    protected $metadataObjectRepository = null;
79
80
    /**
81
     * depositLicenseLogRepository
82
     *
83
     * @var \EWW\Dpf\Domain\Repository\DepositLicenseLogRepository
84
     * @TYPO3\CMS\Extbase\Annotation\Inject
85
     */
86
    protected $depositLicenseLogRepository = null;
87
88
    /**
89
     * persistence manager
90
     *
91
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
92
     * @TYPO3\CMS\Extbase\Annotation\Inject
93
     */
94
    protected $persistenceManager;
95
96
    /**
97
     * fisDataService
98
     *
99
     * @var \EWW\Dpf\Services\FeUser\FisDataService
100
     * @TYPO3\CMS\Extbase\Annotation\Inject
101
     */
102
    protected $fisDataService = null;
103
104
    /**
105
     * @var Document
106
     */
107
    protected $newDocument = null;
108
109
    /**
110
     * action list
111
     *
112
     * @return void
113
     */
114
    public function listAction()
115
    {
116
        $documents = $this->documentRepository->findAll();
117
        $docTypes = $this->documentTypeRepository->getDocumentTypesAlphabetically();
118
119
        if ($this->request->hasArgument('message')) {
120
            $this->view->assign('message', $this->request->getArgument('message'));
121
        }
122
123
        if ($this->request->hasArgument('errorFiles')) {
124
            $this->view->assign('errorFiles', $this->request->getArgument('errorFiles'));
125
        }
126
127
        $this->view->assign('documentTypes', $docTypes);
128
        $this->view->assign('documents', $documents);
129
    }
130
131
    /**
132
     * initialize newAction
133
     *
134
     * @return void
135
     */
136
    public function initializeNewAction()
137
    {
138
139
        $requestArguments = $this->request->getArguments();
140
141
        if (array_key_exists('documentData', $requestArguments)) {
142
            die('Error: initializeNewAction');
0 ignored issues
show
Best Practice introduced by
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...
143
        } elseif (array_key_exists('documentType', $requestArguments)) {
144
            $docTypeUid   = $this->request->getArgument('documentType');
145
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
0 ignored issues
show
Bug introduced by
$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

145
            $documentType = $this->documentTypeRepository->findByUid(/** @scrutinizer ignore-type */ $docTypeUid);
Loading history...
146
            $document     = $this->objectManager->get(Document::class);
147
            $document->setDocumentType($documentType);
148
            $mapper  = $this->objectManager->get(DocumentMapper::class);
149
            $docForm = $mapper->getDocumentForm($document);
150
        } elseif (array_key_exists('newDocumentForm', $requestArguments)) {
151
            $docForm = $this->request->getArgument('newDocumentForm');
152
            if (is_numeric($docForm)) {
153
                $sessionData = $this->session->getData();
154
                $docForm = null;
155
                if (array_key_exists('newDocumentForm', $sessionData)) {
156
                    $docForm = unserialize($sessionData['newDocumentForm']);
157
                }
158
            }
159
        }
160
161
        $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...
162
        $this->request->setArguments($requestArguments);
163
    }
164
165
    /**
166
     * action new
167
     *
168
     * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm
169
     * @param int $returnDocumentId
170
     * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("newDocumentForm")
171
     * @return void
172
     */
173
    public function newAction(DocumentForm $newDocumentForm = null, $returnDocumentId = 0)
174
    {
175
        $this->view->assign('returnDocumentId', $returnDocumentId);
176
        $this->view->assign('documentForm', $newDocumentForm);
177
178
        if (!empty($this->security->getUserAccessToGroups())) {
179
            $this->view->assign('currentUserAccessToGroup', $this->security->getUserAccessToGroups());
180
        }
181
182
        if ($this->fisDataService->getPersonData($this->security->getFisPersId())) {
183
            $this->view->assign('fisPersId', $this->security->getFisPersId());
184
        }
185
    }
186
187
    public function initializeCreateAction()
188
    {
189
        $this->documentFormMapping();
190
    }
191
192
    /**
193
     * action create
194
     *
195
     * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm
196
     * @return void
197
     */
198
    public function createAction(DocumentForm $newDocumentForm)
199
    {
200
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
201
202
        /* @var $newDocument \EWW\Dpf\Domain\Model\Document */
203
        $newDocument    = $documentMapper->getDocument($newDocumentForm);
204
        $this->newDocument = $newDocument;
205
206
        $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow();
207
208
        $workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE);
209
210
        if ($this->request->getPluginName() === "Backoffice") {
211
            $newDocument->setCreator($this->security->getUser()->getUid());
212
            //$workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE);
213
        } else {
214
            $newDocument->setCreator(0);
215
            $newDocument->setTemporary(true);
216
            //$workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE_REGISTER);
217
        }
218
219
        // xml data fields are limited to 64 KB
220
        if (strlen($newDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) {
221
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
222
        }
223
224
        $this->documentRepository->add($newDocument);
225
        $this->persistenceManager->persistAll();
226
227
        $newDocument = $this->documentRepository->findByUid($newDocument->getUid());
228
        $this->persistenceManager->persistAll();
229
230
        $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($newDocument->getProcessNumber());
0 ignored issues
show
Bug introduced by
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

230
        /** @scrutinizer ignore-call */ 
231
        $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($newDocument->getProcessNumber());
Loading history...
231
        if (empty($depositLicenseLog) && $newDocument->getDepositLicense()) {
232
            // Only if there was no deposit license a notification may be sent
233
234
            /** @var DepositLicenseLog $depositLicenseLog */
235
            $depositLicenseLog = $this->objectManager->get(DepositLicenseLog::class);
236
            $depositLicenseLog->setUsername($this->security->getUsername());
237
            $depositLicenseLog->setObjectIdentifier($newDocument->getObjectIdentifier());
238
            $depositLicenseLog->setProcessNumber($newDocument->getProcessNumber());
239
            $depositLicenseLog->setTitle($newDocument->getTitle());
240
            $depositLicenseLog->setUrn($newDocument->getPrimaryUrn());
241
            $depositLicenseLog->setLicenceUri($newDocument->getDepositLicense());
242
243
            if ($newDocument->hasFiles()) {
244
                $fileList = [];
245
                foreach ($newDocument->getFile() as $file) {
246
                    if (!$file->isFileGroupDeleted()) {
247
                        $fileList[] = $file->getTitle();
248
                    }
249
                }
250
                $depositLicenseLog->setFileNames(implode(", ", $fileList));
251
            }
252
253
            $this->depositLicenseLogRepository->add($depositLicenseLog);
254
255
            if (!$newDocument->isTemporary()) {
256
                /** @var Notifier $notifier */
257
                $notifier = $this->objectManager->get(Notifier::class);
258
                $notifier->sendDepositLicenseNotification($newDocument);
259
            }
260
        }
261
262
        // Add or update files
263
        $files = $newDocumentForm->getFiles();
264
        // TODO: Is this still necessary?
265
        if (is_array($files)) {
0 ignored issues
show
introduced by
The condition is_array($files) is always true.
Loading history...
266
            foreach ($files as $file) {
267
                // TODO: Is this still necessary?
268
                if ($file->getUID()) {
269
                    $this->fileRepository->update($file);
270
                } else {
271
                    $file->setDocument($newDocument);
272
                    $this->fileRepository->add($file);
273
                    $newDocument->addFile($file);
274
                    $this->documentRepository->update($newDocument);
275
                }
276
            }
277
        }
278
        $this->persistenceManager->persistAll();
279
280
        if (!$newDocument->isTemporary()) {
281
            // index the document
282
            $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$newDocument]);
283
        }
284
    }
285
286
    public function initializeEditAction()
287
    {
288
        $requestArguments = $this->request->getArguments();
289
290
        if (array_key_exists('document', $requestArguments)) {
291
292
            $document = $this->documentManager->read(
293
                $this->request->getArgument('document')
0 ignored issues
show
Bug introduced by
It seems like $this->request->getArgument('document') can also be of type array; however, parameter $identifier of EWW\Dpf\Services\Document\DocumentManager::read() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

293
                /** @scrutinizer ignore-type */ $this->request->getArgument('document')
Loading history...
294
            );
295
296
            if ($document) {
0 ignored issues
show
introduced by
$document is of type EWW\Dpf\Domain\Model\Document, thus it always evaluated to true.
Loading history...
297
                $mapper = $this->objectManager->get(DocumentMapper::class);
298
                $documentForm = $mapper->getDocumentForm($document);
299
            }
300
301
        } elseif (array_key_exists('documentForm', $requestArguments)) {
302
            $documentForm = $this->request->getArgument('documentForm');
303
        }
304
305
        $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...
306
        $this->request->setArguments($requestArguments);
307
    }
308
309
    /**
310
     * action edit
311
     *
312
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
313
     * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("documentForm")
314
     * @return void
315
     */
316
    public function editAction(DocumentForm $documentForm)
317
    {
318
        $this->view->assign('documentForm', $documentForm);
319
320
        if (!empty($this->security->getUserAccessToGroups())) {
321
            $this->view->assign('currentUserAccessToGroup', $this->security->getUserAccessToGroups());
322
        }
323
324
        if ($this->fisDataService->getPersonData($this->security->getFisPersId())) {
325
            $this->view->assign('fisPersId', $this->security->getFisPersId());
326
        }
327
    }
328
329
    public function initializeUpdateAction()
330
    {
331
        $requestArguments = $this->request->getArguments();
332
333
        if ($this->request->hasArgument('documentData')) {
334
            $documentData = $this->request->getArgument('documentData');
335
336
            $formDataReader = $this->objectManager->get(FormDataReader::class);
337
            $formDataReader->setFormData($documentData);
338
            $docForm = $formDataReader->getDocumentForm();
339
340
            if (!$docForm->hasValidCsrfToken()) {
341
                throw new Exception("Invalid CSRF Token");
342
            }
343
344
            $requestArguments['documentForm'] = $docForm;
345
346
            $docTypeUid = $documentData['type'];
347
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
348
            $virtualType = $documentType->getVirtualType();
349
350
351
            if (!$formDataReader->uploadError() || $virtualType === true) {
352
                $this->request->setArguments($requestArguments);
353
            } else {
354
                $t = $docForm->getFileNames();
355
                $this->redirect('list', 'Document', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t));
356
            }
357
        } else {
358
            $this->redirectToList("UPLOAD_POST_SIZE_ERROR");
359
        }
360
    }
361
362
    /**
363
     * action cancel
364
     *
365
     * @return void
366
     */
367
    public function cancelAction()
368
    {
369
        $this->redirectToList();
370
    }
371
372
    protected function redirectAfterUpdate()
373
    {
374
        $this->redirect('list');
375
    }
376
377
    protected function redirectToList($message = null)
0 ignored issues
show
Unused Code introduced by
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

377
    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...
378
    {
379
        $this->redirect('list');
380
    }
381
382
    protected function documentFormMapping()
383
    {
384
        $requestArguments = $this->request->getArguments();
385
386
        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($requestArguments, null, 20);
387
388
389
        if ($this->request->hasArgument('documentData')) {
390
            $documentData = $this->request->getArgument('documentData');
391
392
            $formDataReader = $this->objectManager->get(FormDataReader::class);
393
            $formDataReader->setFormData($documentData);
394
395
            $docForm = $formDataReader->getDocumentForm();
396
397
            if (!$docForm->hasValidCsrfToken()) {
398
                throw new Exception("Invalid CSRF Token");
399
            }
400
            \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($docForm, null, 20);
401
402
403
            $requestArguments['newDocumentForm'] = $docForm;
404
405
            $docTypeUid = $documentData['type'];
406
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
407
            $virtualType = $documentType->getVirtualType();
408
409
            if (!$formDataReader->uploadError() || $virtualType === true) {
410
                $this->request->setArguments($requestArguments);
411
            } else {
412
                $t = $docForm->getFileNames();
413
                $this->redirect('list', 'DocumentForm', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t));
414
            }
415
        } else {
416
            $this->redirectToList("UPLOAD_POST_SIZE_ERROR");
417
        }
418
    }
419
420
}
421