-
Notifications
You must be signed in to change notification settings - Fork 1
/
webform_external.module
352 lines (271 loc) · 11.4 KB
/
webform_external.module
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
<?php
/**
* @file
* Dump information from webforms into an external system using an API
*/
if (variable_get('webform_external_civi_enabled',false)) {
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'webform_external') . '/apis/' . 'civicrm-rest-api.class.php';
}
if (variable_get('webform_external_salsa_enabled',false)) {
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'webform_external') . '/apis/' . 'salsa-api.class.php';
}
if (variable_get('webform_external_actionnetwork_enabled',false)) {
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'webform_external') . '/apis/' . 'actionnetwork.class.php';
}
if (variable_get('webform_external_mcommons_enabled',false)) {
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'webform_external') . '/apis/' . 'mobilecommons.class.php';
}
/**
* Implements hook_menu().
*/
function webform_external_menu() {
$items = array();
$items['node/%webform_menu/webform/external'] = array(
'title' => 'Form external',
'page callback' => 'drupal_get_form',
'page arguments' => array('webform_external_manage_options',1),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'file' => 'webform_external.admin.inc',
'weight' => 3,
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/services/webform-external'] = array(
'title' => 'Configure Webform External',
'description' => 'Choose which external APIs are available, and set up site-wide defaults. Each webform will need to be enabled separately, but will inherit these settings as defaults.',
'page callback' => 'drupal_get_form',
'page arguments' => array('webform_external_config'),
'access arguments' => array('administer site configuration'),
'file' => 'webform_external.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/*
function structure (move to object-oriented architecture for 7.x-3.0 or webform_rest):
webform_external_webform_submission_insert: loads options, checks for whether service is enabled and has necessary info, calls _webform_external_push_to_[service]:
* maps submitted and default data to an array ($[service]_data) using
_webform_external_map_webform_components to process [service]_field_mapping and [service]_field_defaults
* converts $[service]_data into a format that can be consumed by the API
* checks that $[service]_data has necessary data (e.g., a valid email address & postal code for Action Network, phone for Mobile Commons)
_webform_external_map_webform_components: maps submission data to an array
* needs to handle boolean mapping
* needs to handle multiple key->value mappings, either of the form
[service]_field_1|form_key_1
[service]_field_1|form_key_2
or
[service]_field_1|form_key_1,form_key_2
* concatenate multiple values of data (either from form component submission data being an array, or from multiple fields) into comma-separated strings
* returns an array of the format:
(
'civi_field_1' => 'value1',
'civi_field_2' => 'value2,value3',
)
* also has to use [service]_field_defaults to add default values to the $data array
* needs to handle multiple key->value mapping, in the same way as above (except values will be given, not match a form_key)
_webform_external_add_value_to_data(&$data, $key, $value):
* adds $value to $webform_external_data[$key]
*/
/**
* Implements hook_webform_submission_insert().
*/
function webform_external_webform_submission_insert($node, $submission) {
$debug = "*** DEBUG INFO ***";
// get the external-submission info for this form
$query = db_select('webform_external_options','o');
$query->fields('o')->condition('nid',$node->nid);
$result = $query->execute();
foreach ($result as $row) {
$options = $row;
}
// CiviCRM
if (variable_get('webform_external_civi_enabled') && $options->civi_enabled
&& $options->civi_rest_url && $options->civi_key && $options->civi_api_key && $options->civi_field_mapping) {
_webform_external_push_to_civi($node, $submission, $options);
}
// Salsa
if (variable_get('webform_external_salsa_enabled') && $options->salsa_enabled
&& $options->salsa_node_url && $options->salsa_account_email && $options->salsa_password
&& $options->salsa_field_mapping && $options->salsa_organization_key) {
_webform_external_push_to_salsa($node, $submission, $options);
}
// Action Network
if (variable_get('webform_external_actionnetwork_enabled') && $options->actionnetwork_enabled
&& $options->actionnetwork_api_key && $options->actionnetwork_field_mapping) {
_webform_external_push_to_actionnetwork($node, $submission, $options);
}
// Mobile Commons
if (variable_get('webform_external_mcommons_enabled') && $options->mcommons_enabled
&& $options->mcommons_username && $options->mcommons_password && $options->mcommons_api_key
&& $options->mcommons_field_mapping) {
_webform_external_push_to_mcommons($node, $submission, $options);
}
}
/**
* API-specific functions
*/
function _webform_external_push_to_civi($node, $submission, $options) {
$data = _webform_external_map_webform_components($node, $submission, $options->civi_field_mapping, $options->civi_field_defaults);
// $debug = '$data:'."\n\n".print_r($data,1)."\n\n";
/* convert data to associative array structured for CiviCRM API */
$address = array();
$phone = array();
$website = array();
$email = array();
foreach($data as $k => $v) {
switch($k) {
/* email fields -> array */
case 'email':
$email[$k] = $v;
break;
case 'email_type_id':
$email['location_type_id'] = $v;
break;
/* address fields -> array */
case 'street_address':
case 'supplemental_address_1':
case 'supplemental_address_2':
case 'city':
case 'state':
case 'postal_code':
case 'country':
case 'location_type_id':
$address[$k] = $v;
break;
/* phone fields -> array */
case 'phone':
case 'phone_type_id':
$phone[$k] = $v;
break;
/* website fields -> array */
case 'url':
case 'website_type_id':
$website[$k] = $v;
break;
/* tags */
case 'tags':
$civi_data['EntityTag'] = explode(',',$v);
break;
/* tags */
case 'groups':
$civi_data['GroupContact'] = explode(',',$v);
break;
/* everything else */
default:
$civi_data[$k] = $v;
break;
}
}
if ($email) {
$email['is_primary'] = 1;
$civi_data['email'] = $email;
}
if ($address) { $civi_data['address'] = $address; }
if ($phone) { $civi_data['phone'] = $phone; }
if ($website) { $civi_data['website'] = $website; }
// assume contact_type is Individual unless otherwise indicated
if (!isset($civi_data['contact_type'])) { $civi_data['contact_type'] = 'Individual'; }
/* must have a first name, last name, email or display_name */
if (!isset($civi_data['first_name']) && !isset($civi_data['last_name']) && !isset($civi_data['email']) && !isset($civi_data['display_name'])) {
return;
}
// $debug .= "Civi data:\n\n".print_r($civi_data,1)."\n\n";
$civi = new CiviAPI($options->civi_rest_url, $options->civi_key, $options->civi_api_key);
$response = $civi->create_contact($civi_data);
global $user;
if (isset($user->uid) && ($user->uid == 1) && $debug) {
drupal_set_message('<h2>webform external debug info:</h2><pre>'.$debug.'</pre>');
}
}
function _webform_external_push_to_salsa($node, $submission, $options) {
$data = _webform_external_map_webform_components($node, $submission, $options->salsa_field_mapping, $options->salsa_field_defaults);
$salsa = new SalsaAPI($options->salsa_node_url, $options->salsa_account_email, $options->salsa_password);
$salsa->save_supporter($data);
}
function _webform_external_push_to_actionnetwork($node, $submission, $options) {
// mail('[email protected]','_webform_external_push_to_actionnetwork called','',"From: [email protected]\r\n");
$data = _webform_external_map_webform_components($node, $submission, $options->actionnetwork_field_mapping, $options->actionnetwork_field_defaults);
$actionnetwork = new ActionNetwork($options->actionnetwork_api_key);
// must have an email and a postal code
if (!isset($data['email']) || !$data['email'] || !isset($data['postal_code']) || !$data['postal_code']) { return; }
$tags = null;
if (isset($data['tags'])) {
$tags = explode(',',$data['tags']);
unset($data['tags']);
}
// mail('[email protected]','_webform_external_push_to_actionnetwork data',print_r($data,1),"From: [email protected]\r\n");
$response = $actionnetwork->signupPerson($data, $tags);
// mail('[email protected]','_webform_external_push_to_actionnetwork response',print_r($response,1),"From: [email protected]\r\n");
}
function _webform_external_push_to_mcommons($node, $submission, $options) {
$data = _webform_external_map_webform_components($node, $submission, $options->mcommons_field_mapping, $options->mcommons_field_defaults);
$mcommons = new MobileCommons($options->mcommons_username, $options->mcommons_password, $options->mcommons_api_key);
// must have a phone number
if (!isset($data['phone_number']) || !$data['phone_number']) { return; }
$response = $mcommons->profile_update($data);
// mail('[email protected]','_webform_external_push_to_mcommons response',print_r($response,1),"From: [email protected]\r\n");
}
/**
* Utility functions
*/
function _webform_external_map_webform_components($node, $submission, $mapping, $defaults) {
$debug = '';
// $debug .= '$submission->data:'."\n\n".print_r($submission->data,1)."\n\n";
// get webform version info
$module_info = system_get_info('module','webform');
$version = substr($module_info['version'],4,1);
// get data into a format keyed by form_key
$data_by_key = array();
$components = $node->webform['components'];
foreach ($components as $i => $component) {
$key = $component['form_key'];
$data_by_key[$key] = ($version == 3) ? $submission->data[$i]['value'] : $submission->data[$i];
}
$data = array();
$boolean = '';
// load mapping data
$mapping_lines = explode("\n",$mapping);
foreach ($mapping_lines as $l) {
$kv = explode('|',trim($l));
if ($kv[0] == 'boolean') { $boolean .= $boolean ? ',' : ''; $boolean .= $kv[1]; continue; }
if (!isset($kv[1]) || !$kv[1]) { $kv[1] = $kv[0]; }
$form_keys = explode(',', $kv[1]);
foreach ($form_keys as $k) {
if (isset($data_by_key[$k]) && $data_by_key[$k]) {
_webform_external_add_value_to_data($data, $kv[0], $data_by_key[$k]);
}
}
}
// load boolean data
$boolean_form_keys = explode(',',$boolean);
foreach ($boolean_form_keys as $key) {
if (isset($data_by_key[$key]) && $data_by_key[$key]) {
foreach($data_by_key[$key] as $boolean_field) {
_webform_external_add_value_to_data($data, $boolean_field, 1);
}
}
}
// $debug .= '$boolean: '.$boolean."\n\n".'$boolean_form_keys:'."\n\n".print_r($boolean_form_keys,1)."\n\n";
// load defaults
$defaults_lines = explode("\n",$defaults);
foreach ($defaults_lines as $l) {
$kv = explode('|',trim($l));
if (!isset($kv[1]) || !$kv[1]) { continue; }
_webform_external_add_value_to_data($data, $kv[0], $kv[1]);
}
// $debug .= '$data:'."\n\n".print_r($data,1)."\n\n";
return $data;
}
function _webform_external_add_value_to_data(&$data, $key, $value) {
if (is_array($value)) {
foreach ($value as $v) {
_webform_external_add_value_to_data($data, $key, $v);
}
return;
}
if (isset($data[$key]) && $data[$key]) {
$data[$key] .= ','. (string) $value;
} else {
$data[$key] = (string) $value;
}
}