-
Notifications
You must be signed in to change notification settings - Fork 19
/
extension.driver.php
235 lines (202 loc) · 9.2 KB
/
extension.driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
require_once(dirname(__FILE__) . '/lib/class.emailtemplatemanager.php');
require_once(TOOLKIT . '/class.datasourcemanager.php');
class extension_email_template_manager extends Extension
{
public function fetchNavigation()
{
return array(
array(
'location' => __('Blueprints'),
'name' => __('Email Templates'),
'link' => '/templates/'
)
);
}
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendEventFilter'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendEventFilter'
),
array(
'page' => '/frontend/',
'delegate' => 'EventFinalSaveFilter',
'callback' => 'eventFinalSaveFilter'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'appendEventFilterDocumentation'
),
array(
'page' => '/blueprints/datasources/',
'delegate' => 'DatasourcePostEdit',
'callback' => 'datasourcePostEdit'
),
);
}
public function install()
{
if (!is_dir(WORKSPACE . '/email-templates')) {
try {
mkdir(WORKSPACE . '/email-templates');
} catch (Exception $e) {
return false;
}
}
return true;
}
public function uninstall()
{
try {
General::deleteDirectory(WORKSPACE.'/email-templates');
} catch (Exception $e) {
return false;
}
return true;
}
public function datasourcePostEdit($file)
{
$ds_handle = DatasourceManager::__getHandleFromFileName(basename($file['file']));
$templates = EmailTemplateManager::listAll();
foreach ($templates as $template) {
$config = $template->getProperties();
if (!is_null($file['previous_file']) && ($key = array_search(DatasourceManager::__getHandleFromFilename(basename($file['previous_file'])), $config['datasources'])) !== false) {
$config['datasources'][$key] = $ds_handle;
return EmailTemplateManager::editConfig($template->getHandle(), array_merge($template->getAbout(), $config));
}
}
}
public function appendEventFilter($context)
{
$templates = EmailTemplateManager::listAll();
if (empty($templates)) return;
foreach ($templates as $template) {
$tmp[$template->getHandle()] = $template;
}
$templates = is_array($tmp)?$tmp:array();
ksort($templates, SORT_STRING);
foreach ($templates as $template) {
$handle = 'etm-' . $template->getHandle();
$selected = (in_array($handle, $context['selected']));
$context['options'][] = array(
$handle, $selected, General::sanitize('Send Email Template: ' . $template->getName())
);
}
}
public function eventFinalSaveFilter($context)
{
$templates = EmailTemplateManager::listAll();
foreach ($templates as $template) {
$handle = 'etm-' . $template->getHandle();
if (in_array($handle, (array) $context['event']->eParamFILTERS)) {
if (($response = $this->_sendEmail($template, $context)) !== false) {
$context['errors'][] = array('etm-' . $template->getHandle(), ($response['sent']>0), null, $response);
}
}
}
}
protected function _sendEmail($template, $context)
{
try {
$template->addParams(array('etm-entry-id' => $context['entry']->get('id')));
Symphony::Engine()->Page()->_param['etm-entry-id'] = $context['entry']->get('id');
//Add POST as page parameters
foreach ($context['fields'] as $field => $val) {
if (is_array($val)) {
foreach ($val as $key => $value) {
if (is_array($value)) $value = implode($value,',');
$template->addParams(array('etm-post-'.$field.'.'.$key => $value));
Symphony::Engine()->Page()->_param['etm-post-'.$field.'.'.$key] = $value;
}
} else {
$template->addParams(array('etm-post-'.$field => $val));
Symphony::Engine()->Page()->_param['etm-post-'.$field] = $val;
}
}
$xml = $template->processDatasources();
$about = $context['event']->about();
General::array_to_xml($xml, array('events' => array($about['name'] => array('post-values' => $context['fields']))));
$template->setXML($xml->generate());
$template->parseProperties();
$properties = $template->getParsedProperties();
$recipients = array_unique((array) $properties['recipients']);
$sent = 0;
if (count($recipients) > 0) {
foreach ($recipients as $name => $emailaddr) {
try {
$email = Email::create();
$template->addParams(array('etm-recipient' => $emailaddr));
$xml = $template->processDatasources();
$about = $context['event']->about();
General::array_to_xml($xml, array('events' => array($about['name'] => array('post-values' => $context['fields']))));
$template->setXML($xml->generate());
$template->recipients = $emailaddr;
$content = $template->render();
if (!empty($content['subject'])) {
$email->subject = $content['subject'];
} else {
throw new EmailTemplateException(__('Can not send emails without a subject'));
}
if (!empty($content['from-name'])) {
$email->sender_name = $content['from-name'];
}
if (!empty($content['from-email-address'])) {
$email->sender_email_address = $content['from-email-address'];
}
if (isset($content['reply-to-name'])) {
$email->reply_to_name = $content['reply-to-name'];
}
if (isset($content['reply-to-email-address'])) {
$email->reply_to_email_address = $content['reply-to-email-address'];
}
if (isset($content['plain'])) {
$email->text_plain = $content['plain'];
}
if (isset($content['html'])) {
$email->text_html = $content['html'];
}
if (!empty($content['attachments'])) {
$email->attachments = $content['attachments'];
}
if (isset($content['ignore-attachment-errors'])) {
$email->validate_attachment_errors = !$content['ignore-attachment-errors'];
}
require_once(TOOLKIT . '/util.validators.php');
if (General::validateString($emailaddr, $validators['email'])) {
$email->recipients = array($name => $emailaddr);
} else {
throw new EmailTemplateException(__('Email address invalid:') . ' ' . $emailaddr);
}
$email->send();
$sent++;
} catch (EmailTemplateException $e) {
Symphony::Log()->pushToLog(__('Email Template Manager: ') . $e->getMessage(), null, true);
//$context['errors'][] = array('etm-' . $template->getHandle() . '-' . Lang::createHandle($emailaddr), false, $e->getMessage());
continue;
}
}
} else {
throw new EmailTemplateException('Can not send an email to nobody, please set a recipient.');
}
} catch (EmailTemplateException $e) {
$context['errors'][] = array('etm-' . $template->getHandle(), false, $e->getMessage());
return false;
} catch (EmailValidationException $e) {
$context['errors'][] = array('etm-' . $template->getHandle(), false, $e->getMessage());
return false;
} catch (EmailGatewayException $e) {
$context['errors'][] = array('etm-' . $template->getHandle(), false, $e->getMessage());
return false;
}
return array('total' => count($recipients), 'sent' => $sent);
}
}