-
Notifications
You must be signed in to change notification settings - Fork 2
/
webform_share.module
392 lines (362 loc) · 13.9 KB
/
webform_share.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* @file
* Module to handle importing and exporting of webforms, as well as adding the
* ability to set content type defaults.
*/
/**
* Implements hook_config_info().
*/
function webform_share_config_info() {
$prefixes['webform_share.settings'] = array(
'label' => t('Webform Share settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_permission().
*/
function webform_share_permission() {
return array(
'access webform share functionality' => array(
'title' => t('Configure Webform Share'),
'description' => t("This permission enables the user to import, export and set content-type defaults for webforms. The latter also requires the 'administer content type' permission."),
'restrict access' => FALSE,
),
);
}
/**
* Helper function to get the content type defaults.
*/
function webform_share_node_type_defaults($type) {
if (in_array($type, webform_variable_get('webform_node_types'))) {
return config_get('webform_share.settings', 'webform_default_components_' . $type);
}
return '';
}
/**
* Implements hook_menu().
*/
function webform_share_menu() {
$items = array();
$items['node/%webform_menu/webform/ws-export'] = array(
'title' => 'Export',
'page callback' => 'webform_share_export',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'weight' => 31,
'type' => MENU_LOCAL_TASK,
);
$items['node/%webform_menu/webform/ws-import'] = array(
'title' => 'Import',
'page callback' => 'backdrop_get_form',
'page arguments' => array('webform_share_components_update_form', 1),
'access callback' => 'webform_share_menu_access',
'access arguments' => array(1),
'weight' => 30,
'type' => MENU_LOCAL_TASK,
);
$items['node/%webform_menu/webform/ws-reset'] = array(
'title' => 'Reset',
'page callback' => 'backdrop_get_form',
'page arguments' => array('webform_share_components_update_form', 1, TRUE),
'access callback' => 'webform_share_menu_access',
'access arguments' => array(1, TRUE),
'weight' => 32,
'type' => MENU_LOCAL_TASK,
);
$items['node/%webform_menu/webform/ws-default'] = array(
'title' => 'Set default',
'description' => 'Set this webform as the default for the current content type.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('webform_share_set_default_confirm',1),
'access callback' => 'webform_share_menu_access',
'access arguments' => array(1, FALSE, TRUE),
'weight' => 33,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Menu access callback.
* Custom check on both the user access and node access.
*/
function webform_share_menu_access($node, $reset = FALSE, $default = FALSE) {
if (node_access('update', $node)) {
// We can only reset if there is some defaults set.
if ($reset && !webform_share_node_type_defaults($node->type)) {
return FALSE;
}
// We can only set default if user has 'administer content types'
// permission.
if ($default && !user_access('administer content types')) {
return FALSE;
}
return user_access('access webform share functionality');
}
return FALSE;
}
/**
* This form is used to update or to reset the webform.
*/
function webform_share_components_update_form($form, &$form_state, $node, $op = 'import') {
$form_state['node'] = $node;
$form['components_only'] = array(
'#type' => 'checkbox',
'#title' => t('Update components and conditionals only'),
'#default_value' => 1,
'#description' => t('If unchecked, the emails and form settings will be overridden.'),
);
$form['keep_existing_components'] = array(
'#type' => 'checkbox',
'#title' => t('Keep existing components that are not included in the import'),
'#default_value' => 1,
'#description' => t('If unchecked, the existing components that do not map to an imported webform form_key will be deleted. All submission data associated with those components will be lost.'),
);
$form['import'] = array(
'#type' => 'textarea',
'#title' => t('Import code'),
'#default_value' => '',
'#description' => t('Copy the code that was generated from a webform share export.'),
'#required' => TRUE,
);
if ($op == 'reset') {
$form['import']['#access'] = FALSE;
$form['import']['#default_value'] = config_get('webform_share.settings', 'webform_default_components_' . $node->type);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => $op == 'reset' ? t('Reset') : t('Import'),
);
return $form;
}
/**
* Menu callback for 'Set default'.
* Check that the user wants to proceed with a confirm_form.
*/
function webform_share_set_default_confirm($form, &$form_state, $node) {
$type = $node->type;
$type_name = node_type_get_name($node);
$nid = $node->nid;
$title = $node->title;
$form['nid'] = array('#type' => 'value', '#value' => $nid);
$form['type'] = array('#type' => 'value', '#value' => $type);
$form['type-name'] = array('#type' => 'value', '#value' => $type_name);
$question = t('Are you sure you want to set this webform as the default for the current content type?');
$path = 'node/' . $nid . '/webform';
$description = '<p>' . t('This will replace any existing default webform configuration for the %type_name content type with the webform configuration on %title.', array(
'%title' => $title,
'%type_name' => $type_name,
)) . '</p>';
$yes = t('Set default');
return confirm_form($form, $question, $path, $description, $yes);
}
/**
* Set current node webform config as default for content type.
*/
function webform_share_set_default_confirm_submit($form, &$form_state) {
$node = node_load($form_state['values']['nid']);
$webform_share = backdrop_json_encode($node->webform);
config_set('webform_share.settings', 'webform_default_components_' . $form_state['values']['type'], (string) $webform_share);
backdrop_set_message(t('The webform configuration from %title has been set as the default for the %type_name content type.', array(
'%title' => $node->title,
'%type_name' => $form_state['values']['type-name'],
)),'status', FALSE);
$form_state['redirect'] = 'node/' . $form_state['values']['nid'] . '/webform';
return;
}
/**
* Submit callback to update the node.
*/
function webform_share_components_update_form_submit($form, &$form_state) {
$node = $form_state['node'];
if ($webform = backdrop_json_decode($form_state['values']['import'])) {
$hook_params = array(
'operation' => 'update',
'node' => $node,
'options' => array(
'components_only' => $form_state['values']['components_only'],
'keep_existing_components' => $form_state['values']['keep_existing_components'],
),
);
backdrop_alter('webform_share_import', $webform, $hook_params);
// Load the original node and use this to map any fields based off
// the form_key parameter. We need to preserve the existing cid if
// possible to preserve the submission data.
$original = node_load($node->nid, NULL, TRUE);
$existing_components = array();
foreach ($original->webform['components'] as $cid => $component) {
$existing_components[$component['form_key']] = $cid;
}
// Get the max cid for this node's webform so we know where our safe
// starting point is.
$current_max_cid = webform_share_get_next_max_cid($node->nid);
// Overwrite the entire form if the user is updating everything.
if (empty($form_state['values']['components_only'])) {
$node->webform = $webform;
$webform['nid'] = $node->nid;
}
$old_to_new_mappings = array();
// Map the imported components to the existing webform components.
$node->webform['components'] = array();
foreach ($webform['components'] as $index => $component) {
if (isset($existing_components[$component['form_key']])) {
$cid = $existing_components[$component['form_key']];
unset($existing_components[$component['form_key']]);
}
else {
$cid = ++$current_max_cid;
}
$old_to_new_mappings[$component['cid']] = $cid;
if (!empty($component['pid'])) {
// This mapping assumes that parents always come before children.
if (empty($old_to_new_mappings[$component['pid']])) {
backdrop_set_message(t('Unable to find the correct fieldset for %component.', array(
'%component' => $component['name'],
)), 'error', TRUE);
}
else {
$component['pid'] = $old_to_new_mappings[$component['pid']];
}
}
// Set the new cid.
$component['cid'] = $cid;
$component['nid'] = $node->nid;
$node->webform['components'][$cid] = $component;
}
// Map the imported conditionals.
$webform['conditionals'] = isset($webform['conditionals']) ? $webform['conditionals'] : array();
foreach ($webform['conditionals'] as $index => $conditional) {
foreach ($conditional['rules'] as $rindex => $rule) {
if (isset($rule['source']) && $rule['source'] != NULL) {
$rule['nid'] = $node->nid;
if (!empty($old_to_new_mappings[$rule['source']])) {
$rule['source'] = $old_to_new_mappings[$rule['source']];
}
else {
backdrop_set_message(t('Unable to find the correct sources for conditionals.'), 'error', FALSE);
}
$conditional['rules'][$rindex] = $rule;
}
}
foreach ($conditional['actions'] as $aindex => $action) {
if (isset($action['target']) && $action['target'] != NULL) {
$action['nid'] = $node->nid;
if (!empty($old_to_new_mappings[$action['target']])) {
$action['target'] = $old_to_new_mappings[$action['target']];
}
else {
backdrop_set_message(t('Unable to find the correct targets for conditionals.'), 'error', FALSE);
}
$conditional['actions'][$aindex] = $action;
}
}
$node->webform['conditionals'][$index] = $conditional;
}
// If requested, re-add the existing components rather than allowing these
// to be deleted. Existing cid values are safe to reuse.
if (!empty($form_state['values']['keep_existing_components'])) {
foreach ($existing_components as $form_key => $cid) {
$node->webform['components'][$cid] = $original->webform['components'][$cid];
}
}
if (isset($node->webform['emails'])) {
foreach ($node->webform['emails'] as $index => $component) {
$node->webform['emails'][$index]['nid'] = $node->nid;
}
}
node_save($node);
}
$form_state['redirect'] = 'node/' . $node->nid . '/webform';
}
/**
* Retrieves that last "cid" for a node's webform.
*
* @param int $nid
* @return int
*/
function webform_share_get_next_max_cid($nid) {
$max_cid = db_select('webform_component', 'w')
->fields('w', array('cid'))
->condition('nid', $nid)
->orderBy('cid', 'DESC')
->range(0, 1)
->execute()
->fetchField();
return (empty($max_cid) ? 0 : $max_cid);
}
/**
* Menu callback to generate the webform dump.
*/
function webform_share_export($node) {
backdrop_alter('webform_share_export', $node);
$webform_share = backdrop_json_encode($node->webform);
backdrop_add_http_header('Content-Type', 'application/json; charset=utf-8');
backdrop_add_http_header('Content-Disposition', 'attachment; filename="webform-' . $node->type . '-' . $node->nid . '.json";');
print $webform_share;
exit();
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* This hooks into the node type form to add the webform share default settings
* textarea.
*/
function webform_share_form_node_type_form_alter(&$form, $form_state) {
// Targets content type edit forms
if (isset($form['type'])) {
// Only adds the element to content types that have been tag for webforms.
if (in_array($form['#node_type']->type, webform_variable_get('webform_node_types'))) {
// Make sure that the user has permission.
if (user_access('access webform share functionality')) {
$form['webform']['webform_share'] = array(
'#type' => 'textarea',
'#title' => t('Webform default components'),
'#default_value' => config_get('webform_share.settings', 'webform_default_components_' . $form['#node_type']->type),
'#access' => user_access('access webform share functionality'),
'#description' => t('Copy the code that was generated from a webform share export.'),
);
$form['#submit'][] = 'webform_share_form_node_type_form_submit';
}
}
}
}
function webform_share_form_node_type_form_submit(&$form, $form_state) {
if (isset($form_state['values']['webform_share'])) {
config_set('webform_share.settings', 'webform_default_components_' . $form_state['values']['type'], (string) $form_state['values']['webform_share']);
}
}
/**
* Implements hook_node_presave().
*/
function webform_share_node_presave(Node $node) {
if ($type_defaults = webform_share_node_type_defaults($node->type)) {
$webform_defaults = backdrop_json_decode($type_defaults);
if ($webform = $webform_defaults) {
$hook_params = array(
'operation' => 'insert',
'node' => $node,
'options' => array(
'components_only' => FALSE,
'keep_existing_components' => FALSE,
),
);
backdrop_alter('webform_share_import', $webform, $hook_params);
$node->webform = $webform;
$node->webform['nid'] = $node->nid;
if (isset($node->webform['components'])) {
$node->webform['components'] = array_filter((array) $node->webform['components']);
foreach ($node->webform['components'] as $index => $component) {
$node->webform['components'][$index]['nid'] = $node->nid;
}
}
if (isset($node->webform['emails'])) {
foreach ($node->webform['emails'] as $index => $email) {
$node->webform['emails'][$index]['nid'] = $node->nid;
}
}
}
}
}