swfupload.admin.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // $Id: swfupload.admin.inc,v 1.2 2010/06/17 18:24:57 eugenmayer Exp $
  3. /**
  4. * @file
  5. * Include file for all functions required while using the CCK widget (e.g.: in node-edit forms).
  6. */
  7. /**
  8. * Menu callback for menu item 'swfupload'.
  9. * This function is executed when SFWUpload.swf requests a file upload
  10. */
  11. function swfupload_js() {
  12. $p = (object) $_POST;
  13. $op = $p->op;
  14. $file = (isset($p->file) ? json_decode($p->file) : NULL);
  15. $instance = json_decode($p->instance);
  16. $instance_settings = json_decode($p->instance_settings);
  17. $file_path = $p->file_path;
  18. unset($p);
  19. switch ($op) {
  20. case 'init':
  21. // Add the default callback functions for the SWF Upload
  22. $instance->type = 'table';
  23. $instance->callbacks = array(
  24. 'swfupload_loaded_handler' => 'ref.swfUploadLoaded',
  25. 'file_queued_handler' => 'ref.fileQueued',
  26. 'queue_complete_handler' => 'ref.queueComplete',
  27. 'file_queue_error_handler' => 'ref.fileQueueError',
  28. 'file_dialog_complete_handler' => 'ref.dialogComplete',
  29. 'upload_success_handler' => 'ref.uploadSuccess',
  30. 'upload_progress_handler' => 'ref.uploadProgress',
  31. 'upload_error_handler' => 'ref.uploadError',
  32. 'upload_complete_handler' => 'ref.uploadComplete',
  33. 'init_complete_handler' => 'ref.initComplete',// This custom javascript callback function is used to place the markup inside the dom
  34. );
  35. $instance->elements = array(
  36. 'drag' => array(
  37. 'class' => 'drag first indentation',
  38. 'type' => 'drag',
  39. 'colspan' => 3,
  40. 'title' => t('Filename'),
  41. 'add_separator' => TRUE,
  42. ),
  43. 'icon' => array(
  44. 'type' => 'icon',
  45. 'class' => 'icon',
  46. ),
  47. 'filename' => array(
  48. 'type' => 'markup',
  49. 'value' => '[filename]',
  50. 'class' => 'text title',
  51. ),
  52. );
  53. break;
  54. case 'move_uploaded_file':
  55. $max_filesize = parse_size(file_upload_max_size());
  56. if (!empty($instance_settings->max_filesize) && parse_size($instance_settings->max_filesize) < $max_filesize) {
  57. $max_filesize = parse_size($instance_settings->max_filesize);
  58. }
  59. $file->validators = array(
  60. 'file_validate_extensions' => array($instance_settings->file_extensions),
  61. // 'filefield_validate_image_resolution' => array($instance_settings->max_resolution, $instance_settings->min_resolution),
  62. 'file_validate_size' => array($max_filesize),
  63. );
  64. $file->file_path = $file_path;
  65. // Allow other modules to change the file_path an validators
  66. foreach (module_implements('swfupload') as $module) {
  67. $function = $module . '_swfupload';
  68. $function($file, $op, $instance, $instance_settings);
  69. }
  70. $op = 'upload_complete';
  71. break;
  72. }
  73. // Allow other modules to change the returned data
  74. foreach (module_implements('swfupload') as $module) {
  75. $function = $module . '_swfupload';
  76. $function($file, $op, $instance, $instance_settings);
  77. // We want to make sure the last column of each tablerow contains the 'cancel' or 'delete' button.
  78. if ($op == 'init') {
  79. $instance->elements['cancel'] = array('class' => 'last', 'type' => 'cancel');
  80. }
  81. }
  82. // Replace keys named 'class' to 'classname'
  83. if (!empty($instance->elements)) {
  84. array_walk($instance->elements, '_class_to_classname');
  85. }
  86. $p->op = $op;
  87. $p->file = $file;
  88. $p->file_path = $file_path;
  89. $p->instance = $instance;
  90. $p->instance_settings = $instance_settings;
  91. $p->messages = drupal_get_messages();
  92. echo json_encode($p);
  93. exit(0);
  94. }
  95. /**
  96. * Helper function to replace instance elements class keys to classname keys
  97. * Safari hangs if you use 'class' in javascript
  98. */
  99. function _class_to_classname(&$element) {
  100. $element['classname'] = $element['class'];
  101. unset($element['class']);
  102. }