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

getNewPublicationNotificationRecipients()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 15
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 27
rs 8.0555
1
<?php
2
namespace EWW\Dpf\Services\Document;
3
4
use EWW\Dpf\Domain\Model\Bookmark;
5
use EWW\Dpf\Domain\Model\Document;
6
use EWW\Dpf\Domain\Model\File;
7
use EWW\Dpf\Domain\Model\FrontendUser;
8
use EWW\Dpf\Security\Security;
9
use EWW\Dpf\Services\ElasticSearch\ElasticSearch;
10
use EWW\Dpf\Services\Transfer\FedoraRepository;
11
use EWW\Dpf\Services\Transfer\DocumentTransferManager;
12
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
13
use EWW\Dpf\Controller\AbstractController;
14
use EWW\Dpf\Services\Email\Notifier;
15
use Symfony\Component\Workflow\Workflow;
16
use Httpful\Request;
17
18
class DocumentManager
19
{
20
    /**
21
     * objectManager
22
     *
23
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
24
     * @inject
25
     */
26
    protected $objectManager = null;
27
28
    /**
29
     * documentRepository
30
     *
31
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
32
     * @inject
33
     */
34
    protected $documentRepository = null;
35
36
    /**
37
     * fileRepository
38
     *
39
     * @var \EWW\Dpf\Domain\Repository\FileRepository
40
     * @inject
41
     */
42
    protected $fileRepository = null;
43
44
    /**
45
     * bookmarkRepository
46
     *
47
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
48
     * @inject
49
     */
50
    protected $bookmarkRepository = null;
51
52
    /**
53
     * persistence manager
54
     *
55
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
56
     * @inject
57
     */
58
    protected $persistenceManager;
59
60
    /**
61
     * signalSlotDispatcher
62
     *
63
     * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
64
     * @inject
65
     */
66
    protected $signalSlotDispatcher = null;
67
68
    /**
69
     * notifier
70
     *
71
     * @var \EWW\Dpf\Services\Email\Notifier
72
     * @inject
73
     */
74
    protected $notifier = null;
75
76
    /**
77
     * security
78
     *
79
     * @var \EWW\Dpf\Security\Security
80
     * @inject
81
     */
82
    protected $security = null;
83
84
    /**
85
     * frontendUserRepository
86
     *
87
     * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository
88
     * @inject
89
     */
90
    protected $frontendUserRepository = null;
91
92
    /**
93
     * elasticSearch
94
     *
95
     * @var \EWW\Dpf\Services\ElasticSearch\ElasticSearch
96
     * @inject
97
     */
98
    protected $elasticSearch = null;
99
100
    /**
101
     * queryBuilder
102
     *
103
     * @var \EWW\Dpf\Services\ElasticSearch\QueryBuilder
104
     * @inject
105
     */
106
    protected $queryBuilder = null;
107
108
    /**
109
     * Returns the localized document identifiers (uid/objectIdentifier).
110
     *
111
     * @param $identifier
112
     * @return array
113
     */
114
    public function resolveIdentifier($identifier) {
115
116
        $localizedIdentifiers = [];
117
118
        $document = $this->documentRepository->findByIdentifier($identifier);
119
120
        if ($document instanceof Document) {
0 ignored issues
show
introduced by
$document is always a sub-type of EWW\Dpf\Domain\Model\Document.
Loading history...
121
122
            if ($document->getObjectIdentifier()) {
123
                $localizedIdentifiers['objectIdentifier'] = $document->getObjectIdentifier();
124
            }
125
126
            if ($document->getUid()) {
127
                $localizedIdentifiers['uid'] = $document->getUid();
128
            }
129
        } else {
130
131
            $query = $this->queryBuilder->buildQuery(
132
                1, [], 0,
133
                [], [], [], null, null,
134
                'identifier:"'.$identifier.'"'
135
            );
136
137
            try {
138
                $results =  $this->elasticSearch->search($query, 'object');
139
                if (is_array($results) && $results['hits']['total']['value'] > 0) {
140
                    $localizedIdentifiers['objectIdentifier'] = $results['hits']['hits'][0]['_id'];
141
                }
142
            } catch (\Exception $e) {
143
                return [];
144
            }
145
146
        }
147
148
        return $localizedIdentifiers;
149
    }
150
151
152
    /**
153
     * Returns a document specified by repository object identifier, a typo3 uid or a process number.
154
     *
155
     * @param string $identifier
156
     * @return Document|null
157
     */
158
    public function read($identifier)
159
    {
160
        if (!$identifier) {
161
            return null;
162
        }
163
164
        $localizedIdentifiers = $this->resolveIdentifier($identifier);
165
166
        if (array_key_exists('uid', $localizedIdentifiers)) {
167
            return $this->documentRepository->findByUid($localizedIdentifiers['uid']);
168
        }
169
170
        if (array_key_exists('objectIdentifier', $localizedIdentifiers)) {
171
            try {
172
                /** @var \EWW\Dpf\Domain\Model\Document $document */
173
                $document = $this->getDocumentTransferManager()->retrieve($localizedIdentifiers['objectIdentifier']);
174
175
                // index the document
176
                $this->signalSlotDispatcher->dispatch(
177
                    AbstractController::class, 'indexDocument', [$document]
178
                );
179
180
                return $document;
181
            } catch (\Exception $exception) {
182
                return null;
183
            }
184
        }
185
186
        return null;
187
    }
188
189
    /**
190
     * Updates a document locally or remotely.
191
     *
192
     * @param Document $document
193
     * @param string $workflowTransition
194
     * @param array $deletedFiles
195
     * @param array $newFiles
196
     * @return string|false
197
     * @throws \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException
198
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
199
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
200
     */
201
    public function update(Document $document, $workflowTransition = null, $deletedFiles = [], $newFiles = [])
202
    {
203
        // xml data fields are limited to 64 KB
204
        if (strlen($document->getXmlData()) >= 64 * 1024) {
205
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
206
        }
207
208
        /** @var \Symfony\Component\Workflow\Workflow $workflow */
209
        $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow();
210
211
        if ($workflowTransition) {
212
            if (!$workflow->can($document, $workflowTransition)) {
213
                return false;
214
            }
215
            $workflow->apply($document, $workflowTransition);
216
        }
217
218
        if ($document->isSuggestion()) {
219
220
            // if local suggestion copy
221
            $updateResult = false;
222
223
        } elseif ($document->isTemporaryCopy()) {
224
225
            // if temporary working copy
226
            $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles);
227
228
        } elseif (
229
            $document->isWorkingCopy() &&
230
            (
231
                $workflowTransition === DocumentWorkflow::TRANSITION_POSTPONE ||
232
                $workflowTransition === DocumentWorkflow::TRANSITION_DISCARD ||
233
                $workflowTransition === DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE
234
            )
235
        ) {
236
237
            // if local working copy with state change
238
            $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles);
239
240
        } elseif ($document->isWorkingCopy()) {
241
242
            // if local working copy with no state change
243
            $this->updateFiles($document, $deletedFiles, $newFiles);
244
            $this->documentRepository->update($document);
245
            $updateResult = $document->getDocumentIdentifier();
246
247
        } elseif ($workflowTransition == DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) {
248
            // Fedora ingest
249
            if ($ingestedDocument = $this->getDocumentTransferManager()->ingest($document)) {
250
251
                // After ingest all related bookmarks need an update of the identifier into an fedora object identifier.
252
                if ($ingestedDocument instanceof Document) {
0 ignored issues
show
introduced by
$ingestedDocument is always a sub-type of EWW\Dpf\Domain\Model\Document.
Loading history...
253
                    /** @var Bookmark $bookmark */
254
                    foreach ($this->bookmarkRepository->findByDocumentIdentifier($ingestedDocument->getUid()) as $bookmark) {
0 ignored issues
show
Bug introduced by
The method findByDocumentIdentifier() 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

254
                    foreach ($this->bookmarkRepository->/** @scrutinizer ignore-call */ findByDocumentIdentifier($ingestedDocument->getUid()) as $bookmark) {
Loading history...
255
                        $bookmark->setDocumentIdentifier($ingestedDocument->getDocumentIdentifier());
256
                        $this->bookmarkRepository->update($bookmark);
257
                    }
258
                    $this->persistenceManager->persistAll();
259
                } else {
260
                    throw \Exception("Logical exception while updating bookmarks.");
0 ignored issues
show
Bug introduced by
The function Exception was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

260
                    throw /** @scrutinizer ignore-call */ \Exception("Logical exception while updating bookmarks.");
Loading history...
261
                }
262
263
                // check embargo
264
                if(!$this->hasActiveEmbargo($document)){
265
                    $this->removeDocument($document);
266
                } else {
267
                    $document->setState(DocumentWorkflow::constructState(DocumentWorkflow::LOCAL_STATE_IN_PROGRESS, $document->getRemoteState()));
268
                }
269
                $updateResult = $document->getDocumentIdentifier();
270
            } else {
271
                $updateResult = false;
272
            }
273
        } else {
274
            $this->updateFiles($document, $deletedFiles, $newFiles);
275
            $this->documentRepository->update($document);
276
            $updateResult = $document->getDocumentIdentifier();
277
        }
278
279
        if ($updateResult) {
280
281
            if (DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) {
282
                // delete local document from index
283
                $this->signalSlotDispatcher->dispatch(
284
                    AbstractController::class, 'deleteDocumentFromIndex', [$document->getUid()]
285
                );
286
            }
287
288
            // index the document
289
            $this->signalSlotDispatcher->dispatch(
290
                AbstractController::class, 'indexDocument', [$document]
291
            );
292
293
            // Notify assigned users
294
            $recipients = $this->getUpdateNotificationRecipients($document);
295
            $this->notifier->sendMyPublicationUpdateNotification($document, $recipients);
296
297
            $recipients = $this->getNewPublicationNotificationRecipients($document);
298
            $this->notifier->sendMyPublicationNewNotification($document, $recipients);
299
300
            /** @var Notifier $notifier */
301
            $notifier = $this->objectManager->get(Notifier::class);
302
            $notifier->sendChangedDocumentNotification($document);
303
        }
304
305
        return $updateResult;
306
    }
