diff --git a/components/file.inc b/components/file.inc index 4662011..976483c 100644 --- a/components/file.inc +++ b/components/file.inc @@ -21,6 +21,8 @@ function _webform_defaults_file() { 'addextensions' => '', 'size' => '2 MB', ), + 'use_rename' => FALSE, + 'rename' => '', 'scheme' => 'public', 'directory' => '', 'progress_indicator' => 'throbber', @@ -156,6 +158,27 @@ function _webform_edit_file($component) { '#field_prefix' => 'webform/', ); + $form['extra']['use_rename'] = array( + '#type' => 'checkbox', + '#title' => t('Rename file name after upload'), + '#default_value' => $component['extra']['use_rename'], + '#description' => t('Check this option if You want to rename file name after uploaded.'), + '#weight' => 6, + ); + + $form['extra']['rename'] = array( + '#type' => 'textfield', + '#title' => t('New File Name'), + '#default_value' => $component['extra']['rename'], + '#description' => t('Rename the file using tokens to create a pattern. Don\'t include an extension; it will be added automatically.').' '.theme('webform_token_help', array('groups' => array('node', 'submission'))), + '#weight' => 7, + '#states' => array( + 'visible' => array( + ':input[name="extra[use_rename]"]' => array('checked' => TRUE), + ), + ), + ); + $form['display']['progress_indicator'] = array( '#type' => 'radios', '#title' => t('Progress indicator'), diff --git a/webform.module b/webform.module index 54e394c..96d7a83 100644 --- a/webform.module +++ b/webform.module @@ -1224,6 +1224,9 @@ function webform_webform_submission_presave($node, &$submission) { } } $new_fids = array_merge($new_fids, $submission->data[$cid]); + if ($component['extra']['use_rename'] && !empty($component['extra']['rename'])) { + webform_file_process_rename_action($node, $submission, $component, $submission->data[$cid][0]); + } } } } @@ -1255,6 +1258,49 @@ function webform_webform_submission_presave($node, &$submission) { } /** + * Function to rename the uploaded file name based on the text we entered on + * file component. + * + * @param $node + * The webform node object. + * @param $submission + * The webform submission object. + * @param $component + * Component settings array for which fid is going to be processed. + * @param $fid + * A file id to be processed. + */ +function webform_file_process_rename_action($node, $submission, $component, $fid) { + webform_component_include('file'); + $file = webform_get_file($fid); + + if ($file) { + // Get the destination uri. + $scheme = $component['extra']['scheme']; + $directory = $component['extra']['directory']; + $destination_dir = file_stream_wrapper_uri_normalize($scheme . '://webform/' . $directory); + + // Get the file extension. + $info = pathinfo($file->uri); + $extension = $info['extension']; + + // Prepare new file name without extension. + $rename = webform_replace_tokens($component['extra']['rename'], $node, $submission, NULL, TRUE); + $new_file_name = _webform_safe_name($rename); + + if (!empty($new_file_name)) { + // Prepare the new uri with new filename. + $destination = $destination_dir ."/$new_file_name.$extension"; + + // Compare the uri and Rename the file name. + if ($file->uri != $destination) { + file_move($file, $destination, FILE_EXISTS_RENAME); + } + } + } +} + +/** * Implements hook_webform_submission_insert(). */ function webform_webform_submission_insert($node, $submission) {