swfupload_widget.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // $Id: swfupload_widget.inc,v 1.6 2010/12/02 16:02:53 skilip Exp $
  3. /**
  4. * @file
  5. * SWFUpload widget hooks and callbacks.
  6. */
  7. /**
  8. * Implements hook_field_widget_info().
  9. */
  10. function swfupload_field_widget_info() {
  11. return array(
  12. 'swfupload' => array(
  13. 'label' => 'SWFUpload',
  14. 'field types' => array('file', 'image'),
  15. 'settings' => array(
  16. 'progress_indicator' => 'throbber',
  17. 'preview_image_style' => 'thumbnail',
  18. ),
  19. 'behaviors' => array(
  20. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  21. 'default value' => FIELD_BEHAVIOR_NONE,
  22. ),
  23. ),
  24. );
  25. }
  26. /**
  27. * Implements hook_field_widget_form().
  28. */
  29. function swfupload_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  30. // Collect the files already stored in this field indexed by their file ID.
  31. $files = array();
  32. foreach ($items as $delta => $file) {
  33. if ($file['fid'] != 0) {
  34. $files[$file['fid']] = $file;
  35. }
  36. }
  37. $element['#default_value'] = $files;
  38. $element_info = element_info('swfupload_widget');
  39. $element += array(
  40. '#type' => 'swfupload_widget',
  41. '#process' => $element_info['#process'],
  42. '#value_callback' => $element_info['#value_callback'],
  43. // Allows this field to return an array instead of a single value.
  44. '#extended' => TRUE,
  45. );
  46. // $element['#display_field'] = $field['settings']['display_field'];
  47. return $element;
  48. }
  49. /**
  50. * The #value_callback for the swfupload_widget type element.
  51. */
  52. function swfupload_widget_value(&$element, $input = FALSE, $form_state = NULL) {
  53. if (is_string($input)) {
  54. $input = json_decode($input, TRUE);
  55. }
  56. if ($input === FALSE) {
  57. $default_value = array();
  58. if (!empty($element['#default_value'])) {
  59. foreach ($element['#default_value'] as $file) {
  60. if ($file) {
  61. // If we're dealing with an image, create a thumbpath
  62. if (image_get_info($file['uri'])) {
  63. $file['thumb'] = swfupload_thumb($file);
  64. }
  65. $default_value[$file['fid']] = $file;
  66. }
  67. }
  68. }
  69. return $default_value;
  70. }
  71. else {
  72. // Files need an integer value for the 'display' field.
  73. foreach ($input as $fid => $file) {
  74. if (empty($file['display'])) {
  75. $input[$fid]['display'] = 0;
  76. }
  77. }
  78. return $input;
  79. }
  80. }
  81. /**
  82. * Process the link type element before displaying the field.
  83. *
  84. * Build the form element. When creating a form using FAPI #process,
  85. * note that $element['#value'] is already set.
  86. *
  87. * The $fields array is in $form['#field_info'][$element['#field_name']].
  88. */
  89. function swfupload_widget_process($element, $form_state, $form) {
  90. $field = field_info_field($element['#field_name']);
  91. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  92. // Make sure the element's name exists.
  93. if (!isset($element['#name'])) {
  94. $element['#name'] = array_shift($element['#parents']);
  95. }
  96. $element['#default_value'] = '[]';
  97. // Set a file upload limit for SWFUpload where 0 is unlimited.
  98. $limit = ($field['cardinality'] == -1 ? 0 : $field['cardinality']);
  99. // Set the button title which is used in the theme function.
  100. if ($field['type'] == 'image') {
  101. $element['#button_title'] = format_plural(($limit === 0 ? 2 : $limit), 'Upload new image', 'Upload new images');
  102. }
  103. else {
  104. $element['#button_title'] = format_plural(($limit === 0 ? 2 : $limit), 'Upload new file', 'Upload new files');
  105. }
  106. // Construct the JavaScript settings array.
  107. if ($library = libraries_load('swfupload')) {
  108. $settings['swfupload_settings'][$element['#id']] = array(
  109. 'module_path' => drupal_get_path('module', 'swfupload'),
  110. 'flash_url' => url($library['library path'] . '/Flash/swfupload.swf'),
  111. 'upload_url' => url('swfupload'), // Relative to the SWF file
  112. 'upload_button_id' => $element['#id'],
  113. 'file_post_name' => $element['#name'],
  114. 'file_queue_limit' => $limit,
  115. 'post_params' => array(
  116. 'sid' => _swfupload_post_key(),
  117. 'file_path' => $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'],
  118. 'op' => 'move_uploaded_file',
  119. 'instance' => json_encode(array('name' => $element['#field_name'])),
  120. 'instance_settings' => json_encode($instance['settings'] + $field['settings']),
  121. ),
  122. 'file_size_limit' => ($instance['settings']['max_filesize'] ? (parse_size($instance['settings']['max_filesize']) / 1048576) . 'MB' : 0),
  123. 'file_types' => (empty($instance['settings']['file_extensions']) ? '' : '*.' . str_replace(" ", ";*.", $instance['settings']['file_extensions'])),
  124. 'file_types_description' => ($element['#description'] ? $element['#description'] : ''),
  125. 'file_upload_limit' => $limit,
  126. 'custom_settings' => array(
  127. 'upload_stack_value' => (!empty($element['#value'])) ? json_encode($element['#value']) : '[]',
  128. 'max_queue_size' => ($instance['settings']['max_filesize'] ? $instance['settings']['max_filesize'] : 0),
  129. ),
  130. );
  131. drupal_add_js($settings, 'setting');
  132. }
  133. return $element;
  134. }
  135. /**
  136. * Theme function for the swfupload form element
  137. */
  138. function theme_swfupload_widget($variables) {
  139. $element = $variables['element'];
  140. // Force the classes swfupload_button and disabled to be added to the button
  141. _form_set_class($element, array('swfupload_button', 'disabled'));
  142. $element['#attributes']['class'] = str_replace(' error', ' swfupload-error', $element['#attributes']['class']);
  143. $output[] = '<div id="' . $element['#id'] . '" ' . drupal_attributes($element['#attributes']) . '>';
  144. $output[] = ' <div class="swfupload-wrapper">';
  145. $output[] = ' <div id="' . $element['#name'] . '-swfwrapper">&nbsp;</div>';
  146. $output[] = ' </div>';
  147. $output[] = ' <div class="left">&nbsp;</div>';
  148. $output[] = ' <div class="center">' . $element['#button_title'] . '</div>';
  149. $output[] = ' <div class="right">&nbsp;</div><br />';
  150. $output[] = '</div>';
  151. if ($element['#description']) {
  152. $output[] = ' <div class="description">' . $element['#description'] . '</div>';
  153. }
  154. return join("\n", $output);
  155. }