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