Passed
Pull Request — master (#173)
by
unknown
11:53
created

arrayRecursiveDiff()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 18
rs 9.2222
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\Transfer\DocumentTransferManager;
25
use EWW\Dpf\Services\Transfer\FedoraRepository;
26
use TYPO3\CMS\Core\Messaging\AbstractMessage;
27
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
28
29
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
30
31
class DocumentFormBackofficeController extends AbstractDocumentFormController
32
{
33
    /**
34
     * documentTransferManager
35
     *
36
     * @var \EWW\Dpf\Services\Transfer\DocumentTransferManager $documentTransferManager
37
     */
38
    protected $documentTransferManager;
39
40
    /**
41
     * fedoraRepository
42
     *
43
     * @var \EWW\Dpf\Services\Transfer\FedoraRepository $fedoraRepository
44
     */
45
    protected $fedoraRepository;
46
47
    /**
48
     * editingLockService
49
     *
50
     * @var \EWW\Dpf\Services\Document\EditingLockService
51
     * @inject
52
     */
53
    protected $editingLockService = null;
54
55
    /**
56
     * documentManager
57
     *
58
     * @var \EWW\Dpf\Services\Document\DocumentManager
59
     * @inject
60
     */
61
    protected $documentManager = null;
62
63
    /**
64
     * bookmarkRepository
65
     *
66
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
67
     * @inject
68
     */
69
    protected $bookmarkRepository = null;
70
71
    /**
72
     * DocumentController constructor.
73
     */
74
    public function __construct()
75
    {
76
        parent::__construct();
77
78
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
79
        $this->documentTransferManager = $objectManager->get(DocumentTransferManager::class);
80
        $this->fedoraRepository = $objectManager->get(FedoraRepository::class);
81
        $this->documentTransferManager->setRemoteRepository($this->fedoraRepository);
82
    }
83
84
    public function arrayRecursiveDiff($aArray1, $aArray2) {
85
        $aReturn = array();
86
87
        foreach ($aArray1 as $mKey => $mValue) {
88
            if (array_key_exists($mKey, $aArray2)) {
89
                if (is_array($mValue)) {
90
                    $aRecursiveDiff = $this->arrayRecursiveDiff($mValue, $aArray2[$mKey]);
91
                    if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
92
                } else {
93
                    if ($mValue != $aArray2[$mKey]) {
94
                        $aReturn[$mKey] = $mValue;
95
                    }
96
                }
97
            } else {
98
                $aReturn[$mKey] = $mValue;
99
            }
100
        }
101
        return $aReturn;
102
    }
103
104
105
    /**
106
     * action edit
107
     *
108
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
109
     * @param bool $suggestMod
110
     * @param bool activeFileTab
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Controller\activeFileTab 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...
111
     * @ignorevalidation $documentForm
112
     * @return void
113
     */
114
    public function editAction(
115
        \EWW\Dpf\Domain\Model\DocumentForm $documentForm, bool $suggestMod = false, $activeFileTab = false
116
    )
117
    {
118
        /** @var \EWW\Dpf\Domain\Model\Document $document */
119
        $document = $this->documentRepository->findByUid($documentForm->getDocumentUid());
120
121
        if ($suggestMod) {
122
            $documentVoterAttribute = DocumentVoter::SUGGEST_MODIFICATION;
123
        } else {
124
            $documentVoterAttribute = DocumentVoter::EDIT;
125
        }
126
127
        if (!$this->authorizationChecker->isGranted($documentVoterAttribute, $document)) {
128
129
            if ($document->getCreator() !== $this->security->getUser()->getUid()) {
130
                $message = LocalizationUtility::translate(
131
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.accessDenied',
132
                    'dpf',
133
                    array($document->getTitle())
134
                );
135
            } else {
136
                $message = LocalizationUtility::translate(
137
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.failureBlocked',
138
                    'dpf',
139
                    array($document->getTitle())
140
                );
141
            }
142
143
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
144
            $this->redirect('showDetails', 'Document', null, ['document' => $document]);
145
            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...
146
        }
147
148
        $this->view->assign('document', $document);
149
        $this->view->assign('suggestMod', $suggestMod);
150
151
        $this->editingLockService->lock(
152
            ($document->getObjectIdentifier()? $document->getObjectIdentifier() : $document->getUid()),
153
            $this->security->getUser()->getUid()
154
        );
155
156
        $this->view->assign('activeFileTab', $activeFileTab);
157
        parent::editAction($documentForm);
158
    }
159
160
    /**
161
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
162
     * @param bool $restore
163
     */
164
    public function createSuggestionDocumentAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm, $restore = FALSE)
165
    {
166
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
167
168
        $hasFilesFlag = true;
169
170
        $workingCopy = $this->documentRepository->findByUid($documentForm->getDocumentUid());
171
172
        if ($workingCopy->isTemporary()) {
173
            $workingCopy->setTemporary(false);
174
        }
175
176
        if (empty($workingCopy->getFileData())) {
177
            // no files are linked to the document
178
            $hasFilesFlag = false;
179
        }
180
181
        $newDocument = $this->objectManager->get(Document::class);
182
183
        $this->documentRepository->add($newDocument);
184
        $this->persistenceManager->persistAll();
185
186
        /* @var $document \EWW\Dpf\Domain\Model\Document */
187
        $document = $documentMapper->getDocument($documentForm);
188
189
        /* @var $newDocument \EWW\Dpf\Domain\Model\Document */
190
        $newDocument = $newDocument->copy($document);
191
192
        if ($document->getObjectIdentifier()) {
193
            $newDocument->setLinkedUid($document->getObjectIdentifier());
194
        } else {
195
            $newDocument->setLinkedUid($document->getUid());
196
        }
197
198
        $newDocument->setSuggestion(true);
199
        $newDocument->setComment($document->getComment());
200
201
        if ($restore) {
202
            $newDocument->setTransferStatus("RESTORE");
203
        }
204
205
        if (!$hasFilesFlag) {
206
            // Add or update files
207
            foreach ($documentForm->getNewFiles() as $newFile) {
208
                if ($newFile->getUID()) {
209
                    $this->fileRepository->update($newFile);
210
                } else {
211
                    $newFile->setDocument($newDocument);
212
                    $this->fileRepository->add($newFile);
213
                }
214
215
            }
216
        } else {
217
            // remove files for suggest object
218
            $newDocument->setFile($this->objectManager->get(ObjectStorage::class));
219
        }
220
221
222
        try {
223
            $newDocument->setCreator($this->security->getUser()->getUid());
224
            $this->documentRepository->add($newDocument);
225
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::SUCCESS;
0 ignored issues
show
Bug introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::SUCCESS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
226
            $this->addFlashMessage("Success", '', $severity,false);
227
        } catch (\Throwable $t) {
228
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
229
            $this->addFlashMessage("Failed", '', $severity,false);
230
        }
231
232
        $this->redirectToDocumentList();
233
    }
234
235
236
    public function updateAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
237
    {
238
        if ($this->request->getArgument('documentData')['suggestMod']) {
239
            $restore = $this->request->getArgument('documentData')['suggestRestore'];
240
            $this->forward('createSuggestionDocument', null, null, ['documentForm' => $documentForm, 'restore' => $restore]);
241
        }
242
243
        if ($this->request->hasArgument('saveAndUpdate')) {
244
            $saveMode = 'saveAndUpdate';
245
        } elseif ($this->request->hasArgument('saveWorkingCopy')) {
246
            $saveMode = 'saveWorkingCopy';
247
        } else {
248
            $saveMode = null;
249
        }
250
251
        $this->forward(
252
253
            'updateDocument',
254
            NULL,
255
            NULL,
256
                [
257
                    'documentForm' => $documentForm,
258
                    'saveMode' => $saveMode
259
                ]
260
        );
261
    }
262
263
264
    /**
265
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
266
     * @param string $saveMode
267
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
268
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
269
     */
270
    public function updateDocumentAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm, $saveMode = null)
