Passed
Pull Request — master (#195)
by
unknown
07:19
created

WorkspaceController::batchRelease()   D

Complexity

Conditions 18
Paths 9

Size

Total Lines 101
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 66
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 101
rs 4.8666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Document;
19
use EWW\Dpf\Security\DocumentVoter;
20
use EWW\Dpf\Security\Security;
21
use EWW\Dpf\Services\Email\Notifier;
22
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
23
use TYPO3\CMS\Core\Messaging\AbstractMessage;
24
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
25
use EWW\Dpf\Session\SearchSessionData;
26
27
/**
28
 * Controller for the "workspace"/"my publications" area.
29
 */
30
class WorkspaceController extends AbstractController
31
{
32
    /**
33
     * FrontendUserRepository
34
     *
35
     * @var  TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Controller\TYPO3...\FrontendUserRepository was not found. Did you mean TYPO3\CMS\Extbase\Domain...\FrontendUserRepository? If so, make sure to prefix the type with \.
Loading history...
36
     * @inject
37
     */
38
    protected $frontendUserRepository;
39
40
    /**
41
     * documentRepository
42
     *
43
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
44
     * @inject
45
     */
46
    protected $documentRepository = null;
47
48
    /**
49
     * documentTypeRepository
50
     *
51
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
52
     * @inject
53
     */
54
    protected $documentTypeRepository = null;
55
56
    /**
57
     * bookmarkRepository
58
     *
59
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
60
     * @inject
61
     */
62
    protected $bookmarkRepository = null;
63
64
    /**
65
     * elasticSearch
66
     *
67
     * @var \EWW\Dpf\Services\ElasticSearch\ElasticSearch
68
     * @inject
69
     */
70
    protected $elasticSearch = null;
71
72
73
    /**
74
     * queryBuilder
75
     *
76
     * @var \EWW\Dpf\Services\ElasticSearch\QueryBuilder
77
     * @inject
78
     */
79
    protected $queryBuilder = null;
80
81
82
    /**
83
     * documentManager
84
     *
85
     * @var \EWW\Dpf\Services\Document\DocumentManager
86
     * @inject
87
     */
88
    protected $documentManager = null;
89
90
    /**
91
     * documentValidator
92
     *
93
     * @var \EWW\Dpf\Helper\DocumentValidator
94
     * @inject
95
     */
96
    protected $documentValidator;
97
98
    /**
99
     * editingLockService
100
     *
101
     * @var \EWW\Dpf\Services\Document\EditingLockService
102
     * @inject
103
     */
104
    protected $editingLockService = null;
105
106
107
    /**
108
     * metadataGroupRepository
109
     *
110
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
111
     * @inject
112
     */
113
    protected $metadataGroupRepository;
114
115
    /**
116
     * fisDataService
117
     *
118
     * @var \EWW\Dpf\Services\FeUser\FisDataService
119
     * @inject
120
     */
121
    protected $fisDataService = null;
122
123
    /**
124
     * list
125
     *
126
     * @param int $from
127
     * @return void
128
     */
129
    protected function list($from = 0)
130
    {
131
        $bookmarkIdentifiers = [];
132
        foreach ($this->bookmarkRepository->findByFeUserUid($this->security->getUser()->getUid()) as $bookmark) {
0 ignored issues
show
Bug introduced by
The method findByFeUserUid() does not exist on EWW\Dpf\Domain\Repository\BookmarkRepository. 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

132
        foreach ($this->bookmarkRepository->/** @scrutinizer ignore-call */ findByFeUserUid($this->security->getUser()->getUid()) as $bookmark) {
Loading history...
133
            $bookmarkIdentifiers[] = $bookmark->getDocumentIdentifier();
134
        }
135
136
        /** @var SearchSessionData $workspaceSessionData */
137
        $workspaceSessionData = $this->session->getWorkspaceData();
138
        $filters = $workspaceSessionData->getFilters();
139
        $excludeFilters = $workspaceSessionData->getExcludeFilters();
140
        $sortField = $workspaceSessionData->getSortField();
141
        $sortOrder = $workspaceSessionData->getSortOrder();
142
143
        if ($this->security->getUser()->getUserRole() == Security::ROLE_LIBRARIAN) {
144
            $query = $this->getWorkspaceQuery($from, $bookmarkIdentifiers,
145
                $filters, $excludeFilters, $sortField, $sortOrder);
146
        } elseif ($this->security->getUser()->getUserRole() == Security::ROLE_RESEARCHER) {
147
            $query = $this->getMyPublicationsQuery($from, $bookmarkIdentifiers,
148
                $filters, $excludeFilters, $sortField, $sortOrder);
149
        }
150
151
        try {
152
            $results = $this->elasticSearch->search($query, 'object');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.
Loading history...
153
        } catch (\Exception $e) {
154
            $workspaceSessionData->clearSort();
155
            $workspaceSessionData->clearFilters();
156
            $this->session->setWorkspaceData($workspaceSessionData);
157
            $this->addFlashMessage(
158
                "Error while buildig the list!", '', AbstractMessage::ERROR
159
            );
160
        }
161
162
        if ($this->request->hasArgument('message')) {
163
            $this->view->assign('message', $this->request->getArgument('message'));
164
        }
165
166
        if ($this->request->hasArgument('errorFiles')) {
167
            $this->view->assign('errorFiles', $this->request->getArgument('errorFiles'));
168
        }
169
170
        if ($filters && $results['hits']['total']['value'] < 1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
171
            $workspaceSessionData->clearSort();
172
            $workspaceSessionData->clearFilters();
173
            $this->session->setWorkspaceData($workspaceSessionData);
174
            list($redirectAction, $redirectController) = $this->session->getStoredAction();
175
            $this->redirect(
176
                $redirectAction, $redirectController, null,
177
                array('message' => [], 'checkedDocumentIdentifiers' => [])
178
            );
179
        }
180
181
        $this->view->assign('documentCount', $results['hits']['total']['value']);
182
        $this->view->assign('documents', $results['hits']['hits']);
183
        $this->view->assign('pages', range(1, $results['hits']['total']['value']));
184
        $this->view->assign('itemsPerPage', $this->itemsPerPage());
185
        $this->view->assign('aggregations', $results['aggregations']);
186
        $this->view->assign('filters', $filters);
187
        $this->view->assign('isHideDiscarded', array_key_exists('aliasState', $excludeFilters));
188
        $this->view->assign('isBookmarksOnly', array_key_exists('bookmarks', $excludeFilters));
189
        $this->view->assign('bookmarkIdentifiers', $bookmarkIdentifiers);
190
191
        if ($this->fisDataService->getPersonData($this->security->getUser()->getFisPersId())) {
192
          $this->view->assign('currentFisPersId', $this->security->getUser()->getFisPersId());
193
        }
194
195
        $personGroup = $this->metadataGroupRepository->findPersonGroup();
196
        $this->view->assign('personGroup', $personGroup->getUid());
197
    }
198
199
    /**
200
     * Lists documents of the workspace
201
     *
202
     * @param array $checkedDocumentIdentifiers
203
     *
204
     * @return void
205
     */
206
    protected function listWorkspaceAction($checkedDocumentIdentifiers = [])
207
    {
208
        $args = $this->request->getArguments();
209
        if ($args['refresh']) {
210
            $workspaceSessionData = $this->session->getWorkspaceData();
211
            $workspaceSessionData->clearSort();
212
            $workspaceSessionData->clearFilters();
213
214
            $this->session->setWorkspaceData($workspaceSessionData);
215
        }
216
217
        if ($this->security->getUser()->getUserRole() === Security::ROLE_LIBRARIAN) {
218
            $this->view->assign('isWorkspace', true);
219
        } elseif ($this->security->getUser()->getUserRole() === Security::ROLE_RESEARCHER) {
220
            $this->view->assign('isWorkspace', false);
221
        } else {
222
            $message = LocalizationUtility::translate(
223
                'manager.workspace.accessDenied', 'dpf'
224
            );
225
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
226
        }
227
228
        $this->session->setStoredAction($this->getCurrentAction(), $this->getCurrentController(),
229
            $this->uriBuilder->getRequest()->getRequestUri()
0 ignored issues
show
introduced by
The method getRequestUri() does not exist on TYPO3\CMS\Extbase\Mvc\Request. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

229
            $this->uriBuilder->getRequest()->/** @scrutinizer ignore-call */ getRequestUri()
Loading history...
230
        );
231
232
        $currentPage = null;
233
        $pagination = $this->getParametersSafely('@widget_0');
234
        if ($pagination) {
235
            $checkedDocumentIdentifiers = [];
236
            $currentPage = $pagination['currentPage'];
237
        } else {
238
            $currentPage = 1;
239
        }
240
241
        $this->list((empty($currentPage)? 0 : ($currentPage-1) * $this->itemsPerPage()));
242
243
        $this->view->assign('currentPage', $currentPage);
244
        $this->view->assign('workspaceListAction', $this->getCurrentAction());
245
        $this->view->assign('checkedDocumentIdentifiers', $checkedDocumentIdentifiers);
246
    }
247
248
249
    /**
250
     * Batch operations action.
251
     * @param array $listData
252
     */
253
    public function batchAction($listData)
254
    {
255
        if (array_key_exists('action', $listData)) {
256
            $this->forward($listData['action'], null, null, ['listData' => $listData]);
257
        }
258
    }
259
260
    /**
261
     * Batch operation, register documents.
262
     * @param array $listData
263
     * @throws \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException
264
     */
265
    public function batchRegisterAction($listData)
266
    {
267
        $successful = [];
268
        $checkedDocumentIdentifiers = [];
269
270
        if (array_key_exists('documentIdentifiers', $listData) && is_array($listData['documentIdentifiers']) ) {
271
            $checkedDocumentIdentifiers = $listData['documentIdentifiers'];
272
            foreach ($listData['documentIdentifiers'] as $documentIdentifier) {
273
274
                $this->editingLockService->lock(
275
                    $documentIdentifier, $this->security->getUser()->getUid()
276
                );
277
278
                if (is_numeric($documentIdentifier)) {
279
                    $document = $this->documentManager->read($documentIdentifier);
280
281
                    if ($this->authorizationChecker->isGranted(DocumentVoter::REGISTER, $document)) {
282
283
                        if ($this->documentValidator->validate($document)) {
284
285
                            if (
286
                                $this->documentManager->update(
287
                                    $document,
288
                                    DocumentWorkflow::TRANSITION_REGISTER
289
                                )
290
                            ) {
291
                                $this->bookmarkRepository->addBookmark(
292
                                    $document,
293
                                    $this->security->getUser()->getUid()
294
                                );
295
296
                                $successful[] = $documentIdentifier;
297
298
                                $notifier = $this->objectManager->get(Notifier::class);
299
                                $notifier->sendRegisterNotification($document);
300
301
                                // index the document
302
                                $this->signalSlotDispatcher->dispatch(
303
                                    \EWW\Dpf\Controller\AbstractController::class,
304
                                    'indexDocument', [$document]
305
                                );
306
                            }
307
                        }
308
                    }
309
                }
310
            }
311
312
313
            if (sizeof($successful) == 1) {
314
                $locallangKey = 'manager.workspace.batchAction.register.success.singular';
315
            } else {
316
                $locallangKey = 'manager.workspace.batchAction.register.success.plural';
317
            }
318
319
            $message = LocalizationUtility::translate(
320
                $locallangKey,
321
                'dpf',
322
                [sizeof($successful), sizeof($listData['documentIdentifiers'])]
323
            );
324
325
326
            $this->addFlashMessage(
327
                $message, '',
328
                (sizeof($successful) > 0 ? AbstractMessage::OK : AbstractMessage::WARNING)
329
            );
330
331
        } else {
332
            $message = LocalizationUtility::translate(
333
                'manager.workspace.batchAction.failure',
334
                'dpf');
335
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
336
        }
337
338
        list($redirectAction, $redirectController) = $this->session->getStoredAction();
339
        $this->redirect(
340
            $redirectAction, $redirectController, null,
341
            array('message' => $message, 'checkedDocumentIdentifiers' =>  $checkedDocumentIdentifiers));
342
    }
343
344
    /**
345
     * Batch operation, set documents to "In progress".
346
     * @param array $listData
347
     */
348
    public function batchSetInProgressAction($listData)
349
    {
350
        $successful = [];
351
        $checkedDocumentIdentifiers = [];
352
353
        if (array_key_exists('documentIdentifiers', $listData) && is_array($listData['documentIdentifiers']) ) {
354
            $checkedDocumentIdentifiers = $listData['documentIdentifiers'];
355
            foreach ($listData['documentIdentifiers'] as $documentIdentifier) {
356
357
                $this->editingLockService->lock(
358
                    $documentIdentifier, $this->security->getUser()->getUid()
359
                );
360
361
                $document = $this->documentManager->read($documentIdentifier);
362
363
                    if ($this->authorizationChecker->isGranted(DocumentVoter::UPDATE, $document)) {
364
365
                        $document->setTemporary(false);
366
367
                            if (
368
                                $this->documentManager->update(
369
                                    $document,
370
                                    DocumentWorkflow::TRANSITION_IN_PROGRESS
371
                                )
372
                            ) {
373
                                $successful[] = $documentIdentifier;
374
375
                                // index the document
376
                                $this->signalSlotDispatcher->dispatch(
377
                                    \EWW\Dpf\Controller\AbstractController::class,
378
                                    'indexDocument', [$document]
379
                                );
380
                            }
381
                    }
382
            }
383
384
            if (sizeof($successful) == 1) {
385
                $locallangKey = 'manager.workspace.batchAction.setInProgress.success.singular';
386
            } else {
387
                $locallangKey = 'manager.workspace.batchAction.setInProgress.success.plural';
388
            }
389
390
            $message = LocalizationUtility::translate(
391
                $locallangKey,
392
                'dpf',
393
                [sizeof($successful), sizeof($listData['documentIdentifiers'])]
394
            );
395
396
            $this->addFlashMessage(
397
                $message, '',
398
                (sizeof($successful) > 0 ? AbstractMessage::OK : AbstractMessage::WARNING)
399
            );
400
401
        } else {
402
            $message = LocalizationUtility::translate(
403
                'manager.workspace.batchAction.failure',
404
                'dpf');
405
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
406
        }
407
408
        list($redirectAction, $redirectController) = $this->session->getStoredAction();
409
        $this->redirect(
410
            $redirectAction, $redirectController, null,
411
            array('message' => $message, 'checkedDocumentIdentifiers' =>  $checkedDocumentIdentifiers));
412
    }
413
414
    /**
415
     * Batch operation, remove documents.
416
     * @param array $listData
417
     */
418
    public function batchRemoveAction($listData)
419
    {
420
        $successful = [];
421
        $checkedDocumentIdentifiers = [];
422
423
        if (array_key_exists('documentIdentifiers', $listData) && is_array($listData['documentIdentifiers']) ) {
424
            $checkedDocumentIdentifiers = $listData['documentIdentifiers'];
425
            foreach ($listData['documentIdentifiers'] as $documentIdentifier) {
426
                $feUserUid = $this->security->getUser()->getUid();
427
                $bookmark = $this->bookmarkRepository->findBookmark($feUserUid, $documentIdentifier);
428
                if ($bookmark instanceof Bookmark) {
429
                    $this->bookmarkRepository->remove($bookmark);
430
                    $successful[] = $documentIdentifier;
431
                }
432
            }
433
434
435
            if (sizeof($successful) == 1) {
436
                $locallangKey = 'manager.workspace.batchAction.remove.success.singular';
437
            } else {
438
                $locallangKey = 'manager.workspace.batchAction.remove.success.plural';
439
            }
440
441
            $message = LocalizationUtility::translate(
442
                $locallangKey,
443
                'dpf',
444
                [sizeof($successful), sizeof($listData['documentIdentifiers'])]
445
            );
446
            $this->addFlashMessage(
447
                $message, '',
448
                (sizeof($successful) > 0 ? AbstractMessage::OK : AbstractMessage::WARNING)
449
            );
450
        } else {
451
            $message = LocalizationUtility::translate(
452
                'manager.workspace.batchAction.failure',
453
                'dpf');
454
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
455
        }
456
457
        list($redirectAction, $redirectController) = $this->session->getStoredAction();
458
        $this->redirect(
459
            $redirectAction, $redirectController, null,
460
            array('message' => $message, 'checkedDocumentIdentifiers' =>  $checkedDocumentIdentifiers));
461
    }
462
463
464
    /**
465
     * Batch operation, release documents.
466
     * @param array $listData
467
     */
468
    public function batchReleaseValidatedAction($listData)
469
    {
470
        $this->batchRelease($listData, true);
471
    }
472
473
    /**
474
     * Batch operation, release as unvalidated documents.
475
     * @param array $listData
476
     */
477
    public function batchReleaseUnvalidatedAction($listData)
478
    {
479
        $this->batchRelease($listData, false);
480
    }
481
482
483
484
485
    /**
486
     * Batch operation, release documents.
487
     * @param array $listData
488
     * @param bool $validated
489
     */
490
    protected function batchRelease($listData, $validated)
491
    {
492
        $successful = [];
493
        $checkedDocumentIdentifiers = [];
494
495
        if (array_key_exists('documentIdentifiers', $listData) && is_array($listData['documentIdentifiers']) ) {
496
            $checkedDocumentIdentifiers = $listData['documentIdentifiers'];
497
            foreach ($listData['documentIdentifiers'] as $documentIdentifier) {
498
499
                $this->editingLockService->lock(
500
                    $documentIdentifier, $this->security->getUser()->getUid()
501
                );
502
503
                $document = $this->documentManager->read($documentIdentifier);
504
505
                switch ($document->getState()) {
506
                    case DocumentWorkflow::STATE_REGISTERED_NONE:
507
                    case DocumentWorkflow::STATE_DISCARDED_NONE:
508
                    case DocumentWorkflow::STATE_POSTPONED_NONE:
509
                        $documentVoterAttribute = DocumentVoter::RELEASE_PUBLISH;
510
                        $documentWorkflowTransition = DocumentWorkflow::TRANSITION_RELEASE_PUBLISH;
511
                        break;
512
513
                    case DocumentWorkflow::STATE_NONE_DELETED:
514
                    case DocumentWorkflow::STATE_NONE_INACTIVE:
515
                    case DocumentWorkflow::STATE_IN_PROGRESS_DELETED:
516
                    case DocumentWorkflow::STATE_IN_PROGRESS_INACTIVE:
517
                    case DocumentWorkflow::STATE_IN_PROGRESS_ACTIVE:
518
                        $documentVoterAttribute = DocumentVoter::RELEASE_ACTIVATE;
519
                        $documentWorkflowTransition = DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE;
520
                        break;
521
                    default:
522
                        $documentVoterAttribute = null;
523
                        $documentWorkflowTransition = null;
524
                        break;
525
                }
526
527
                if ($this->authorizationChecker->isGranted($documentVoterAttribute, $document)) {
528
529
                    $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
530
                    $internalFormat->setValidation($validated);
531
                    $document->setXmlData($internalFormat->getXml());
532
533
                    if ($this->documentManager->update($document, $documentWorkflowTransition)) {
534
                        $successful[] = $documentIdentifier;
535
536
                        $this->bookmarkRepository->removeBookmark(
537
                            $document, $this->security->getUser()->getUid()
538
                        );
539
540
                        //$notifier = $this->objectManager->get(Notifier::class);
541
                        //$notifier->sendRegisterNotification($document);
542
                    }
543
                }
544
            }
545
546
            if (sizeof($successful) == 1) {
547
                $locallangKey = 'manager.workspace.batchAction.release.success.singular';
548
            } else {
549
                $locallangKey = 'manager.workspace.batchAction.release.success.plural';
550
            }
551
552
            $message = LocalizationUtility::translate(
553
                $locallangKey,
554
                'dpf',
555
                [sizeof($successful), sizeof($listData['documentIdentifiers'])]
556
            );
557
            $this->addFlashMessage(
558
                $message, '',
559
                (sizeof($successful) > 0 ? AbstractMessage::OK : AbstractMessage::WARNING)
560
            );
561
562
            if (sizeof($successful) === 1 ) {
563
                $this->addFlashMessage(
564
                    "1 ".LocalizationUtility::translate("manager.workspace.bookmarkRemoved.singular", "dpf"),
565
                    '',
566
                    AbstractMessage::INFO
567
                );
568
            }
569
570
            if (sizeof($successful) > 1 ) {
571
                $this->addFlashMessage(
572
                    LocalizationUtility::translate(
573
                        "manager.workspace.bookmarkRemoved.plural", "dpf", [sizeof($successful)]
574
                    ),
575
                    '',
576
                    AbstractMessage::INFO
577
                );
578
            }
579
580
        } else {
581
            $message = LocalizationUtility::translate(
582
                'manager.workspace.batchAction.failure',
583
                'dpf');
584
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
585
        }
586
587
        list($redirectAction, $redirectController) = $this->session->getStoredAction();
588
        $this->redirect(
589
            $redirectAction, $redirectController, null,
590
            array('message' => $message, 'checkedDocumentIdentifiers' =>  $checkedDocumentIdentifiers));
591
592
    }
593
594
    /**
595
     * get list view data for the workspace
596
     *
597
     * @param int $from
598
     * @param array $bookmarkIdentifiers
599
     * @param array $filters
600
     * @param array $excludeFilters
601
     * @param string $sortField
602
     * @param string $sortOrder
603
     *
604
     * @return array
605
     */
606
    protected function getWorkspaceQuery(
607
        $from = 0, $bookmarkIdentifiers = [], $filters = [], $excludeFilters = [], $sortField = null, $sortOrder = null
608
    )
609
    {
610
        $workspaceFilter = [
611
            'bool' => [
612
                'must' => [
613
                    [
614
                        'bool' => [
615
                            'must' => [
616
                                [
617
                                    'term' => [
618
                                        'creator' => $this->security->getUser()->getUid()
619
                                    ]
620
                                ],
621
                                [
622
                                    'bool' => [
623
                                        'should' => [
624
                                            [
625
                                                'term' => [
626
                                                    'state' => DocumentWorkflow::STATE_NEW_NONE
627
                                                ]
628
                                            ]
629
                                        ]
630
                                    ]
631
                                ]
632
                            ]
633
                        ]
634
                    ]
635
                ]
636
            ]
637
        ];
638
639
        return $this->queryBuilder->buildQuery(
640
            $this->itemsPerPage(), $workspaceFilter, $from, $bookmarkIdentifiers, $filters,
641
            $excludeFilters, $sortField, $sortOrder
642
        );
643
    }
644
645
646
    /**
647
     * get list view data for the my publications list.
648
     *
649
     * @param int $from
650
     * @param array $bookmarkIdentifiers
651
     * @param array $filters
652
     * @param array $excludeFilters
653
     * @param string $sortField
654
     * @param string $sortOrder
655
     *
656
     * @return array
657
     */
658
    protected function getMyPublicationsQuery(
659
        $from = 0, $bookmarkIdentifiers = [], $filters = [], $excludeFilters = [], $sortField = null, $sortOrder = null
660
    )
661
    {
662
        $workspaceFilter = [
663
            'bool' => [
664
                'must' => [
665
                    [
666
                        'bool' => [
667
                            'should' => [
668
                                [
669
                                    'term' => [
670
                                        'creator' => $this->security->getUser()->getUid()
671
                                    ]
672
                                ]
673
                            ]
674
                        ]
675
                    ]
676
                ]
677
            ]
678
        ];
679
680
        $fisPersIdFilter =  [
681
            'bool' => [
682
                'must' => [
683
                    [
684
                        'term' => [
685
                            'fobIdentifiers' => $this->security->getUser()->getFisPersId()
686
                        ]
687
                    ],
688
                    [
689
                        'bool' => [
690
                            'must_not' => [
691
                                'term' => [
692
                                    'state' => DocumentWorkflow::STATE_NEW_NONE
693
                                ]
694
                            ]
695
                        ]
696
                    ]
697
                ]
698
            ],
699
        ];
700
701
        if ($this->security->getUser()->getFisPersId()) {
702
            $workspaceFilter['bool']['must'][0]['bool']['should'][1] = $fisPersIdFilter;
703
        }
704
705
        return $this->queryBuilder->buildQuery(
706
            $this->itemsPerPage(), $workspaceFilter, $from, $bookmarkIdentifiers, $filters,
707
            $excludeFilters, $sortField, $sortOrder
708
        );
709
    }
710
711
712
    /**
713
     * A temporary solution to initialize the index.
714
     *
715
     * @param int $start
716
     * @param int $stop
717
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
718
     */
719
    public function initIndexAction($start = 1, $stop = 100)
720
    {
721
        /** @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher */
722
        $signalSlotDispatcher = $this->objectManager->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
723
724
        /** @var \EWW\Dpf\Services\Transfer\DocumentTransferManager $documentTransferManager */
725
        $documentTransferManager = $this->objectManager->get(\EWW\Dpf\Services\Transfer\DocumentTransferManager::class);
726
727
        $fedoraRepository = $this->objectManager->get(\EWW\Dpf\Services\Transfer\FedoraRepository::class);
728
        $documentTransferManager->setRemoteRepository($fedoraRepository);
729
730
        for ($i = $start; $i < $stop; $i++) {
731
            try {
732
                $document = $documentTransferManager->retrieve('qucosa:' . $i);
733
734
                if ($document instanceof Document) {
735
                    $state = $document->getState();
736
                    $document->setState(
737
                        str_replace(
738
                            DocumentWorkflow::LOCAL_STATE_IN_PROGRESS,
739
                            DocumentWorkflow::LOCAL_STATE_NONE,
740
                            $state
741
                        )
742
                    );
743
744
                    // index the document
745
                    $signalSlotDispatcher->dispatch(
746
                        \EWW\Dpf\Controller\AbstractController::class,
747
                        'indexDocument', [$document]
748
                    );
749
750
                    $this->documentRepository->remove($document);
751
                }
752
            } catch (\EWW\Dpf\Exceptions\RetrieveDocumentErrorException $e) {
753
                // Nothing to be done.
754
            }
755
        }
756
757
        foreach ($this->documentRepository->findAll() as $document) {
758
759
            $creationDate = $document->getCreationDate();
760
            if (empty($creationDate) && $document->getObjectIdentifier()) {
761
                $creationDate = $document->getCreationDate();
762
            }
763
            $document->setCreationDate($creationDate);
764
            $this->documentRepository->update($document);
765
766
            if (!$document->isTemporary() && !$document->isSuggestion()) {
767
                // index the document
768
                $signalSlotDispatcher->dispatch(
769
                    \EWW\Dpf\Controller\AbstractController::class,
770
                    'indexDocument', [$document]
771
                );
772
            }
773
        }
774
775
    }
776
777
778
    /**
779
     * Action editDocument
780
     *
781
     * @param string $documentIdentifier
782
     * @param string $activeGroup
783
     * @param int $activeGroupIndex
784
     * @return void
785
     */
786
    public function editDocumentAction($documentIdentifier, $activeGroup = '', $activeGroupIndex = 0)
787
    {
788
        $document = $this->documentManager->read(
789
            $documentIdentifier,
790
            $this->security->getUser()->getUID()
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Services\Document\DocumentManager::read() has too many arguments starting with $this->security->getUser()->getUID(). ( Ignorable by Annotation )

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

790
        /** @scrutinizer ignore-call */ 
791
        $document = $this->documentManager->read(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
791
        );
792
793
        if ($document instanceof Document) {
794
            if ($this->authorizationChecker->isGranted(DocumentVoter::EDIT, $document)) {
795
                $this->redirect(
796
                    'edit',
797
                    'DocumentFormBackoffice',
798
                    null,
799
                    [
800
                        'document' => $document,
801
                        'activeGroup' => $activeGroup,
802
                        'activeGroupIndex' => $activeGroupIndex
803
                    ]
804
                );
805
            } elseif ($this->authorizationChecker->isGranted(DocumentVoter::SUGGEST_MODIFICATION, $document)) {
806
                $this->redirect(
807
                    'edit',
808
                    'DocumentFormBackoffice',
809
                    null,
810
                    [
811
                        'document' => $document,
812
                        'suggestMod' => true,
813
                        'activeGroup' => $activeGroup,
814
                        'activeGroupIndex' => $activeGroupIndex
815
                    ]
816
                );
817
            } else {
818
                if ($document->getCreator() !== $this->security->getUser()->getUid()) {
819
                    $message = LocalizationUtility::translate(
820
                        'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.accessDenied',
821
                        'dpf',
822
                        array($document->getTitle())
823
                    );
824
                } else {
825
                    $message = LocalizationUtility::translate(
826
                        'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_edit.failureBlocked',
827
                        'dpf',
828
                        array($document->getTitle())
829
                    );
830
                }
831
            }
832
        } else {
833
            $message = LocalizationUtility::translate(
834
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected',
835
                'dpf'
836
            );
837
        }
838
839
        $this->addFlashMessage($message, '', AbstractMessage::ERROR);
840
841
        list($action, $controller, $redirectUri) = $this->session->getStoredAction();
842
843
        if ($redirectUri) {
844
            $this->redirectToUri($redirectUri);
845
        } else {
846
            $this->redirect($action, $controller, null, array('message' => $message));
847
        }
848
849
    }
850
851
852
    /**
853
     * Returns the number of items to be shown per page.
854
     *
855
     * @return int
856
     */
857
    protected function itemsPerPage()
858
    {
859
        /** @var SearchSessionData $workspaceData */
860
        $workspaceData = $this->session->getWorkspaceData();
861
        $itemsPerPage = $workspaceData->getItemsPerPage();
862
863
        $default = ($this->settings['workspaceItemsPerPage'])? $this->settings['workspaceItemsPerPage'] : 10;
864
        return ($itemsPerPage)? $itemsPerPage : $default;
865
    }
866
867
}
868