Passed
Push — master ( 87cf99...8ae428 )
by Ralf
08:45
created

DocumentController::duplicateAction()   A

Complexity

Conditions 3
Paths 37

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 29
nc 37
nop 1
dl 0
loc 48
rs 9.456
c 0
b 0
f 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\Services\Transfer\ElasticsearchRepository;
19
use EWW\Dpf\Services\Transfer\DocumentTransferManager;
20
use EWW\Dpf\Services\Transfer\FedoraRepository;
21
use EWW\Dpf\Services\ProcessNumber\ProcessNumberGenerator;
22
use EWW\Dpf\Services\Identifier\Urn;
23
use EWW\Dpf\Services\Email\Notifier;
24
use EWW\Dpf\Helper\ElasticsearchMapper;
25
use EWW\Dpf\Exceptions\DPFExceptionInterface;
26
27
/**
28
 * DocumentController
29
 */
30
class DocumentController extends \EWW\Dpf\Controller\AbstractController
31
{
32
33
    /**
34
     * documentRepository
35
     *
36
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
37
     * @inject
38
     */
39
    protected $documentRepository = null;
40
41
    /**
42
     * persistence manager
43
     *
44
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
45
     * @inject
46
     */
47
    protected $persistenceManager;
48
49
    /**
50
     * action list
51
     *
52
     * @return void
53
     */
54
    public function listAction()
55
    {
56
        $documents = $this->documentRepository->findAll();
57
58
        if ($this->request->hasArgument('message')) {
59
            $this->view->assign('message', $this->request->getArgument('message'));
60
        }
61
62
        if ($this->request->hasArgument('errorFiles')) {
63
            $this->view->assign('errorFiles', $this->request->getArgument('errorFiles'));
64
        }
65
66
        $this->view->assign('documents', $documents);
67
    }
68
69
    public function listNewAction()
70
    {
71
        $documents = $this->documentRepository->getNewDocuments();
72
        $this->view->assign('documents', $documents);
73
    }
74
75
    public function listEditAction()
76
    {
77
        $documents = $this->documentRepository->getInProgressDocuments();
78
        $this->view->assign('documents', $documents);
79
    }
80
81
    /**
82
     * action discardConfirm
83
     *
84
     * @param \EWW\Dpf\Domain\Model\Document $document
85
     * @return void
86
     */
87
    public function discardConfirmAction(\EWW\Dpf\Domain\Model\Document $document)
88
    {
89
        $this->view->assign('document', $document);
90
    }
91
92
    /**
93
     * action discard
94
     *
95
     * @param \EWW\Dpf\Domain\Model\Document $document
96
     * @return void
97
     */
98
    public function discardAction(\EWW\Dpf\Domain\Model\Document $document)
99
    {
100
        try {
101
            // remove document from local index
102
            $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class);
103
            // send document to index
104
            $elasticsearchRepository->delete($document, "");
105
106
            $this->documentRepository->remove($document);
107
108
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_discard.success';
109
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
110
111
        } catch (\Exception $exception) {
112
113
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
114
115
            if ($exception instanceof DPFExceptionInterface) {
116
                $key = $exception->messageLanguageKey();
117
            } else {
118
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_discard.failure';
119
            }
120
        }
121
122
        $this->flashMessage($document, $key, $severity);
123
        $this->redirect('list');
124
    }
125
126
    /**
127
     * action duplicate
128
     *
129
     * @param \EWW\Dpf\Domain\Model\Document $document
130
     * @return void
131
     */
132
    public function duplicateAction(\EWW\Dpf\Domain\Model\Document $document)
133
    {
134
        try {
135
            $newDocument = $this->objectManager->get(Document::class);
136
137
            $newDocument->setTitle($document->getTitle());
138
            $newDocument->setAuthors($document->getAuthors());
139
140
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
141
            $mods->clearAllUrn();
142
            $newDocument->setXmlData($mods->getModsXml());
143
144
            $newDocument->setDocumentType($document->getDocumentType());
145
146
            $processNumberGenerator = $this->objectManager->get(ProcessNumberGenerator::class);
147
            $processNumber = $processNumberGenerator->getProcessNumber();
148
            $newDocument->setProcessNumber($processNumber);
149
150
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
151
            $slub->setProcessNumber($processNumber);
152
            $newDocument->setSlubInfoData($slub->getSlubXml());
153
154
            // send document to index
155
            $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class);
156
157
            $elasticsearchMapper = $this->objectManager->get(ElasticsearchMapper::class);
158
            $json = $elasticsearchMapper->getElasticsearchJson($newDocument);
159
160
            $elasticsearchRepository->add($newDocument, $json);
161
162
            $this->documentRepository->add($newDocument);
163
164
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_duplicate.success';
165
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
166
        } catch (\Exception $exception) {
167
168
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
169
170
            if ($exception instanceof DPFExceptionInterface) {
171
                $key = $exception->messageLanguageKey();
172
            } else {
173
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_duplicate.failure';
174
            }
175
        }
