Passed
Pull Request — master (#195)
by
unknown
06:45
created

DocumentFormBackofficeController   F

Complexity

Total Complexity 71

Size/Duplication

Total Lines 616
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 294
c 1
b 0
f 0
dl 0
loc 616
rs 2.7199
wmc 71

11 Methods

Rating   Name   Duplication   Size   Complexity  
A cancelNewAction() 0 3 1
A editAction() 0 50 5
A cancelEditAction() 0 9 3
A updateAction() 0 26 4
A redirectToDocumentList() 0 8 2
F updateDocumentAction() 0 197 29
A __construct() 0 8 1
A arrayRecursiveDiff() 0 18 6
A createAction() 0 54 5
A initializeAction() 0 5 1
F createSuggestionDocumentAction() 0 111 14

How to fix   Complexity   

Complex Class

Complex classes like DocumentFormBackofficeController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DocumentFormBackofficeController, and based on these observations, apply Extract Interface, too.

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
505
506
            throw $exception;
507
508
            $severity = AbstractMessage::ERROR;
0 ignored issues
show
Unused Code introduced by
$severity = TYPO3\CMS\Co...\AbstractMessage::ERROR is not reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
509
510
            if ($exception instanceof DPFExceptionInterface) {
511
                $key = $exception->messageLanguageKey();
512
            } else {
513
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
514
            }
515
516
            $exceptionMsg[] = LocalizationUtility::translate(
517
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.failure',
518
                'dpf',
519
                array($updateDocument->getTitle())
520
            );
521
522
            $exceptionMsg[] = LocalizationUtility::translate($key, 'dpf');
523
524
            $this->addFlashMessage(implode(" ", $exceptionMsg), '', $severity, true);
525
            $this->redirect('cancelEdit',
526
                null,
527
                null,
528
                ['documentUid' => $updateDocument->getUid(), 'backToList' => $backToList]
529
            );
530
            $this->redirect('showDetails', 'Document', null, ['document' => $updateDocument]);
531
        }
532
    }
533
534
    public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm)
535
    {
536
        /** @var \EWW\Dpf\Helper\DocumentMapper $documentMapper */
537
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
538
539
        /** @var \EWW\Dpf\Domain\Model\Document $document */
540
        $document = $documentMapper->getDocument($newDocumentForm);
541
542
        if (!$this->authorizationChecker->isGranted(DocumentVoter::CREATE, $document)) {
543
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:documentForm.create.accessDenied';
544
            $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...
545
            $message = LocalizationUtility::translate($key, 'dpf', $args);
546
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
547
            $this->redirect('showDetails', 'Document', null, ['document' => $document]);
548
            return FALSE;
549
        }
550
551
        try {
552
            parent::createAction($newDocumentForm);
553
554
            $severity = AbstractMessage::OK;
555
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:documentForm.create.ok';
556
            $message = LocalizationUtility::translate($key, 'dpf');
557
            $this->addFlashMessage(
558
                $message,
559
                '',
560
                $severity,
561
                true
562
            );
563
564
        } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
565
            // A redirect always throws this exception, but in this case, however,
566
            // redirection is desired and should not lead to an exception handling
567
        } catch (\Exception $exception) {
568
569
            $severity = AbstractMessage::ERROR;
570
571
            if ($exception instanceof DPFExceptionInterface) {
572
                $key = $exception->messageLanguageKey();
573
            } else {
574
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
575
            }
576
577
            $message[] = LocalizationUtility::translate($key, 'dpf');
578
579
            $this->addFlashMessage(
580
                implode(" ", $message),
581
                '',
582
                $severity,
583
                true
584
            );
585
        }
586
587
        $this->redirect('listWorkspace', 'Workspace');
588
    }
589
590
591
    /**
592
     * action cancel edit
593
     *
594
     * @param integer $documentUid
595
     * @param bool $backToList
596
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
597
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
598
     *
599
     * @return void
600
     */
601
    public function cancelEditAction($documentUid = 0, $backToList = false)
602
    {
603
        if (empty($documentUid) || $backToList) {
604
            $this->redirectToDocumentList();
605
        }
606
607
        /** @var $document \EWW\Dpf\Domain\Model\Document */
608
        $document = $this->documentRepository->findByUid($documentUid);
609
        $this->redirect('showDetails', 'Document', null, ['document' => $document]);
610
    }
611
612
    /**
613
     * action cancel new
614
     *
615
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
616
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
617
     *
618
     * @return void
619
     */
620
    public function cancelNewAction()
621
    {
622
        $this->redirect('list');
623
    }
624
625
626
    public function initializeAction()
627
    {
628
        $this->authorizationChecker->denyAccessUnlessLoggedIn();
629
630
        parent::initializeAction();
631
632
    }
633
634
    /**
635
     * Redirect to the current document list.
636
     *
637
     * @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...
638
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
639
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
640
     */
641
    protected function redirectToDocumentList($message = null)
642
    {
643
        list($action, $controller, $redirectUri) = $this->session->getStoredAction();
644
645
        if ($redirectUri) {
646
            $this->redirectToUri($redirectUri);
647
        } else {
648
            $this->redirect($action, $controller, null, array('message' => $message));
649
        }
650
    }
651
}
652