Passed
Pull Request — master (#195)
by
unknown
09:41 queued 02:26
created

updateDocumentAction()   F

Complexity

Conditions 29
Paths 6834

Size

Total Lines 193
Code Lines 116

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 29
eloc 116
nc 6834
nop 3
dl 0
loc 193
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Helper\DocumentMapper;
19
use EWW\Dpf\Exceptions\AccessDeniedExcepion;
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Exceptions\AccessDeniedExcepion 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...
20
use EWW\Dpf\Security\DocumentVoter;
21
use EWW\Dpf\Security\Security;
22
use EWW\Dpf\Exceptions\DPFExceptionInterface;
23
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
24
use EWW\Dpf\Services\Email\Notifier;
25
use EWW\Dpf\Services\Transfer\DocumentTransferManager;
26
use EWW\Dpf\Services\Transfer\FedoraRepository;
27
use EWW\Dpf\Domain\Model\DepositLicenseLog;
28
use TYPO3\CMS\Core\Messaging\AbstractMessage;
29
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
30
31
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
32
33
class DocumentFormBackofficeController extends AbstractDocumentFormController
34
{
35
    /**
36
     * documentTransferManager
37
     *
38
     * @var \EWW\Dpf\Services\Transfer\DocumentTransferManager $documentTransferManager
39
     */
40
    protected $documentTransferManager;
41
42
    /**
43
     * fedoraRepository
44
     *
45
     * @var \EWW\Dpf\Services\Transfer\FedoraRepository $fedoraRepository
46
     */
47
    protected $fedoraRepository;
48
49
    /**
50
     * editingLockService
51
     *
52
     * @var \EWW\Dpf\Services\Document\EditingLockService
53
     * @inject
54
     */
55
    protected $editingLockService = null;
56
57
    /**
58
     * documentManager
59
     *
60
     * @var \EWW\Dpf\Services\Document\DocumentManager
61
     * @inject
62
     */
63
    protected $documentManager = null;
64
65
    /**
66
     * bookmarkRepository
67
     *
68
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
69
     * @inject
70
     */
71
    protected $bookmarkRepository = null;
72
73
    /**
74
     * clientConfigurationManager
75
     *
76
     * @var \EWW\Dpf\Configuration\ClientConfigurationManager
77
     * @inject
78
     */
79
    protected $clientConfigurationManager;
80
81
    /**
82
     * DocumentController constructor.
83
     */
84
    public function __construct()
85
    {
86
        parent::__construct();
87
88
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
89
        $this->documentTransferManager = $objectManager->get(DocumentTransferManager::class);
90
        $this->fedoraRepository = $objectManager->get(FedoraRepository::class);
91
        $this->documentTransferManager->setRemoteRepository($this->fedoraRepository);
92
    }
93
94
    public function arrayRecursiveDiff($aArray1, $aArray2) {
95
        $aReturn = array();
96
97
        foreach ($aArray1 as $mKey => $mValue) {
98
            if (array_key_exists($mKey, $aArray2)) {
99
                if (is_array($mValue)) {
100
                    $aRecursiveDiff = $this->arrayRecursiveDiff($mValue, $aArray2[$mKey]);
101
                    if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
102
                } else {
103
                    if ($mValue != $aArray2[$mKey]) {
104
                        $aReturn[$mKey] = $mValue;
105
                    }
106
                }
107
            } else {
108
                $aReturn[$mKey] = $mValue;
109
            }
110
        }
111
        return $aReturn;
112
    }
113
114
115
    /**
116
     * action edit
117
     *
118
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
119
     * @param bool $suggestMod
120
     * @param string activeGroup
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Controller\activeGroup 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...
121
     * @param int activeGroupIndex
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Controller\activeGroupIndex 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...
122
     * @param bool $addCurrentFeUser
123
     * @ignorevalidation $documentForm
124
     * @return void
125
     */
126
    public function editAction(
127
        \EWW\Dpf\Domain\Model\DocumentForm $documentForm,
128
        bool $suggestMod = false,
129
        $activeGroup = '',
130
        $activeGroupIndex = 0,
131
        $addCurrentFeUser = true
132
    )
133
    {
134
        /** @var \EWW\Dpf\Domain\Model\Document $document */
135
        $document = $this->documentRepository->findByUid($documentForm->getDocumentUid());
136
137
        if ($suggestMod) {
138
            $documentVoterAttribute = DocumentVoter::SUGGEST_MODIFICATION;
139
        } else {
140
            $documentVoterAttribute = DocumentVoter::EDIT;
141
        }
142
143
        if (!$this->authorizationChecker->isGranted($documentVoterAttribute, $document)) {
144
145
            if ($document->getCreator() !== $this->security->getUser()->getUid()) {
146
                $message = LocalizationUtility::translate(
147
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.accessDenied',
148
                    'dpf',
149
                    array($document->getTitle())
150
                );
151
            } else {
152
                $message = LocalizationUtility::translate(
153
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.failureBlocked',
154
                    'dpf',
155
                    array($document->getTitle())
156
                );
157
            }
158
159
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
160
            $this->redirect('showDetails', 'Document', null, ['document' => $document]);
161
            return FALSE;
0 ignored issues
show
Bug Best Practice introduced by
The expression return FALSE returns the type false which is incompatible with the documented return type void.
Loading history...
162
        }
163
164
        $this->view->assign('document', $document);
165
        $this->view->assign('suggestMod', $suggestMod);
166
167
        $this->editingLockService->lock(
168
            ($document->getObjectIdentifier()? $document->getObjectIdentifier() : $document->getUid()),
169
            $this->security->getUser()->getUid()
170
        );
171
172
        $this->view->assign('activeGroup', $activeGroup);
173
        $this->view->assign('activeGroupIndex', $activeGroupIndex);
174
        $this->view->assign('addCurrentFeUser', $addCurrentFeUser);
175
        parent::editAction($documentForm);
176
    }
177
178
    /**
179
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
180
     * @param bool $restore
181
     */
182
    public function createSuggestionDocumentAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm, $restore = FALSE)
183
    {
184
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
185
186
        $hasFilesFlag = true;
187
188
        $workingCopy = $this->documentRepository->findByUid($documentForm->getDocumentUid());
189
190
        if ($workingCopy->isTemporary()) {
191
            $workingCopy->setTemporary(false);
192
        }
193
194
        if (empty($workingCopy->getFileData())) {
195
            // no files are linked to the document
196
            $hasFilesFlag = false;
197
        }
198
199
        $newDocument = $this->objectManager->get(Document::class);
200
201
        $this->documentRepository->add($newDocument);
202
        $this->persistenceManager->persistAll();
203
204
        /* @var $document \EWW\Dpf\Domain\Model\Document */
205
        $document = $documentMapper->getDocument($documentForm);
206
207
        /* @var $newDocument \EWW\Dpf\Domain\Model\Document */
208
        $newDocument = $newDocument->copy($document);
209
210
        if ($document->getObjectIdentifier()) {
211
            $newDocument->setLinkedUid($document->getObjectIdentifier());
212
        } else {
213
            $newDocument->setLinkedUid($document->getUid());
214
        }
215
216
        $newDocument->setSuggestion(true);
217
        $newDocument->setComment($document->getComment());
218
219
        if ($restore) {
220
            $newDocument->setTransferStatus("RESTORE");
221
        }
222
223
        if (!$hasFilesFlag) {
224
            // Add or update files
225
            foreach ($documentForm->getNewFiles() as $newFile) {
226
                if ($newFile->getUID()) {
227
                    $this->fileRepository->update($newFile);
228
                } else {
229
                    $newFile->setDocument($newDocument);
230
                    $this->fileRepository->add($newFile);
231
                }
232
233
                $newDocument->addFile($newFile);
234
            }
235
        } else {
236
            // remove files for suggest object
237
            $newDocument->setFile($this->objectManager->get(ObjectStorage::class));
238
        }
239
240
241
        try {
242
            $newDocument->setCreator($this->security->getUser()->getUid());
243
            $this->documentRepository->add($newDocument);
244
245
            $flashMessage = $this->clientConfigurationManager->getSuggestionFlashMessage();
246
            if (!$flashMessage) {
247
                $flashMessage = LocalizationUtility::translate(
248
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:message.suggestion_flashmessage',
249
                    'dpf',
250
                    ''
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type array expected by parameter $arguments of TYPO3\CMS\Extbase\Utilit...ionUtility::translate(). ( Ignorable by Annotation )

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

250
                    /** @scrutinizer ignore-type */ ''
Loading history...
251
                );
252
            }
253
            $this->addFlashMessage($flashMessage, '', AbstractMessage::OK, true);
254
255
            $notifier = $this->objectManager->get(Notifier::class);
256
            $notifier->sendAdminNewSuggestionNotification($newDocument);
257
258
            $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

258
            /** @scrutinizer ignore-call */ 
259
            $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($newDocument->getProcessNumber());
Loading history...
259
            if (empty($depositLicenseLog) && $newDocument->getDepositLicense()) {
260
                // Only if there was no deposit license a notification may be sent
261
262
                /** @var DepositLicenseLog $depositLicenseLog */
263
                $depositLicenseLog = $this->objectManager->get(DepositLicenseLog::class);
264
                $depositLicenseLog->setUsername($this->security->getUser()->getUsername());
265
                $depositLicenseLog->setObjectIdentifier($newDocument->getObjectIdentifier());
266
                $depositLicenseLog->setProcessNumber($newDocument->getProcessNumber());
267
                $depositLicenseLog->setTitle($newDocument->getTitle());
268
                $depositLicenseLog->setUrn($newDocument->getQucosaUrn());
269
                $depositLicenseLog->setLicenceUri($newDocument->getDepositLicense());
270
271
                if ($newDocument->getFileData()) {
272
                    $fileList = [];
273
                    foreach ($newDocument->getFile() as $file) {
274
                        $fileList[] = $file->getTitle();
275
                    }
276
                    $depositLicenseLog->setFileNames(implode(", ", $fileList));
277
                }
278
279
280
                $this->depositLicenseLogRepository->add($depositLicenseLog);
281
282
                /** @var Notifier $notifier */
283
                $notifier = $this->objectManager->get(Notifier::class);
284
                $notifier->sendDepositLicenseNotification($newDocument);
285
            }
286
287
        } catch (\Throwable $t) {
288
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
289
            $this->addFlashMessage("Failed", '', $severity,false);
290
        }
291
292
        $this->redirectToDocumentList();
293
    }
294
295
296
    public function updateAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
297
    {
298
        if ($this->request->getArgument('documentData')['suggestMod']) {
299
            $restore = $this->request->getArgument('documentData')['suggestRestore'];
300
            $this->forward('createSuggestionDocument', null, null, ['documentForm' => $documentForm, 'restore' => $restore]);
301
        }
302
303
        $backToList = $this->request->getArgument('documentData')['backToList'];
304
        
305
        if ($this->request->hasArgument('saveAndUpdate')) {
306
            $saveMode = 'saveAndUpdate';
307
        } elseif ($this->request->hasArgument('saveWorkingCopy')) {
308
            $saveMode = 'saveWorkingCopy';
309
        } else {
310
            $saveMode = null;
311
        }
312
313
        $this->forward(
314
315
            'updateDocument',
316
            NULL,
317
            NULL,
318
                [
319
                    'documentForm' => $documentForm,
320
                    'saveMode' => $saveMode,
321
                    'backToList' => $backToList
322
                ]
323
        );
324
    }
325
326
327
    /**
328
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
329
     * @param string $saveMode
330
     * @param bool $backToList
331
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
332
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
333
     */
334
    public function updateDocumentAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm, $saveMode = null, $backToList = false)