176
177
        $this->flashMessage($document, $key, $severity);
178
179
        $this->redirect('list');
180
    }
181
182
    /**
183
     * action releaseConfirm
184
     *
185
     * @param \EWW\Dpf\Domain\Model\Document $document
186
     * @param string $releaseType
187
     * @return void
188
     */
189
    public function releaseConfirmAction(\EWW\Dpf\Domain\Model\Document $document, $releaseType)
190
    {
191
        $this->view->assign('releaseType', $releaseType);
192
        $this->view->assign('document', $document);
193
    }
194
195
    /**
196
     * action release
197
     *
198
     * @param \EWW\Dpf\Domain\Model\Document $document
199
     * @return void
200
     */
201
    public function releaseAction(\EWW\Dpf\Domain\Model\Document $document)
202
    {
203
        try {
204
            // generate URN if needed
205
            $qucosaId = $document->getObjectIdentifier();
206
            if (empty($qucosaId)) {
207
                $qucosaId = $document->getReservedObjectIdentifier();
208
            }
209
            if (empty($qucosaId)) {
210
                $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class);
211
                $remoteRepository        = $this->objectManager->get(FedoraRepository::class);
212
                $documentTransferManager->setRemoteRepository($remoteRepository);
213
                $qucosaId = $documentTransferManager->getNextDocumentId();
214
                $document->setReservedObjectIdentifier($qucosaId);
215
            }
216
217
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
218
            if (!$mods->hasQucosaUrn()) {
219
                $urnService = $this->objectManager->get(Urn::class);
220
                $urn        = $urnService->getUrn($qucosaId);
221
                $mods->addQucosaUrn($urn);
222
                $document->setXmlData($mods->getModsXml());
223
            }
224
225
            $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class);
226
            $remoteRepository        = $this->objectManager->get(FedoraRepository::class);
227
            $documentTransferManager->setRemoteRepository($remoteRepository);
228
229
            $objectIdentifier = $document->getObjectIdentifier();
230
231
            if (empty($objectIdentifier)) {
232
233
                // Document is not in the fedora repository.
234
235
                if ($documentTransferManager->ingest($document)) {
236
                    $key      = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_ingest.success';
237
                    $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
238
                    $notifier = $this->objectManager->get(Notifier::class);
239
                    $notifier->sendIngestNotification($document);
240
                }
241
            } else {
242
243
                // Document needs to be updated.
244
245
                if ($documentTransferManager->update($document)) {
246
                    $key      = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.success';
247
                    $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
248
                }
249
            }
250
        } catch (\Exception $exception) {
251
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
252
253
            if ($exception instanceof DPFExceptionInterface) {
254
                $key = $exception->messageLanguageKey();
255
            } else {
256
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
257
            }
258
        }
259
260
        $this->flashMessage($document, $key, $severity);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $severity does not seem to be defined for all execution paths leading up to this point.
Loading history...
261
262
        $this->redirect('list');
263
    }
264
265
    /**
266
     * action restoreConfirm
267
     *
268
     * @param \EWW\Dpf\Domain\Model\Document $document
269
     * @return void
270
     */
271
    public function restoreConfirmAction(\EWW\Dpf\Domain\Model\Document $document)
272
    {
273
        $this->view->assign('document', $document);
274
    }
275
276
    /**
277
     * action restore
278
     *
279
     * @param \EWW\Dpf\Domain\Model\Document $document
280
     * @return void
281
     */
282
    public function restoreAction(\EWW\Dpf\Domain\Model\Document $document)
283
    {
284
285
        $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class);
286
        $remoteRepository        = $this->objectManager->get(FedoraRepository::class);
287
        $documentTransferManager->setRemoteRepository($remoteRepository);
288
289
        try {
290
            if ($documentTransferManager->delete($document, "inactivate")) {
291
                $key      = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_restore.success';
292
                $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
293
            }
294
        } catch (\Exception $exception) {
295
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
296
297
            if ($exception instanceof DPFExceptionInterface) {
298
                $key = $exception->messageLanguageKey();
299
            } else {
300
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
301
            }
302
        }
303
304
        $this->flashMessage($document, $key, $severity);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $severity does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.
Loading history...
305
306
        $this->redirect('list');
307
    }
308
309
    /**
310
     * action deleteConfirm
311
     *
312
     * @param \EWW\Dpf\Domain\Model\Document $document
313
     * @return void
314
     */
315
    public function deleteConfirmAction(\EWW\Dpf\Domain\Model\Document $document)
316
    {
317
        $this->view->assign('document', $document);
318
    }
