Notifier::sendAdminNewSuggestionNotification()   A
last analyzed

Complexity

Conditions 5
Paths 18

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
dl 0
loc 36
rs 9.2728
c 1
b 0
f 1
cc 5
nc 18
nop 1
1
<?php
2
namespace EWW\Dpf\Services\Email;
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\Document;
18
use \TYPO3\CMS\Core\Log\LogLevel;
19
use \TYPO3\CMS\Core\Log\LogManager;
20
use \TYPO3\CMS\Core\Utility\GeneralUtility;
21
use EWW\Dpf\Domain\Model\FrontendUser;
22
use EWW\Dpf\Domain\Model\Client;
23
use \Httpful\Request;
24
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
25
26
class Notifier
27
{
28
    /**
29
     * clientRepository
30
     *
31
     * @var \EWW\Dpf\Domain\Repository\ClientRepository
32
     * @TYPO3\CMS\Extbase\Annotation\Inject
33
     */
34
    protected $clientRepository = null;
35
36
    /**
37
     * documentTypeRepository
38
     *
39
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
40
     * @TYPO3\CMS\Extbase\Annotation\Inject
41
     */
42
    protected $documentTypeRepository = null;
43
44
    /**
45
     * depositLicenseRepository
46
     *
47
     * @var \EWW\Dpf\Domain\Repository\DepositLicenseRepository
48
     * @TYPO3\CMS\Extbase\Annotation\Inject
49
     */
50
    protected $depositLicenseRepository = null;
51
52
    /**
53
     * security
54
     *
55
     * @var \EWW\Dpf\Security\Security
56
     * @TYPO3\CMS\Extbase\Annotation\Inject
57
     */
58
    protected $security = null;
59
60
    /**
61
     * clientConfigurationManager
62
     *
63
     * @var \EWW\Dpf\Configuration\ClientConfigurationManager
64
     * @TYPO3\CMS\Extbase\Annotation\Inject
65
     */
66
    protected $clientConfigurationManager;
67
68
69
    public function sendAdminNewSuggestionNotification(\EWW\Dpf\Domain\Model\Document $document) {
70
        try {
71
            /** @var $client \EWW\Dpf\Domain\Model\Client */
72
            $client = $this->clientRepository->findAll()->current();
73
            $clientAdminEmail = $client->getAdminEmail();
74
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
0 ignored issues
show
Bug introduced by
The method findOneByUid() does not exist on EWW\Dpf\Domain\Repository\DocumentTypeRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

74
            /** @scrutinizer ignore-call */ 
75
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
Loading history...
75
76
            $args = $this->getMailMarkerArray($document, $client, $documentType);
77
78
            // Notify client admin
79
            if ($clientAdminEmail) {
80
                $subject = $client->getAdminNewSuggestionSubject();
81
                $body = $client->getAdminNewSuggestionBody();
82
                $mailType = 'text/html';
83
84
                if (empty($subject)) {
85
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newSuggestion.admin.subject', 'dpf');
86
                }
87
88
                if (empty($body)) {
89
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newSuggestion.admin.body', 'dpf');
90
                    $mailType = 'text/plain';
91
                }
92
93
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
94
95
            }
96
97
        } catch (\Exception $e) {
98
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
99
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
100
101
            $logger->log(
102
                LogLevel::ERROR, "sendAdminNewSuggestionNotification failed",
103
                array(
104
                    'document' => $document
105
                )
106
            );
107
        }
108
    }
109
110
111
    public function sendAdminEmbargoExpiredNotification(\EWW\Dpf\Domain\Model\Document $document) {
112
        try {
113
            /** @var $client \EWW\Dpf\Domain\Model\Client */
114
            $client = $this->clientRepository->findAll()->current();
115
            $clientAdminEmail = $client->getAdminEmail();
116
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
117
118
            $args = $this->getMailMarkerArray($document, $client, $documentType);
119
120
            // Notify client admin
121
            if ($clientAdminEmail) {
122
                $subject = $client->getAdminEmbargoSubject();
123
                $body = $client->getAdminEmbargoBody();
124
                $mailType = 'text/html';
125
126
                if (empty($subject)) {
127
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.embargoExpired.admin.subject', 'dpf');
128
                }
129
130
                if (empty($body)) {
131
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.embargoExpired.admin.body', 'dpf');
132
                    $mailType = 'text/plain';
133
                }
134
135
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
136
137
            }
138
139
        } catch (\Exception $e) {
140
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
141
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
142
143
            $logger->log(
144
                LogLevel::ERROR, "sendAdminEmbargoExpiredNotification failed",
145
                array(
146
                    'document' => $document
147
                )
148
            );
149
        }
150
    }
151
152
    public function getMailMarkerArray(Document $document, $client, $documentType, $reason = "") {
153
154
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
155
156
        /** @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager */
157
        $configurationManager = $objectManager->get('TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface');
158
        $settings = $configurationManager->getConfiguration(
159
            \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT,
160
            'dpf',
161
            'backoffice'
162
        );
163
164
        $args['###CLIENT###'] = $client->getClient();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args = array(); before regardless.
Loading history...
165
        $args['###PROCESS_NUMBER###'] = $document->getProcessNumber();
166
167
        $args['###DOCUMENT_IDENTIFIER###'] = $document->getObjectIdentifier();
168
169
        if ($documentType) {
170
            $args['###DOCUMENT_TYPE###'] = $documentType->getDisplayName();
171
        } else {
172
            $args['###DOCUMENT_TYPE###'] = '';
173
        }
174
175
        $args['###TITLE###'] = $document->getTitle();
176
177
        $author = array_shift($document->getAuthors());
0 ignored issues
show
Bug introduced by
$document->getAuthors() cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

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

177
        $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
178
        $args['###AUTHOR###'] = $author['name'];
179
180
        $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
181
        $args['###SUBMITTER_NAME###'] = $internalFormat->getSubmitterName();
182
        $args['###SUBMITTER_EMAIL###'] = $internalFormat->getSubmitterEmail();
183
        $args['###SUBMITTER_NOTICE###'] = $internalFormat->getSubmitterNotice();
184
185
        $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
186
        $args['###URN###'] = $internalFormat->getPrimaryUrn();
187
        $args['###URL###'] = 'http://nbn-resolving.de/' . $internalFormat->getPrimaryUrn();
0 ignored issues
show
Bug introduced by
Are you sure $internalFormat->getPrimaryUrn() of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

187
        $args['###URL###'] = 'http://nbn-resolving.de/' . /** @scrutinizer ignore-type */ $internalFormat->getPrimaryUrn();
Loading history...
188
189
        $args['###REASON###'] = $reason;
190
191
        $host = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
192
        $backofficePageId = $settings['plugin.']['tx_dpf.']['settings.']['backofficePluginPage'];
193
194
        /** @var \EWW\Dpf\Domain\Model\DepositLicense $depositLicense */
195
        $depositLicense = $this->depositLicenseRepository->findOneByUri($document->getDepositLicense());
0 ignored issues
show
Bug introduced by
The method findOneByUri() does not exist on EWW\Dpf\Domain\Repository\DepositLicenseRepository. 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

195
        /** @scrutinizer ignore-call */ 
196
        $depositLicense = $this->depositLicenseRepository->findOneByUri($document->getDepositLicense());
Loading history...
196
        if ($depositLicense instanceof \EWW\Dpf\Domain\Model\DepositLicense) {
0 ignored issues
show
introduced by
$depositLicense is always a sub-type of EWW\Dpf\Domain\Model\DepositLicense.
Loading history...
197
            $args['###LICENSE_URI###'] = $depositLicense->getUri();
198
            $args['###LICENSE_TEXT###'] = $depositLicense->getText();
199
        }
200
201
        $args['###LICENSE_USERNAME###'] = $this->security->getUsername();
202
203
        if ($document->isSuggestion()) {
204
            $detailUrl = '<a href="' . $host . '/index.php?id=' . $backofficePageId;
205
            $detailUrl .= '&tx_dpf_backoffice[document]=' . $document->getUid();
206
            $detailUrl .= '&tx_dpf_backoffice[action]=showSuggestionDetails';
207
            $detailUrl .= '&tx_dpf_backoffice[controller]=Document">Link zum Änderungsvorschlag</a>';
208
        } else {
209
            $documentIdentifier = $document->getProcessNumber();
210
            if (empty($documentIdentifier)) {
211
                $documentIdentifier = $document->getDocumentIdentifier();
212
            }
213
            $detailUrl = '<a href="' . $host . '/index.php?id=' . $backofficePageId;
214
            $detailUrl .= '&tx_dpf_backoffice[document]=' . $documentIdentifier;
215
            $detailUrl .= '&tx_dpf_backoffice[action]=showDetails';
216
            $detailUrl .= '&tx_dpf_backoffice[controller]=Document">Link zum Dokument</a>';
217
        }
218
219
        $args['###DETAIL_URL###'] = $detailUrl;
220
221
        $args['###HAS_FILES###'] = 'Metadata only';
222
223
        if ($document->hasFiles()) {
224
            $args['###HAS_FILES###'] = 'Attachment';
225
            $fileList = [];
226
            foreach ($document->getFile() as $file) {
227
                if (!$file->isFileGroupDeleted()) {
228
                    $fileList[] = $file->getTitle();
229
                }
230
            }
231
            $args['###FILE_LIST###'] .= implode(", ", $fileList);
232
        }
233
234
        return $args;
235
    }
236
237
    public function sendSuggestionAcceptNotification(\EWW\Dpf\Domain\Model\Document $document) {
238
239
        try {
240
            /** @var Client $client */
241
            $client = $this->clientRepository->findAll()->current();
242
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
243
244
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
245
246
            $args = $this->getMailMarkerArray($document, $client, $documentType);
247
248
            // Active messaging: Suggestion accept
249
            if ($client->getActiveMessagingSuggestionAcceptUrl()) {
250
                if ($internalFormat->getFisId()) {
0 ignored issues
show
Bug introduced by
The method getFisId() does not exist on EWW\Dpf\Helper\InternalFormat. ( Ignorable by Annotation )

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

250
                if ($internalFormat->/** @scrutinizer ignore-call */ getFisId()) {

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

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

Loading history...
251
                    $request = Request::post($client->getActiveMessagingSuggestionAcceptUrl());
252
                    if ($body = $client->getActiveMessagingSuggestionAcceptUrlBody()) {
253
                        $request->body($this->replaceMarkers($body, $args));
254
                    }
255
                    $request->send();
256
                }
257
            }
258
259
        } catch (\Exception $e) {
260
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
261
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
262
263
            $logger->log(
264
                LogLevel::ERROR, "sendSuggestionAcceptNotification failed",
265
                array(
266
                    'document' => $document
267
                )
268
            );
269
        }
270
    }
271
272
    /**
273
     * @param Document $document
274
     * @param string $reason
275
     */
276
    public function sendSuggestionDeclineNotification(\EWW\Dpf\Domain\Model\Document $document, $reason = "") {
277
278
        try {
279
            /** @var Client $client */
280
            $client = $this->clientRepository->findAll()->current();
281
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
282
283
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
284
285
            $args = $this->getMailMarkerArray($document, $client, $documentType, $reason);
286
287
            // Active messaging: Suggestion accept
288
            if ($client->getActiveMessagingSuggestionDeclineUrl()) {
289
                if ($internalFormat->getFisId()) {
290
                    $request = Request::post($client->getActiveMessagingSuggestionDeclineUrl());
291
                    if ($body = $client->getActiveMessagingSuggestionDeclineUrlBody()) {
292
                        $request->body($this->replaceMarkers($body, $args));
293
                    }
294
                    $request->send();
295
                }
296
            }
297
298
        } catch (\Exception $e) {
299
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
300
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
301
302
            $logger->log(
303
                LogLevel::ERROR, "sendSuggestionDeclineNotification failed",
304
                array(
305
                    'document' => $document
306
                )
307
            );
308
        }
309
    }
310
311
    public function sendChangedDocumentNotification(\EWW\Dpf\Domain\Model\Document $document, $addedFisIdOnly = false) {
312
313
        try {
314
            /** @var Client $client */
315
            $client = $this->clientRepository->findAll()->current();
316
317
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
318
319
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
320
321
            $args = $this->getMailMarkerArray($document, $client, $documentType);
322
323
            // Active messaging: Suggestion accept
324
            if (!$addedFisIdOnly && $client->getActiveMessagingChangedDocumentUrl()) {
325
                if ($internalFormat->getFisId()) {
326
                    $request = Request::post($client->getActiveMessagingChangedDocumentUrl());
327
                    if ($body = $client->getActiveMessagingChangedDocumentUrlBody()) {
328
                        $request->body($this->replaceMarkers($body,$args));
329
                    }
330
                    $request->send();
331
                }
332
            }
333
334
        } catch (\Exception $e) {
335
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
336
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
337
338
            $logger->log(
339
                LogLevel::ERROR, "sendChangedDocumentNotification failed",
340
                array(
341
                    'document' => $document
342
                )
343
            );
344
        }
345
    }
346
347
348
    public function sendReleasePublishNotification(\EWW\Dpf\Domain\Model\Document $document)
349
    {
350
        try {
351
            /** @var Client $client */
352
            $client = $this->clientRepository->findAll()->current();
353
354
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
355
356
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
357
358
            $args = $this->getMailMarkerArray($document, $client, $documentType);
359
360
            // Active messaging: New document (Release publish)
361
            if ($client->getActiveMessagingNewDocumentUrl()) {
362
                $fisId = $internalFormat->getFisId();
363
                if (empty($fisId)) {
364
                    $request = Request::post($client->getActiveMessagingNewDocumentUrl());
365
                    if ($body = $client->getActiveMessagingNewDocumentUrlBody()) {
366
                        $request->body($this->replaceMarkers($body, $args));
367
                    }
368
                    $request->send();
369
                }
370
            }
371
372
        } catch (\Exception $e) {
373
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
374
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
375
376
            $logger->log(
377
                LogLevel::ERROR, "sendReleasePublishNotification failed",
378
                array(
379
                    'document' => $document
380
                )
381
            );
382
        }
383
    }
384
385
386
    public function sendNewDocumentNotification(\EWW\Dpf\Domain\Model\Document $document)
387
    {
388
389
        try {
390
            /** @var Client $client */
391
            $client = $this->clientRepository->findAll()->current();
392
            $clientAdminEmail = $client->getAdminEmail();
393
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
394
            $submitterEmail = $internalFormat->getSubmitterEmail();
395
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
396
            $authors = $document->getAuthors();
0 ignored issues
show
Unused Code introduced by
The assignment to $authors is dead and can be removed.
Loading history...
397
398
            $args = $this->getMailMarkerArray($document, $client, $documentType);
399
400
            // Notify client admin
401
            if ($clientAdminEmail) {
402
                $subject = $client->getAdminNewDocumentNotificationSubject();
403
                $body = $client->getAdminNewDocumentNotificationBody();
404
                $mailType = 'text/html';
405
406
                if (empty($subject)) {
407
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.subject', 'dpf');
408
                }
409
410
                if (empty($body)) {
411
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.body', 'dpf');
412
                    $mailType = 'text/plain';
413
                }
414
415
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
416
417
            }
418
419
420
            // Notify submitter
421
            if ($submitterEmail) {
422
                $subject = $client->getSubmitterNewDocumentNotificationSubject();
423
                $body = $client->getSubmitterNewDocumentNotificationBody();
424
                $mailType = 'text/html';
425
426
                if (empty($subject)) {
427
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.subject', 'dpf');
428
                }
429
430
                if (empty($body)) {
431
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.body', 'dpf');
432
                    $mailType = 'text/plain';
433
                }
434
435
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
436
            }
437
438
        } catch (\Exception $e) {
439
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
440
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
441
442
            $logger->log(
443
                LogLevel::ERROR, "sendNewDocumentNotification failed",
444
                array(
445
                    'document' => $document
446
                )
447
            );
448
        }
449
450
    }
451
452
    public function sendIngestNotification(\EWW\Dpf\Domain\Model\Document $document)
453
    {
454
455
        try {
456
            $client = $this->clientRepository->findAll()->current();
457
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
458
            $submitterEmail = $internalFormat->getSubmitterEmail();
459
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
460
461
            $args = $this->getMailMarkerArray($document, $client, $documentType);
462
463
            // Notify submitter
464
            if ($submitterEmail) {
465
                $subject = $client->getSubmitterIngestNotificationSubject();
466
                $body = $client->getSubmitterIngestNotificationBody();
467
                $mailType = 'text/html';
468
469
                if (empty($subject)) {
470
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.subject', 'dpf');
471
                }
472
473
                if (empty($body)) {
474
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.body', 'dpf');
475
                    $mailType = 'text/plain';
476
                }
477
478
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
479
            }
480
        } catch (\Exception $e) {
481
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
482
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
483
484
            $logger->log(
485
                LogLevel::ERROR, "sendIngestNotification failed",
486
                array(
487
                    'document' => $document
488
                )
489
            );
490
        }
491
492
    }
493
494
    public function sendEmbargoNotification(\EWW\Dpf\Domain\Model\Document $document) {
495
        try {
496
            $client = $this->clientRepository->findAllByPid($document->getPid())->current();
497
            $clientAdminEmail = $client->getAdminEmail();
498
499
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
500
501
            $args = $this->getMailMarkerArray($document, $client, $documentType);
502
503
            // Notify client admin
504
            if ($clientAdminEmail) {
505
                $subject = $client->getAdminEmbargoSubject();
506
                $body = $client->getAdminEmbargoBody();
507
                $mailType = 'text/html';
508
509
                if (empty($subject)) {
510
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.document.embargo.admin.subject', 'dpf');
511
                }
512
513
                if (empty($body)) {
514
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.document.embargo.admin.body', 'dpf');
515
                    $mailType = 'text/plain';
516
                }
517
518
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
519
520
            }
521
522
        } catch (\Exception $e) {
523
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
524
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
525
526
            $logger->log(
527
                LogLevel::ERROR, "sendRegisterNotification failed",
528
                array(
529
                    'document' => $document
530
                )
531
            );
532
        }
533
    }
534
535
    public function sendRegisterNotification(\EWW\Dpf\Domain\Model\Document $document)
536
    {
537
538
        try {
539
            $client = $this->clientRepository->findAll()->current();
540
            $clientAdminEmail = $client->getAdminEmail();
541
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
542
543
            $args = $this->getMailMarkerArray($document, $client, $documentType);
544
545
            // Notify client admin
546
            if ($clientAdminEmail) {
547
                $subject = $client->getAdminRegisterDocumentNotificationSubject();
548
                $body = $client->getAdminRegisterDocumentNotificationBody();
549
                $mailType = 'text/html';
550
551
                if (empty($subject)) {
552
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.subject', 'dpf');
553
                }
554
555
                if (empty($body)) {
556
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.body', 'dpf');
557
                    $mailType = 'text/plain';
558
                }
559
560
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
561
562
            }
563
564
        } catch (\Exception $e) {
565
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
566
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
567
568
            $logger->log(
569
                LogLevel::ERROR, "sendRegisterNotification failed",
570
                array(
571
                    'document' => $document
572
                )
573
            );
574
        }
575
576
    }
577
578
    /**
579
     * @param \EWW\Dpf\Domain\Model\Document $document
580
     * @param array $recipients
581
     */
582
    public function sendMyPublicationUpdateNotification(\EWW\Dpf\Domain\Model\Document $document, $recipients)
583
    {
584
585
        try {
586
            /** @var Client $client */
587
            $client = $this->clientRepository->findAll()->current();
588
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
589
            $author = array_shift($document->getAuthors());
0 ignored issues
show
Unused Code introduced by
The assignment to $author is dead and can be removed.
Loading history...
Bug introduced by
$document->getAuthors() cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

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

589
            $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
590
591
            $args = $this->getMailMarkerArray($document, $client, $documentType);
592
593
            // Notify client admin
594
            /** @var FrontendUser $recipient */
595
            foreach ($recipients as $recipient) {
596
597
                if ($recipient->getEmail()) {
598
599
                    $subject = $client->getMypublicationsUpdateNotificationSubject();
600
                    $body = $client->getMypublicationsUpdateNotificationBody();
601
                    $mailType = 'text/html';
602
603
                    if (empty($subject)) {
604
                        $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.updatePublication.mypublications.subject',
605
                            'dpf');
606
                    }
607
608
                    if (empty($body)) {
609
                        $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.updatePublication.mypublications.body',
610
                            'dpf');
611
                        $mailType = 'text/plain';
612
                    }
613
614
                    $this->sendMail($recipient->getEmail(), $subject, $body, $args, $mailType);
615
                }
616
            }
617
618
        } catch (\Exception $e) {
619
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
620
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
621
622
            $logger->log(
623
                LogLevel::ERROR, "sendRegisterNotification failed",
624
                array(
625
                    'document' => $document
626
                )
627
            );
628
        }
629
630
    }