335
    {
336
        try {
337
            /** @var \EWW\Dpf\Domain\Model\Document $document */
338
            $document = $this->documentRepository->findByUid($documentForm->getDocumentUid());
339
            $depositLicense = $document->getDepositLicense();
0 ignored issues
show
Unused Code introduced by
The assignment to $depositLicense is dead and can be removed.
Loading history...
340
341
            if (
342
                !$this->authorizationChecker->isGranted(DocumentVoter::UPDATE, $document) ||
343
                (
344
                    $saveMode == 'saveWorkingCopy' &&
345
                    $this->security->getUser()->getUserRole() !== Security::ROLE_LIBRARIAN
346
                )
347
            ) {
348
                $message = LocalizationUtility::translate(
349
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.accessDenied',
350
                    'dpf',
351
                    array($document->getTitle())
352
                );
353
                $this->addFlashMessage($message, '', AbstractMessage::ERROR);
354
355
                $this->redirect('cancelEdit',
356
                    null,
357
                    null,
358
                    ['documentUid' => $document->getUid(), 'backToList' => $backToList]
359
                );
360
                /*
361
                $this->redirect(
362
                    'showDetails', 'Document',
363
                    null, ['document' => $document]
364
                );
365
                */
366
367
            }
368
369
            /** @var  \EWW\Dpf\Helper\DocumentMapper $documentMapper */
370
            $documentMapper = $this->objectManager->get(DocumentMapper::class);
371
372
            /** @var \EWW\Dpf\Domain\Model\Document $updateDocument */
373
            $updateDocument = $documentMapper->getDocument($documentForm);
374
375
            $saveWorkingCopy = false;
376
            $workflowTransition = null;
377
378
            // Convert the temporary copy into a local working copy if needed.
379
            if ( $updateDocument->isTemporaryCopy() && $saveMode == 'saveWorkingCopy') {
380
                $saveWorkingCopy = true;
381
                $updateDocument->setTemporary(false);
382
                $workflowTransition = DocumentWorkflow::TRANSITION_IN_PROGRESS;
383
            } elseif ($updateDocument->isTemporaryCopy() && $saveMode == 'saveAndUpdate') {
384
                $workflowTransition = DocumentWorkflow::TRANSITION_REMOTE_UPDATE;
385
            } elseif (
386
                $this->security->getUser()->getUserRole() === Security::ROLE_LIBRARIAN &&
387
                $updateDocument->getState() === DocumentWorkflow::STATE_REGISTERED_NONE
388
            ) {
389
                $workflowTransition = DocumentWorkflow::TRANSITION_IN_PROGRESS;
390
            }
391
392
            if (
393
                $this->documentManager->update(
394
                    $updateDocument, $workflowTransition,
395
                    $documentForm->getDeletedFiles(), $documentForm->getNewFiles()
396
                )
397
            ) {
398
399
                $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($document->getProcessNumber());
400
                if (empty($depositLicenseLog) && $updateDocument->getDepositLicense()) {
401
                    // Only if there was no deposit license a notification may be sent
402
403
                    /** @var DepositLicenseLog $depositLicenseLog */
404
                    $depositLicenseLog = $this->objectManager->get(DepositLicenseLog::class);
405
                    $depositLicenseLog->setUsername($this->security->getUser()->getUsername());
406
                    $depositLicenseLog->setObjectIdentifier($document->getObjectIdentifier());
407
                    $depositLicenseLog->setProcessNumber($document->getProcessNumber());
408
                    $depositLicenseLog->setTitle($document->getTitle());
409
                    $depositLicenseLog->setUrn($document->getQucosaUrn());
410
                    $depositLicenseLog->setLicenceUri($document->getDepositLicense());
411
412
                    if ($document->getFileData()) {
413
                        $fileList = [];
414
                        foreach ($document->getFile() as $file) {
415
                            $fileList[] = $file->getTitle();
416
                        }
417
                        $depositLicenseLog->setFileNames(implode(", ", $fileList));
418
                    }
419
420
421
                    $this->depositLicenseLogRepository->add($depositLicenseLog);
422
423
                    /** @var Notifier $notifier */
424
                    $notifier = $this->objectManager->get(Notifier::class);
425
                    $notifier->sendDepositLicenseNotification($updateDocument);
426
                }
427
428
                $message = LocalizationUtility::translate(
429
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.success',
430
                    'dpf',
431
                    array($updateDocument->getTitle())
432
                );
433
                $this->addFlashMessage($message, '', AbstractMessage::OK);
434
435
                if ($this->security->getUser()->getUserRole() === Security::ROLE_LIBRARIAN) {
436
                    if ($saveWorkingCopy) {
437
                        if (
438
                            $this->bookmarkRepository->addBookmark(
439
                                $updateDocument,
440
                                $this->security->getUser()->getUid()
441
                            )
442
                        ) {
443
                            $this->addFlashMessage(
444
                                LocalizationUtility::translate(
445
                                    "manager.workspace.bookmarkAdded", "dpf"
446
                                ),
447
                                '',
448
                                AbstractMessage::INFO
449
                            );
450
                        }
451
                    } else {
452
                        switch ($document->getState()) {
453
                            case DocumentWorkflow::STATE_POSTPONED_NONE:
454
                            case DocumentWorkflow::STATE_DISCARDED_NONE:
455
                            case DocumentWorkflow::STATE_NONE_INACTIVE:
456
                            case DocumentWorkflow::STATE_NONE_ACTIVE:
457
                            case DocumentWorkflow::STATE_NONE_DELETED:
458
459
                                if (
460
                                    $this->bookmarkRepository->removeBookmark(
461
                                        $updateDocument,
462
                                        $this->security->getUser()->getUid()
463
                                    )
464
                                ) {
465
                                    $this->addFlashMessage(
466
                                        LocalizationUtility::translate(
467
                                            "manager.workspace.bookmarkRemoved.singular", "dpf"
468
                                        ),
469
                                        '',
470
                                        AbstractMessage::INFO
471
                                    );
472
                                }
473
474
                                $this->redirectToDocumentList();
475
476
                                break;
477
                        }
478
                    }
479
                }
480
481
            } else {
482
                $message = LocalizationUtility::translate(
483
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.failure',
484
                    'dpf',
485
                    array($updateDocument->getTitle())
486
                );
487
                $this->addFlashMessage($message, '', AbstractMessage::ERROR);
488
            }
489
490
            if ($workflowTransition && $workflowTransition === DocumentWorkflow::TRANSITION_REMOTE_UPDATE) {
491
                $this->redirectToDocumentList();
492
            } else {
493
                $this->redirect('cancelEdit',
494
                    null,
495
                    null,
496
                    ['documentUid' => $updateDocument->getUid(), 'backToList' => $backToList]
497
                );
498
                // $this->redirect('showDetails', 'Document', null, ['document' => $updateDocument]);
499
            }
500
        } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
501
            // A redirect always throws this exception, but in this case, however,
502
            // redirection is desired and should not lead to an exception handling
503
        } catch (\Exception $exception) {
504
            $severity = AbstractMessage::ERROR;
505
506
            if ($exception instanceof DPFExceptionInterface) {
507
                $key = $exception->messageLanguageKey();
508
            } else {
509
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
510
            }
511
512
            $exceptionMsg[] = LocalizationUtility::translate(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$exceptionMsg was never initialized. Although not strictly required by PHP, it is generally a good practice to add $exceptionMsg = array(); before regardless.
Loading history...
513
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.failure',
514
                'dpf',
515
                array($updateDocument->getTitle())
516
            );
517
518
            $exceptionMsg[] = LocalizationUtility::translate($key, 'dpf');
519
520
            $this->addFlashMessage(implode(" ", $exceptionMsg), '', $severity, true);
521
            $this->redirect('cancelEdit',
522
                null,
523
                null,
524
                ['documentUid' => $updateDocument->getUid(), 'backToList' => $backToList]
525
            );
526
            $this->redirect('showDetails', 'Document', null, ['document' => $updateDocument]);
527
        }
