forked from mazedigital/activecampaign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.driver.php
381 lines (309 loc) · 11 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
require_once EXTENSIONS . '/activecampaign/vendor/autoload.php';
Class extension_Activecampaign extends Extension{
private $ac;
private $clientId;
private $clientSecret;
private $messages = array();
public function __construct() {
// die('construct');
$this->apiKey = Symphony::Configuration()->get('api-key','activecampaign');
$this->apiURL = Symphony::Configuration()->get('url','activecampaign');
$this->datasources = Symphony::Configuration()->get('datasources','activecampaign');
$this->ac = new ActiveCampaign($this->apiURL,$this->apiKey);
}
public function getActiveCampaign(){
return $this->ac;
}
/**
* Installation
*/
public function install() {
// A table to keep track of user tokens in relation to the current current user id
// Symphony::Database()->query("CREATE TABLE IF NOT EXISTS `tbl_paypal_token` (
// `user_id` VARCHAR(255) NOT NULL ,
// `refresh_token` VARCHAR(255) NOT NULL
// PRIMARY KEY (`user_id`,`system`)
// )ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
return true;
}
/**
* Update
*/
public function update() {
$this->install();
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'eventPostSaveFilter'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendFilter'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendFilter'
),
// array(
// 'page' => '/system/preferences/',
// 'delegate' => 'AddCustomPreferenceFieldsets',
// 'callback' => 'appendPreferences'
// ),
// array(
// 'page' => '/system/preferences/',
// 'delegate' => 'Save',
// 'callback' => 'savePreferences'
// ),
// array(
// 'page' => '/frontend/',
// 'delegate' => 'FrontendProcessEvents',
// 'callback' => 'appendEventXML'
// ),
// array(
// 'page' => '/frontend/',
// 'delegate' => 'FrontendParamsResolve',
// 'callback' => 'appendAccessToken'
// ),
// array(
// 'page' => '/frontend/',
// 'delegate' => 'FrontendPageResolved',
// 'callback' => 'frontendPageResolved'
// ),
);
}
public function appendFilter($context) {
$selected = !is_array($context['selected']) ? array() : $context['selected'];
// Add Contact
$context['options'][] = array(
'activecampaign-add-contact',
in_array('activecampaign-add-contact', $selected),
__('Active Campaign Add Contact')
);
// Add Deal
$context['options'][] = array(
'activecampaign-add-deal',
in_array('activecampaign-add-deal', $selected),
__('Active Campaign Add Deal')
);
}
public function eventPostSaveFilter($context){
if (in_array('activecampaign-add-contact', $context['event']->eParamFILTERS)) {
// now generate the data from xPATH
$xml = $this->getPostDetailsXML($context['entry'],$context['event']->ROOTELEMENT . '-add-contact.xsl');
$children = $xml->getChildren();
$fields = array();
foreach ($children as $key => $value) {
if (strlen($value->getValue()) > 0){
//only keep in array if not empty
$fields[$value->getName()] = $value->getValue();
} elseif (is_object($value->getChildren()[0])){
foreach ($value->getChildren() as $index => $node) {
if ($node->getName() == 'item'){
$fields[$value->getName()][$node->getAttribute('name')] = $node->getValue();
} else {
$fields[$value->getName()][$node->getName()] = $value->getValue();
}
}
}
}
// here is where we get to submit the stuff
$ac = $this->getActiveCampaign();
$apiResult = $ac->api("contact/add", $fields);
//if contact exists update
if ($apiResult->success == 1){
$context['messages'][] = Array('activecampaign-add-contact' , true, null,
array('method'=>'add')
);
} elseif ( $apiResult->success == 0 && isset($apiResult->{'0'}->id) ){
$fields['id'] = $apiResult->{'0'}->id;
$apiResult = $ac->api("contact/edit", $fields);
if ($apiResult->success == 1){
$context['messages'][] = Array('activecampaign-add-contact' , true, null,
array('method'=>'update')
);
} else {
$context['errors'][] = Array('activecampaign-add-contact' , false, null,
array('method'=>'failed')
);
}
}
}
}
private function updateContact($context){
if ( Symphony::Configuration()->get('section_' . $context['section']->get('id') ,'activecampaign') ){
$sectionConfig = Symphony::Configuration()->get('section_' . $context['section']->get('id') ,'activecampaign');
foreach ($sectionConfig['Properties'] as $key => $value) {
$sectionConfig['Properties'][$key] = $this->compile($context['entry'],$value);
}
$body = [
'Action' => 'addnoforce',
'Email' => $this->compile($context['entry'],$sectionConfig['Email']),
'Name' => $this->compile($context['entry'],$sectionConfig['Name']),
'Properties' => $sectionConfig['Properties']/*[
'Name' => $this->compile($context['entry'],'{/data/entry/name}'),
'Surname' => $this->compile($context['entry'],'{/data/entry/surname}'),
]*/
];
$response = $this->ac->post(Resources::$ContactslistManagecontact, [
'body' => $body,
'id' => $sectionConfig['id']
]);
}
}
public function entryPostCreate($context){
// $this->updateContact($context);
}
public function entryPostEdit($context){
// $this->updateContact($context);
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
public function getPostDetailsXML($entry, $XSLTfilename = NULL, $fetch_associated_counts = NULL) {
$entry_xml = new XMLElement('entry');
$data = $entry->getData();
$fields = array();
$entry_xml->setAttribute('id', $entry->get('id'));
//Add date created and edited values
$date = new XMLElement('system-date');
$date->appendChild(
General::createXMLDateObject(
DateTimeObj::get('U', $entry->get('creation_date')),
'created'
)
);
$date->appendChild(
General::createXMLDateObject(
DateTimeObj::get('U', $entry->get('modification_date')),
'modified'
)
);
$entry_xml->appendChild($date);
// Add associated entry counts
if($fetch_associated_counts == 'yes') {
$associated = $entry->fetchAllAssociatedEntryCounts();
if (is_array($associated) and !empty($associated)) {
foreach ($associated as $section_id => $count) {
$section = SectionManager::fetch($section_id);
if(($section instanceof Section) === false) continue;
$entry_xml->setAttribute($section->get('handle'), (string)$count);
}
}
}
// Add fields:
foreach ($data as $field_id => $values) {
if (empty($field_id)) continue;
$field = FieldManager::fetch($field_id);
$field->appendFormattedElement($entry_xml, $values, false, null, $entry->get('id'));
}
$xml = new XMLElement('data');
$xml->appendChild($entry_xml);
// Build some context
$section = SectionManager::fetch($entry->get('section_id'));
//generate parameters such as root and add into dom
$date = new DateTime();
$params = array(
'today' => $date->format('Y-m-d'),
'current-time' => $date->format('H:i'),
'this-year' => $date->format('Y'),
'this-month' => $date->format('m'),
'this-day' => $date->format('d'),
'timezone' => $date->format('P'),
'website-name' => Symphony::Configuration()->get('sitename', 'general'),
'root' => URL,
'workspace' => URL . '/workspace',
'http-host' => HTTP_HOST,
'entry-id' => $entry->get('id'),
'section-handle' => $section->get('handle'),
);
if (!empty($this->datasources)){
$datasources = explode(',',$this->datasources);
foreach ($datasources as $dsName) {
$ds = DatasourceManager::create($dsName, $params);
$arr = array();
$dsXml = $ds->execute($arr);
$xml->appendChild($dsXml);
}
}
//in case there are url params they will also be added in the xml
$paramsXML = new XMLElement('params');
foreach ($params as $key => $value) {
$paramsXML->appendChild(new XMLElement($key,$value));
}
$xml->appendChild($paramsXML);
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
$dom->loadXML($xml->generate(true));
if (!empty($XSLTfilename)) {
$XSLTfilename = WORKSPACE . '/utilities/activecampaign/'. preg_replace(array('%/+%', '%(^|/)../%'), '/', $XSLTfilename);
if (file_exists($XSLTfilename)) {
$XSLProc = new XsltProcessor;
$xslt = new DomDocument;
$xslt->load($XSLTfilename);
$XSLProc->importStyleSheet($xslt);
// Set some context
$XSLProc->setParameter('', array(
'section-handle' => $section->get('handle'),
'entry-id' => $entry->get('id')
));
$temp = $XSLProc->transformToDoc($dom);
if ($temp instanceof DOMDocument) {
$dom = $temp;
}
}
}
return XMLElement::convertFromDOMDocument('data',$dom);
}
/**
* Allows a user to select which section they would like to use as their
* active members section. This allows developers to build multiple sections
* for migration during development.
*
* @uses AddCustomPreferenceFieldsets
* @todo Look at how this could be expanded so users can log into multiple sections. This is not in scope for 1.0
*
* @param array $context
*/
public function appendPreferences($context){
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', 'Active Campaign'));
$div = new XMLElement('div', NULL, array('class' => 'group'));
$label = Widget::Label('URL');
$label->appendChild(Widget::Input('settings[activecampaign][url]', $this->currency));
$div->appendChild($label);
$label = Widget::Label('API Key');
$label->appendChild(Widget::Input('settings[activecampaign][api-key]', $this->apiKey, 'password'));
$div->appendChild($label);
$group->appendChild($div);
$selectedDatasources = explode(',',$this->datasources);
$datasources = DatasourceManager::listAll();
$options = array();
foreach ($datasources as $handle => $datasource) {
$selected = in_array($handle,$selectedDatasources);
$options[] = array($handle, $selected, $datasource['name']);
}
$label = Widget::Label(__('Datasources to include for processing'));
$label->appendChild(Widget::Select(
"settings[activecampaign][datasources]",
$options,
array('multiple'=>'multiple')
));
$help = new XMLElement('p', __('Add datasources to complete transaction details.'));
$help->setAttribute('class', 'help');
$label->appendChild($help);
$group->appendChild($label);
$context['wrapper']->appendChild($group);
}
}