]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/NotifyMailBuilder.php
split mailbuilder types
[friendica.git] / src / Util / EMailer / NotifyMailBuilder.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
13 /**
14  * Builder for notification emails (notification, source, links, ...)
15  */
16 class NotifyMailBuilder extends MailBuilder
17 {
18         /** @var string */
19         protected $subject;
20         /** @var string */
21         protected $preamble;
22         /** @var string */
23         protected $body;
24
25         /** @var string */
26         protected $siteAdmin;
27
28         /** @var bool */
29         private $contentAllowed = true;
30         /** @var string */
31         private $title = '';
32         /** @var array Details to print a photo:
33          * - image
34          * - link
35          * - name
36          */
37         private $photo = [
38                 'image' => null,
39                 'link'  => null,
40                 'name'  => null,
41         ];
42         /** @var array HTML/Plain version of the Site Link:
43          * - html
44          * - text
45          */
46         private $siteLink = [
47                 'html' => '',
48                 'text' => '',
49         ];
50         /** @var string The item link */
51         private $itemLink = '';
52
53         public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, string $siteEmailAddress, string $siteName)
54         {
55                 parent::__construct($l10n, $baseUrl, $config);
56
57                 if ($this->config->get('config', 'admin_name')) {
58                         $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
59                 } else {
60                         $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
61                 }
62
63                 // Set the system wide site address/name as sender (default for system mails)
64                 $this->withSender($siteName, $siteEmailAddress, $siteEmailAddress);
65
66                 // check whether sending post content in email notifications is allowed
67                 $this->contentAllowed = $this->config->get('system', 'enotify_no_content');
68         }
69
70         /**
71          * Adds a notification (in fact a more detailed message)
72          *
73          * @param string      $subject
74          * @param string      $preamble
75          * @param string      $title
76          * @param string|null $body
77          *
78          * @return static
79          */
80         public function withNotification(string $subject, string $preamble, string $title, string $body = null)
81         {
82                 if (!isset($body)) {
83                         $body = $preamble;
84                 }
85
86                 $this->title = stripslashes($title);
87                 $this->subject  = $subject;
88                 $this->preamble = $preamble;
89                 $this->body     = $body;
90
91                 return $this;
92         }
93
94         /**
95          * Adds a photo of the source of the notify
96          *
97          * @param string $image The image link to the photo
98          * @param string $link  The link to the source
99          * @param string $name  The name of the source
100          *
101          * @return static
102          */
103         public function withPhoto(string $image, string $link, string $name)
104         {
105                 $this->photo = [
106                         'image' => $image ?? '',
107                         'link'  => $link ?? '',
108                         'name'  => $name ?? '',
109                 ];
110
111                 return $this;
112         }
113
114         /**
115          * Adds a sitelink to the notification
116          *
117          * @param string $text The text version of the site link
118          * @param string $html The html version of the site link
119          *
120          * @return static
121          */
122         public function withSiteLink(string $text, string $html = '')
123         {
124                 $this->siteLink = [
125                         'text' => $text,
126                         'html' => $html,
127                 ];
128
129                 return $this;
130         }
131
132         /**
133          * Adds a link to the item of the notification
134          *
135          * @param string $link The text version of the item link
136          *
137          * @return static
138          */
139         public function withItemLink(string $link)
140         {
141                 $this->itemLink = $link;
142
143                 return $this;
144         }
145
146         /**
147          * {@inheritDoc}
148          */
149         protected function getSubject()
150         {
151                 return $this->subject;
152         }
153
154         /**
155          * {@inheritDoc}
156          *
157          * @throws InternalServerErrorException
158          * @throws Exception
159          */
160         protected function getHtmlMessage()
161         {
162                 $htmlVersion = BBCode::convert($this->body);
163
164                 // load the template for private message notifications
165                 $tpl = Renderer::getMarkupTemplate('email/notify/html.tpl');
166                 return Renderer::replaceMacros($tpl, [
167                         '$preamble'        => str_replace("\n", "<br>\n", $this->preamble),
168                         '$source_name'     => $this->photo['name'],
169                         '$source_link'     => $this->photo['link'],
170                         '$source_photo'    => $this->photo['image'],
171                         '$hsitelink'       => $this->siteLink['html'],
172                         '$hitemlink'       => sprintf('<a href="%s">%s</a>', $this->itemLink, $this->itemLink),
173                         '$thanks'          => $this->l10n->t('thanks'),
174                         '$site_admin'      => $this->siteAdmin,
175                         '$title'           => $this->title,
176                         '$htmlversion'     => $htmlVersion,
177                         '$content_allowed' => $this->contentAllowed,
178                 ]);
179
180         }
181
182         /**
183          * {@inheritDoc}
184          *
185          * @throws Exception
186          */
187         protected function getPlaintextMessage()
188         {
189                 $textVersion = BBCode::toPlaintext($this->body);
190
191                 // load the template for private message notifications
192                 $tpl = Renderer::getMarkupTemplate('email/notify/text.tpl');
193                 return Renderer::replaceMacros($tpl, [
194                         '$preamble'        => $this->preamble,
195                         '$tsitelink'       => $this->siteLink['text'],
196                         '$titemlink'       => $this->itemLink,
197                         '$thanks'          => $this->l10n->t('thanks'),
198                         '$site_admin'      => $this->siteAdmin,
199                         '$title'           => $this->title,
200                         '$textversion'     => $textVersion,
201                         '$content_allowed' => $this->contentAllowed,
202                 ]);
203         }
204 }