271
    {
272
        try {
273
            /** @var \EWW\Dpf\Domain\Model\Document $document */
274
            $document = $this->documentRepository->findByUid($documentForm->getDocumentUid());
275
276
            if (
277
                !$this->authorizationChecker->isGranted(DocumentVoter::UPDATE, $document) ||
278
                (
279
                    $saveMode == 'saveWorkingCopy' &&
280
                    $this->security->getUser()->getUserRole() !== Security::ROLE_LIBRARIAN
281
                )
282
            ) {
283
                $message = LocalizationUtility::translate(
284
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.accessDenied',
285
                    'dpf',
286
                    array($document->getTitle())
287
                );
288
                $this->addFlashMessage($message, '', AbstractMessage::ERROR);
289
                $this->redirect(
290
                    'showDetails', 'Document',
291
                    null, ['document' => $document]
292
                );
293
            }
294
295
            /** @var  \EWW\Dpf\Helper\DocumentMapper $documentMapper */
296
            $documentMapper = $this->objectManager->get(DocumentMapper::class);
297
298
            /** @var \EWW\Dpf\Domain\Model\Document $updateDocument */
299
            $updateDocument = $documentMapper->getDocument($documentForm);
300
301
            $saveWorkingCopy = false;
302
            $workflowTransition = null;
303
304
            // Convert the temporary copy into a local working copy if needed.
305
            if ( $updateDocument->isTemporaryCopy() && $saveMode == 'saveWorkingCopy') {
306
                $saveWorkingCopy = true;
307
                $updateDocument->setTemporary(false);
308
                $workflowTransition = DocumentWorkflow::TRANSITION_IN_PROGRESS;
309
            } elseif ($updateDocument->isTemporaryCopy() && $saveMode == 'saveAndUpdate') {
310
                $workflowTransition = DocumentWorkflow::TRANSITION_REMOTE_UPDATE;
311
            } elseif (
312
                $this->security->getUser()->getUserRole() === Security::ROLE_LIBRARIAN &&
313
                $updateDocument->getState() === DocumentWorkflow::STATE_REGISTERED_NONE
314
            ) {
315
                $workflowTransition = DocumentWorkflow::TRANSITION_IN_PROGRESS;
316
            }
317
318
            if (
319
                $this->documentManager->update(
320
                    $updateDocument, $workflowTransition,
321
                    $documentForm->getDeletedFiles(), $documentForm->getNewFiles()
322
                )
323
            ) {
324
325
                $message = LocalizationUtility::translate(
326
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.success',
327
                    'dpf',
328
                    array($updateDocument->getTitle())
329
                );
330
                $this->addFlashMessage($message, '', AbstractMessage::OK);
331
332
                if ($this->security->getUser()->getUserRole() === Security::ROLE_LIBRARIAN) {
333
                    if ($saveWorkingCopy) {
334
                        if (
335
                            $this->bookmarkRepository->addBookmark(
336
                                $updateDocument,
337
                                $this->security->getUser()->getUid()
338
                            )
339
                        ) {
340
                            $this->addFlashMessage(
341
                                LocalizationUtility::translate(
342
                                    "manager.workspace.bookmarkAdded", "dpf"
343
                                ),
344
                                '',
345
                                AbstractMessage::INFO
346
                            );
347
                        }
348
                    } else {
349
                        switch ($document->getState()) {
350
                            case DocumentWorkflow::STATE_POSTPONED_NONE:
351
                            case DocumentWorkflow::STATE_DISCARDED_NONE:
352
                            case DocumentWorkflow::STATE_NONE_INACTIVE:
353
                            case DocumentWorkflow::STATE_NONE_ACTIVE:
354
                            case DocumentWorkflow::STATE_NONE_DELETED:
355
356
                                if (
357
                                    $this->bookmarkRepository->removeBookmark(
358
                                        $updateDocument,
359
                                        $this->security->getUser()->getUid()
360
                                    )
361
                                ) {
362
                                    $this->addFlashMessage(
363
                                        LocalizationUtility::translate(
364
                                            "manager.workspace.bookmarkRemoved.singular", "dpf"
365
                                        ),
366
                                        '',
367
                                        AbstractMessage::INFO
368
                                    );
369
                                }
370
371
                                $this->redirectToDocumentList();
372
373
                                break;
374
                        }
375
                    }
376
                }
377
378
            } else {
379
                $message = LocalizationUtility::translate(
380
                    'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.failure',
381
                    'dpf',
382
                    array($updateDocument->getTitle())
383
                );
384
                $this->addFlashMessage($message, '', AbstractMessage::ERROR);
385
            }
386
387
            if ($workflowTransition && $workflowTransition === DocumentWorkflow::TRANSITION_REMOTE_UPDATE) {
388
                $this->redirectToDocumentList();
389
            } else {
390
                $this->redirect('showDetails', 'Document', null, ['document' => $updateDocument]);
391
            }
392
        } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
393
            // A redirect always throws this exception, but in this case, however,
394
            // redirection is desired and should not lead to an exception handling
395
        } catch (\Exception $exception) {
396
397
            $severity = AbstractMessage::ERROR;
398
399
            if ($exception instanceof DPFExceptionInterface) {
400
                $key = $exception->messageLanguageKey();
401
            } else {
402
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
403
            }
404
405
            $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...
406
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.failure',
407
                'dpf',
408
                array($updateDocument->getTitle())
409
            );
