]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/SystemMailBuilder.php
Extract System emails from enotify
[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 Friendica\Util\Emailer;
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)
30         {
31                 parent::__construct($l10n, $baseUrl, $config);
32
33                 $siteName = $this->config->get('config', 'sitename');
34
35                 if ($this->config->get('config', 'admin_name')) {
36                         $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
37                 } else {
38                         $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
39                 }
40         }
41
42         /**
43          * Adds a message
44          *
45          * @param string      $subject  The subject of the email
46          * @param string      $preamble The preamble of the email
47          * @param string|null $body     The body of the email (if not set, the preamble will get used as body)
48          *
49          * @return static
50          */
51         public function withMessage(string $subject, string $preamble, string $body = null)
52         {
53                 if (!isset($body)) {
54                         $body = $preamble;
55                 }
56
57                 $this->subject  = $subject;
58                 $this->preamble = $preamble;
59                 $this->body     = $body;
60
61                 return $this;
62         }
63
64         /**
65          * {@inheritDoc}
66          */
67         protected function getSubject()
68         {
69                 return $this->subject;
70         }
71
72         /**
73          * {@inheritDoc}
74          *
75          * @throws InternalServerErrorException
76          * @throws Exception
77          */
78         protected function getHtmlMessage()
79         {
80                 $htmlVersion = BBCode::convert($this->body);
81
82                 // load the template for private message notifications
83                 $tpl = Renderer::getMarkupTemplate('email/system/html.tpl');
84                 return Renderer::replaceMacros($tpl, [
85                         '$preamble'    => str_replace("\n", "<br>\n", $this->preamble),
86                         '$thanks'      => $this->l10n->t('thanks'),
87                         '$site_admin'  => $this->siteAdmin,
88                         '$htmlversion' => $htmlVersion,
89                 ]);
90         }
91
92         /**
93          * {@inheritDoc}
94          *
95          * @throws Exception
96          */
97         protected function getPlaintextMessage()
98         {
99                 $textVersion = BBCode::toPlaintext($this->body);
100
101                 // load the template for private message notifications
102                 $tpl = Renderer::getMarkupTemplate('email/system/text.tpl');
103                 return Renderer::replaceMacros($tpl, [
104                         '$preamble'    => $this->preamble,
105                         '$thanks'      => $this->l10n->t('thanks'),
106                         '$site_admin'  => $this->siteAdmin,
107                         '$textversion' => $textVersion,
108                 ]);
109         }
110 }