-
Notifications
You must be signed in to change notification settings - Fork 1
/
webform_salsa.module
159 lines (129 loc) · 5.1 KB
/
webform_salsa.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
<?php
/**
* @file
* Dump information from webforms into an external system using an API
*/
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'webform_salsa') . '/' . 'webform_salsa.apis.inc';
/**
* Implements hook_menu().
*/
function webform_salsa_menu() {
$items = array();
$items['node/%webform_menu/webform/salsa'] = array(
'title' => 'Salsa',
'page callback' => 'drupal_get_form',
'page arguments' => array('webform_salsa_manage_options',1),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'file' => 'webform_salsa.admin.inc',
'weight' => 3,
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/content/webform-salsa'] = array(
'title' => 'Configure Salsa Webform defaults',
'description' => 'Set up site-wide defaults for Salsa Webform integration. Each webform will need to be enabled separately, but will inherent these settings as defaults.',
'page callback' => 'drupal_get_form',
'page arguments' => array('webform_salsa_config'),
'access arguments' => array('access administration pages'),
'file' => 'webform_salsa.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_webform_submission_insert().
*/
function webform_salsa_webform_submission_insert($node, $submission) {
$debug = "*** DEBUG INFO ***";
// get the external-submission info for this form
$query = db_select('webform_salsa_options','o');
$query->fields('o')->condition('nid',$node->nid);
$result = $query->execute();
foreach ($result as $row) {
$info = $row;
}
// get the component form_keys, to match to field mapping
$cid_mapping = _webform_salsa_webform_component_mapping($node);
if ($info->salsa_enabled && $info->salsa_node_url && $info->salsa_account_email
&& $info->salsa_password && $info->salsa_field_mapping & $info->salsa_organization_key) {
$salsa_mapping_lines = explode("\n",$info->salsa_field_mapping);
foreach ($salsa_mapping_lines as $l) {
$kv = explode('|',trim($l));
if (!isset($kv[1]) || !$kv[1]) { $kv[1] = $kv[0]; }
$salsa_mapping[$kv[0]] = $kv[1];
}
// $debug .= "\n\nCivi_mapping:\n\n".print_r($civi_mapping,1);
$salsa = new SalsaAPI($info->salsa_node_url, $info->salsa_account_email, $info->salsa_password);
foreach($salsa_mapping as $k => $v) {
$salsa_data[$k] = isset($submission->data[$cid_mapping[$v]]['value'][0]) ? $submission->data[$cid_mapping[$v]]['value'][0] : '';
}
$apifields = array('xml' => 1, 'object' => 'supporter', 'organization_KEY' => $info->salsa_organization_key);
$salsaGroups = array();
$callSalsa = false;
foreach($salsa_data as $k => $v) {
if ($v) { $apifields[$k] = $v; $callSalsa = true; }
}
if ($info->salsa_chapter_key) {
$apifields['chapter_KEY'] = $info->salsa_chapter_key;
}
if ($info->salsa_boolean_field_mapping) {
$boolean_field_mapping = explode("\n", $info->salsa_boolean_field_mapping);
foreach ($boolean_field_mapping as $f) {
$sfs = $submission->data[$cid_mapping[$f]]['value'];
foreach ($sfs as $sf) {
$apifields[$sf] = 1;
}
}
}
if ($info->salsa_groups_field_mapping) {
$groups_field_mapping = explode("\n", $info->salsa_groups_field_mapping);
foreach ($groups_field_mapping as $g) {
$sgs = $submission->data[$cid_mapping[$g]]['value'];
foreach ($sgs as $sg) {
$salsaGroups[] = $sg;
}
}
}
if ($info->salsa_group_id) {
$salsaGroups[] = $info->salsa_group_id;
}
if ($info->salsa_autoresponder_ids) {
$apifields['email_trigger_KEYS'] = $info->salsa_autoresponder_ids;
}
if ($info->salsa_tag) {
$apifields['tag'] = $info->salsa_tag;
}
// total hack for LGK, because I forgot to include action_KEY on module database!
if ($info->nid == 34) {
$apifields['action_KEY'] = 10747;
}
$debug .= "\n\nSalsa mapping:\n\n".print_r($salsa_mapping,1);
$debug .= "\n\nSalsa data:\n\n".print_r($salsa_data,1);
$debug .= "\n\napifields:\n\n".print_r($apifields,1);
$debug .= "\n\nsalsaGroups:\n\n".print_r($salsaGroups,1);
// while we're developing, don't call Salsa
// $callSalsa = false;
$debug .= "\n\nCID Mapping:\n\n".print_r($cid_mapping,1);
$debug .= "\n\nsubmission:\n\n".print_r($submission,1);
// mail('[email protected]','webform external debug',$debug,"From: Firestick Dev <[email protected]>\r\n");
if ($callSalsa) {
// $auth = $salsa->authenticate();
$response = $salsa->call('save',$apifields,$salsaGroups);
}
$debug .= "\n\nauth:\n\n".print_r($auth,1);
$debug .= "\n\nSalsa response:\n\n".print_r($response,1);
}
// $debug .= "\n\nCID Mapping:\n\n".print_r($cid_mapping,1);
// $debug .= "\n\n__________________\n\nExternal information:\n\n".print_r($info,1);
// $debug .= "\n\n__________________\n\nSubmission:\n\n".print_r($submission,1);
// mail('[email protected]','webform external debug',$debug,"From: Webskillet Dev <[email protected]>\r\n");
}
function _webform_salsa_webform_component_mapping($node) {
$mapping = array();
$components = $node->webform['components'];
foreach ($components as $i => $component) {
$key = $component['form_key'];
$mapping[$key] = $i;
}
return $mapping;
}