410
411
            $exceptionMsg[] = LocalizationUtility::translate($key, 'dpf');
412
413
            $this->addFlashMessage(implode(" ", $exceptionMsg), '', $severity, true);
414
            $this->redirect('showDetails', 'Document', null, ['document' => $updateDocument]);
415
        }
416
    }
417
418
    public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm)
419
    {
420
        /** @var \EWW\Dpf\Helper\DocumentMapper $documentMapper */
421
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
422
423
        /** @var \EWW\Dpf\Domain\Model\Document $document */
424
        $document = $documentMapper->getDocument($newDocumentForm);
425
426
        if (!$this->authorizationChecker->isGranted(DocumentVoter::CREATE, $document)) {
427
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:documentForm.create.accessDenied';
428
            $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...
429
            $message = LocalizationUtility::translate($key, 'dpf', $args);
430
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
431
            $this->redirect('showDetails', 'Document', null, ['document' => $document]);
432
            return FALSE;
433
        }
434
435
        try {
436
            parent::createAction($newDocumentForm);
437
438
            $severity = AbstractMessage::OK;
439
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:documentForm.create.ok';
440
            $message = LocalizationUtility::translate($key, 'dpf');
441
            $this->addFlashMessage(
442
                $message,
443
                '',
444
                $severity,
445
                true
446
            );
447
448
        } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
449
            // A redirect always throws this exception, but in this case, however,
450
            // redirection is desired and should not lead to an exception handling
451
        } catch (\Exception $exception) {
452
453
            $severity = AbstractMessage::ERROR;
454
455
            if ($exception instanceof DPFExceptionInterface) {
456
                $key = $exception->messageLanguageKey();
457
            } else {
458
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
459
            }
460
461
            $message[] = LocalizationUtility::translate($key, 'dpf');
462
463
            $this->addFlashMessage(
464
                implode(" ", $message),
465
                '',
466
                $severity,
467
                true
468
            );
469
        }