307
308
    /**
309
     * @return DocumentTransferManager
310
     */
311
    protected function getDocumentTransferManager()
312
    {
313
        /** @var DocumentTransferManager $documentTransferManager */
314
        $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class);
315
316
        /** @var  FedoraRepository $remoteRepository */
317
        $remoteRepository = $this->objectManager->get(FedoraRepository::class);
318
319
        $documentTransferManager->setRemoteRepository($remoteRepository);
320
321
        return $documentTransferManager;
322
    }
323
324
325
    /**
326
     * Adds and delete file model objects attached to the document.
327
     *
328
     * @param Document $document
329
     * @param array $deletedFiles
330
     * @param array $newFiles
331
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
332
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
333
     */
334
    protected function updateFiles(Document $document, $deletedFiles, $newFiles)
335
    {
336
        // Delete files
337
        /** @var File $deleteFile */
338
        foreach ($deletedFiles as $deleteFile) {
339
            $deleteFile->setStatus(File::STATUS_DELETED);
340
            $this->fileRepository->update($deleteFile);
341
        }
342
343
        // Add or update files
344
        /** @var File $newFile */
345
        foreach ($newFiles as $newFile) {
346
347
            if ($newFile->getUID()) {
348
                $this->fileRepository->update($newFile);
349
            } else {
350
                $document->addFile($newFile);
351
            }
352
353
        }
354
    }
