function webform_webform_submission_presave in Webform 7.3
Same name and namespace in other branches
- 7.4 webform.module \webform_webform_submission_presave()
Implements hook_webform_submission_update().
We implement our own hook here to facilitate the File component, which needs to clean up manage file usage records and delete files from submissions that have been edited if necessary.
File
- ./
webform.module, line 945 - This module provides a simple way to create forms and questionnaires.
Code
function webform_webform_submission_presave($node, &$submission) {
// Check if there are any file components in this submission and if any of
// them currently contain files.
$has_file_components = FALSE;
$new_fids = array();
$old_fids = array();
foreach ($node->webform['components'] as $cid => $component) {
if ($component['type'] == 'file') {
$has_file_components = TRUE;
if (!empty($submission->data[$cid]['value'])) {
foreach ($submission->data[$cid]['value'] as $key => $value) {
if (empty($value)) {
unset($submission->data[$cid]['value'][$key]);
}
}
$new_fids = array_merge($new_fids, $submission->data[$cid]['value']);
}
}
}
if ($has_file_components) {
// If we're updating a submission, build a list of previous files.
if (isset($submission->sid)) {
$old_submission = webform_get_submission($node->nid, $submission->sid, TRUE);
foreach ($node->webform['components'] as $cid => $component) {
if ($component['type'] == 'file') {
if (!empty($old_submission->data[$cid]['value'])) {
$old_fids = array_merge($old_fids, $old_submission->data[$cid]['value']);
}
}
}
}
// Save the list of added or removed files so we can add usage in
// hook_webform_submission_insert() or _update().
$submission->file_usage = array(
// Diff the old against new to determine what files were deleted.
'deleted_fids' => array_diff($old_fids, $new_fids),
// Diff the new files against old to determine new uploads.
'added_fids' => array_diff($new_fids, $old_fids),
);
}
}