470
471
        $this->redirect('listWorkspace', 'Workspace');
472
    }
473
474
475
    /**
476
     * action cancel edit
477
     *
478
     * @param integer $documentUid
479
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
480
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
481
     *
482
     * @return void
483
     */
484
    public function cancelEditAction($documentUid = 0)
485
    {
486
        if ($documentUid) {
487
            /** @var $document \EWW\Dpf\Domain\Model\Document */
488
            $document = $this->documentRepository->findByUid($documentUid);
489
490
            $this->redirect('showDetails', 'Document', null, ['document' => $document]);
491
        } else {
492
            $this->redirectToDocumentList();
493
        }
494
    }
495
496
    /**
497
     * action cancel new
498
     *
499
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
500
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
501
     *
502
     * @return void
503
     */
504
    public function cancelNewAction()
505
    {
506
        $this->redirect('list');
507
    }
508
509
510
    public function initializeAction()
511
    {
512
        $this->authorizationChecker->denyAccessUnlessLoggedIn();
513
514
        parent::initializeAction();
515
516
    }
517
518
    /**
519
     * Redirect to the current document list.
520
     *
521
     * @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...
522
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
523
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
524
     */
525
    protected function redirectToDocumentList($message = null)
526
    {
527
        list($action, $controller, $redirectUri) = $this->session->getStoredAction();
528
529
        if ($redirectUri) {
530
            $this->redirectToUri($redirectUri);
531
        } else {
532
            $this->redirect($action, $controller, null, array('message' => $message));
533
        }
534
    }
535
536
537
}
538