355
356
    /**
357
     * Removes the document from the local database.
358
     *
359
     * @param $document
360
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
361
     */
362
    protected function removeDocument($document)
363
    {
364
        $files = $this->fileRepository->findByDocument($document->getUid());
0 ignored issues
show
Bug introduced by
The method findByDocument() does not exist on EWW\Dpf\Domain\Repository\FileRepository. 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

364
        /** @scrutinizer ignore-call */ 
365
        $files = $this->fileRepository->findByDocument($document->getUid());
Loading history...
365
        foreach ($files as $file) {
366
            $this->fileRepository->remove($file);
367
        }
368
        $this->documentRepository->remove($document);
369
    }
370
371
372
373
    /**
374
     * @param Document $document
375
     * @param string $workflowTransition
376
     * @param array $deletedFiles
377
     * @param array $newFiles
378
     * @return string
379
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
380
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
381
     */
382
    protected function updateRemotely($document, $workflowTransition = null, $deletedFiles = [], $newFiles = [])
383
    {
384
        $lastModDate = $this->getDocumentTransferManager()->getLastModDate($document->getObjectIdentifier());
385
        $docLastModDate = $document->getRemoteLastModDate();
386
        if ($lastModDate !== $docLastModDate && !empty($docLastModDate)) {
387
            // There is a newer version in the fedora repository.
388
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
389
        }
390
391
        $this->updateFiles($document, $deletedFiles, $newFiles);
392
        $this->documentRepository->update($document);
393
394
        switch ($workflowTransition) {
395
            case DocumentWorkflow::TRANSITION_POSTPONE:
396
                $transferState = DocumentTransferManager::INACTIVATE;
397
                break;
398
399
            case DocumentWorkflow::TRANSITION_DISCARD:
400
                $transferState = DocumentTransferManager::DELETE;
401
                break;
402
403
            case DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE:
404
                $transferState = DocumentTransferManager::REVERT;
405
                break;
406
407
            default:
408
                $transferState = null;
409
                break;
410
        }
411
412
        if ($transferState) {
413
            if (!$this->getDocumentTransferManager()->delete($document, $transferState)) {
414
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
415
            }
416
        }
417
418
        if ($this->getDocumentTransferManager()->update($document)) {
419
420
            if(!$this->hasActiveEmbargo($document)){
421
                $this->removeDocument($document);
422
            } else {
423
                $document->setState(DocumentWorkflow::LOCAL_STATE_IN_PROGRESS . ':' . $document->getRemoteState());
424
            }
425
            return $document->getDocumentIdentifier();
426
        }
427
428
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
429
    }
