1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\EmailTemplates\Admin; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use LeKoala\EmailTemplates\Helpers\FluentHelper; |
7
|
|
|
use SilverStripe\Admin\ModelAdmin; |
8
|
|
|
use SilverStripe\View\Requirements; |
9
|
|
|
use LeKoala\EmailTemplates\Models\Emailing; |
10
|
|
|
use LeKoala\EmailTemplates\Models\SentEmail; |
11
|
|
|
use LeKoala\EmailTemplates\Models\EmailTemplate; |
12
|
|
|
use SilverStripe\Control\Director; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Manage your email templates |
16
|
|
|
* |
17
|
|
|
* @author lekoala |
18
|
|
|
*/ |
19
|
|
|
class EmailTemplatesAdmin extends ModelAdmin |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
private static $managed_models = array( |
|
|
|
|
23
|
|
|
EmailTemplate::class, |
24
|
|
|
SentEmail::class, |
25
|
|
|
Emailing::class, |
26
|
|
|
); |
27
|
|
|
private static $url_segment = 'email-templates'; |
|
|
|
|
28
|
|
|
private static $menu_title = 'Emails'; |
|
|
|
|
29
|
|
|
private static $menu_icon = 'lekoala/silverstripe-email-templates:images/mail.png'; |
|
|
|
|
30
|
|
|
private static $allowed_actions = array( |
|
|
|
|
31
|
|
|
'ImportForm', |
32
|
|
|
'SearchForm', |
33
|
|
|
'PreviewEmail', |
34
|
|
|
'PreviewEmailing', |
35
|
|
|
'SendEmailing', |
36
|
|
|
'ViewSentEmail', |
37
|
|
|
'SentTestEmail', |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
public function subsiteCMSShowInMenu() |
41
|
|
|
{ |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getList() |
46
|
|
|
{ |
47
|
|
|
$list = parent::getList(); |
48
|
|
|
return $list; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Called by EmailTemplate |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function SendEmailing() |
57
|
|
|
{ |
58
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
59
|
|
|
|
60
|
|
|
/* @var $Emailing Emailing */ |
61
|
|
|
$Emailing = Emailing::get()->byID($id); |
62
|
|
|
$emails = $Emailing->getEmailByLocales(); |
63
|
|
|
|
64
|
|
|
$errors = 0; |
65
|
|
|
foreach ($emails as $locale => $email) { |
66
|
|
|
// Wrap with withLocale to make sure any environment variable (urls, etc) are properly set when sending |
67
|
|
|
$res = null; |
68
|
|
|
FluentHelper::withLocale($locale, function () use ($email, &$res) { |
69
|
|
|
try { |
70
|
|
|
$res = $email->send(); |
71
|
|
|
} catch (Exception $ex) { |
72
|
|
|
return $ex->getMessage(); |
73
|
|
|
} |
74
|
|
|
return $res; |
75
|
|
|
}); |
76
|
|
|
if (!$res) { |
77
|
|
|
$errors++; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$message = _t('EmailTemplatesAdmin.EMAILING_ERROR', 'There was an error sending email'); |
82
|
|
|
|
83
|
|
|
if ($errors == 0) { |
84
|
|
|
$Emailing->LastSent = date('Y-m-d H:i:s'); |
|
|
|
|
85
|
|
|
$Emailing->write(); |
86
|
|
|
|
87
|
|
|
$message = _t('EmailTemplatesAdmin.EMAILING_SENT', 'Emailing sent'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (Director::is_ajax()) { |
91
|
|
|
return $message; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->redirectBack(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Called by EmailTemplate::previewTab |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function PreviewEmail() |
103
|
|
|
{ |
104
|
|
|
// Prevent CMS styles to interfere with preview |
105
|
|
|
Requirements::clear(); |
106
|
|
|
|
107
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
108
|
|
|
|
109
|
|
|
/* @var $EmailTemplate EmailTemplate */ |
110
|
|
|
$EmailTemplate = EmailTemplate::get()->byID($id); |
111
|
|
|
$html = $EmailTemplate->renderTemplate(true); |
112
|
|
|
|
113
|
|
|
Requirements::restore(); |
114
|
|
|
|
115
|
|
|
return $html; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Called by Emailing::previewTab |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function PreviewEmailing() |
124
|
|
|
{ |
125
|
|
|
// Prevent CMS styles to interfere with preview |
126
|
|
|
Requirements::clear(); |
127
|
|
|
|
128
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
129
|
|
|
|
130
|
|
|
/* @var $Emailing Emailing */ |
131
|
|
|
$Emailing = Emailing::get()->byID($id); |
132
|
|
|
$html = $Emailing->renderTemplate(true); |
|
|
|
|
133
|
|
|
|
134
|
|
|
Requirements::restore(); |
135
|
|
|
|
136
|
|
|
return $html; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function SentTestEmail() |
140
|
|
|
{ |
141
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
142
|
|
|
|
143
|
|
|
/* @var $emailTemplate EmailTemplate */ |
144
|
|
|
$emailTemplate = EmailTemplate::get()->byID($id); |
145
|
|
|
|
146
|
|
|
$email = $emailTemplate->getEmail(); |
147
|
|
|
$emailTemplate->setPreviewData($email); |
148
|
|
|
$result = $email->send(); |
149
|
|
|
|
150
|
|
|
print_r($result); |
151
|
|
|
die(); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Called by SentEmail |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function ViewSentEmail() |
160
|
|
|
{ |
161
|
|
|
// Prevent CMS styles to interfere with preview |
162
|
|
|
Requirements::clear(); |
163
|
|
|
|
164
|
|
|
$id = (int) $this->getRequest()->getVar('id'); |
165
|
|
|
|
166
|
|
|
/* @var $SentEmail SentEmail */ |
167
|
|
|
$SentEmail = SentEmail::get()->byID($id); |
168
|
|
|
$html = $SentEmail->Body; |
169
|
|
|
|
170
|
|
|
Requirements::restore(); |
171
|
|
|
|
172
|
|
|
return $html; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|