Passed
Pull Request — master (#123)
by
unknown
09:44 queued 11s
created

DocumentController::duplicateAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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

422
            /** @scrutinizer ignore-type */ $severity,
Loading history...
423
            true
424
        );
425
426
    }
427
}
428