/
webform_multiple_file.module
93 lines (82 loc) · 3.15 KB
/
webform_multiple_file.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
<?php
/**
* @file
* Provides functions and hook implementations for webform multiple file module.
*/
/**
* Value for field API indicating a field accepts an unlimited number of values.
*/
define('WEBFORM_MULTIPLE_FILE_CARDINALITY_UNLIMITED', -1);
/**
* Implements hook_webform_component_info().
*/
function webform_multiple_file_webform_component_info() {
$components = array();
webform_component_include('file');
$components['multiple_file'] = array(
'label' => t('Multiple file'),
'description' => t('Multiple file upload support for webform.'),
'features' => array(
'conditional' => FALSE,
'attachment' => TRUE,
),
'file' => 'webform_multiple_file.webform.inc',
);
return $components;
}
/**
* Implements hook_webform_submission_presave().
*
* @see webform_webform_submission_presave()
*/
function webform_multiple_file_webform_submission_presave($node, &$submission) {
$has_file_components = FALSE;
$new_fids = array();
$old_fids = array();
$renameable = array();
foreach ($node->webform['components'] as $cid => $component) {
if ($component['type'] == 'multiple_file') {
$has_file_components = TRUE;
if (!empty($submission->data[$cid])) {
foreach ($submission->data[$cid] as $key => $value) {
if (empty($value)) {
unset($submission->data[$cid][$key]);
}
if (strlen($component['extra']['rename'])) {
$renameable[$cid][] = $value;
}
}
$new_fids = array_merge($new_fids, $submission->data[$cid]);
}
}
}
if ($has_file_components) {
// If we're updating a submission, build a list of previous files.
if (isset($submission->sid)) {
backdrop_static_reset('webform_get_submission');
$old_submission = webform_get_submission($node->nid, $submission->sid);
foreach ($node->webform['components'] as $cid => $component) {
if ($component['type'] == 'multiple_file') {
if (!empty($old_submission->data[$cid])) {
$old_fids = array_merge($old_fids, $old_submission->data[$cid]);
}
}
}
}
// Only rename files if this is the first time the submission is being saved as finished.
if ($submission->is_draft || (isset($old_submission) && !$old_submission->is_draft)) {
$renameable = array();
}
// Save the list of added or removed files so we can add usage in
// hook_webform_submission_insert() or _update().
// Also we should keep $submission->file_usage values from previous hook implementations.
$deleted_fids = isset($submission->file_usage['deleted_fids']) ? $submission->file_usage['deleted_fids'] : array();
$added_fids = isset($submission->file_usage['added_fids']) ? $submission->file_usage['added_fids'] : array();
$previous_renameable = isset($submission->file_usage['renameable']) ? $submission->file_usage['renameable'] : array();
$submission->file_usage = array(
'deleted_fids' => array_merge($deleted_fids, array_diff($old_fids, $new_fids)),
'added_fids' => array_merge($added_fids, array_diff($new_fids, $old_fids)),
'renameable' => $previous_renameable + $renameable,
);
}
}