-
Notifications
You must be signed in to change notification settings - Fork 0
/
uw_webform_to_sf.module
113 lines (95 loc) · 3.88 KB
/
uw_webform_to_sf.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
<?php
/**
* @file
* Webform to SF module
*
* The webfrom to sf module is based on webform remote post it works along the
* @link http://drupal.org/project/webform Webform @endlink module.
*
*/
/**
* Implements hook_menu().
*
* @see webform_menu_load()
*/
function uw_webform_to_sf_menu() {
$items = array();
// Targets list, %webform_menu is an auto-loader wildcard component
// provided by the webform module (method is webform_menu_load), and it
// auto-loads a webform node.
$items['node/%webform_menu/webform/sfconfig'] = array(
'title' => 'Send to Salesforce',
'page callback' => 'drupal_get_form',
'page arguments' => array('uw_webform_to_sf_config_form', 1),
'access callback' => 'user_access',
'access arguments' => array('admin webform to sf'),
'file' => 'includes/uw_webform_to_sf_config.inc',
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_perm().
*/
function uw_webform_to_sf_permission() {
return array(
'admin webform to sf' => array(
'title' => t('Admin webform to sf'),
'description' => t('Grants access to the "Send to Salesforce" webform settings on all webform content.'),
),
);
}
function uw_webform_to_sf_webform_submission_insert($node, $submission) {
//save the form and attachments to salesforce, requires an appropriate webservice to be setup
$attachment_map = array();
$componentinfo = '';
$config = db_select('webform_to_sf_config')
->fields('webform_to_sf_config', array('target', 'sobject', 'enabled'))
->condition('nid', $node->nid)
->execute()->fetchAssoc();
if ($config && $config['enabled']) {
//service url
$url = $config['target'];
$data = '';
$payload = array();
$payload['targetobject'] = $config['sobject'];
$payload['files'] = array();
foreach ($node->webform['components'] as $component) {
if (isset($submission->data[$component['cid']])) {
if ($component['type'] == 'file') {
if (isset($submission->data[$component['cid']][0])) {
$attachment_map[$component['cid']] = $component;
$component_data = $submission->data[$component['cid']];
$query = db_select('file_managed');
$query->fields('file_managed', array('filename', 'uri', 'filemime'));
$query->condition('fid', $submission->data[$component['cid']][0]);
$result = $query->execute()->fetchAssoc();
$thefile = array();
$thefile['name'] = $result['filename'];
$thefile['mime'] = $result['filemime'];
$thefile['data'] = base64_encode(file_get_contents($result['uri']));
array_push($payload['files'], $thefile);
}
}
else {
$payload[$component['form_key']] = implode(';', $submission->data[$component['cid']]);
}
}
}
$json_encoded = json_encode($payload);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_encoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json_encoded))
);
$result = curl_exec($ch);
if ($result === FALSE) {
watchdog('uw_webform_to_sf', 'Webfrom to SF post to salesforce failed with error: ' . curl_error($ch), array(), WATCHDOG_ERROR);
}
}
}