Passed
Pull Request — master (#214)
by Ralf
11:24
created

DocumentFormController::editAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\DepositLicenseLog;
18
use EWW\Dpf\Domain\Model\Document;
19
use EWW\Dpf\Domain\Model\DocumentForm;
20
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
21
use EWW\Dpf\Exceptions\DPFExceptionInterface;
22
use EWW\Dpf\Helper\DocumentMapper;
23
use EWW\Dpf\Security\DocumentVoter;
24
use EWW\Dpf\Services\Email\Notifier;
25
use TYPO3\CMS\Core\Messaging\AbstractMessage;
26
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
27
28
class DocumentFormController extends AbstractDocumentFormController
29
{
30
    /**
31
     * documentRepository
32
     *
33
     * @var \EWW\Dpf\Helper\DocumentMapper
34
     * @TYPO3\CMS\Extbase\Annotation\Inject
35
     */
36
    protected $documentMapper= null;
37
38
    protected function redirectToList($message = null)
39
    {
40
        $this->redirect('list', 'DocumentForm', null, array('message' => $message));
41
    }
42
43
    /**
44
     * action new
45
     *
46
     * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm
47
     * @param int $returnDocumentId
48
     * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("newDocumentForm")
49
     * @return void
50
     */
51
    public function newAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm = null, $returnDocumentId = 0)
52
    {
53
        $this->view->assign('documentForm', $newDocumentForm);
54
    }
55
56
    /**
57
     * action create
58
     *
59
     * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm
60
     * @return void
61
     */
62
    public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm)
63
    {
64
        // Replaced by the form configuration (download/archive)
65
        /*
66
           foreach ($newDocumentForm->getFiles() as $file) {
67
               $uid = $file->getUID();
68
               if (empty($uid)) {
69
                   $file->setDownload(true);
70
               }
71
               $files[] = $file;
72
           }
73
           $newDocumentForm->setFiles($files);
74
        */
75
76
        try {
77
            parent::createAction($newDocumentForm);
78
            $this->redirect(
79
                'summary',
80
                null,
81
                null,
82
                ['document' => $this->newDocument]
83
            );
84
        } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
85
            // A redirect always throws this exception, but in this case, however,
86
            // redirection is desired and should not lead to an exception handling
87
        } catch (\Exception $exception) {
88
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
89
90
            if ($exception instanceof DPFExceptionInterface) {
91
                $key = $exception->messageLanguageKey();
92
            } else {
93
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
94
            }
95
96
            $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$message was never initialized. Although not strictly required by PHP, it is generally a good practice to add $message = array(); before regardless.
Loading history...
97
98
            $this->addFlashMessage(implode(" ", $message), '', $severity,true);
99
            $this->forward('new', 'DocumentForm', null, array('newDocumentForm' => $newDocumentForm));
100
        }
101
    }
102
103
    /**
104
     * action edit
105
     *
106
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
107
     * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("documentForm")
108
     * @return void
109
     */
110
    public function editAction(DocumentForm $documentForm)
111
    {
112
        $document = $this->documentMapper->getDocument($documentForm);
113
114
        if (!$this->authorizationChecker->isGranted(DocumentVoter::EDIT_ANONYMOUSLY, $document)) {
115
116
            $message = LocalizationUtility::translate(
117
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.accessDenied',
118
                'dpf',
119
                array($document->getTitle())
120
            );
121
122
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
123
            $this->redirectToList();
124
            return;
125
        }
126
127
        parent::editAction($documentForm);
128
    }
129
130
    /**
131
     * action update
132
     *
133
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
134
     * @return void
135
     */
136
    public function updateAction(DocumentForm $documentForm)