528
    }
529
530
    public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm)
531
    {
532
        /** @var \EWW\Dpf\Helper\DocumentMapper $documentMapper */
533
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
534
535
        /** @var \EWW\Dpf\Domain\Model\Document $document */
536
        $document = $documentMapper->getDocument($newDocumentForm);
537
538
        if (!$this->authorizationChecker->isGranted(DocumentVoter::CREATE, $document)) {
539
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:documentForm.create.accessDenied';
540
            $args[] = $document->getTitle();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args = array(); before regardless.
Loading history...
541
            $message = LocalizationUtility::translate($key, 'dpf', $args);
542
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
543
            $this->redirect('showDetails', 'Document', null, ['document' => $document]);
544
            return FALSE;
545
        }
546
547
        try {
548
            parent::createAction($newDocumentForm);
549
550
            $severity = AbstractMessage::OK;
551
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:documentForm.create.ok';
552
            $message = LocalizationUtility::translate($key, 'dpf');
553
            $this->addFlashMessage(
554
                $message,
555
                '',
556
                $severity,
557
                true
558
            );
559
560
        } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
561
            // A redirect always throws this exception, but in this case, however,
562
            // redirection is desired and should not lead to an exception handling
563
        } catch (\Exception $exception) {
564
565
            $severity = AbstractMessage::ERROR;
566
567
            if ($exception instanceof DPFExceptionInterface) {
568
                $key = $exception->messageLanguageKey();
569
            } else {
570
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
571
            }
572
573
            $message[] = LocalizationUtility::translate($key, 'dpf');
574
575
            $this->addFlashMessage(
576
                implode(" ", $message),
577
                '',
578
                $severity,
579
                true
580
            );
581
        }
