-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebform_remote_post.module
More file actions
290 lines (264 loc) · 10.2 KB
/
webform_remote_post.module
File metadata and controls
290 lines (264 loc) · 10.2 KB
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
<?php
/**
* @file
* Webform Remote Post module.
*
* Webform Remote Post is a module that works along the
* @link http://drupal.org/project/webform Webform @endlink module.
* It eases the integration between Webforms and other web
* applications.
*/
// Define pre/post save modes
define('WEBFORM_REMOTE_POST_MODE_PRE', 2);
define('WEBFORM_REMOTE_POST_MODE_POST', 1);
/**
* Implements hook_help().
*
* Displays help and module information.
*/
function webform_remote_post_help($path, $arg) {
switch ($path) {
case 'admin/help#webform_remote_post':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Webform Remote Post is a module that works along the Webform module. It eases the integration between Webforms and other web applications like Salesforce and Eloqua.') . '</p>';
$output .= '<p>' . t('Webform Remote Post works by POSTing form submissions to any arbitrary URL, presumably, an application or script that will use the form data and perform further processing of it. It respects the form\'s validation and will only send submissions that passed validation and are no longer in a draft state. Multiple remote posts can be setup for each individual form, allowing for the submission of data to multiple systems at once.') . '</p>';
$output .= '<h3>' . t('Use Cases') . '</h3>';
$output .= '<ul>';
$output .= '<li>' . t('CRM Integration – If you have a CRM like Salesforce, you can use this module to push submissions using the web-to-lead mechanism to create a Lead from every Webform submission in your Drupal site.') . '</li>';
$output .= '<li>' . t('Eloqua Integration – Create lead forms to be submitted to Eloqua. Add the hidden fields as indicated in Eloqua and it\'s ready to go.') . '</li>';
$output .= '<li>' . t('Re-posting to any 3rd party system – This module is general purpose. You need the form data to be immediately submitted to another system automatically? Add a remote post target to it!') . '</li>';
$output .= '</ul>';
return $output;
}
}
/**
* Implements hook_menu().
*
* @see webform_menu_load()
*/
function webform_remote_post_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/targets'] = array(
'title' => 'Remote Posts',
'page callback' => 'drupal_get_form',
'page arguments' => array('webform_remote_post_targets_form', 1),
'access callback' => 'user_access',
'access arguments' => array('admin webform remote posts'),
'file' => 'includes/webform_remote_post.targets.inc',
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
// Edit or create a target.
$items['node/%webform_menu/webform/targets/%webform_remote_post_menu_target'] = array(
'title' => 'Edit remote post settings',
'load arguments' => array(1),
'page arguments' => array(
'webform_remote_post_target_edit_form', 1, 4),
'access callback' => 'user_access',
'access arguments' => array('admin webform remote posts'),
'file' => 'includes/webform_remote_post.target.inc',
'type' => MENU_CALLBACK,
);
// Delete a target.
$items['node/%webform_menu/webform/targets/%/delete'] = array(
'title' => 'Delete repost target',
'load arguments' => array(1),
'page arguments' => array(
'webform_remote_post_target_delete_form_submit', 1, 4),
'access callback' => 'user_access',
'access arguments' => array('admin webform remote posts'),
'file' => 'includes/webform_remote_post.targets.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_webform_submission_insert().
*
* Respond to a Webform submission being inserted.
* Note: This hook fires POST save.
*/
function webform_remote_post_webform_submission_insert($node, $submission) {
// Skip draft forms:
if ($submission->is_draft) {
return;
}
// Get this webform node remote targets from the DB:
$targets = array();
$targets = db_select('webform_remote_post_targets')
->fields('webform_remote_post_targets')
->condition('nid', $node->nid)
->condition('enabled', 1)
->condition('mode', WEBFORM_REMOTE_POST_MODE_POST)
->execute()
->fetchAllAssoc('tid', PDO::FETCH_ASSOC);
webform_remote_post_process_targets($node, $submission, $targets);
}
/**
* Implements hook_webform_submission_presave().
*
* Respond to a Webform submission being inserted.
* Note: This hook fires POST save.
*/
function webform_remote_post_webform_submission_presave($node, $submission) {
// Skip draft forms:
if ($submission->is_draft) {
return;
}
// Get this webform node remote targets from the DB:
$targets = array();
$targets = db_select('webform_remote_post_targets')
->fields('webform_remote_post_targets')
->condition('nid', $node->nid)
->condition('enabled', 1)
->condition('mode', WEBFORM_REMOTE_POST_MODE_PRE)
->execute()
->fetchAllAssoc('tid', PDO::FETCH_ASSOC);
webform_remote_post_process_targets($node, $submission, $targets);
}
/**
* Callback to fire off HTTP POST(s) to configured targets.
* This is leveraged by the implementations of both
* hook_webform_submission_insert() and hook_webform_submission_presave().
*
* @todo Support of Grid component.
* @todo Support of File component.
*/
function webform_remote_post_process_targets($node, $submission, $targets) {
// Create a map with webform component ID's and the component themselves,
// handy later on.
$component_map = array();
foreach ($node->webform['components'] as $component) {
$component_map[$component['cid']] = $component;
}
// Prepare the submission data for remote posting. Creating a two-dimensional
// array of form field names and the data.
$payload = array();
foreach ($submission->data as $cid => $component_data) {
// Handle different data structure for webform 4.x:
if (webform_remote_post_webform_version() == '4') {
$payload[$component_map[$cid]['form_key']] = implode(', ', $component_data);
}
else {
$payload[$component_map[$cid]['form_key']] = implode(', ', $component_data['value']);
}
}
// Acceptable server response codes.
$benign_reponse_codes = array('200', '301', '302', '307');
// Repost data to each target.
foreach ($targets as $tid => $target) {
if ($target['extra']) {
$extra = unserialize($target['extra']);
$extra = explode("\n", $extra);
foreach ($extra as $k => $v) {
unset($extra[$k]);
$v = explode('|', $v);
$payload[$v[0]] = isset($v[1]) ? $v[1] : '';
}
}
// Allow other modules to modify payload and target.
drupal_alter('webform_remote_post_repost', $payload, $target);
// Set default referer value if required
if (empty($target['referer'])) {
$target['referer'] = $GLOBALS['base_url'];
}
if($target['type'] == 'json'){
// JSON Encode the payload.
$post_data = json_encode($payload);
// Repost data to each target. Begin by setting the
// options for drupal_http_request().
$drupal_http_request_options = array(
'method' => 'POST',
'data' => $post_data,
'timeout' => 15,
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => '*/*',
'Referer' => $target['referer']
),
);
}
else {
// URL-encode the payload.
$post_data = drupal_http_build_query($payload);
// Repost data to each target. Begin by setting the
// options for drupal_http_request().
$drupal_http_request_options = array(
'method' => 'POST',
'data' => $post_data,
'timeout' => 15,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Referer' => $target['referer']
),
);
}
// Where the magic happens:
$request = drupal_http_request($target['url'], $drupal_http_request_options);
// Log any errors.
if (isset($request->code) && !in_array($request->code, $benign_reponse_codes)) {
$log_msg = 'A remote (%type) post to %url by webform node ID %id returned a \'%code\' code, which is a different HTTP response code than expected. Please make sure that the remote post URL is correct in the Remote Posts webform settings, and that the post was received in the remote system.';
watchdog(
'webform_remote_post',
$log_msg,
array(
'%id' => $node->nid,
'%url' => $target['url'],
'%code' => $request->code,
'%type' => $target['type'],
),
WATCHDOG_WARNING);
module_invoke_all('webform_remote_post_request_invalid', array('node' => $node, 'submission' => $submission, 'request' => $request, 'payload' => $payload) );
}
}
}
/**
* Implements hook_perm().
*/
function webform_remote_post_permission() {
return array(
'admin webform remote posts' => array(
'title' => t('Admin webform remote posts'),
'description' => t('Grants access to the "Remote Posts" webform settings on all webform content.'),
),
);
}
/**
* Implements hook_theme().
*/
function webform_remote_post_theme($existing, $type, $theme, $path) {
$theme = array(
// webform_remote_posts.targets.inc.
'webform_remote_post_targets_form' => array(
'render element' => 'form',
'file' => 'includes/webform_remote_post.targets.inc',
),
);
return $theme;
};
/**
* Menu loader callback. Load a remote post target if the given tid is a valid.
*/
function webform_remote_post_menu_target_load($tid, $nid) {
module_load_include('inc', 'webform_remote_post', 'includes/webform_remote_post.targets');
$target = webform_remote_post_target_load($tid, $nid);
watchdog('webform_remote_post',"The target id to load is $tid");
watchdog('webform_remote_post','The auto-loaded target is ' . print_r($target, true));
return $target;
}
/**
* Helper function to fetch webform major version number.
*/
function webform_remote_post_webform_version() {
$info = system_get_info('module', 'webform');
$version = $info['version'];
if ($version) {
$version = explode('-', $version);
$version = explode('.', $version[1]);
return $version[0];
}
}