-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension.driver.php
355 lines (305 loc) · 10.4 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
class Extension_Campaign_Monitor extends Extension {
private $params = array();
public function about() {
return array(
'name' => 'Campaign Monitor',
'version' => '0.9.1',
'release-date' => '2011-02-18',
'author' => array(
array(
'name' => 'Brendan Abbott',
'website' => 'http://www.bloodbone.ws',
'email' => '[email protected]'
),
array(
'name' => 'Rowan Lewis',
'website' => 'http://rowanlewis.com/',
'email' => '[email protected]'
)
),
'description' => 'A simple Event Filter to add Subscribers to your Campaign Monitor lists via Symphony events.'
);
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendFilter'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendFilter'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'appendDocumentation'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'appendDocumentation'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'preProcessData'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'postProcessData'
)
);
}
/*-------------------------------------------------------------------------
Installation:
-------------------------------------------------------------------------*/
public function uninstall() {
Symphony::Configuration()->remove('campaignmonitor');
Administration::instance()->saveConfig();
}
/*-------------------------------------------------------------------------
Delegate Callbacks:
-------------------------------------------------------------------------*/
public function appendFilter($context) {
$context['options'][] = array(
'campaignmonitor',
@in_array(
'campaignmonitor', $context['selected']
),
'Campaign Monitor'
);
}
public function appendDocumentation($context) {
if (!in_array('campaignmonitor', $context['selected'])) return;
$context['documentation'][] = new XMLElement('h3', 'Campaign Monitor Filter');
$context['documentation'][] = new XMLElement('p', '
To use the Campaign Monitor filter, add the following field to your form:
');
$context['documentation'][] = contentBlueprintsEvents::processDocumentationCode('
<input name="campaignmonitor[list]" value="{$your-list-id}" type="hidden" />
<input name="campaignmonitor[field][Name]" value="$field-first-name, $field-last-name" type="hidden" />
<input name="campaignmonitor[field][Email]" value="$field-email-address" type="hidden" />
<input name="campaignmonitor[field][Custom]" value="Value for field Custom Field..." type="hidden" />
');
$context['documentation'][] = new XMLElement('p', '
If you require any existing Campaign Monitor subscriber\'s data to be merged, you can provide
the fields you want to merge like so:
');
$context['documentation'][] = contentBlueprintsEvents::processDocumentationCode('
<input name="campaignmonitor[merge-fields]" value="Name of Custom Field1, Name of CustomField2" type="hidden" />
');
}
public function appendPreferences($context) {
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(
new XMLElement('legend', 'Campaign Monitor Filter')
);
$uniqueID = Widget::Label('API Key');
$uniqueID->appendChild(Widget::Input(
'settings[campaignmonitor][apikey]', Extension_Campaign_Monitor::getAPIKey()
));
$group->appendChild($uniqueID);
$context['wrapper']->appendChild($group);
}
public function preProcessData($context) {
if (!in_array('campaignmonitor', $context['event']->eParamFILTERS)) return;
if (
!isset($_POST['campaignmonitor']['list'])
or !isset($_POST['campaignmonitor']['field']['Email'])
) {
$context['messages'][] = array(
'campaignmonitor',
false,
'Required field missing, see event documentation.'
);
}
}
public function postProcessData($context) {
if (!in_array('campaignmonitor', $context['event']->eParamFILTERS)) return;
// Create params:
$this->params = $this->prepareFields('field', $_POST['fields']);
// Parse values:
$values = $this->parseFields($_POST['campaignmonitor']['field']);
// If there is any fields that should be merged:
if(array_key_exists('merge-fields', $_POST['campaignmonitor'])) {
$values = $this->mergeFields($_POST['campaignmonitor']['merge-fields'], $values);
}
$request = array();
// Add fields:
foreach ($values as $name => $value) {
if ($name == 'Name') {
$request[$name] = $value;
}
elseif ($name == 'Email') {
$request['EmailAddress'] = $value;
}
elseif ($name == 'Resubscribe') {
$request['Resubscribe'] = $value;
}
else {
if(is_array($value)) {
foreach($value as $nv) {
$request['CustomFields'][] = array(
'Key' => $name,
'Value' => $nv
);
}
}
else {
$request['CustomFields'][] = array(
'Key' => $name,
'Value' => $value
);
}
}
}
$request = json_encode($request);
if($_POST['campaignmonitor']['unsubscribe'] == 'yes') {
$api = sprintf(
"http://api.createsend.com/api/v3/subscribers/%s/unsubscribe.json", $_POST['campaignmonitor']['list']
);
} else {
$api = sprintf(
"http://api.createsend.com/api/v3/subscribers/%s.json", $_POST['campaignmonitor']['list']
);
}
$ch = curl_init($api);
curl_setopt_array($ch, array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => Extension_Campaign_Monitor::getAPIKey() . ":magic",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request,
CURLOPT_HTTPHEADER => array("Content-type: application/json; charset=utf-8"),
CURLOPT_RETURNTRANSFER => true
));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$response = json_decode($response);
if(in_array($info['http_code'], array(200, 201))) {
$context['messages'][] = array('campaignmonitor', true, $response);
}
else {
$context['messages'][] = array('campaignmonitor', false, $response->Message);
Symphony::Log()->pushToLog(
__('There was an error subscribing to ListID, %s. Campaign Monitor returned code %d, %s.', array(
$_POST['campaignmonitor']['list'], $response->Code, $response->Message
)),
E_ERROR,
true
);
}
curl_close($ch);
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
public static function getAPIKey() {
return Symphony::Configuration()->get('apikey', 'campaignmonitor');
}
/**
* Takes the `$_POST['fields']` and generates a flat array of
* all the fields.
*
* @param string $path
* The default string to use when a field needs to be flattened
* @param array $fields
* The `$_POST['fields']` array
* @return array
*/
public function prepareFields($path, $fields) {
$output = array();
foreach($fields as $key => $value) {
if (!is_numeric($key)) {
$key = "{$path}-{$key}";
if (is_array($value)) {
$temp = $this->prepareFields($key, $value);
$output = array_merge($output, $temp);
}
else {
$output[$key] = $value;
}
}
else {
$key = $path;
$output[$key][] = $value;
}
}
return $output;
}
/**
* Given an associative array of values, this will strip the
* dollar notation from the value, and then will map the fields
* to their Campaign Monitor equivalents.
*
* @param array $values
* @return array
*/
public function parseFields($values) {
foreach($values as $key => $value) {
$value = preg_replace('/^\$/', null, $value);
if(isset($this->params[$value])) {
$values[$key] = $this->params[$value];
}
}
return $values;
}
/**
* Allows fields from the form to merge onto existing Campaign Monitor
* data for this subscriber. For instance, if you have Select Many custom
* field in Campaign Monitor, it expects all the values of the field to
* be provided, otherwise it will erase them and add the ones provided in
* the API call. This function will contact the CM API for the subscriber,
* and their existing record with the fields as supplied in `$merge`
*
* @param string $merge_fields
* A comma separated string of all the fields that need to merged with
* the current $_POST data. This should be the field names as they are named
* in Campaign Monitor.
* @param array $values
* The current parsed values for the current $_POST data. These have been parsed
* and already mapped to the Campaign Monitor field names.
*/
public function mergeFields($merge_fields, $values) {
$merge_fields = explode(",", $merge_fields);
$merge_fields = array_map('trim', $merge_fields);
// Contact C+S and check to see if this subscriber already exists
// and if so, merge the values so that it won't be overidden.
$api = sprintf(
"http://api.createsend.com/api/v3/subscribers/%s.json?email=%s",
$_POST['campaignmonitor']['list'], urlencode($values['Email'])
);
$ch = curl_init($api);
curl_setopt_array($ch, array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->getAPIKey() . ":magic",
CURLOPT_HTTPHEADER => array("Content-type: application/json; charset=utf-8"),
CURLOPT_RETURNTRANSFER => true
));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
// If subscriber is new, there's no data to fetch!
if($info['http_code'] == 203) return $values;
$response = json_decode($response);
if(is_array($response->CustomFields)) {
foreach($response->CustomFields as $object) {
if(!in_array($object->Key, $merge_fields)) continue;
if(!is_array($values[$object->Key])) {
$values[$object->Key] = array($values[$object->Key]);
}
$values[$object->Key][] = $object->Value;
}
}
return $values;
}
}