diff --git a/components/file.inc b/components/file.inc index 4662011..76f8e8e 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/includes/webform.report.inc b/includes/webform.report.inc index 9f4ddb3..dbfb8b5 100644 --- a/includes/webform.report.inc +++ b/includes/webform.report.inc @@ -215,7 +215,7 @@ function template_preprocess_webform_results_submissions(&$vars) { * Create a table containing all submitted values for a webform node. */ function webform_results_table($node, $pager_count = 0) { - + // Determine whether views or hard-coded tables should be used for the submissions table. if (!webform_variable_get('webform_table')) { // Load and preview the results view with a node id argument @@ -748,7 +748,7 @@ function webform_results_download_form_submit(&$form, &$form_state) { // Allow a non-UI configuration to override the batch size. $batch_size = variable_get('webform_export_batch_size', $batch_size); - + $options['range']['batch_size'] = $batch_size; } @@ -1262,8 +1262,8 @@ function webform_results_batch_rows($node, $format = 'delimited', $options = arr // Display status message $context['message'] = t('Exported @count of @total submissions...', array('@count' => $context['sandbox']['serial'], '@total' => $context['sandbox']['sid_count'])); $context['finished'] = $context['sandbox']['batch_number'] < $context['sandbox']['batch_max'] - ? $context['sandbox']['batch_number'] / $context['sandbox']['batch_max'] - : 1.0; + ? $context['sandbox']['batch_number'] / $context['sandbox']['batch_max'] + : 1.0; $context['results']['last_sid'] = $context['sandbox']['last_sid']; } diff --git a/webform.info b/webform.info index a735218..9bd4a7f 100644 --- a/webform.info +++ b/webform.info @@ -7,6 +7,7 @@ configure = admin/config/content/webform php = 5.3 dependencies[] = ctools dependencies[] = views +dependencies[] = transliteration test_dependencies[] = token ; Files that contain classes: diff --git a/webform.module b/webform.module index 07c0ff3..7e8a028 100644 --- a/webform.module +++ b/webform.module @@ -1224,6 +1224,11 @@ 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 +1260,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 = transliteration_clean_filename($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) {