631
632
633
    /**
634
     * @param \EWW\Dpf\Domain\Model\Document $document
635
     * @param array $recipients
636
     */
637
    public function sendMyPublicationNewNotification(\EWW\Dpf\Domain\Model\Document $document, $recipients)
638
    {
639
640
        try {
641
            /** @var Client $client */
642
            $client = $this->clientRepository->findAll()->current();
643
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
644
            $author = array_shift($document->getAuthors());
0 ignored issues
show
Unused Code introduced by
The assignment to $author is dead and can be removed.
Loading history...
Bug introduced by
$document->getAuthors() cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

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

644
            $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
645
646
            $args = $this->getMailMarkerArray($document, $client, $documentType);
647
648
            // Notify client admin
649
            /** @var FrontendUser $recipient */
650
            foreach ($recipients as $recipient) {
651
652
                if ($recipient->getEmail()) {
653
654
                    $subject = $client->getMypublicationsNewNotificationSubject();
655
                    $body = $client->getMypublicationsNewNotificationBody();
656
                    $mailType = 'text/html';
657
658
                    if (empty($subject)) {
659
                        $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newPublication.mypublications.subject',
660
                            'dpf');
661
                    }
662
663
                    if (empty($body)) {
664
                        $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newPublication.mypublications.body',
665
                            'dpf');
666
                        $mailType = 'text/plain';
667
                    }
668
669
                    $this->sendMail($recipient->getEmail(), $subject, $body, $args, $mailType);
670
                }
671
            }
