Passed
Pull Request — master (#209)
by
unknown
12:34
created

BackendAdminController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 318
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 136
c 1
b 0
f 0
dl 0
loc 318
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canChangeClient() 0 10 1
A initializeView() 0 13 5
A chooseNewClientAction() 0 42 5
A searchDocumentAction() 0 24 3
B changeClientAction() 0 94 7
A flashMessage() 0 22 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\Bookmark;
18
use EWW\Dpf\Domain\Model\Client;
19
use EWW\Dpf\Domain\Model\Document;
20
use EWW\Dpf\Domain\Model\DocumentType;
21
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
22
use EWW\Dpf\Helper\DocumentMapper;
23
use EWW\Dpf\Helper\InternalFormat;
24
use EWW\Dpf\Services\ElasticSearch\ElasticSearch;
25
use EWW\Dpf\Services\ProcessNumber\ProcessNumberGenerator;
26
use TYPO3\CMS\Backend\View\BackendTemplateView;
27
use TYPO3\CMS\Core\Messaging\AbstractMessage;
28
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
29
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
30
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
31
32
/**
33
* Backend module user/group action controller
34
*/
35
class BackendAdminController extends ActionController
36
{
37
    /**
38
     * documentRepository
39
     *
40
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
41
     * @TYPO3\CMS\Extbase\Annotation\Inject
42
     */
43
    protected $documentRepository = null;
44
45
    /**
46
     * fileRepository
47
     *
48
     * @var \EWW\Dpf\Domain\Repository\FileRepository
49
     * @TYPO3\CMS\Extbase\Annotation\Inject
50
     */
51
    protected $fileRepository = null;
52
53
    /**
54
     * documentTypeRepository
55
     *
56
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
57
     * @TYPO3\CMS\Extbase\Annotation\Inject
58
     */
59
    protected $documentTypeRepository = null;
60
61
    /**
62
     * bookmarkRepository
63
     *
64
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
65
     * @TYPO3\CMS\Extbase\Annotation\Inject
66
     */
67
    protected $bookmarkRepository = null;
68
69
    /**
70
     * frontendUserRepository
71
     *
72
     * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository
73
     * @TYPO3\CMS\Extbase\Annotation\Inject
74
     */
75
    protected $frontendUserRepository = null;
76
77
    /**
78
     * clientRepository
79
     *
80
     * @var \EWW\Dpf\Domain\Repository\ClientRepository
81
     * @TYPO3\CMS\Extbase\Annotation\Inject
82
     */
83
    protected $clientRepository = null;
84
85
    /**
86
    * Backend Template Container
87
    *
88
    * @var string
89
    */
90
    protected $defaultViewObjectName = BackendTemplateView::class;
91
92
    /**
93
     * Set up the doc header properly here
94
     *
95
     * @param ViewInterface $view
96
     * @return void
97
     */
98
    protected function initializeView(ViewInterface $view)
99
    {
100
        /** @var BackendTemplateView $view */
101
        parent::initializeView($view);
102
        if ($this->actionMethodName == 'indexAction'
103
            || $this->actionMethodName == 'onlineAction'
104
            || $this->actionMethodName == 'compareAction') {
105
            $this->generateMenu();
0 ignored issues
show
Bug introduced by
The method generateMenu() does not exist on EWW\Dpf\Controller\BackendAdminController. ( Ignorable by Annotation )

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

105
            $this->/** @scrutinizer ignore-call */ 
106
                   generateMenu();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
            $this->registerDocheaderButtons();
0 ignored issues
show
Bug introduced by
The method registerDocheaderButtons() does not exist on EWW\Dpf\Controller\BackendAdminController. ( Ignorable by Annotation )

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

106
            $this->/** @scrutinizer ignore-call */ 
107
                   registerDocheaderButtons();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
            $view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
108
        }
109
        if ($view instanceof BackendTemplateView) {
0 ignored issues
show
introduced by
$view is always a sub-type of TYPO3\CMS\Backend\View\BackendTemplateView.
Loading history...
110
            $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
111
        }
112
    }
113
114
    /**
115
     * @param string $identifier
116
     */
117
    public function searchDocumentAction(string $identifier = '')
118
    {
119
        $identifier = trim($identifier);
120
        $this->view->assign('identifier', $identifier);
121
122
        if ($identifier) {
123
124
            $this->documentRepository->crossClient(true);
125
126
            /** @var Document $document */
127
            $document = $this->documentRepository->findByIdentifier($identifier);
128
129
            if ($document) {
0 ignored issues
show
introduced by
$document is of type EWW\Dpf\Domain\Model\Document, thus it always evaluated to true.
Loading history...
130
                /** @var Client $client */
131
                $client = $this->clientRepository->findAllByPid($document->getPid())->current();
132
                $this->view->assign('document', $document);
133
                $this->view->assign('clientName', $client->getClient());
134
            } else {
135
                $this->flashMessage(
136
                    'nothing_found',
137
                    'nothing_found_message',
138
                    AbstractMessage::ERROR,
139
                    [],
140
                    [$identifier]
141
                );
142
            }
143
        }
144
    }
145
146
    /**
147
     * @param Document $document
148
     * @param string $identifier
149
     */
150
    public function chooseNewClientAction(Document $document, string $identifier)
151
    {
152
        if ($document) {
0 ignored issues
show
introduced by
$document is of type EWW\Dpf\Domain\Model\Document, thus it always evaluated to true.
Loading history...
153
            if ($this->canChangeClient($document)) {
154
                /** @var Client $currentClient */
155
                $currentClient = $this->clientRepository->findAllByPid($document->getPid())->current();
156
157
                $allClients = $this->clientRepository->crossClientFindAll(false);
158
159
                $documentTypes = [];
160
                $clients = [];
161
162
                /** @var Client $client */
163
                foreach ($allClients as $client) {
164
                    if ($client->getUid() != $currentClient->getUid()) {
165
                        $clients[] = $client;
166
                        $this->documentTypeRepository->crossClient(true);
167
                        $documentTypes[$client->getPid()] = $this->documentTypeRepository->findByPid($client->getPid());
0 ignored issues
show
Bug introduced by
The method findByPid() does not exist on EWW\Dpf\Domain\Repository\DocumentTypeRepository. 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

167
                        /** @scrutinizer ignore-call */ 
168
                        $documentTypes[$client->getPid()] = $this->documentTypeRepository->findByPid($client->getPid());
Loading history...
168
                    }
169
                }
170
171
                $this->view->assign('canMoveDocument', $this->canChangeClient($document));
172
                $this->view->assign('document', $document);
173
                $this->view->assign('clients', $clients);
174
                $this->view->assign('documentTypes', $documentTypes);
175
                $this->view->assign('currentClient', $currentClient);
176
                $this->view->assign('identifier', $identifier);
177
            } else {
178
                $this->flashMessage(
179
                    'change_client_forbidden',
180
                    'change_client_forbidden_message',
181
                    AbstractMessage::ERROR,
182
                    [],
183
                    [$identifier]
184
                );
185
186
                $this->forward(
187
                    'searchDocument',
188
                    null,
189
                    null,
190
                    [
191
                        'identifier' => $identifier
192
                    ]
193
                );
194
            }
195
        }
196
    }
197
198
    /**
199
     * @param Document $document
200
     * @param Client $client
201
     * @param string $identifier
202
     * @param DocumentType $documentType
203
     */
204
    public function changeClientAction(Document $document, Client $client, string $identifier, DocumentType $documentType = null)
205
    {
206
        if ($documentType instanceof DocumentType) {
207
            if ($this->canChangeClient($document)) {
208
209
                /** @var Client $currentClient */
210
                $currentClient = $this->clientRepository->findAllByPid($document->getPid())->current();
211
212
                // Move the document to the target client
213
                // Fixme: How should the creator be dealt with?
214
                // $document->setCreator();
215
                $document->setPid($client->getPid());
216
                $document->setDocumentType($documentType);
217
                $documentMapper = $this->objectManager->get(DocumentMapper::class);
218
                $documentMapper->setClientPid($client->getPid());
219
                $documentForm = $documentMapper->getDocumentForm($document);
220
                $document = $documentMapper->getDocument($documentForm);
221
                $internalFormat = new InternalFormat($document->getXmlData(), $client->getPid());
222
                $internalFormat->setDocumentType($documentType->getName());
223
                $document->setXmlData($internalFormat->getXml());
224
225
                if ($client->getOwnerId()) {
226
                    /** @var ProcessNumberGenerator $processNumberGenerator */
227
                    $processNumberGenerator = $this->objectManager->get(ProcessNumberGenerator::class);
228
                    $processNumber = $processNumberGenerator->getProcessNumber($client->getOwnerId());
229
                    $document->setProcessNumber($processNumber);
230
                } else {
231
                    throw new \Exception('Missing client configuration: "owner id"');
232
                }
233
234
                $this->documentRepository->update($document);
235
236
                // Move files to the target client
237
                $files = $document->getFile();
238
                foreach ($files as $file) {
239
                    $file->setPid($client->getPid());
240
                    $this->fileRepository->update($file);
241
                }
242
243
                // If there are bookmarks, they have to be adjusted, depending on the existence of the related user
244
                // inside the target client, and either deleted or moved to the target client.
245
                $this->bookmarkRepository->crossClient(true);
246
                $bookmarks = $this->bookmarkRepository->findDocumentBookmarks($document);
247
                /** @var Bookmark $bookmark */
248
                foreach ($bookmarks as $bookmark) {
249
                    if ($this->frontendUserRepository->isUserInClient($bookmark->getFeUserUid(), $client->getPid())) {
250
                        $bookmark->setPid($client->getPid());
251
                        $this->bookmarkRepository->update($bookmark);
252
                    } else {
253
                        $this->bookmarkRepository->remove($bookmark);
254
                    }
255
                }
256
257
                // Move document into the target search index.
258
                $elasticSearch = new ElasticSearch($currentClient->getPid());
259
                $elasticSearch->delete($document->getDocumentIdentifier());
260
                $targetElasticSearch = new ElasticSearch($client->getPid());
261
                $targetElasticSearch->index($document);
262
263
                $this->flashMessage(
264
                   'client_changed',
265
                   'client_changed_message',
266
                   AbstractMessage::OK,
267
                   [],
268
                   [$identifier]
269
                );
270
           } else {
271
               $this->flashMessage(
272
                   'change_client_forbidden',
273
                   'change_client_forbidden_message',
274
                   AbstractMessage::ERROR,
275
                   [],
276
                   [$identifier]
277
               );
278
           }
279
280
           $this->view->assign('currentClient', $client);
281
           $this->view->assign('document', $document);
282
       } else {
283
            $this->flashMessage(
284
                'missing_document_type',
285
                'missing_document_type_message',
286
                AbstractMessage::ERROR,
287
                [],
288
                [$identifier]
289
            );
290
291
            $this->forward(
292
              'chooseNewClient',
293
              null,
294
              null,
295
              [
296
                  'document' => $document,
297
                  'identifier' => $identifier
298
              ]
299
            );
300
       }
301
    }
302
303
    /**
304
     * @param Document $document
305
     */
306
    protected function canChangeClient(Document $document)
307
    {
308
        return (
309
            in_array(
310
             $document->getState(),
311
             [
312
                 DocumentWorkflow::STATE_REGISTERED_NONE,
313
                 DocumentWorkflow::STATE_IN_PROGRESS_NONE,
314
                 DocumentWorkflow::STATE_POSTPONED_NONE,
315
                 DocumentWorkflow::STATE_DISCARDED_NONE
316
             ]
317
            )
318
        );
319
    }
320
321
    /**
322
     * flashMessage
323
     *
324
     * @param string $headerKey
325
     * @param string $bodyKey
326
     * @param array $headerArguments
327
     * @param array $bodyArguments
328
     * @param int $severity
329
     * @param bool $storeInSession
330
     */
331
    protected function flashMessage(
332
        string $headerKey,
333
        string $bodyKey,
334
        int $severity = AbstractMessage::INFO,
335
        array $headerArguments,
336
        array $bodyArguments,
337
        bool $storeInSession = false
338
    )
339
    {
340
        $messageHeader = LocalizationUtility::translate(
341
            'LLL:EXT:dpf/Resources/Private/Language/locallang_mod.xlf:admin_module.'.$headerKey,
342
            'dpf',
343
            $headerArguments
344
        );
345
346
        $messageBody = LocalizationUtility::translate(
347
            'LLL:EXT:dpf/Resources/Private/Language/locallang_mod.xlf:admin_module.'.$bodyKey,
348
            'dpf',
349
            $bodyArguments
350
        );
351
352
        $this->addFlashMessage($messageBody, $messageHeader, $severity, $storeInSession);
353
    }
354
355
}
356