PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.4.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.4.01
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / app / Services / Libs / EmailComposer.php

EmailComposer.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.4.01, at app/Services/Libs/EmailComposer.php

222 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Services\Libs;
4
5 use FluentCommunity\App\App;
6 use FluentCommunity\App\Functions\Utility;
7 use FluentCommunity\App\Services\Helper;
8 use FluentCommunity\Framework\Support\Arr;
9
10 class EmailComposer
11 {
12 private $blocks = [];
13
14 private $headingBlocks = [];
15
16 private $footerBlocks = [];
17
18 private $logo = '';
19
20 public function addBlock($type, $content, $options = [])
21 {
22 if ($content) {
23 $this->blocks[] = [
24 'type' => $type,
25 'content' => $content,
26 'options' => $options
27 ];
28 }
29
30 return $this;
31 }
32
33 public function addHeadingBlock($type, $content, $options = [])
34 {
35 $this->headingBlocks[] = [
36 'type' => $type,
37 'content' => $content,
38 'options' => $options
39 ];
40
41 return $this;
42 }
43
44 private function compileBody()
45 {
46 $html = '';
47
48 foreach ($this->blocks as $block) {
49 $html .= $this->compileBlock($block);
50 }
51
52 return $html;
53 }
54
55 private function complileHeadingBlocks()
56 {
57 $html = '';
58
59 foreach ($this->headingBlocks as $block) {
60 $html .= $this->compileBlock($block);
61 }
62
63 return $html;
64 }
65
66 public function compileBlock($block)
67 {
68 if ($block['type'] == 'paragraph') {
69 return '<p style="font-family: Arial, sans-serif; line-height: 1.6; font-size: 16px; font-weight: normal; margin: 0; margin-bottom: 10px;">' . $block['content'] . '</p>';
70 }
71
72 if ($block['type'] == 'heading') {
73 return '<h2 style="font-family: Arial, sans-serif; font-size: 20px; font-weight: bold; margin: 0; margin-bottom: 10px;">' . $block['content'] . '</h2>';
74 }
75
76 if ($block['type'] == 'button') {
77 return (string)App::make('view')->make('email.Default._button', [
78 'link' => Arr::get($block['options'], 'link'),
79 'btnText' => $block['content']
80 ]);
81 }
82
83 if ($block['type'] == 'boxed_content') {
84 return (string)App::make('view')->make('email.Default._user_box_content', [
85 'user_name' => $block['options']['user']->display_name,
86 'user_avatar' => !(empty($block['options']['user']->xprofile->avatar)) ? $block['options']['user']->xprofile->avatar : $block['options']['user']->photo,
87 'content' => $block['content'],
88 'permalink' => $block['options']['permalink'],
89 'post_content' => $block['options']['post_content']
90 ]);
91 }
92
93 if ($block['type'] == 'post_boxed_content') {
94 return (string)App::make('view')->make('email.Default._user_post_content', [
95 'user_name' => $block['options']['user']->display_name,
96 'user_avatar' => !(empty($block['options']['user']->xprofile->avatar)) ? $block['options']['user']->xprofile->avatar : $block['options']['user']->photo,
97 'content' => $block['content'],
98 'title' => Arr::get($block['options'], 'title'),
99 'permalink' => $block['options']['permalink'],
100 'space_name' => $block['options']['space_name']
101 ]);
102 }
103
104 if ($block['type'] == 'html_content') {
105 return (string)$block['content'];
106 }
107
108 return $block['content'];
109 }
110
111 public function setLogo($logo)
112 {
113 if ($logo) {
114 $this->logo = $logo;
115 }
116
117 return $this;
118 }
119
120 public function setDefaultLogo()
121 {
122 $emailSettings = Utility::getEmailNotificationSettings();
123 if (!empty($emailSettings['logo'])) {
124 $this->logo = $emailSettings['logo'];
125 }
126
127 return $this;
128 }
129
130 public function addFooterLine($type, $line)
131 {
132 $this->footerBlocks[] = [
133 'type' => $type,
134 'content' => $line
135 ];
136
137 return $this;
138 }
139
140 public function setDefaultFooter($withPlaceHolder = true)
141 {
142 $settings = Utility::getEmailNotificationSettings();
143 $footerTextHtml = Arr::get($settings, 'email_footer_rendered', '');
144
145 if (!$footerTextHtml) {
146 if ($settings['disable_powered_by'] == 'no') {
147 $utmParams = ['utm_campaign' => 'email', 'utm_source' => 'footer', 'utm_medium' => 'email'];
148 $poweredBy = '<a href="' . Utility::getProductUrl(true, $utmParams) . '" target="_blank" style="font-size: 12px; margin: 15px 0; display: block; text-decoration: none;color: #9a9ea6;">Powered by FluentCommunity</a>';
149 $this->addFooterLine('paragraph', $poweredBy);
150 }
151 return $this;
152 }
153
154 $generalSettings = Helper::generalSettings();
155
156 $replaces = [
157 '{{site_name_with_url}}' => '<a target="_blank" href="' . Helper::baseUrl('/') . '">' . Arr::get($generalSettings, 'site_title') . '</a>',
158 '{{site_name}}' => Arr::get($generalSettings, 'site_title')
159 ];
160
161 $footerTextHtml = str_replace(array_keys($replaces), array_values($replaces), $footerTextHtml);
162
163 // find pattern like this: {{manage_email_notification_url|Manage Your Email Notifications Preference}}
164 $footerTextHtml = preg_replace_callback('/{{(.*?)\|(.*?)}}/', function ($matches) use ($withPlaceHolder) {
165 $match1 = $matches[1];
166 if ($match1 != 'manage_email_notification_url') {
167 return $matches[0];
168 }
169
170 if ($withPlaceHolder) {
171 $url = '##email_notification_url##';
172 } else {
173 $url = Helper::baseUrl('fcom_route?route=user_notification_settings&auth=yes');
174 }
175
176 $text = $matches[2];
177 return '<a target="_blank" rel="noopener noreferrer" href="' . $url . '">' . $text . '</a>';
178 }, $footerTextHtml);
179
180 $this->addFooterLine('paragraph', $footerTextHtml);
181
182 if ($settings['disable_powered_by'] == 'no') {
183 $utmParams = ['utm_campaign' => 'email', 'utm_source' => 'footer', 'utm_medium' => 'email'];
184 $poweredBy = '<p style="margin-top: 10px;" class="powered_by"><a href="' . Utility::getProductUrl(true, $utmParams) . '" target="_blank" style="font-size: 12px; margin: 15px 0; text-decoration: none;color: #9a9ea6; display: block;">Powered by FluentCommunity</a></p>';
185 $this->addFooterLine('paragraph', $poweredBy);
186 }
187
188 return $this;
189 }
190
191 public function getHtml()
192 {
193 $generalSettings = Helper::generalSettings();
194
195 $data = [
196 'logo' => [],
197 'bodyContent' => $this->compileBody()
198 ];
199
200 if ($this->logo) {
201 $data['logo'] = [
202 'url' => $this->logo,
203 'alt' => Arr::get($generalSettings, 'site_title')
204 ];
205 }
206
207 $footerLines = [];
208 foreach ($this->footerBlocks as $footerBlock) {
209 $footerLines[] = $footerBlock['content'];
210 }
211
212 $data['footerLines'] = $footerLines;
213
214 if ($this->headingBlocks) {
215 $data['headingContent'] = $this->complileHeadingBlocks();
216 }
217
218 return (string)App::make('view')->make('email.template', $data);
219 }
220
221 }
222