]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/SystemMailBuilder.php
Merge pull request #8240 from annando/a11y-3
[friendica.git] / src / Util / EMailer / SystemMailBuilder.php
1 <?php
2
3 namespace Friendica\Util\EMailer;
4
5 use Exception;
6 use Friendica\App\BaseURL;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\Config\IConfig;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Network\HTTPException\InternalServerErrorException;
12 use Psr\Log\LoggerInterface;
13
14 /**
15  * Builder for system-wide emails without any dependency to concrete entities (like items, activities, ..)
16  */
17 class SystemMailBuilder extends MailBuilder
18 {
19         /** @var string */
20         protected $subject;
21         /** @var string */
22         protected $preamble;
23         /** @var string */
24         protected $body;
25
26         /** @var string */
27         protected $siteAdmin;
28
29         public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, LoggerInterface $logger,
30                                     string $siteEmailAddress, string $siteName)
31         {
32                 parent::__construct($l10n, $baseUrl, $config, $logger);
33
34                 if ($this->config->get('config', 'admin_name')) {
35                         $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
36                 } else {
37                         $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
38                 }
39
40                 // Set the system wide site address/name as sender (default for system mails)
41                 $this->withSender($siteName, $siteEmailAddress, $siteEmailAddress);
42         }
43
44         /**
45          * Adds a message
46          *
47          * @param string      $subject  The subject of the email
48          * @param string      $preamble The preamble of the email
49          * @param string|null $body     The body of the email (if not set, the preamble will get used as body)
50          *
51          * @return static
52          */
53         public function withMessage(string $subject, string $preamble, string $body = null)
54         {
55                 if (!isset($body)) {
56                         $body = $preamble;
57                 }
58
59                 $this->subject  = $subject;
60                 $this->preamble = $preamble;
61                 $this->body     = $body;
62
63                 return $this;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         protected function getSubject()
70         {
71                 return $this->subject;
72         }
73
74         /**
75          * {@inheritDoc}
76          *
77          * @throws InternalServerErrorException
78          * @throws Exception
79          */
80         protected function getHtmlMessage()
81         {
82                 $htmlVersion = BBCode::convert($this->body);
83
84                 // load the template for private message notifications
85                 $tpl = Renderer::getMarkupTemplate('email/system/html.tpl');
86                 return Renderer::replaceMacros($tpl, [
87                         '$preamble'    => str_replace("\n", "<br>\n", $this->preamble),
88                         '$thanks'      => $this->l10n->t('thanks'),
89                         '$site_admin'  => $this->siteAdmin,
90                         '$htmlversion' => $htmlVersion,
91                 ]);
92         }
93
94         /**
95          * {@inheritDoc}
96          *
97          * @throws Exception
98          */
99         protected function getPlaintextMessage()
100         {
101                 $textVersion = BBCode::toPlaintext($this->body);
102
103                 // load the template for private message notifications
104                 $tpl = Renderer::getMarkupTemplate('email/system/text.tpl');
105                 return Renderer::replaceMacros($tpl, [
106                         '$preamble'    => $this->preamble,
107                         '$thanks'      => $this->l10n->t('thanks'),
108                         '$site_admin'  => $this->siteAdmin,
109                         '$textversion' => $textVersion,
110                 ]);
111         }
112 }