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