|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\EmailTemplates\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SilverStripe\Admin\ModelAdmin; |
|
7
|
|
|
use SilverStripe\Control\Director; |
|
8
|
|
|
use SilverStripe\View\Requirements; |
|
9
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
10
|
|
|
use LeKoala\EmailTemplates\Models\Emailing; |
|
11
|
|
|
use LeKoala\EmailTemplates\Models\SentEmail; |
|
12
|
|
|
use LeKoala\EmailTemplates\Helpers\FluentHelper; |
|
13
|
|
|
use LeKoala\EmailTemplates\Models\EmailTemplate; |
|
|
|
|
|
|
14
|
|
|
use SilverStripe\Core\Environment; |
|
15
|
|
|
use SilverStripe\Security\Member; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Manage your email templates |
|
19
|
|
|
* |
|
20
|
|
|
* @author lekoala |
|
21
|
|
|
*/ |
|
22
|
|
|
class EmailTemplatesAdmin extends ModelAdmin |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
private static $managed_models = [ |
|
|
|
|
|
|
26
|
|
|
EmailTemplate::class, |
|
27
|
|
|
SentEmail::class, |
|
28
|
|
|
Emailing::class, |
|
29
|
|
|
]; |
|
30
|
|
|
private static $url_segment = 'email-templates'; |
|
|
|
|
|
|
31
|
|
|
private static $menu_title = 'Emails'; |
|
|
|
|
|
|
32
|
|
|
private static $menu_icon = 'lekoala/silverstripe-email-templates:images/mail.png'; |
|
|
|
|
|
|
33
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
34
|
|
|
'ImportForm', |
|
35
|
|
|
'SearchForm', |
|
36
|
|
|
'PreviewEmail', |
|
37
|
|
|
'PreviewEmailing', |
|
38
|
|
|
'SendEmailing', |
|
39
|
|
|
'ViewSentEmail', |
|
40
|
|
|
'SendTestEmailTemplate', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
public function subsiteCMSShowInMenu() |
|
44
|
|
|
{ |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getList() |
|
49
|
|
|
{ |
|
50
|
|
|
$list = parent::getList(); |
|
51
|
|
|
return $list; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Called by EmailTemplate |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function SendEmailing() |
|
60
|
|
|
{ |
|
61
|
|
|
Environment::increaseTimeLimitTo(); |
|
62
|
|
|
|
|
63
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
|
64
|
|
|
|
|
65
|
|
|
/* @var $Emailing Emailing */ |
|
66
|
|
|
$Emailing = self::getEmailingById($id); |
|
67
|
|
|
$emails = $Emailing->getEmailsByLocales(); |
|
68
|
|
|
|
|
69
|
|
|
$errors = 0; |
|
70
|
|
|
$messages = []; |
|
71
|
|
|
foreach ($emails as $locale => $emails) { |
|
72
|
|
|
foreach ($emails as $email) { |
|
73
|
|
|
$res = null; |
|
74
|
|
|
$msg = null; |
|
75
|
|
|
// Wrap with withLocale to make sure any environment variable (urls, etc) are properly set when sending |
|
76
|
|
|
FluentHelper::withLocale($locale, function () use ($email, &$res, &$msg) { |
|
77
|
|
|
try { |
|
78
|
|
|
$res = $email->send(); |
|
79
|
|
|
} catch (Exception $ex) { |
|
80
|
|
|
$res = false; |
|
81
|
|
|
$msg = $ex->getMessage(); |
|
82
|
|
|
} |
|
83
|
|
|
}); |
|
84
|
|
|
if (!$res) { |
|
85
|
|
|
$errors++; |
|
86
|
|
|
$messages[] = $msg; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($errors == 0) { |
|
92
|
|
|
$Emailing->LastSent = date('Y-m-d H:i:s'); |
|
93
|
|
|
$Emailing->LastSentCount = count($emails); |
|
94
|
|
|
$Emailing->write(); |
|
95
|
|
|
$message = _t('EmailTemplatesAdmin.EMAILING_SENT', 'Emailing sent'); |
|
96
|
|
|
} else { |
|
97
|
|
|
$Emailing->LastError = implode(", ", $messages); |
|
98
|
|
|
$Emailing->write(); |
|
99
|
|
|
|
|
100
|
|
|
$message = _t('EmailTemplatesAdmin.EMAILING_ERROR', 'There was an error sending email'); |
|
101
|
|
|
$message .= ": " . implode(", ", $messages); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (Director::is_ajax()) { |
|
105
|
|
|
$this->getResponse()->addHeader('X-Status', rawurlencode($message)); |
|
106
|
|
|
if ($errors > 0) { |
|
107
|
|
|
// $this->getResponse()->setStatusCode(400); |
|
108
|
|
|
} |
|
109
|
|
|
return $this->getResponse(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $message; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Called by EmailTemplate::previewTab |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function PreviewEmail() |
|
121
|
|
|
{ |
|
122
|
|
|
// Prevent CMS styles to interfere with preview |
|
123
|
|
|
Requirements::clear(); |
|
124
|
|
|
|
|
125
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
|
126
|
|
|
|
|
127
|
|
|
$EmailTemplate = self::getEmailTemplateById($id); |
|
128
|
|
|
$html = $EmailTemplate->renderTemplate(true); |
|
129
|
|
|
|
|
130
|
|
|
Requirements::restore(); |
|
131
|
|
|
|
|
132
|
|
|
return $html; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Called by Emailing::previewTab |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public function PreviewEmailing() |
|
141
|
|
|
{ |
|
142
|
|
|
// Prevent CMS styles to interfere with preview |
|
143
|
|
|
Requirements::clear(); |
|
144
|
|
|
|
|
145
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
|
146
|
|
|
|
|
147
|
|
|
$Emailing = self::getEmailingById($id); |
|
148
|
|
|
$html = $Emailing->renderTemplate(); |
|
149
|
|
|
|
|
150
|
|
|
Requirements::restore(); |
|
151
|
|
|
|
|
152
|
|
|
return $html; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function SendTestEmailTemplate() |
|
156
|
|
|
{ |
|
157
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
|
158
|
|
|
$to = (string) $this->getRequest()->getVar('to'); |
|
159
|
|
|
|
|
160
|
|
|
if (!$to) { |
|
161
|
|
|
die("Please set a [email protected]"); |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$member = Member::get()->filter('Email', $to)->first(); |
|
165
|
|
|
|
|
166
|
|
|
$emailTemplate = self::getEmailTemplateById($id); |
|
167
|
|
|
|
|
168
|
|
|
$email = $emailTemplate->getEmail(); |
|
169
|
|
|
|
|
170
|
|
|
$emailTemplate->setPreviewData($email); |
|
171
|
|
|
if ($member) { |
|
172
|
|
|
$email->setToMember($member); |
|
173
|
|
|
$email->addData("Member", $member); |
|
174
|
|
|
} else { |
|
175
|
|
|
$email->setTo($to); |
|
176
|
|
|
} |
|
177
|
|
|
$result = $email->doSend(); |
|
178
|
|
|
|
|
179
|
|
|
print_r($result); |
|
180
|
|
|
die(); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Called by SentEmail |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
|
public function ViewSentEmail() |
|
189
|
|
|
{ |
|
190
|
|
|
// Prevent CMS styles to interfere with preview |
|
191
|
|
|
Requirements::clear(); |
|
192
|
|
|
|
|
193
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
|
194
|
|
|
|
|
195
|
|
|
$SentEmail = self::getSentEmailById($id); |
|
196
|
|
|
$html = $SentEmail->Body; |
|
197
|
|
|
|
|
198
|
|
|
Requirements::restore(); |
|
199
|
|
|
|
|
200
|
|
|
return $html; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param int $id |
|
205
|
|
|
* @return Emailing |
|
206
|
|
|
*/ |
|
207
|
|
|
protected static function getEmailingById($id) |
|
208
|
|
|
{ |
|
209
|
|
|
return Emailing::get()->byID($id); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param int $id |
|
214
|
|
|
* @return EmailTemplate |
|
215
|
|
|
*/ |
|
216
|
|
|
protected static function getEmailTemplateById($id) |
|
217
|
|
|
{ |
|
218
|
|
|
return EmailTemplate::get()->byID($id); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param int $id |
|
223
|
|
|
* @return SentEmail |
|
224
|
|
|
*/ |
|
225
|
|
|
protected static function getSentEmailById($id) |
|
226
|
|
|
{ |
|
227
|
|
|
return SentEmail::get()->byID($id); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths