-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebform_gdpr.module
201 lines (191 loc) · 5.95 KB
/
webform_gdpr.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* @file webform_gdpr.module
*
* Module hooks and callbacks.
*/
/**
* Implements hook_module_implements_alter().
*/
function webform_gdpr_module_implements_alter(&$implementations, $hook) {
$k = 'webform_gdpr';
if ( $hook == 'form_alter' ) {
// Ensure form_alter hook implementation is invoked after webform_civicrm.
if( isset($implementations[$k]) ) {
$group = $implementations[$k];
unset($implementations[$k]);
$implementations[$k] = $group;
}
}
}
/**
* Implements hook_webform_component_info()
*/
function webform_gdpr_webform_component_info() {
$components = array();
$components['gdpr'] = array(
'label' => t('GDPR'),
'description' => t('Terms & Conditions / Data Policy'),
'file' => 'includes/webform_gdpr_component.inc',
'value' => 1,
'conditional_type' => 'select',
'features' => array(
'analysis' => FALSE,
'csv' => FALSE,
'default_value' => FALSE,
'description' => TRUE,
'email' => TRUE,
'email_address' => 'FALSE',
'email_name' => FALSE,
'required' => TRUE,
'title' => TRUE,
'title_display' => TRUE,
'title_inline' => TRUE,
),
);
$components['gdpr_comms_prefs'] = array(
'label' => t('GDPR Comms Prefs'),
'description' => t('Communications Preferences'),
'file' => 'includes/webform_gdpr_comms_prefs_component.inc',
'value' => 1,
'features' => array(
'analysis' => FALSE,
'csv' => FALSE,
'default_value' => FALSE,
'description' => TRUE,
'email' => TRUE,
'email_address' => 'FALSE',
'email_name' => FALSE,
'required' => TRUE,
'title' => TRUE,
'title_display' => TRUE,
'title_inline' => TRUE,
),
);
return $components;
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for a webform_client_form.
*/
function webform_gdpr_form_webform_client_form_alter(&$form, &$form_state) {
if ($components = webform_gdpr_get_components($form['#node'])) {
$form['#submit'][] = 'webform_gdpr_submit';
$form['#attached']['js'][] = drupal_get_path('module', 'webform_gdpr') . '/js/webform_gdpr.js';
}
}
/**
*
*/
function webform_gdpr_validate_gdpr($element, &$form_state) {
if (empty($element['#value']) && $element['#required'] ) {
$msg = t("Please check %name.", array('%name' => strip_tags($element['#title'])));
form_error($element, $msg);
}
}
/**
* Validation callback function for Comms Prefs element.
*
*/
function webform_gdpr_validate_comms_prefs($element, &$form_state) {
// Which groups have been selected.
if (empty($element['groups'])) {
return;
}
$selected_channels = [];
foreach (element_children($element['channels']) as $key) {
$elem = $element['channels'][$key];
if ($elem['#value'] == 'YES') {
$selected_channels[] = $key;
}
}
foreach (element_children($element['groups']) as $key) {
// If the group is selected then at least one of it's channels
// should also be selected.
$elem = $element['groups'][$key];
if (!empty($elem['#value'])) {
$channels = $elem['#channels'];
if ($channels && empty(array_intersect($channels, $selected_channels))) {
$msg = t('You have selected @group but have opted out of all the channels that it uses.', array(
'@group' => $elem['#title'],
));
form_error($elem, $msg);
}
}
}
}
/**
* Submit callback for webform.
*/
function webform_gdpr_submit(&$form, &$form_state) {
if (!empty($form_state['save_draft']) || empty($form_state['webform_completed'])) {
return;
}
$components = webform_gdpr_get_components($form['#node'], 'gdpr');
$comms_prefs_components = webform_gdpr_get_components($form['#node'], 'gdpr_comms_prefs');
if (!$components && !$comms_prefs_components) {
return;
}
// Load the created submission to get contacts.
$sid = !empty($form_state['values']['details']['sid']) ? $form_state['values']['details']['sid'] : NULL;
$nid = !empty($form['#node']->nid) ? $form['#node']->nid : NULL;
if ($sid && $nid) {
$submission = webform_get_submission($nid, $sid);
}
if (empty($submission->civicrm['contact'])) {
return;
}
$contacts = $submission->civicrm['contact'];
// Handle gdpr components.
foreach ($components as $cid => $component) {
if (!empty($submission->data[$cid])) {
$contact_idx = $component['extra']['contact'];
$contact_id = !empty($contacts[$contact_idx]['id']) ? $contacts[$contact_idx]['id'] : 0;
if (!$contact_id) {
continue;
}
if (!empty($component['extra']['enable_data_policy'])) {
WebformGdpr::recordAcceptance($contact_id);
}
if (!empty($component['extra']['enable_tc_event'])) {
WebformGdpr::recordAcceptance($contact_id, 'Event', $component['extra']['tc_event']);
}
if (!empty($component['extra']['enable_tc_contribution'])) {
WebformGdpr::recordAcceptance($contact_id, 'ContributionPage', $component['extra']['tc_contribution']);
}
}
}
// Handle comms prefs components.
foreach ($comms_prefs_components as $cid => $component) {
$values = $submission->data[$cid];
$values = $values[0];
if (is_string($values)) {
$values = unserialize($values);
}
$contact_id = $contacts[1]['id'];
WebformGDPRCommsPrefs::processCommsPrefs($component, $values, $contact_id);
}
}
/**
* Returns gdpr components for a webform.
*
* @param stdObject $node
* Webform node.
* @param string $type
* Optional filter by component type.
* gdpr|gdpr_comms_prefs
*
* @return array
* Gdpr component definitions, keyed by component id.
*/
function webform_gdpr_get_components($node, $type = '') {
$gdpr_components = [];
$types = $type ? [$type] : ['gdpr', 'gdpr_comms_prefs'];
if (!empty($node->webform['components'])) {
foreach ($node->webform['components'] as $cid => $component) {
if (in_array($component['type'], $types)) {
$gdpr_components[$cid] = $component;
}
}
}
return $gdpr_components;
}