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

WorkspaceController::batchAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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
        try {
196
            $personGroup = $this->metadataGroupRepository->findPersonGroup();
197
            $this->view->assign('personGroup', $personGroup->getUid());
198
        } catch (\Throwable $e) {
199
            $this->addFlashMessage(
200
                "Missing configuration: Person group.", '', AbstractMessage::ERROR
201
            );
202
        }
203
    }
204
205
    /**
206
     * Lists documents of the workspace
207
     *
208
     * @param array $checkedDocumentIdentifiers
209
     *
210
     * @return void
211
     */
212
    protected function listWorkspaceAction($checkedDocumentIdentifiers = [])
213
    {
214
        $args = $this->request->getArguments();
215
        if ($args['refresh']) {
216
            $workspaceSessionData = $this->session->getWorkspaceData();
217
            $workspaceSessionData->clearSort();
218
            $workspaceSessionData->clearFilters();
219
220
            $this->session->setWorkspaceData($workspaceSessionData);
221
        }
222
223
        if ($this->security->getUser()->getUserRole() === Security::ROLE_LIBRARIAN) {
224
            $this->view->assign('isWorkspace', true);
225
        } elseif ($this->security->getUser()->getUserRole() === Security::ROLE_RESEARCHER) {
226
            $this->view->assign('isWorkspace', false);
227
        } else {
228
            $message = LocalizationUtility::translate(
229
                'manager.workspace.accessDenied', 'dpf'
230
            );
231
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
232
        }
233
234
        $this->session->setStoredAction($this->getCurrentAction(), $this->getCurrentController(),
235
            $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

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

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