Passed
Pull Request — master (#195)
by
unknown
09:41 queued 02:26
created

Notifier::sendMail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
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
     * @inject
33
     */
34
    protected $clientRepository = null;
35
36
    /**
37
     * documentTypeRepository
38
     *
39
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
40
     * @inject
41
     */
42
    protected $documentTypeRepository = null;
43
44
    /**
45
     * depositLicenseRepository
46
     *
47
     * @var \EWW\Dpf\Domain\Repository\DepositLicenseRepository
48
     * @inject
49
     */
50
    protected $depositLicenseRepository = null;
51
52
    /**
53
     * security
54
     *
55
     * @var \EWW\Dpf\Security\Security
56
     * @inject
57
     */
58
    protected $security = null;
59
60
    public function sendAdminNewSuggestionNotification(\EWW\Dpf\Domain\Model\Document $document) {
61
        try {
62
            /** @var $client \EWW\Dpf\Domain\Model\Client */
63
            $client = $this->clientRepository->findAll()->current();
64
            $clientAdminEmail = $client->getAdminEmail();
65
            $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

65
            /** @scrutinizer ignore-call */ 
66
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
Loading history...
66
67
            $args = $this->getMailMarkerArray($document, $client, $documentType);
68
69
            // Notify client admin
70
            if ($clientAdminEmail) {
71
                $subject = $client->getAdminNewSuggestionSubject();
72
                $body = $client->getAdminNewSuggestionBody();
73
                $mailType = 'text/html';
74
75
                if (empty($subject)) {
76
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newSuggestion.admin.subject', 'dpf');
77
                }
78
79
                if (empty($body)) {
80
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newSuggestion.admin.body', 'dpf');
81
                    $mailType = 'text/plain';
82
                }
83
84
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
85
86
            }
87
88
        } catch (\Exception $e) {
89
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
90
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
91
92
            $logger->log(
93
                LogLevel::ERROR, "sendAdminNewSuggestionNotification failed",
94
                array(
95
                    'document' => $document
96
                )
97
            );
98
        }
99
    }
100
101
102
    public function sendAdminEmbargoExpiredNotification(\EWW\Dpf\Domain\Model\Document $document) {
103
        try {
104
            /** @var $client \EWW\Dpf\Domain\Model\Client */
105
            $client = $this->clientRepository->findAll()->current();
106
            $clientAdminEmail = $client->getAdminEmail();
107
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
108
109
            $args = $this->getMailMarkerArray($document, $client, $documentType);
110
111
            // Notify client admin
112
            if ($clientAdminEmail) {
113
                $subject = $client->getAdminEmbargoSubject();
114
                $body = $client->getAdminEmbargoBody();
115
                $mailType = 'text/html';
116
117
                if (empty($subject)) {
118
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.embargoExpired.admin.subject', 'dpf');
119
                }
120
121
                if (empty($body)) {
122
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.embargoExpired.admin.body', 'dpf');
123
                    $mailType = 'text/plain';
124
                }
125
126
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
127
128
            }
129
130
        } catch (\Exception $e) {
131
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
132
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
133
134
            $logger->log(
135
                LogLevel::ERROR, "sendAdminEmbargoExpiredNotification failed",
136
                array(
137
                    'document' => $document
138
                )
139
            );
140
        }
141
    }
142
143
    public function getMailMarkerArray(Document $document, $client, $documentType, $reason = "") {
144
145
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
146
147
        /** @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager */
148
        $configurationManager = $objectManager->get('TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface');
149
        $settings = $configurationManager->getConfiguration(
150
            \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT,
151
            'dpf',
152
            'backoffice'
153
        );
154
155
        $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...
156
        $args['###PROCESS_NUMBER###'] = $document->getProcessNumber();
157
158
        $args['###DOCUMENT_IDENTIFIER###'] = $document->getObjectIdentifier();
159
160
        if ($documentType) {
161
            $args['###DOCUMENT_TYPE###'] = $documentType->getDisplayName();
162
        } else {
163
            $args['###DOCUMENT_TYPE###'] = '';
164
        }
165
166
        $args['###TITLE###'] = $document->getTitle();
167
168
        $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

168
        $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
169
        $args['###AUTHOR###'] = $author['name'];
170
171
        $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
172
        $args['###SUBMITTER_NAME###'] = $internalFormat->getSubmitterName();
173
        $args['###SUBMITTER_EMAIL###'] = $internalFormat->getSubmitterEmail();
174
        $args['###SUBMITTER_NOTICE###'] = $internalFormat->getSubmitterNotice();
175
176
        $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
177
        $args['###URN###'] = $internalFormat->getQucosaUrn();
178
        $args['###URL###'] = 'http://nbn-resolving.de/' . $internalFormat->getQucosaUrn();
0 ignored issues
show
Bug introduced by
Are you sure $internalFormat->getQucosaUrn() 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

178
        $args['###URL###'] = 'http://nbn-resolving.de/' . /** @scrutinizer ignore-type */ $internalFormat->getQucosaUrn();
Loading history...
179
180
        $args['###REASON###'] = $reason;
181
182
        $host = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
183
        $backofficePageId = $settings['plugin.']['tx_dpf.']['settings.']['backofficePluginPage'];
184
185
        /** @var \EWW\Dpf\Domain\Model\DepositLicense $depositLicense */
186
        $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

186
        /** @scrutinizer ignore-call */ 
187
        $depositLicense = $this->depositLicenseRepository->findOneByUri($document->getDepositLicense());
Loading history...
187
        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...
188
            $args['###LICENSE_URI###'] = $depositLicense->getUri();
189
            $args['###LICENSE_TEXT###'] = $depositLicense->getText();
190
        }
191
192
        $args['###LICENSE_USERNAME###'] = $this->security->getUser()->getUsername();
193
194
        if ($document->isSuggestion()) {
195
            $detailUrl = '<a href="' . $host . '/index.php?id=' . $backofficePageId;
196
            $detailUrl .= '&tx_dpf_backoffice[document]=' . $document->getUid();
197
            $detailUrl .= '&tx_dpf_backoffice[action]=showSuggestionDetails';
198
            $detailUrl .= '&tx_dpf_backoffice[controller]=Document">Link zum Änderungsvorschlag</a>';
199
        } else {
200
            $documentIdentifier = $document->getProcessNumber();
201
            if (empty($documentIdentifier)) {
202
                $documentIdentifier = $document->getDocumentIdentifier();
203
            }
204
            $detailUrl = '<a href="' . $host . '/index.php?id=' . $backofficePageId;
205
            $detailUrl .= '&tx_dpf_backoffice[document]=' . $documentIdentifier;
206
            $detailUrl .= '&tx_dpf_backoffice[action]=showDetails';
207
            $detailUrl .= '&tx_dpf_backoffice[controller]=Document">Link zum Dokument</a>';
208
        }
209
210
        $args['###DETAIL_URL###'] = $detailUrl;
211
212
        $args['###HAS_FILES###'] = 'Metadata only';
213
214
        if ($document->getFileData()) {
215
            $args['###HAS_FILES###'] = 'Attachment';
216
            $fileList = [];
217
            foreach ($document->getFile() as $file) {
218
                if (!$file->isFileGroupDeleted()) {
219
                    $fileList[] = $file->getTitle();
220
                }
221
            }
222
            $args['###FILE_LIST###'] .= implode(", ", $fileList);
223
        }
224
225
        return $args;
226
    }
