-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.driver.php
289 lines (263 loc) · 12 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
if (!defined('ENMDIR')) define('ENMDIR', EXTENSIONS . "/email_newsletter_manager");
require_once(ENMDIR . '/lib/class.emailnewslettermanager.php');
class extension_email_newsletter_manager extends Extension
{
public function fetchNavigation()
{
return array(
array(
'location' => __('Blueprints'),
'name' => __('Newsletter Recipients'),
'link' => '/recipientgroups/'
),
array(
'location' => __('Blueprints'),
'name' => __('Newsletter Senders'),
'link' => '/senders/'
)
);
}
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/backend/',
'delegate' => 'InitialiseAdminPageHead',
'callback' => 'appendStyles'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPreEdit',
'callback' => 'stopRestartNewsletter'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'initEmailNewsletter'
),
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'initEmailNewsletter'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/extension/email_newsletter_manager/',
'delegate' => 'PostSenderSaved',
'callback' => 'senderSaved'
),
array(
'page' => '/extension/email_newsletter_manager/',
'delegate' => 'PostRecipientgroupSaved',
'callback' => 'groupSaved'
),
array(
'page' => '/extension/email_template_manager/',
'delegate' => 'EmailTemplatePostSave',
'callback' => 'templateSaved'
),
);
}
public function appendStyles($context)
{
$callback = Administration::instance()->getPageCallback();
if (is_a(Symphony::Engine(),'Administration')) {
Symphony::Engine()->Page->addScriptToHead(URL . '/extensions/email_newsletter_manager/assets/email_newsletter_manager.admin.js', 70);
Symphony::Engine()->Page->addStylesheetToHead(URL . '/extensions/email_newsletter_manager/assets/email_newsletter_manager.recipientgroups.preview.css', 'screen', 1000);
}
}
/**
* Callback function to change a sender handle in each newsletter field.
*
* @param array $context
* @return void
*/
public function senderSaved($context)
{
$old_handle = $context['handle'];
$new_handle = Lang::createHandle($context['fields']['name'], 255, '-');
EmailNewsletterManager::updateSenderHandle($old_handle, $new_handle);
}
/**
* Callback function to change a recipient group handle in each newsletter field
*
* @param array $context
* @return void
*/
public function groupSaved($context)
{
$old_handle = $context['handle'];
$new_handle = Lang::createHandle($context['fields']['name'], 255, '-');
EmailNewsletterManager::updateRecipientsHandle($old_handle, $new_handle);
return true;
}
/**
* Callback function to change a template handle in each newsletter field.
*
* @param array $context
* @return void
*/
public function templateSaved($context)
{
$old_handle = $context['old_handle'];
$new_handle = Lang::createHandle($context['config']['name'], 255, '-');
EmailNewsletterManager::updateTemplateHandle($old_handle, $new_handle);
}
public function stopRestartNewsletter(&$context)
{
if (substr($_POST['action']['save'], 0, 12) == 'enm-restart:') {
$vars = explode(":",$_POST['action']['save']);
$field_id = $vars[1];
$entry_id = $context['entry']->get('id');
$data = $this->_getEntryData($field_id, $entry_id);
if (!empty($data)) {
try {
$newsletter = EmailNewsletterManager::create($data['newsletter_id']);
$array = array(
'template' => is_object($newsletter->getTemplate()) ? $newsletter->getTemplate()->getHandle() : null,
'sender' => is_object($newsletter->getSender()) ? $newsletter->getSender()->getHandle() : null,
'recipients' => implode(', ', $newsletter->getRecipientGroups(false, true)),
);
$news = EmailNewsletterManager::save($array);
$context['entry']->setData($field_id, array('author_id'=>Symphony::Author()->get('id'), 'entry_id'=>$entry_id, 'newsletter_id'=>$news->getId()));
//$news->start();
} catch (Exception $e) {
Administration::instance()->throwCustomError(__('Error restarting email newsletter'), __($e->getMessage()));
}
}
}
if (substr($_POST['action']['save'], 0, 9) == 'enm-stop:') {
$vars = explode(":",$_POST['action']['save']);
$field_id = $vars[1];
$entry_id = $context['entry']->get('id');
$data = $this->_getEntryData($field_id, $entry_id);
if (!empty($data)) {
try {
$newsletter = EmailNewsletterManager::create($data['newsletter_id']);
$newsletter->stop();
} catch (Exception $e) {
Administration::instance()->throwCustomError(__('Error stopping email newsletter'), __($e->getMessage()));
}
}
}
if (substr($_POST['action']['save'], 0, 10) == 'enm-pause:') {
$vars = explode(":",$_POST['action']['save']);
$field_id = $vars[1];
$entry_id = $context['entry']->get('id');
$data = $this->_getEntryData($field_id, $entry_id);
if (!empty($data)) {
try {
$newsletter = EmailNewsletterManager::create($data['newsletter_id']);
$newsletter->pause();
} catch (Exception $e) {
Administration::instance()->throwCustomError(__('Error pausing email newsletter'), __($e->getMessage()));
}
}
}
}
public function initEmailNewsletter($context)
{
// The field has a 'save and send' button. We trigger the newsletter
// start using the 'action' string, which seems to be the only way.
if (isset($_POST['action']) && @array_key_exists('save', $_POST['action']) && substr($_POST['action']['save'], 0, 9) == 'enm-send:') {
$vars = explode(":",$_POST['action']['save']);
$field_id = $vars[1];
$entry_id = $context['entry']->get('id');
$data = $this->_getEntryData($field_id, $entry_id);
if (!empty($data)) {
try {
$this->newsletter = EmailNewsletterManager::create($data['newsletter_id']);
// The reason the newsletter is started here and not in the field save function is because it must only send if all other fields are completed successfully.
$this->newsletter->start();
}
// This is the last resort. All checks should be done before saving the entry, so this error should ideally never be shown. Ever.
// Because the delegate this function hooks into can not undo the saving, or display any warning messages, a "hard" error is the only way to communicate what is going wrong.
catch(Exception $e) {
Administration::instance()->throwCustomError(__('Error sending email newsletter'), __($e->getMessage()));
}
}
}
}
public function install()
{
$etm = Symphony::ExtensionManager()->getInstance('email_template_manager');
if ($etm instanceof Extension) {
try {
if (@mkdir(WORKSPACE . '/email-newsletters') || is_dir(WORKSPACE . '/email-newsletters')) {
Symphony::Database()->query("CREATE TABLE IF NOT EXISTS `tbl_fields_email_newsletter_manager` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`templates` text,
`senders` text,
`recipient_groups` text,
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
Symphony::Database()->query("CREATE TABLE IF NOT EXISTS `tbl_email_newsletters` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`template` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`recipients` text CHARACTER SET utf8,
`completed_recipients` text CHARACTER SET utf8,
`sender` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`pseudo_root` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`total` int(11) DEFAULT '0',
`sent` int(11) DEFAULT '0',
`failed` int(11) DEFAULT '0',
`started_on` timestamp NULL,
`started_by` int(10) unsigned NULL,
`completed_on` timestamp NULL,
`status` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`pauth` varchar(23) CHARACTER SET utf8 DEFAULT NULL,
`pid` varchar(13) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
return true;
}
} catch (Exception $e) {
throw new Exception(__('Installation failed:') . ' ' . $e->getMessage());
}
} else {
throw new Exception(__('The Email Template Manager is required for this extension to work.'));
}
}
public function update($previousVersion = false)
{
return true;
}
public function uninstall()
{
Symphony::Database()->query("DROP TABLE `tbl_fields_email_newsletter_manager`");
Symphony::Database()->query("DROP TABLE `tbl_email_newsletters`");
return true;
}
public function appendPreferences($context)
{
$config = Symphony::Configuration()->get('email_newsletter_manager');
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', 'Email Newsletter Manager'));
$group = new XMLElement('div');
$label = Widget::Label(__('Path to PHP Executable'));
$label->appendChild(new XMLElement('i', __('optional')));
$label->appendChild(Widget::Input(
'settings[email_newsletter_manager][php_executable]',
General::sanitize($config['php_executable']),
'text',
array(
'placeholder' => 'e.g. /usr/bin/php',
)
));
$group->appendChild($label);
$fieldset->appendChild($group);
$context['wrapper']->appendChild($fieldset);
}
private function _getEntryData($field_id, $entry_id)
{
return Symphony::Database()->fetchRow(0, "SELECT * FROM `tbl_entries_data_".$field_id."` WHERE `entry_id` = '".$entry_id."' LIMIT 1");
}
}