Révision a45e4bc1
Ajouté par Assos Assos il y a environ 9 ans
drupal7/sites/all/modules/webform/components/file.inc | ||
---|---|---|
12 | 12 |
return array( |
13 | 13 |
'name' => '', |
14 | 14 |
'form_key' => NULL, |
15 |
'mandatory' => 0,
|
|
15 |
'required' => 0,
|
|
16 | 16 |
'pid' => 0, |
17 | 17 |
'weight' => 0, |
18 | 18 |
'extra' => array( |
... | ... | |
21 | 21 |
'addextensions' => '', |
22 | 22 |
'size' => '2 MB', |
23 | 23 |
), |
24 |
'rename' => '', |
|
24 | 25 |
'scheme' => 'public', |
25 | 26 |
'directory' => '', |
26 | 27 |
'progress_indicator' => 'throbber', |
... | ... | |
28 | 29 |
'description' => '', |
29 | 30 |
'attributes' => array(), |
30 | 31 |
'private' => FALSE, |
32 |
'analysis' => FALSE, |
|
31 | 33 |
), |
32 | 34 |
); |
33 | 35 |
} |
... | ... | |
45 | 47 |
'render element' => 'element', |
46 | 48 |
'file' => 'components/file.inc', |
47 | 49 |
), |
50 |
'webform_managed_file' => array( |
|
51 |
'render element' => 'element', |
|
52 |
'file' => 'components/file.inc', |
|
53 |
), |
|
48 | 54 |
); |
49 | 55 |
} |
50 | 56 |
|
... | ... | |
77 | 83 |
'js' => array(drupal_get_path('module', 'webform') . '/js/webform-admin.js'), |
78 | 84 |
'css' => array(drupal_get_path('module', 'webform') . '/css/webform-admin.css'), |
79 | 85 |
), |
86 |
'#type' => 'webform_file_extensions', |
|
80 | 87 |
'#weight' => 2, |
81 | 88 |
); |
82 | 89 |
|
... | ... | |
99 | 106 |
'#default_value' => array_intersect($current_types, $types), |
100 | 107 |
); |
101 | 108 |
|
102 |
$types = array('txt', 'rtf', 'html', 'odf', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'xml');
|
|
109 |
$types = array('txt', 'rtf', 'html', 'pdf', 'doc', 'docx', 'odt', 'ppt', 'pptx', 'odp', 'xls', 'xlsx', 'ods', 'xml');
|
|
103 | 110 |
$form['validation']['extensions']['types']['documents'] = array( |
104 | 111 |
'#type' => 'checkboxes', |
105 | 112 |
'#title' => t('Documents'), |
... | ... | |
149 | 156 |
'#type' => 'textfield', |
150 | 157 |
'#title' => t('Upload directory'), |
151 | 158 |
'#default_value' => $component['extra']['directory'], |
152 |
'#description' => t('You may optionally specify a sub-directory to store your files.'), |
|
159 |
'#description' => t('You may optionally specify a sub-directory to store your files.') . ' ' . theme('webform_token_help'),
|
|
153 | 160 |
'#weight' => 5, |
154 | 161 |
'#field_prefix' => 'webform/', |
155 | 162 |
); |
156 | 163 |
|
164 |
$form['extra']['rename'] = array( |
|
165 |
'#type' => 'textfield', |
|
166 |
'#title' => t('Rename files'), |
|
167 |
'#default_value' => $component['extra']['rename'], |
|
168 |
'#description' => t('You may optionally use tokens to create a pattern used to rename files upon submission. Omit the extension; it will be added automatically.') . ' ' . theme('webform_token_help', array('groups' => array('node', 'submission'))), |
|
169 |
'#weight' => 6, |
|
170 |
'#element_validate' => array('_webform_edit_file_rename_validate'), |
|
171 |
); |
|
172 |
|
|
157 | 173 |
$form['display']['progress_indicator'] = array( |
158 | 174 |
'#type' => 'radios', |
159 | 175 |
'#title' => t('Progress indicator'), |
... | ... | |
185 | 201 |
return $form; |
186 | 202 |
} |
187 | 203 |
|
204 |
/** |
|
205 |
* A Form API element validate function to ensure that the rename string is |
|
206 |
* either empty or contains at least one token. |
|
207 |
*/ |
|
208 |
function _webform_edit_file_rename_validate($element, &$form_state, $form) { |
|
209 |
$rename = trim($form_state['values']['extra']['rename']); |
|
210 |
form_set_value($element, $rename, $form_state); |
|
211 |
if (strlen($rename) && !count(token_scan($rename))) { |
|
212 |
form_error($element, t('To create unique file names, use at least one token in the file name pattern.')); |
|
213 |
} |
|
214 |
} |
|
215 |
|
|
188 | 216 |
/** |
189 | 217 |
* A Form API element validate function to check filesize is valid. |
190 | 218 |
*/ |
... | ... | |
213 | 241 |
$directory = $element['extra']['directory']['#value']; |
214 | 242 |
|
215 | 243 |
$destination_dir = file_stream_wrapper_uri_normalize($scheme . '://webform/' . $directory); |
244 |
$tokenized_dir = drupal_strtolower(webform_replace_tokens($destination_dir, $element['#node'])); |
|
216 | 245 |
|
217 | 246 |
// Sanity check input to prevent use parent (../) directories. |
218 |
if (preg_match('/\.\.[\/\\\]/', $destination_dir . '/')) {
|
|
219 |
form_error($element['extra']['directory'], t('The save directory %directory is not valid.', array('%directory' => $directory)));
|
|
247 |
if (preg_match('/\.\.[\/\\\]/', $tokenized_dir . '/')) {
|
|
248 |
form_error($element['extra']['directory'], t('The save directory %directory is not valid.', array('%directory' => $tokenized_dir)));
|
|
220 | 249 |
} |
221 | 250 |
else { |
222 |
$destination_success = file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY); |
|
223 |
if (!$destination_success) { |
|
224 |
form_error($element['extra']['directory'], t('The save directory %directory could not be created. Check that the webform files directory is writable.', array('%directory' => $directory))); |
|
251 |
if (!file_prepare_directory($tokenized_dir, FILE_CREATE_DIRECTORY)) { |
|
252 |
form_error($element['extra']['directory'], t('The save directory %directory could not be created. Check that the webform files directory is writable.', array('%directory' => $tokenized_dir))); |
|
225 | 253 |
} |
226 | 254 |
} |
227 | 255 |
|
... | ... | |
241 | 269 |
foreach (array_keys($element['types'][$category]['#value']) as $extension) { |
242 | 270 |
if ($element['types'][$category][$extension]['#value']) { |
243 | 271 |
$extensions[] = $extension; |
272 |
// jpeg is an exception. It is allowed anytime jpg is allowed. |
|
273 |
if ($extension == 'jpg') { |
|
274 |
$extensions[] = 'jpeg'; |
|
275 |
} |
|
244 | 276 |
} |
245 | 277 |
} |
246 | 278 |
} |
... | ... | |
302 | 334 |
/** |
303 | 335 |
* Implements _webform_render_component(). |
304 | 336 |
*/ |
305 |
function _webform_render_file($component, $value = NULL, $filter = TRUE) { |
|
337 |
function _webform_render_file($component, $value = NULL, $filter = TRUE, $submission = NULL) {
|
|
306 | 338 |
$node = isset($component['nid']) ? node_load($component['nid']) : NULL; |
307 | 339 |
|
308 | 340 |
// Cap the upload size according to the PHP limit. |
... | ... | |
314 | 346 |
|
315 | 347 |
$element = array( |
316 | 348 |
'#type' => 'managed_file', |
317 |
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'], |
|
349 |
'#theme' => 'webform_managed_file', |
|
350 |
'#title' => $filter ? webform_filter_xss($component['name']) : $component['name'], |
|
318 | 351 |
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before', |
319 |
'#required' => $component['mandatory'],
|
|
352 |
'#required' => $component['required'],
|
|
320 | 353 |
'#default_value' => isset($value[0]) ? $value[0] : NULL, |
321 | 354 |
'#attributes' => $component['extra']['attributes'], |
322 | 355 |
'#upload_validators' => array( |
... | ... | |
324 | 357 |
'file_validate_extensions' => array(implode(' ', $component['extra']['filtering']['types'])), |
325 | 358 |
), |
326 | 359 |
'#pre_render' => array_merge(element_info_property('managed_file', '#pre_render'), array('webform_file_allow_access')), |
327 |
'#upload_location' => $component['extra']['scheme'] . '://webform/' . $component['extra']['directory'], |
|
360 |
'#upload_location' => $component['extra']['scheme'] . '://webform/' . ($filter |
|
361 |
? drupal_strtolower(webform_replace_tokens($component['extra']['directory'], $node)) |
|
362 |
: $component['extra']['directory']), |
|
328 | 363 |
'#progress_indicator' => $component['extra']['progress_indicator'], |
329 |
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
|
|
364 |
'#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'], |
|
330 | 365 |
'#weight' => $component['weight'], |
331 | 366 |
'#theme_wrappers' => array('webform_element'), |
332 | 367 |
'#translatable' => array('title', 'description'), |
333 | 368 |
); |
334 | 369 |
|
370 |
if ($filter) { |
|
371 |
$element['#description'] = theme('file_upload_help', array('description' => $element['#description'], 'upload_validators' => $element['#upload_validators'])); |
|
372 |
} |
|
373 |
|
|
335 | 374 |
return $element; |
336 | 375 |
} |
337 | 376 |
|
377 |
/** |
|
378 |
* Returns HTML for a webform managed file element. |
|
379 |
* |
|
380 |
* See #2495821 and #2497909. The core theme_file_managed_file creates a |
|
381 |
* wrapper around the element with the element's id, thereby creating 2 elements |
|
382 |
* with the same id. |
|
383 |
* |
|
384 |
* @param $variables |
|
385 |
* An associative array containing: |
|
386 |
* - element: A render element representing the file. |
|
387 |
* |
|
388 |
*/ |
|
389 |
function theme_webform_managed_file($variables) { |
|
390 |
$element = $variables['element']; |
|
391 |
|
|
392 |
$attributes = array(); |
|
393 |
|
|
394 |
// For webform use, do not add the id to the wrapper. |
|
395 |
//if (isset($element['#id'])) { |
|
396 |
// $attributes['id'] = $element['#id']; |
|
397 |
//} |
|
398 |
|
|
399 |
if (!empty($element['#attributes']['class'])) { |
|
400 |
$attributes['class'] = (array) $element['#attributes']['class']; |
|
401 |
} |
|
402 |
$attributes['class'][] = 'form-managed-file'; |
|
403 |
|
|
404 |
// This wrapper is required to apply JS behaviors and CSS styling. |
|
405 |
$output = ''; |
|
406 |
$output .= '<div' . drupal_attributes($attributes) . '>'; |
|
407 |
$output .= drupal_render_children($element); |
|
408 |
$output .= '</div>'; |
|
409 |
return $output; |
|
410 |
} |
|
411 |
|
|
412 |
|
|
338 | 413 |
/** |
339 | 414 |
* Implements _webform_submit_component(). |
340 | 415 |
*/ |
341 | 416 |
function _webform_submit_file($component, $value) { |
342 |
if (is_array($value)) { |
|
343 |
return !empty($value['fid']) ? $value['fid'] : ''; |
|
344 |
} |
|
345 |
else { |
|
346 |
return !empty($value) ? $value : ''; |
|
417 |
$fid = is_array($value) |
|
418 |
? (!empty($value['fid']) ? $value['fid'] : '') |
|
419 |
: (!empty($value) ? $value : ''); |
|
420 |
// Extend access to this file, even if the submission has not been saved yet. This may happen when |
|
421 |
// previewing a private file which was selected but not explicitly uploaded, and then previewed. |
|
422 |
if ($fid) { |
|
423 |
$_SESSION['webform_files'][$fid] = $fid; |
|
347 | 424 |
} |
425 |
return $fid; |
|
348 | 426 |
} |
349 | 427 |
|
350 | 428 |
/** |
... | ... | |
369 | 447 |
/** |
370 | 448 |
* Implements _webform_display_component(). |
371 | 449 |
*/ |
372 |
function _webform_display_file($component, $value, $format = 'html') { |
|
450 |
function _webform_display_file($component, $value, $format = 'html', $submission = array()) {
|
|
373 | 451 |
$fid = isset($value[0]) ? $value[0] : NULL; |
374 | 452 |
return array( |
375 | 453 |
'#title' => $component['name'], |
454 |
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before', |
|
376 | 455 |
'#value' => $fid ? webform_get_file($fid) : NULL, |
377 | 456 |
'#weight' => $component['weight'], |
378 | 457 |
'#theme' => 'webform_display_file', |
... | ... | |
417 | 496 |
/** |
418 | 497 |
* Implements _webform_analysis_component(). |
419 | 498 |
*/ |
420 |
function _webform_analysis_file($component, $sids = array()) { |
|
499 |
function _webform_analysis_file($component, $sids = array(), $single = FALSE, $join = NULL) {
|
|
421 | 500 |
$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC)) |
422 | 501 |
->fields('wsd', array('no', 'data')) |
423 |
->condition('nid', $component['nid']) |
|
424 |
->condition('cid', $component['cid']); |
|
502 |
->condition('wsd.nid', $component['nid'])
|
|
503 |
->condition('wsd.cid', $component['cid']);
|
|
425 | 504 |
|
426 | 505 |
if (count($sids)) { |
427 |
$query->condition('sid', $sids, 'IN'); |
|
506 |
$query->condition('wsd.sid', $sids, 'IN'); |
|
507 |
} |
|
508 |
|
|
509 |
if ($join) { |
|
510 |
$query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid'); |
|
428 | 511 |
} |
429 | 512 |
|
430 | 513 |
$nonblanks = 0; |
... | ... | |
443 | 526 |
|
444 | 527 |
$rows[0] = array(t('Left Blank'), ($submissions - $nonblanks)); |
445 | 528 |
$rows[1] = array(t('User uploaded file'), $nonblanks); |
446 |
$rows[2] = array(t('Average uploaded file size'), ($sizetotal != 0 ? (int) (($sizetotal/$nonblanks)/1024) . ' KB' : '0')); |
|
447 |
return $rows; |
|
529 |
$other[0] = array(t('Average uploaded file size'), ($sizetotal != 0 ? (int) (($sizetotal/$nonblanks)/1024) . ' KB' : '0')); |
|
530 |
return array( |
|
531 |
'table_rows' => $rows, |
|
532 |
'other_data' => $other, |
|
533 |
); |
|
448 | 534 |
} |
449 | 535 |
|
450 | 536 |
/** |
... | ... | |
467 | 553 |
$header = array(); |
468 | 554 |
// Two columns in header. |
469 | 555 |
$header[0] = array('', ''); |
470 |
$header[1] = array($component['name'], ''); |
|
556 |
$header[1] = array($export_options['header_keys'] ? $component['form_key'] : $component['name'], '');
|
|
471 | 557 |
$header[2] = array(t('Name'), t('Filesize (KB)')); |
472 | 558 |
return $header; |
473 | 559 |
} |
... | ... | |
530 | 616 |
} |
531 | 617 |
} |
532 | 618 |
|
619 |
/** |
|
620 |
* Rename any files which are eligible for renaming, if this submission is being |
|
621 |
* submitted for the first time. |
|
622 |
*/ |
|
623 |
function webform_file_rename($node, $submission) { |
|
624 |
if (isset($submission->file_usage)) { |
|
625 |
foreach ($submission->file_usage['renameable'] as $cid => $fids) { |
|
626 |
foreach ($fids as $fid) { |
|
627 |
webform_file_process_rename($node, $submission, $node->webform['components'][$cid], $fid); |
|
628 |
} |
|
629 |
} |
|
630 |
} |
|
631 |
} |
|
632 |
|
|
633 |
|
|
634 |
/** |
|
635 |
* Renames the uploaded file name using tokens. |
|
636 |
* |
|
637 |
* @param $node |
|
638 |
* The webform node object. |
|
639 |
* @param $submission |
|
640 |
* The webform submission object. |
|
641 |
* @param $component |
|
642 |
* Component settings array for which fid is going to be processed. |
|
643 |
* @param $fid |
|
644 |
* A file id to be processed. |
|
645 |
*/ |
|
646 |
function webform_file_process_rename($node, $submission, $component, $fid) { |
|
647 |
$file = webform_get_file($fid); |
|
648 |
|
|
649 |
if ($file) { |
|
650 |
// Get the destination uri. |
|
651 |
$destination_dir = $component['extra']['scheme'] . '://webform/' . drupal_strtolower(webform_replace_tokens($component['extra']['directory'], $node)); |
|
652 |
$destination_dir = file_stream_wrapper_uri_normalize($destination_dir); |
|
653 |
|
|
654 |
// Get the file extension. |
|
655 |
$info = pathinfo($file->uri); |
|
656 |
$extension = $info['extension']; |
|
657 |
|
|
658 |
// Prepare new file name without extension. |
|
659 |
$new_file_name = webform_replace_tokens($component['extra']['rename'], $node, $submission, NULL, TRUE); |
|
660 |
$new_file_name = trim($new_file_name); |
|
661 |
$new_file_name = _webform_transliterate($new_file_name); |
|
662 |
$new_file_name = str_replace('/', '_', $new_file_name); |
|
663 |
$new_file_name = preg_replace('/[^a-zA-Z0-9_\- ]/', '', $new_file_name); |
|
664 |
if (strlen($new_file_name)) { |
|
665 |
// Prepare the new uri with new filename. |
|
666 |
$destination = "$destination_dir/$new_file_name.$extension"; |
|
667 |
|
|
668 |
// Compare the uri and Rename the file name. |
|
669 |
if ($file->uri != $destination) { |
|
670 |
file_move($file, $destination, FILE_EXISTS_RENAME); |
|
671 |
} |
|
672 |
} |
|
673 |
} |
|
674 |
} |
Formats disponibles : Unified diff
Update webform to 4.10