137
    {
138
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
139
140
        /* @var $updateDocument \EWW\Dpf\Domain\Model\Document */
141
        $updateDocument = $documentMapper->getDocument($documentForm);
142
143
        // xml data fields are limited to 64 KB
144
        if (strlen($updateDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) {
145
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
146
        }
147
148
        $updateDocument->setChanged(true);
149
        $this->documentRepository->update($updateDocument);
150
151
        $this->redirect(
152
            'summary',
153
            null,
154
            null,
155
            ['document' => $updateDocument]
156
        );
157
    }
158
159
    /**
160
     * action register
161
     *
162
     * @param \EWW\Dpf\Domain\Model\Document $document
163
     * @return void
164
     */
165
    public function registerAction(Document $document)
166
    {
167
        if (!$this->authorizationChecker->isGranted(DocumentVoter::REGISTER_ANONYMOUSLY, $document)) {
168
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_register.accessDenied';
169
            $this->flashMessage($document, $key, AbstractMessage::ERROR);
170
            $this->redirect('summary', 'DocumentForm', null, ['document' => $document]);
171
            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...
172
        }
173
174
        $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow();
175
        $workflow->apply($document, DocumentWorkflow::TRANSITION_REGISTER);
176
177
        $document->setTemporary(false);
178
        $this->documentRepository->update($document);
179
180
        $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($document->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

180
        /** @scrutinizer ignore-call */ 
181
        $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($document->getProcessNumber());
Loading history...
181
        if (empty($depositLicenseLog) && $document->getDepositLicense()) {
182
            // Only if there was no deposit license a notification may be sent
183
184
            /** @var DepositLicenseLog $depositLicenseLog */
185
            $depositLicenseLog = $this->objectManager->get(DepositLicenseLog::class);
186
            $depositLicenseLog->setUsername($this->security->getUsername());
187
            $depositLicenseLog->setObjectIdentifier($document->getObjectIdentifier());
188
            $depositLicenseLog->setProcessNumber($document->getProcessNumber());
189
            $depositLicenseLog->setTitle($document->getTitle());
190
            $depositLicenseLog->setUrn($document->getPrimaryUrn());
191
            $depositLicenseLog->setLicenceUri($document->getDepositLicense());
192
193
            if ($document->hasFiles()) {
194
                $fileList = [];
195
                foreach ($document->getFile() as $file) {
196
                    if (!$file->isFileGroupDeleted()) {
197
                        $fileList[] = $file->getTitle();
198
                    }
199
                }
200
                $depositLicenseLog->setFileNames(implode(", ", $fileList));
201
            }
202
203
            $this->depositLicenseLogRepository->add($depositLicenseLog);
204
205
            /** @var Notifier $notifier */
206
            $notifier = $this->objectManager->get(Notifier::class);
207
            $notifier->sendDepositLicenseNotification($document);
208
        }
209
210
        // admin register notification
211
        $notifier = $this->objectManager->get(Notifier::class);
212
        $notifier->sendRegisterNotification($document);
213
214
        // document updated notification
215
        $recipients = $this->documentManager->getUpdateNotificationRecipients($document);
216
        $notifier->sendMyPublicationUpdateNotification($document, $recipients);
217
218
        $recipients = $this->documentManager->getNewPublicationNotificationRecipients($document);
219
        $notifier->sendMyPublicationNewNotification($document, $recipients);
220
221
        // index the document
222
        $this->signalSlotDispatcher->dispatch(\EWW\Dpf\Controller\AbstractController::class, 'indexDocument', [$document]);
223
224
        $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_register.success';
225
        $this->flashMessage($document, $key, AbstractMessage::OK);
226
        $this->redirectToList();
227
    }
228
229
    /**
230
     * @param Document $document
231
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
232
     */
233
    public function summaryAction(Document $document)
234
    {
235
        if (!$this->authorizationChecker->isGranted(DocumentVoter::EDIT_ANONYMOUSLY, $document)) {
236
            $message = LocalizationUtility::translate(
237
                'manager.workspace.accessDenied', 'dpf'
238
            );
239
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
240
            $this->redirectToList();
241
            return;
242
        }
243
244
        $mapper = $this->objectManager->get(DocumentMapper::class);
245
        $documentForm = $mapper->getDocumentForm($document, false);
246
247
        $this->view->assign('documentForm', $documentForm);
248
        $this->view->assign('document', $document);
249
    }
250
251
    /**
252
     * @param Document $document
253
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
254
     */
255
    public function deleteAction(Document $document)
256
    {
257
        if ($this->authorizationChecker->isGranted(DocumentVoter::DELETE_ANONYMOUSLY, $document)) {
258
           $this->documentRepository->remove($document);
259
        } else {
260
            $message = LocalizationUtility::translate(
261
                'manager.workspace.accessDenied', 'dpf'
262
            );
263
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
264
        }
265
266
        $this->redirectToList();
267
    }
268
}
269