-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebform_remote_post.module
More file actions
198 lines (184 loc) · 6.96 KB
/
Copy pathwebform_remote_post.module
File metadata and controls
198 lines (184 loc) · 6.96 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
<?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.
*/
/**
* 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,
);
// 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'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_webform_submission_insert().
*
* Respond to a Webform submission being inserted.
*
* Note that this hook is called after a submission has already been saved to
* the database.
*
* This is the main purpose of this module, to emit HTTP POSTs based on the
* saved webform data.
*
* @todo Support of Grid component.
* @todo Support of File component.
*/
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)
->execute()
->fetchAllAssoc('tid', PDO::FETCH_ASSOC);
// Create a map with webform component ID's and the component themselves,
// handly 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) {
$payload[$component_map[$cid]['form_key']] = implode(', ', $component_data['value']);
}
// URL-encode the payload.
$post_data = 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'),
);
// Acceptable server response codes.
$benign_reponse_codes = array('200', '301', '302', '307');
// Repost data to each target.
foreach ($targets as $tid => $target) {
// 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 post to %url by webform node ID %id returned a ';
$log_msg .= '\'%code\' code, which is a different HTTP response code ';
$log_msg .= 'than expected. Please make sure that the remote post URL ';
$log_msg .= 'is correct in the Remote Posts webform settings, and that ';
$log_msg .= 'the post was received in the remote system.';
watchdog(
'webform_remote_post',
$log_msg,
array(
'%id' => $node->nid,
'%url' => $target['url'],
'%code' => $request->code,
),
WATCHDOG_WARNING);
}
}
}
/**
* 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;
};