-
Notifications
You must be signed in to change notification settings - Fork 3
/
webform_to_gdocs.module
181 lines (146 loc) · 5.78 KB
/
webform_to_gdocs.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
<?php
require_once __DIR__ . '/google-api-php-client/src/Google/autoload.php';
/**
* Implements hook_menu().
*/
function webform_to_gdocs_menu() {
$items['admin/config/content/webform/overview'] = array(
'title' => 'Webform settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/content/webform/gdocs'] = array(
'title' => 'Webform to Google Docs',
'description' => 'Configure the Webform to Google Docs module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('webform_to_gdocs_configure_form'),
'access callback' => 'user_access',
'access arguments' => array('administer webform_to_gdocs'),
'file' => 'includes/webform_to_gdocs.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['node/%webform_menu/webform/gdocs'] = array(
'title' => 'Google Docs',
'page callback' => 'drupal_get_form',
'page arguments' => array('webform_to_gdocs_attach_form', 1),
'access callback' => 'user_access',
'access arguments' => array('attach webform to gdocs'),
'file' => 'includes/webform_to_gdocs.attach.inc',
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
$items['webform_to_gdocs/oauth2callback'] = array(
'page callback' => 'webform_to_gdocs_oauth2callback',
'access callback' => true,
'file' => 'includes/oauth2callback.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_perm().
*/
function webform_to_gdocs_perm() {
return array('administer webform_to_gdocs', 'attach webform to gdocs');
}
/**
* Implements hook_nodeapi().
*/
function webform_to_gdocs_nodeapi(&$node, $op) {
// Delete any webform_to_gdocs references to this node.
if ($op == 'delete') {
db_query("DELETE FROM {webform_to_gdocs_webforms} WHERE nid = %d", $node->nid);
}
}
/**
* Callbacks for a Webform submission.
*/
function webform_to_gdocs_webform_submission_insert($node, $submission) {
// Is this Webform attached to a Google Doc?
$qresult = db_query("SELECT * FROM {webform_to_gdocs_webforms} gw WHERE gw.nid = :nid LIMIT 1", array(':nid' =>$node->nid));
$result = $qresult->fetchAssoc();
if (0 == $qresult->rowCount() || empty($result['gdoc_name']) || empty($result['gdoc_sheet'])) {
return;
}
// Build out row to insert.
$google_row = array();
foreach ($submission->data as $key => $value) {
// Determine what data to store depending upon component type.
switch ($node->webform['components'][$key]['type']) {
case 'select':
$options = explode("\n", $node->webform['components'][$key]['extra']['items']);
$cleanOptions = array();
foreach ($options as $option) {
$tmp = explode('|', $option);
$cleanOptions[$tmp[0]] = $tmp[1];
}
$insert_value = $cleanOptions[$value['value'][0]];
break;
default:
$insert_value = implode(', ', $value['value']);
break;
}
$spreadsheet_column = $node->webform['components'][$key]['form_key'];
$google_row[$spreadsheet_column] = $insert_value;
}
try {
// Ensure we have a working access token.
_webform_to_gdocs_google_drive_get_service();
$access_token = variable_get('webform_to_gdocs_access_token', '');
$access_token = @json_decode($access_token);
// Insert it via Google Spreadsheets API.
$ch = curl_init();
$url = 'https://spreadsheets.google.com/feeds/list/' . $result['gdoc_name'] . '/' . $result['gdoc_sheet'] . '/private/full';
$post_body = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">';
foreach ($google_row as $col => $val) {
// Only allow alphanumeric + underscores in column name.
$col = preg_replace("/[^\da-z]/i", '', $col);
$post_body .= '<gsx:' . $col . '>' . check_plain($val) . '</gsx:' . $col . '>';
}
$post_body .= '</entry>';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $access_token->access_token,
'Content-type: application/atom+xml'
));
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (!in_array(intval($httpcode), array(200, 201))) {
throw new Exception("Invalid response code ({$httpcode}) from Google Docs API.");
}
curl_close($ch);
} catch (Exception $e) {
$msg = $e->getMessage();
watchdog('webform_to_gdocs', $msg);
$to = variable_get('webform_to_gdocs_error_email_address', false);
if ($to) {
mail($to, "Error inserting webform submission into Google doc, {$node->title}", $msg);
}
}
}
function _webform_to_gdocs_google_api_scopes() {
return array(
'https://www.googleapis.com/auth/drive',
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me',
);
}
function _webform_to_gdocs_google_drive_get_service() {
$client = new Google_Client();
$client->setApplicationName(variable_get('webform_to_gdocs_app_name', ''));
$client->setClientId(variable_get('webform_to_gdocs_client_id', ''));
$client->setClientSecret(variable_get('webform_to_gdocs_client_secret', ''));
$client->setScopes(_webform_to_gdocs_google_api_scopes());
$access_token = variable_get('webform_to_gdocs_access_token', '');
$client->setAccessToken($access_token);
if ($client->getAuth()->isAccessTokenExpired()) {
$access_token_decoded = json_decode($access_token);
$client->getAuth()->refreshToken($access_token_decoded->refresh_token);
$new_access_token = $client->getAccessToken();
variable_set('webform_to_gdocs_access_token', $new_access_token);
}
return new Google_Service_Drive($client);
}