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