319
320
    /**
321
     * action delete
322
     *
323
     * @param \EWW\Dpf\Domain\Model\Document $document
324
     * @return void
325
     */
326
    public function deleteAction(\EWW\Dpf\Domain\Model\Document $document)
327
    {
328
329
        $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class);
330
        $remoteRepository        = $this->objectManager->get(FedoraRepository::class);
331
        $documentTransferManager->setRemoteRepository($remoteRepository);
332
333
        try {
334
            if ($documentTransferManager->delete($document, "")) {
335
                $key      = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_delete.success';
336
                $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
337
            }
338
        } catch (\Exception $exception) {
339
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
340
341
            if ($exception instanceof DPFExceptionInterface) {
342
                $key = $exception->messageLanguageKey();
343
            } else {
344
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
345
            }
346
        }
347
348
        $this->flashMessage($document, $key, $severity);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $severity does not seem to be defined for all execution paths leading up to this point.
Loading history...
349
350
        $this->redirect('list');
351
    }
352
353
    /**
354
     * action activateConfirm
355
     *
356
     * @param \EWW\Dpf\Domain\Model\Document $document
357
     * @return void
358
     */
359
    public function activateConfirmAction(\EWW\Dpf\Domain\Model\Document $document)
360
    {
361
        $this->view->assign('document', $document);
362
    }
363
364
    /**
365
     * action activate
366
     *
367
     * @param \EWW\Dpf\Domain\Model\Document $document
368
     * @return void
369
     */
370
    public function activateAction(\EWW\Dpf\Domain\Model\Document $document)
371
    {
372
373
        $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class);
374
        $remoteRepository        = $this->objectManager->get(FedoraRepository::class);
375
        $documentTransferManager->setRemoteRepository($remoteRepository);
376
377
        try {
378
            if ($documentTransferManager->delete($document, "revert")) {
379
                $key      = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_activate.success';
380
                $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
381
            }
382
        } catch (\Exception $exception) {
383
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
384
385
            if ($exception instanceof DPFExceptionInterface) {
386
                $key = $exception->messageLanguageKey();
387
            } else {
388
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
389
            }
390
        }
391
392
        $this->flashMessage($document, $key, $severity);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $severity does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.
Loading history...
393
394
        $this->redirect('list');
395
    }
396
397
    /**
398
     * action inactivateConfirm
399
     *
400
     * @param \EWW\Dpf\Domain\Model\Document $document
401
     * @return void
402
     */
403
    public function inactivateConfirmAction(\EWW\Dpf\Domain\Model\Document $document)
404
    {
405
        $this->view->assign('document', $document);
406
    }
407
408
    /**
409
     * action inactivate
410
     *
411
     * @param \EWW\Dpf\Domain\Model\Document $document
412
     * @return void
413
     */
414
    public function inactivateAction(\EWW\Dpf\Domain\Model\Document $document)
415
    {
416
417
        $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class);
418
        $remoteRepository        = $this->objectManager->get(FedoraRepository::class);
419
        $documentTransferManager->setRemoteRepository($remoteRepository);
420
421
        try {
422
            if ($documentTransferManager->delete($document, "inactivate")) {
423
                $key      = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_inactivate.success';
424
                $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
425
            }
426
        } catch (\Exception $exception) {
427
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
428
429
            if ($exception instanceof DPFExceptionInterface) {
430
                $key = $exception->messageLanguageKey();
431
            } else {
432
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
433
            }
434
        }
435
436
        $this->flashMessage($document, $key, $severity);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $severity does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.
Loading history...
437
438
        $this->redirect('list');
439
    }
440
441
    protected function getStoragePID()
442
    {
443
        return $this->settings['persistence']['classes']['EWW\Dpf\Domain\Model\Document']['newRecordStoragePid'];
444
    }
445
446
    /**
447
     *
448
     * @param \EWW\Dpf\Domain\Model\Document $document
449
     * @param string $key
450
     * @param string $severity
451
     * @param string $defaultMessage
452
     */
453
    protected function flashMessage(\EWW\Dpf\Domain\Model\Document $document, $key, $severity, $defaultMessage = "")
454
    {
455
456
        // Show success or failure of the action in a flash message
457
        $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...
458
        $args[] = $document->getObjectIdentifier();
459
460
        $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf', $args);
461
        $message = empty($message) ? $defaultMessage : $message;
462
463
        $this->addFlashMessage(
464
            $message,
465
            '',
466
            $severity,
0 ignored issues
show
Bug introduced by
$severity of type string is incompatible with the type integer expected by parameter $severity of TYPO3\CMS\Extbase\Mvc\Co...ller::addFlashMessage(). ( Ignorable by Annotation )

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

466
            /** @scrutinizer ignore-type */ $severity,
Loading history...
467
            true
468
        );
469
470
    }
471
}
472