672
673
        } catch (\Exception $e) {
674
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
675
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
676
677
            $logger->log(
678
                LogLevel::ERROR, "sendRegisterNotification failed",
679
                array(
680
                    'document' => $document
681
                )
682
            );
683
        }
684
685
    }
686
687
    public function sendDepositLicenseNotification(\EWW\Dpf\Domain\Model\Document $document)
688
    {
689
690
        try {
691
            /** @var Client $client */
692
            $client = $this->clientRepository->findAll()->current();
693
            $clientAdminEmail = $client->getAdminEmail();
694
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
695
696
            $args = $this->getMailMarkerArray($document, $client, $documentType);
697
698
            // Notify client admin
699
            if ($clientAdminEmail && $client->isSendAdminDepositLicenseNotification()) {
700
701
                $subject = $client->getAdminDepositLicenseNotificationSubject();
702
                $body = $client->getAdminDepositLicenseNotificationBody();
703
                $mailType = 'text/html';
704
705
                if (empty($subject)) {
706
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.depositLicense.admin.subject', 'dpf');
707
                }
708
709
                if (empty($body)) {
710
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.depositLicense.admin.body', 'dpf');
711
                    $mailType = 'text/plain';
712
                }
713
714
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
715
            }
716
717
        } catch (\Exception $e) {
718
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
719
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
720
721
            $logger->log(
722
                LogLevel::ERROR, "sendDepositLicenseNotification failed",
723
                array(
724
                    'document' => $document
725
                )
726
            );
727
        }
728
729
    }
730
731
    protected function replaceMarkers($message, $args)
732
    {
733
        if (is_array($args)) {
734
            foreach ($args as $key => $value) {
735
                $message = str_replace($key, $value, $message);
736
            }
737
        }
738
        return $message;
739
    }
740
741
742
    protected function sendMail($reveiver, $subject, $body, $args, $mailType)
743
    {
744
        $settings = $this->clientConfigurationManager->getTypoScriptSettings();
745
        $emailReceiver = array();
746
        $emailReceiver[$reveiver] = $reveiver;
747
        $message = (new \TYPO3\CMS\Core\Mail\MailMessage())
748
            ->setFrom(array($settings['noReplyAddress'] => $settings['noReplyAddress']))
749
            ->setTo($emailReceiver)
750
            ->setSubject($this->replaceMarkers($subject,$args))
751
            ->setBody($this->replaceMarkers($body,$args),$mailType);
752
        $message->send();
753
    }
754
755
}
756