430
431
    public function addSuggestion($editDocument, $restore = false, $comment = '') {
432
433
        // add new document
434
        /** @var Document $suggestionDocument */
435
        $suggestionDocument = $this->objectManager->get(Document::class);
436
        $this->documentRepository->add($suggestionDocument);
437
        $this->persistenceManager->persistAll();
438
439
        // copy properties from origin
440
        $suggestionDocument = $suggestionDocument->copy($editDocument);
441
442
        if ($suggestionDocument->isTemporary()) {
443
            $suggestionDocument->setTemporary(false);
444
        }
445
446
        if (empty($suggestionDocument->getFileData())) {
447
            // no files are linked to the document
448
            $hasFilesFlag = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $hasFilesFlag is dead and can be removed.
Loading history...
449
        }
450
451
        if ($editDocument->getObjectIdentifier()) {
452
            $suggestionDocument->setLinkedUid($editDocument->getObjectIdentifier());
453
        } else {
454
            $suggestionDocument->setLinkedUid($editDocument->getUid());
455
        }
456
457
        $suggestionDocument->setSuggestion(true);
458
        if ($comment) {
459
            $suggestionDocument->setComment($comment);
460
        }
461
462
        if ($restore) {
463
            $suggestionDocument->setTransferStatus("RESTORE");
464
        }
465
466
//        if (!$hasFilesFlag) {
467
//            // Add or update files
468
//            foreach ($documentForm->getNewFiles() as $newFile) {
469
//                if ($newFile->getUID()) {
470
//                    $this->fileRepository->update($newFile);
471
//                } else {
472
//                    $newFile->setDocument($suggestionDocument);
473
//                    $this->fileRepository->add($newFile);
474
//                }
475
//
476
//                $suggestionDocument->addFile($newFile);
477
//            }
478
//        } else {
479
//            // remove files for suggest object
480
//            $suggestionDocument->setFile($this->objectManager->get(ObjectStorage::class));
481
//        }
482
483
        try {
484
//            $suggestionDocument->setCreator($this->security->getUser()->getUid());
485
            $this->documentRepository->add($suggestionDocument);
486
        } catch (\Throwable $t) {
487
            return null;
488
        }
489
490
        return $suggestionDocument;
491
492
    }
