-
Notifications
You must be signed in to change notification settings - Fork 0
/
webform_salesforce.module
426 lines (376 loc) · 12.6 KB
/
webform_salesforce.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/*
* Main hooks for Webform Salesforce module
*/
/*
* Implements hook_menu().
*/
function webform_salesforce_menu() {
$items = array();
$items['node/%webform_menu/webform/salesforce'] = array(
'title' => 'Salesforce settings',
'page callback' => 'webform_salesforce_configure_page',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'file' => 'webform_salesforce.admin.inc',
'weight' => 3,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_theme().
*/
function webform_salesforce_theme($existing, $type, $theme, $path) {
return array(
'webform_salesforce_mapping_form_table' => array(
'render element' => 'elements',
'file' => 'webform_salesforce.admin.inc',
),
);
}
/*
* Implements hook_node_delete().
*/
function webform_salesforce_node_delete($node) {
// We're only interested in nodes of type 'webform'
if ('webform' == $node->type) {
// Delete all mapping related to this node
_webform_salesforce_delete_map($node, TRUE);
}
}
/*
* Implements hook_node_revision_delete().
*/
function webform_salesforce_node_revision_delete($node) {
// We're only interested in nodes of type 'webform'
if ('webform' == $node->type) {
// Delete a single revision's mappings
_webform_salesforce_delete_map($node);
}
}
/**
* Implements hook_node_load().
*/
function webform_salesforce_node_load($nodes, $types) {
$result = db_query("SELECT * FROM {wfsf_mappings} m WHERE m.nid IN (:nids)", array(':nids' => array_keys($nodes)), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $record) {
$nodes[$record['nid']]->uses_salesforce = FALSE;
$tmp = unserialize($record['map']);
if (!empty($tmp)) {
$nodes[$record['nid']]->uses_salesforce = TRUE;
$nodes[$record['nid']]->salesforce_object = $record['object'];
}
}
}
/**
* Implements hook_entitycache_ENTITY_TYPE_load().
*/
function webform_salesforce_entitycache_node_load($entities) {
webform_salesforce_node_load($entities, array());
}
/*
* Start of Drupal Webform 3.x hook implementations
*/
/*
* Implements hook_webform_submission_insert().
*/
function webform_salesforce_webform_submission_insert($node, $submission) {
$form_cfg = webform_salesforce_get_map($node);
$mappings = (!empty($form_cfg['map'])) ? $form_cfg['map'] : NULL;
$sf_object = (!empty($form_cfg['object'])) ? $form_cfg['object'] : NULL;
// Make sure we have the mappings and SF object type
if (!empty($mappings) && $sf_object) {
// Build xref for Webform components keyed to the field name:
$wf_fields = array();
foreach ($node->webform['components'] as $idx => $field) {
$wf_fields[$field['form_key']] = $idx;
}
$sub_fields = array();
foreach ($mappings as $sf_field => $wf_field) {
if (isset($wf_fields[$wf_field])) {
$field = $wf_fields[$wf_field];
if (isset($submission->data[$field]['value']) && isset($submission->data[$field]['value'][0])) {
$sub_fields[$sf_field] = implode(';', $submission->data[$field]['value']);
}
}
}
// If this is a new submit, generate an unique id to use to link the submission with salesforce
// If this is a resubmit then use the same key, so a duplicate record doesn't get added in salesforce
$sub_fields['WebformSID__c'] = $node->nid . "-" . _webform_salesforce_generate_sid();
// Call Salesforce API to make the submission but let other modules to alter
// the data or intercept submission.
// Assume other modules won't handle the submit to Salesforce
$is_handled = FALSE;
// Allow other modules to handle the submit to Salesforce
// @note $sub_fields is passed by reference
foreach (module_implements('webform_salesforce_submit') as $module) {
$function = $module . '_webform_salesforce_submit';
$new_id = $function($node->salesforce_object, $sub_fields, $submission);
if ($new_id != FALSE) {
$is_handled = TRUE;
}
}
// If it wasn't handled by another module, then submit it to Salesforce
if ($is_handled == FALSE) {
webform_salesforce_submit_to_salesforce($node->salesforce_object, $sub_fields, $submission);
}
}
}
/**
* Implements hook_webform_submission_load().
*/
function webform_salesforce_webform_submission_load(&$submissions) {
foreach ($submissions as $sid => $submission) {
$result = db_select('wfsf_submissions', 'w')
->fields('w', array('sfid', 'time'))
->condition('sid', $sid,'=')
->execute()
->fetchAssoc();
if (is_array($result) && isset($result['sfid']) && !empty($result['sfid'])) {
$submissions[$sid]->salesforce_record_id = $result['sfid'];
$submissions[$sid]->salesforce_record_save_time = $result['time'];
}
}
}
/**
* Handles Salesforce submissions
*
* @param string $sf_obj_type
* @param array $sub_fields
* @param array $submission
*/
function webform_salesforce_submit_to_salesforce($sf_object_key, $sub_fields, $submission) {
$sfapi = salesforce_get_api();
// Not authorized, we need to bail this time around.
if (!$sfapi->isAuthorized()) {
if (user_access('administer salesforce')) {
drupal_set_message(t('Unable to connect to Salesforce using <a href="!url">current credentials</a>.', array('!url' => url(SALESFORCE_PATH_ADMIN))), 'error');
}
return FALSE;
}
// Populate the Salesforce Object array with gathered fields
$sObject = Array();
foreach ($sub_fields as $sf_field => $value) {
$sObject[$sf_field] = $value;
}
try {
if (isset($sub_fields['WebformSID__c']) && !empty($sub_fields['WebformSID__c'])) {
$response = $sfapi->objectUpsert($sf_object_key, 'WebformSID__c', $sub_fields['WebformSID__c'], $sObject);
}
else {
$response = $sfapi->objectCreate($sf_object_key, $sObject);
}
}
catch (Exception $e) {
watchdog('salesforce', 'Exception while attempting to create/upsert webform submission: %msg <pre>%e</pre>', array('%msg' => $e->getMessage(), '%e' => print_r($e, TRUE)), WATCHDOG_ERROR, l('node ' . $submission->nid, 'node/' . $submission->nid));
}
// If the upsert/create was successful...
if (isset($response['success']) && $response['success']) {
$sf_id = $response['id'];
// Saving submission success message in Watchdog.
watchdog('salesforce', 'Webform submission saved successfully in Salesforce with SF ID: %sf_id', array('%sf_id' => $sf_id), WATCHDOG_INFO, l('node ' . $submission->nid, 'node/' . $submission->nid));
// Saving Salesforce record ID in relation to the webform submission.
db_insert('wfsf_submissions')
->fields(array(
'sid' => $submission->sid,
'sfid' => $sf_id,
'time' => time(),
))
->execute();
}
else {
if (user_access('administer salesforce')) {
drupal_set_message(t('Exception while attempting to create/upsert webform submission: %msg. See watchdog for more details.', array('%msg' => $e->getMessage())), 'error');
}
}
}
/**
* Describe a list of possible Salesforce objects supported
*/
function webform_salesforce_describe_objects() {
$sfapi = salesforce_get_api();
// Not authorized, we need to bail this time around.
if (!$sfapi->isAuthorized()) {
return;
}
// Note that we're filtering SF object types to a reasonable subset.
$sfobjects = $sfapi->objects(array(
'updateable' => TRUE,
));
if (empty($sfobjects) || !is_array($sfobjects)) {
drupal_set_message(t('There was an error retrieving the list of Salesforce objects. Please verify that your Salesforce instance is properly configured.'), 'error');
return;
}
else {
$options = array();
foreach ($sfobjects as $obj) {
$options[$obj['name']] = $obj['label'];
}
}
$sf_objects = variable_get('salesforce_api_enabled_objects', array('Account', 'Contact'));
$list = array();
foreach ($sf_objects as $obj) {
// We filter the objects not included in the array retrieved from SalesForce.
if (isset($options[$obj])) {
$key = strtolower($obj);
$label = isset($options[$obj]) ? $options[$obj] : $obj;
$list[$key] = array('value' => $label, 'object' => $obj);
}
}
// Note: allow other modules to define their own custom objects or
// modify existing ones.
webform_salesforce_get_available_objects($list);
return $list;
}
/**
* Retrieves a list of supported Salesforce objects
* @param array $sf_objects
* @return type
*/
function webform_salesforce_get_available_objects(&$sf_objects) {
// @note using foreach since module_invoke_all doesn't allow pass by reference
foreach (module_implements('webform_salesforce_objects') as $module) {
$function = $module . '_webform_salesforce_objects';
$function($sf_objects);
}
}
/**
* Retrieves a list of supported Salesforce object fields
*
* @param string
* $sf_object Salesforce object key stored in mapping
*
* @return type
*/
function webform_salesforce_get_available_fields($sf_object_key) {
$sfapi = salesforce_get_api();
// Not authorized, we need to bail this time around.
if (!$sfapi->isAuthorized()) {
return;
}
// Fetching object description
$sf_fields_by_object = $sfapi->objectDescribe($sf_object_key);
$sf_fields = array();
if (!empty($sf_fields_by_object) && is_array($sf_fields_by_object['fields'])) {
foreach ($sf_fields_by_object['fields'] as $field) {
$required = !$field['nillable'] && $field['createable'] & !$field['defaultedOnCreate'] ? TRUE : FALSE;
$sf_fields[$field['name']] = array(
'fieldid' => $field['name'],
'label' => $field['label'],
'required' => $required,
'object' => $sf_fields_by_object['name'],
'value' => $sf_fields_by_object['label'],
'object_key' => $sf_object_key,
);
}
// @note using foreach since module_invoke_all doesn't allow pass by reference
foreach (module_implements('webform_salesforce_fields') as $module) {
$function = $module . '_webform_salesforce_fields';
$function($sf_object_key, $sf_fields);
}
return $sf_fields;
}
else {
return array();
}
}
/*
* Helper function to retrieve field mappings for this form (node).
* May be called by other modules implementing module hooks.
*/
function webform_salesforce_get_map($node) {
static $cfg = array();
if (!is_object($node)) {
$node = node_load($node);
}
if (!$node || empty($node->nid)) {
return;
}
$nid = $node->nid;
$vid = (!empty($node->vid)) ? $node->vid : 0;
if (empty($cfg[$nid][$vid])) {
$cfg[$nid][$vid] = array();
$result = db_select('wfsf_mappings', 'wfsf')
->fields('wfsf')
->condition('nid', $nid)
->condition('vid', $vid)
->execute();
if ($result) {
while ($row = $result->fetchAssoc()) {
$tmp = unserialize($row['map']);
if ($tmp) {
$cfg[$nid][$vid]['map'] = $tmp;
}
$tmp = $row['object'];
if ($tmp) {
$cfg[$nid][$vid]['object'] = $tmp;
}
}
}
}
return $cfg[$nid][$vid];
}
/*
* Start of "private" utility functions for this module
*/
/*
* Helper function to delete field mappings for this form (node) when it is deleted
*/
function _webform_salesforce_delete_map($node, $all_revs = FALSE) {
if (!is_object($node)) {
$node = node_load($node);
}
if (!$node || empty($node->nid)) {
return;
}
$nid = $node->nid;
$vid = (!empty($node->vid)) ? $node->vid : 0;
$query = db_delete('wfsf_mappings')
->condition('nid', $nid);
if (!$all_revs) {
$query->condition('vid', $vid);
}
$result = $query->execute();
return $result;
}
/*
* Helper function to set field mappings for this form (node)
*/
function _webform_salesforce_set_map($node, $form_cfg) {
if (!is_object($node)) {
$node = node_load($node);
}
if (!$node || empty($node->nid)) {
return;
}
$nid = $node->nid;
$vid = (!empty($node->vid)) ? $node->vid : 0;
$map_data = isset($form_cfg['map']) ? serialize($form_cfg['map']) : '';
$sf_obj = $form_cfg['object'];
// Using delete then insert approach
db_delete('wfsf_mappings')
->condition('nid', $nid)
->condition('vid', $vid)
->execute();
$result = db_insert('wfsf_mappings')
->fields(array(
'nid' => $nid,
'vid' => $vid,
'map' => $map_data,
'object' => $sf_obj,
))->execute();
return $result;
}
/**
* Returns a unique Salesforce Id that can be used as the external ID to
* map the submission in drupal to the salesforce data
*
* @return
* 8 character ID generated using MD5 uniquid and random number generator
*/
function _webform_salesforce_generate_sid() {
$sid = md5(uniqid(rand(), TRUE));
return substr($sid, 0, 8);
}