forked from michael-e/email_template_manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.driver.php
208 lines (183 loc) · 6.48 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
<?php
require_once(dirname(__FILE__) . '/lib/class.emailtemplatemanager.php');
require_once(TOOLKIT . '/class.datasourcemanager.php');
Class extension_email_template_manager extends Extension{
public function about(){
return array(
'name' => 'Email Template Manager',
'version' => '4.0',
'release-date' => '2012-05-23',
'author' => array(
'name' => 'Huib Keemink',
'website' => 'http://www.creativedutchmen.com',
'email' => '[email protected]'
)
);
}
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 DatasourcePostEdit($file){
$ds_handle = DatasourceManager::__getHandleFromFileName(substr($file['file'], strrpos($file['file'], '/')+1));
$templates = EmailTemplateManager::listAll();
foreach($templates as $template){
$config = $template->getProperties();
if(($key = array_search($file['parent']->Page->_context[1], $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){
$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 = $properties['recipients'];
$sent = 0;
if(count($recipients) > 0){
foreach((array)$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(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'];
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);
}
}