493
494
    /**
495
     * @param $document
496
     * @return bool (true: if no embargo is set or embargo is expired, false: embargo is active)
497
     * @throws \Exception
498
     */
499
    protected function hasActiveEmbargo($document)
500
    {
501
        $currentDate = new \DateTime('now');
502
        if($currentDate > $document->getEmbargoDate()){
503
            // embargo is expired
504
            return false;
505
        } else {
506
            return true;
507
        }
508
509
    }
510
511
512
    /**
513
     * @param Document $document
514
     * @return FrontendUser
515
     */
516
    public function getCreatorUser(Document $document) {
517
        return $this->frontendUserRepository->findByUid($document->getCreator());
518
    }
519
520
    /**
521
     * @param Document $document
522
     * @return array
523
     */
524
    public function getAssignedUsers(Document $document)
525
    {
526
        $assignedUsers = [];
527
528
        foreach ($document->getAssignedFobIdentifiers() as $fobId) {
529
            $feUsers = $this->frontendUserRepository->findByFisPersId($fobId);
0 ignored issues
show
Bug introduced by
The method findByFisPersId() does not exist on EWW\Dpf\Domain\Repository\FrontendUserRepository. 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

529
            /** @scrutinizer ignore-call */ 
530
            $feUsers = $this->frontendUserRepository->findByFisPersId($fobId);
Loading history...
530
            foreach ($feUsers as $feUser) {
531
532
                $assignedUsers[$feUser->getUid()] = $feUser;
533
            }
534
        }
535
536
        return $assignedUsers;
537
    }
538
539
    /**
540
     * @param Document $document
541
     * @return array
542
     */
543
    public function getNewlyAssignedUsers(Document $document)
544
    {
545
        $assignedUsers = [];
546
547
        foreach ($document->getNewlyAssignedFobIdentifiers() as $fobId) {
548
            $feUsers = $this->frontendUserRepository->findByFisPersId($fobId);
549
            foreach ($feUsers as $feUser) {
550
                $assignedUsers[$feUser->getUid()] = $feUser;
551
            }
552
        }
553
554
        return $assignedUsers;
555
    }
556
557
    /**
558
     * @param Document $document
559
     * @return array
560
     */
561
    public function getDocumentBookmarkUsers(Document $document) {
562
563
        $users = [];
564
565
        /** @var Bookmark $bookmark */
566
        $bookmarks = $this->bookmarkRepository->findByDocumentIdentifier($document->getDocumentIdentifier());
567
        foreach ($bookmarks as $bookmark) {
568
            $feUser = $this->frontendUserRepository->findByUid($bookmark->getFeUserUid());
569
            $users[$feUser->getUid()] = $feUser;
570
        }
571
572
        return $users;
573
    }
574
575
    /**
576
     * @param Document $document
577
     * @return array
578
     */
579
    public function getUpdateNotificationRecipients(Document $document)