227
228
    public function sendSuggestionAcceptNotification(\EWW\Dpf\Domain\Model\Document $document) {
229
230
        try {
231
            /** @var Client $client */
232
            $client = $this->clientRepository->findAll()->current();
233
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
234
235
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
236
237
            $args = $this->getMailMarkerArray($document, $client, $documentType);
238
239
            // Active messaging: Suggestion accept
240
            if ($client->getActiveMessagingSuggestionAcceptUrl()) {
241
                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

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

580
            $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
581
582
            $args = $this->getMailMarkerArray($document, $client, $documentType);
583
584
            // Notify client admin
585
            /** @var FrontendUser $recipient */
586
            foreach ($recipients as $recipient) {
587
588
                if ($recipient->getEmail()) {
589
590
                    $subject = $client->getMypublicationsUpdateNotificationSubject();
591
                    $body = $client->getMypublicationsUpdateNotificationBody();
592
                    $mailType = 'text/html';
593
594
                    if (empty($subject)) {
595
                        $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.updatePublication.mypublications.subject',
596
                            'dpf');
597
                    }
598
599
                    if (empty($body)) {
600
                        $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.updatePublication.mypublications.body',
601
                            'dpf');
602
                        $mailType = 'text/plain';
603
                    }
604
605
                    $this->sendMail($recipient->getEmail(), $subject, $body, $args, $mailType);
606
                }
607
            }
608
609
        } catch (\Exception $e) {
610
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
611
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
612
613
            $logger->log(
614
                LogLevel::ERROR, "sendRegisterNotification failed",
615
                array(
616
                    'document' => $document
617
                )
618
            );
619
        }
620
621
    }
622
623
624
    /**
625
     * @param \EWW\Dpf\Domain\Model\Document $document
626
     * @param array $recipients
627
     */
628
    public function sendMyPublicationNewNotification(\EWW\Dpf\Domain\Model\Document $document, $recipients)
629
    {
630
631
        try {
632
            /** @var Client $client */
633
            $client = $this->clientRepository->findAll()->current();
634
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
635
            $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

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