582
583
        $this->redirect('listWorkspace', 'Workspace');
584
    }
585
586
587
    /**
588
     * action cancel edit
589
     *
590
     * @param integer $documentUid
591
     * @param bool $backToList
592
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
593
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
594
     *
595
     * @return void
596
     */
597
    public function cancelEditAction($documentUid = 0, $backToList = false)
598
    {
599
        if (empty($documentUid) || $backToList) {
600
            $this->redirectToDocumentList();
601
        }
602
603
        /** @var $document \EWW\Dpf\Domain\Model\Document */
604
        $document = $this->documentRepository->findByUid($documentUid);
605
        $this->redirect('showDetails', 'Document', null, ['document' => $document]);
606
    }
607
608
    /**
609
     * action cancel new
610
     *
611
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
612
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
613
     *
614
     * @return void
615
     */
616
    public function cancelNewAction()
617
    {
618
        $this->redirect('list');
619
    }
620
621
622
    public function initializeAction()
623
    {
624
        $this->authorizationChecker->denyAccessUnlessLoggedIn();
625
626
        parent::initializeAction();
627
628
    }
629
630
    /**
631
     * Redirect to the current document list.
632
     *
633
     * @param null $message
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $message is correct as it would always require null to be passed?
Loading history...
634
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
635
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
636
     */
637
    protected function redirectToDocumentList($message = null)
638
    {
639
        list($action, $controller, $redirectUri) = $this->session->getStoredAction();
640
641
        if ($redirectUri) {
642
            $this->redirectToUri($redirectUri);
643
        } else {
644
            $this->redirect($action, $controller, null, array('message' => $message));
645
        }
646
    }
647
}
648