580
    {
581
        $users = [];
582
583
        if ($document->getCreator()) {
584
            $users[$this->getCreatorUser($document)->getUid()] = $this->getCreatorUser($document);
585
        }
586
587
        foreach ($this->getAssignedUsers($document) as $user) {
588
            $users[$user->getUid()] = $user;
589
        }
590
591
        foreach ($this->getDocumentBookmarkUsers($document) as $user) {
592
            $users[$user->getUid()] = $user;
593
        }
594
595
        $recipients = [];
596
597
        /** @var FrontendUser $recipient */
598
        foreach ($users as $recipient) {
599
            if (
600
                $recipient->getUid() !== $this->security->getUser()->getUid() &&
601
                $document->getState() !== DocumentWorkflow::STATE_NEW_NONE &&
602
                !(
603
                    in_array(
604
                        $recipient->getFisPersId(), $document->getNewlyAssignedFobIdentifiers()
605
                    ) ||
606
                    $document->isStateChange() &&
607
                    $document->getState() === DocumentWorkflow::STATE_REGISTERED_NONE
608
                )
609
            ) {
610
611
                if ($recipient->isNotifyOnChanges()) {
612
613
                    if (
614
                        $recipient->isNotifyPersonalLink() ||
615
                        $recipient->isNotifyStatusChange() ||
616
                        $recipient->isNotifyFulltextPublished()
617
                    ) {
618
                        if (
619
                            $recipient->isNotifyPersonalLink() &&
620
                            in_array(
621
                                $recipient->getFisPersId(), $document->getAssignedFobIdentifiers()
622
                            ) &&
623
                            !(
624
                                $recipient->isNotifyNewPublicationMyPublication() &&
625
                                (
626
                                    in_array(
627
                                        $recipient->getFisPersId(), $document->getNewlyAssignedFobIdentifiers()
628
                                    ) ||
629
                                    $document->isStateChange() &&
630
                                    $document->getState() === DocumentWorkflow::STATE_REGISTERED_NONE
631
                                )
632
                            )
633
                        ) {
634
                            $recipients[$recipient->getUid()] = $recipient;
635
                        }
636
637
                        if ($recipient->isNotifyStatusChange() && $document->isStateChange()) {
638
                            $recipients[$recipient->getUid()] = $recipient;
639
                        }
640
641
                        if ($recipient->isNotifyFulltextPublished()) {
642
643
                            $embargoDate = $document->getEmbargoDate();
644
                            $currentDate = new \DateTime('now');
645
646
                            $fulltextPublished = false;
647
                            foreach ($document->getFile() as $file) {
648
                                if ($file->getStatus() != 'added') {
649
                                    $fulltextPublished = false;
650
                                    break;
651
                                } else {
652
                                    $fulltextPublished = true;
653
                                }
654
                            }
655
656
                            if (
657
                                $document->getState() === DocumentWorkflow::STATE_NONE_ACTIVE &&
658
                                $fulltextPublished &&
659
                                (
660
                                   empty($embargoDate) ||
661
                                   $embargoDate < $currentDate
662
                                )
663
                            ) {
664
                                $recipients[$recipient->getUid()] = $recipient;
665
                            }
666
                        }
667
668
                    } else {
669
                       $recipients[$recipient->getUid()] = $recipient;
670
                    }
671
                }
672
            }
673
        }
674
        return $recipients;
675
    }
676
677
    /**
678
     * @param Document $document
679
     * @return array
680
     */
681
    public function getNewPublicationNotificationRecipients(Document $document)
682
    {
683
        $users = [];
684
685
        /** @var FrontendUser $user */
686
        foreach ($this->getAssignedUsers($document) as $user) {
687
            if (
688
                $user->getUid() !== $this->security->getUser()->getUid() &&
689
                $document->getState() !== DocumentWorkflow::STATE_NEW_NONE &&
690
                $user->getUid() !== $document->getCreator()
691
            ) {
692
                if (
693
                    $user->isNotifyNewPublicationMyPublication() &&
694
                    (
695
                        in_array(
696
                            $user->getFisPersId(), $document->getNewlyAssignedFobIdentifiers()
697
                        ) ||
698
                        $document->isStateChange() &&
699
                        $document->getState() === DocumentWorkflow::STATE_REGISTERED_NONE
700
                    )
701
                ) {
702
                    $users[$user->getUid()] = $user;
703
                }
704
            }
705
        }
706
707
        return $users;
708
    }
709
}
710
711