upload.module

You are here

  1. 4.6 modules/upload.module
  2. 4.7 modules/upload.module
  3. 5 modules/upload/upload.module
  4. 6 modules/upload/upload.module

File-handling and attaching files to nodes.

Functions

Namesort descending Description
theme_upload_attachments Displays file attachments in table
theme_upload_form_current Theme the attachments list.
theme_upload_form_new Theme the attachment form. Note: required to output prefix/suffix.
upload_delete
upload_delete_revision
upload_file_download Implementation of hook_file_download().
upload_form_alter
upload_help Implementation of hook_help().
upload_js Menu-callback for JavaScript-based uploads.
upload_link Implementation of hook_link().
upload_load
upload_menu Implementation of hook_menu().
upload_menu_alter
upload_nodeapi Implementation of hook_nodeapi().
upload_node_form_submit Save new uploads and store them in the session to be associated to the node on upload_save.
upload_perm Implementation of hook_perm().
upload_save
upload_space_used Determine how much disk space is occupied by a user's uploaded files.
upload_theme Implementation of hook_theme()
upload_total_space_used Determine how much disk space is occupied by uploaded files.
_upload_file_limits Determine the limitations on files that a given user may upload. The user may be in multiple roles so we select the most permissive limitations from all of their roles.
_upload_form

File

modules/upload/upload.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * File-handling and attaching files to nodes.
  5. *
  6. */
  7. /**
  8. * Implementation of hook_help().
  9. */
  10. function upload_help($path, $arg) {
  11. switch ($path) {
  12. case 'admin/help#upload':
  13. $output = '<p>'. t('The upload module allows users to upload files to the site. The ability to upload files is important for members of a community who want to share work. It is also useful to administrators who want to keep uploaded files connected to posts.') .'</p>';
  14. $output .= '<p>'. t('Users with the upload files permission can upload attachments to posts. Uploads may be enabled for specific content types on the content types settings page. Each user role can be customized to limit or control the file size of uploads, or the maximum dimension of image files.') .'</p>';
  15. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@upload">Upload module</a>.', array('@upload' => 'http://drupal.org/handbook/modules/upload/')) .'</p>';
  16. return $output;
  17. case 'admin/settings/uploads':
  18. return '<p>'. t('Users with the <a href="@permissions">upload files permission</a> can upload attachments. Users with the <a href="@permissions">view uploaded files permission</a> can view uploaded attachments. You can choose which post types can take attachments on the <a href="@types">content types settings</a> page.', array('@permissions' => url('admin/user/permissions', array('fragment' => 'module-upload')), '@types' => url('admin/content/types'))) .'</p>';
  19. }
  20. }
  21. /**
  22. * Implementation of hook_theme()
  23. */
  24. function upload_theme() {
  25. return array(
  26. 'upload_attachments' => array(
  27. 'arguments' => array('files' => NULL),
  28. ),
  29. 'upload_form_current' => array(
  30. 'arguments' => array('form' => NULL),
  31. ),
  32. 'upload_form_new' => array(
  33. 'arguments' => array('form' => NULL),
  34. ),
  35. );
  36. }
  37. /**
  38. * Implementation of hook_perm().
  39. */
  40. function upload_perm() {
  41. return array('upload files', 'view uploaded files');
  42. }
  43. /**
  44. * Implementation of hook_link().
  45. */
  46. function upload_link($type, $node = NULL, $teaser = FALSE) {
  47. $links = array();
  48. // Display a link with the number of attachments
  49. if ($teaser && $type == 'node' && isset($node->files) && user_access('view uploaded files')) {
  50. $num_files = 0;
  51. foreach ($node->files as $file) {
  52. if ($file->list) {
  53. $num_files++;
  54. }
  55. }
  56. if ($num_files) {
  57. $links['upload_attachments'] = array(
  58. 'title' => format_plural($num_files, '1 attachment', '@count attachments'),
  59. 'href' => "node/$node->nid",
  60. 'attributes' => array('title' => t('Read full article to view attachments.')),
  61. 'fragment' => 'attachments'
  62. );
  63. }
  64. }
  65. return $links;
  66. }
  67. /**
  68. * Implementation of hook_menu().
  69. */
  70. function upload_menu() {
  71. $items['upload/js'] = array(
  72. 'page callback' => 'upload_js',
  73. 'access arguments' => array('upload files'),
  74. 'type' => MENU_CALLBACK,
  75. );
  76. $items['admin/settings/uploads'] = array(
  77. 'title' => 'File uploads',
  78. 'description' => 'Control how files may be attached to content.',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('upload_admin_settings'),
  81. 'access arguments' => array('administer site configuration'),
  82. 'type' => MENU_NORMAL_ITEM,
  83. 'file' => 'upload.admin.inc',
  84. );
  85. return $items;
  86. }
  87. function upload_menu_alter(&$items) {
  88. $items['system/files']['access arguments'] = array('view uploaded files');
  89. }
  90. /**
  91. * Determine the limitations on files that a given user may upload. The user
  92. * may be in multiple roles so we select the most permissive limitations from
  93. * all of their roles.
  94. *
  95. * @param $user
  96. * A Drupal user object.
  97. * @return
  98. * An associative array with the following keys:
  99. * 'extensions'
  100. * A white space separated string containing all the file extensions this
  101. * user may upload.
  102. * 'file_size'
  103. * The maximum size of a file upload in bytes.
  104. * 'user_size'
  105. * The total number of bytes for all for a user's files.
  106. * 'resolution'
  107. * A string specifying the maximum resolution of images.
  108. */
  109. function _upload_file_limits($user) {
  110. $file_limit = variable_get('upload_uploadsize_default', 1);
  111. $user_limit = variable_get('upload_usersize_default', 1);
  112. $all_extensions = explode(' ', variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'));
  113. foreach ($user->roles as $rid => $name) {
  114. $extensions = variable_get("upload_extensions_$rid", variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'));
  115. $all_extensions = array_merge($all_extensions, explode(' ', $extensions));
  116. // A zero value indicates no limit, take the least restrictive limit.
  117. $file_size = variable_get("upload_uploadsize_$rid", variable_get('upload_uploadsize_default', 1)) * 1024 * 1024;
  118. $file_limit = ($file_limit && $file_size) ? max($file_limit, $file_size) : 0;
  119. $user_size = variable_get("upload_usersize_$rid", variable_get('upload_usersize_default', 1)) * 1024 * 1024;
  120. $user_limit = ($user_limit && $user_size) ? max($user_limit, $user_size) : 0;
  121. }
  122. $all_extensions = implode(' ', array_unique($all_extensions));
  123. return array(
  124. 'extensions' => $all_extensions,
  125. 'file_size' => $file_limit,
  126. 'user_size' => $user_limit,
  127. 'resolution' => variable_get('upload_max_resolution', 0),
  128. );
  129. }
  130. /**
  131. * Implementation of hook_file_download().
  132. */
  133. function upload_file_download($filepath) {
  134. $filepath = file_create_path($filepath);
  135. $result = db_query("SELECT f.*, u.nid FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = '%s'", $filepath);
  136. while ($file = db_fetch_object($result)) {
  137. if ($filepath !== $file->filepath) {
  138. // Since some database servers sometimes use a case-insensitive
  139. // comparison by default, double check that the filename is an exact
  140. // match.
  141. continue;
  142. }
  143. if (user_access('view uploaded files') && ($node = node_load($file->nid)) && node_access('view', $node)) {
  144. return array(
  145. 'Content-Type: ' . $file->filemime,
  146. 'Content-Length: ' . $file->filesize,
  147. );
  148. }
  149. else {
  150. return -1;
  151. }
  152. }
  153. }
  154. /**
  155. * Save new uploads and store them in the session to be associated to the node
  156. * on upload_save.
  157. *
  158. * @param $node
  159. * A node object to associate with uploaded files.
  160. */
  161. function upload_node_form_submit(&$form, &$form_state) {
  162. global $user;
  163. $limits = _upload_file_limits($user);
  164. $validators = array(
  165. 'file_validate_extensions' => array($limits['extensions']),
  166. 'file_validate_image_resolution' => array($limits['resolution']),
  167. 'file_validate_size' => array($limits['file_size'], $limits['user_size']),
  168. );
  169. // Save new file uploads.
  170. if (user_access('upload files') && ($file = file_save_upload('upload', $validators, file_directory_path()))) {
  171. $file->list = variable_get('upload_list_default', 1);
  172. $file->description = $file->filename;
  173. $file->weight = 0;
  174. $file->new = TRUE;
  175. $form['#node']->files[$file->fid] = $file;
  176. $form_state['values']['files'][$file->fid] = (array)$file;
  177. }
  178. if (isset($form_state['values']['files'])) {
  179. foreach ($form_state['values']['files'] as $fid => $file) {
  180. // If the node was previewed prior to saving, $form['#node']->files[$fid]
  181. // is an array instead of an object. Convert file to object for compatibility.
  182. $form['#node']->files[$fid] = (object) $form['#node']->files[$fid];
  183. $form_state['values']['files'][$fid]['new'] = !empty($form['#node']->files[$fid]->new);
  184. }
  185. }
  186. // Order the form according to the set file weight values.
  187. if (!empty($form_state['values']['files'])) {
  188. $microweight = 0.001;
  189. foreach ($form_state['values']['files'] as $fid => $file) {
  190. if (is_numeric($fid)) {
  191. $form_state['values']['files'][$fid]['#weight'] = $file['weight'] + $microweight;
  192. $microweight += 0.001;
  193. }
  194. }
  195. uasort($form_state['values']['files'], 'element_sort');
  196. }
  197. }
  198. function upload_form_alter(&$form, $form_state, $form_id) {
  199. if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
  200. $form['workflow']['upload'] = array(
  201. '#type' => 'radios',
  202. '#title' => t('Attachments'),
  203. '#default_value' => variable_get('upload_'. $form['#node_type']->type, 1),
  204. '#options' => array(t('Disabled'), t('Enabled')),
  205. );
  206. }
  207. if (isset($form['type']) && isset($form['#node'])) {
  208. $node = $form['#node'];
  209. if ($form['type']['#value'] .'_node_form' == $form_id && variable_get("upload_$node->type", TRUE)) {
  210. // Attachments fieldset
  211. $form['attachments'] = array(
  212. '#type' => 'fieldset',
  213. '#access' => user_access('upload files'),
  214. '#title' => t('File attachments'),
  215. '#collapsible' => TRUE,
  216. '#collapsed' => empty($node->files),
  217. '#description' => t('Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.'),
  218. '#prefix' => '<div class="attachments">',
  219. '#suffix' => '</div>',
  220. '#weight' => 30,
  221. );
  222. // Wrapper for fieldset contents (used by ahah.js).
  223. $form['attachments']['wrapper'] = array(
  224. '#prefix' => '<div id="attach-wrapper">',
  225. '#suffix' => '</div>',
  226. );
  227. // Make sure necessary directories for upload.module exist and are
  228. // writable before displaying the attachment form.
  229. $path = file_directory_path();
  230. $temp = file_directory_temp();
  231. // Note: pass by reference
  232. if (!file_check_directory($path, FILE_CREATE_DIRECTORY) || !file_check_directory($temp, FILE_CREATE_DIRECTORY)) {
  233. $form['attachments']['#description'] = t('File attachments are disabled. The file directories have not been properly configured.');
  234. if (user_access('administer site configuration')) {
  235. $form['attachments']['#description'] .= ' '. t('Please visit the <a href="@admin-file-system">file system configuration page</a>.', array('@admin-file-system' => url('admin/settings/file-system')));
  236. }
  237. else {
  238. $form['attachments']['#description'] .= ' '. t('Please contact the site administrator.');
  239. }
  240. }
  241. else {
  242. $form['attachments']['wrapper'] += _upload_form($node);
  243. $form['#attributes']['enctype'] = 'multipart/form-data';
  244. }
  245. $form['#submit'][] = 'upload_node_form_submit';
  246. }
  247. }
  248. }
  249. /**
  250. * Implementation of hook_nodeapi().
  251. */
  252. function upload_nodeapi(&$node, $op, $teaser = NULL) {
  253. switch ($op) {
  254. case 'load':
  255. $output = '';
  256. if (variable_get("upload_$node->type", 1) == 1) {
  257. $output['files'] = upload_load($node);
  258. return $output;
  259. }
  260. break;
  261. case 'view':
  262. if (isset($node->files) && user_access('view uploaded files')) {
  263. // Add the attachments list to node body with a heavy
  264. // weight to ensure they're below other elements
  265. if (count($node->files)) {
  266. if (!$teaser && user_access('view uploaded files')) {
  267. $node->content['files'] = array(
  268. '#value' => theme('upload_attachments', $node->files),
  269. '#weight' => 50,
  270. );
  271. }
  272. }
  273. }
  274. break;
  275. case 'insert':
  276. case 'update':
  277. if (user_access('upload files')) {
  278. upload_save($node);
  279. }
  280. break;
  281. case 'delete':
  282. upload_delete($node);
  283. break;
  284. case 'delete revision':
  285. upload_delete_revision($node);
  286. break;
  287. case 'search result':
  288. return isset($node->files) && is_array($node->files) && user_access('view uploaded files') ? format_plural(count($node->files), '1 attachment', '@count attachments') : NULL;
  289. case 'rss item':
  290. if (is_array($node->files) && user_access('view uploaded files')) {
  291. $files = array();
  292. foreach ($node->files as $file) {
  293. if ($file->list) {
  294. $files[] = $file;
  295. }
  296. }
  297. if (count($files) > 0) {
  298. // RSS only allows one enclosure per item
  299. $file = array_shift($files);
  300. return array(
  301. array(
  302. 'key' => 'enclosure',
  303. 'attributes' => array(
  304. 'url' => file_create_url($file->filepath),
  305. 'length' => $file->filesize,
  306. 'type' => $file->filemime
  307. )
  308. )
  309. );
  310. }
  311. }
  312. return array();
  313. }
  314. }
  315. /**
  316. * Displays file attachments in table
  317. *
  318. * @ingroup themeable
  319. */
  320. function theme_upload_attachments($files) {
  321. $header = array(t('Attachment'), t('Size'));
  322. $rows = array();
  323. foreach ($files as $file) {
  324. $file = (object)$file;
  325. if ($file->list && empty($file->remove)) {
  326. $href = file_create_url($file->filepath);
  327. $text = $file->description ? $file->description : $file->filename;
  328. $rows[] = array(l($text, $href), format_size($file->filesize));
  329. }
  330. }
  331. if (count($rows)) {
  332. return theme('table', $header, $rows, array('id' => 'attachments'));
  333. }
  334. }
  335. /**
  336. * Determine how much disk space is occupied by a user's uploaded files.
  337. *
  338. * @param $uid
  339. * The integer user id of a user.
  340. * @return
  341. * The amount of disk space used by the user in bytes.
  342. */
  343. function upload_space_used($uid) {
  344. return file_space_used($uid);
  345. }
  346. /**
  347. * Determine how much disk space is occupied by uploaded files.
  348. *
  349. * @return
  350. * The amount of disk space used by uploaded files in bytes.
  351. */
  352. function upload_total_space_used() {
  353. return db_result(db_query('SELECT SUM(f.filesize) FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid'));
  354. }
  355. function upload_save(&$node) {
  356. if (empty($node->files) || !is_array($node->files)) {
  357. return;
  358. }
  359. foreach ($node->files as $fid => $file) {
  360. // Convert file to object for compatibility
  361. $file = (object)$file;
  362. // Remove file. Process removals first since no further processing
  363. // will be required.
  364. if (!empty($file->remove)) {
  365. db_query('DELETE FROM {upload} WHERE fid = %d AND vid = %d', $fid, $node->vid);
  366. // If the file isn't used by any other revisions delete it.
  367. $count = db_result(db_query('SELECT COUNT(fid) FROM {upload} WHERE fid = %d', $fid));
  368. if ($count < 1) {
  369. file_delete($file->filepath);
  370. db_query('DELETE FROM {files} WHERE fid = %d', $fid);
  371. }
  372. // Remove it from the session in the case of new uploads,
  373. // that you want to disassociate before node submission.
  374. unset($node->files[$fid]);
  375. // Move on, so the removed file won't be added to new revisions.
  376. continue;
  377. }
  378. // Create a new revision, or associate a new file needed.
  379. if (!empty($node->old_vid) || $file->new) {
  380. db_query("INSERT INTO {upload} (fid, nid, vid, list, description, weight) VALUES (%d, %d, %d, %d, '%s', %d)", $file->fid, $node->nid, $node->vid, $file->list, $file->description, $file->weight);
  381. file_set_status($file, FILE_STATUS_PERMANENT);
  382. }
  383. // Update existing revision.
  384. else {
  385. db_query("UPDATE {upload} SET list = %d, description = '%s', weight = %d WHERE fid = %d AND vid = %d", $file->list, $file->description, $file->weight, $file->fid, $node->vid);
  386. file_set_status($file, FILE_STATUS_PERMANENT);
  387. }
  388. }
  389. }
  390. function upload_delete($node) {
  391. $files = array();
  392. $result = db_query('SELECT DISTINCT f.* FROM {upload} u INNER JOIN {files} f ON u.fid = f.fid WHERE u.nid = %d', $node->nid);
  393. while ($file = db_fetch_object($result)) {
  394. $files[$file->fid] = $file;
  395. }
  396. foreach ($files as $fid => $file) {
  397. // Delete all files associated with the node
  398. db_query('DELETE FROM {files} WHERE fid = %d', $fid);
  399. file_delete($file->filepath);
  400. }
  401. // Delete all file revision information associated with the node
  402. db_query('DELETE FROM {upload} WHERE nid = %d', $node->nid);
  403. }
  404. function upload_delete_revision($node) {
  405. if (is_array($node->files)) {
  406. foreach ($node->files as $file) {
  407. // Check if the file will be used after this revision is deleted
  408. $count = db_result(db_query('SELECT COUNT(fid) FROM {upload} WHERE fid = %d', $file->fid));
  409. // if the file won't be used, delete it
  410. if ($count < 2) {
  411. db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
  412. file_delete($file->filepath);
  413. }
  414. }
  415. }
  416. // delete the revision
  417. db_query('DELETE FROM {upload} WHERE vid = %d', $node->vid);
  418. }
  419. function _upload_form($node) {
  420. global $user;
  421. $form = array(
  422. '#theme' => 'upload_form_new',
  423. '#cache' => TRUE,
  424. );
  425. if (!empty($node->files) && is_array($node->files)) {
  426. $form['files']['#theme'] = 'upload_form_current';
  427. $form['files']['#tree'] = TRUE;
  428. foreach ($node->files as $key => $file) {
  429. $file = (object)$file;
  430. $description = file_create_url($file->filepath);
  431. $description = "<small>". check_plain($description) ."</small>";
  432. $form['files'][$key]['description'] = array('#type' => 'textfield', '#default_value' => !empty($file->description) ? $file->description : $file->filename, '#maxlength' => 256, '#description' => $description );
  433. $form['files'][$key]['size'] = array('#value' => format_size($file->filesize));
  434. $form['files'][$key]['remove'] = array('#type' => 'checkbox', '#default_value' => !empty($file->remove));
  435. $form['files'][$key]['list'] = array('#type' => 'checkbox', '#default_value' => $file->list);
  436. $form['files'][$key]['weight'] = array('#type' => 'weight', '#delta' => count($node->files), '#default_value' => $file->weight);
  437. $form['files'][$key]['filename'] = array('#type' => 'value', '#value' => $file->filename);
  438. $form['files'][$key]['filepath'] = array('#type' => 'value', '#value' => $file->filepath);
  439. $form['files'][$key]['filemime'] = array('#type' => 'value', '#value' => $file->filemime);
  440. $form['files'][$key]['filesize'] = array('#type' => 'value', '#value' => $file->filesize);
  441. $form['files'][$key]['fid'] = array('#type' => 'value', '#value' => $file->fid);
  442. $form['files'][$key]['new'] = array('#type' => 'value', '#value' => FALSE);
  443. }
  444. }
  445. if (user_access('upload files')) {
  446. $limits = _upload_file_limits($user);
  447. $form['new']['#weight'] = 10;
  448. $form['new']['upload'] = array(
  449. '#type' => 'file',
  450. '#title' => t('Attach new file'),
  451. '#size' => 40,
  452. '#description' => ($limits['resolution'] ? t('Images are larger than %resolution will be resized. ', array('%resolution' => $limits['resolution'])) : '') . t('The maximum upload size is %filesize. Only files with the following extensions may be uploaded: %extensions. ', array('%extensions' => $limits['extensions'], '%filesize' => format_size($limits['file_size']))),
  453. );
  454. $form['new']['attach'] = array(
  455. '#type' => 'submit',
  456. '#value' => t('Attach'),
  457. '#name' => 'attach',
  458. '#ahah' => array(
  459. 'path' => 'upload/js',
  460. 'wrapper' => 'attach-wrapper',
  461. 'progress' => array('type' => 'bar', 'message' => t('Please wait...')),
  462. ),
  463. '#submit' => array('node_form_submit_build_node'),
  464. );
  465. }
  466. return $form;
  467. }
  468. /**
  469. * Theme the attachments list.
  470. *
  471. * @ingroup themeable
  472. */
  473. function theme_upload_form_current($form) {
  474. $header = array('', t('Delete'), t('List'), t('Description'), t('Weight'), t('Size'));
  475. drupal_add_tabledrag('upload-attachments', 'order', 'sibling', 'upload-weight');
  476. foreach (element_children($form) as $key) {
  477. // Add class to group weight fields for drag and drop.
  478. $form[$key]['weight']['#attributes']['class'] = 'upload-weight';
  479. $row = array('');
  480. $row[] = drupal_render($form[$key]['remove']);
  481. $row[] = drupal_render($form[$key]['list']);
  482. $row[] = drupal_render($form[$key]['description']);
  483. $row[] = drupal_render($form[$key]['weight']);
  484. $row[] = drupal_render($form[$key]['size']);
  485. $rows[] = array('data' => $row, 'class' => 'draggable');
  486. }
  487. $output = theme('table', $header, $rows, array('id' => 'upload-attachments'));
  488. $output .= drupal_render($form);
  489. return $output;
  490. }
  491. /**
  492. * Theme the attachment form.
  493. * Note: required to output prefix/suffix.
  494. *
  495. * @ingroup themeable
  496. */
  497. function theme_upload_form_new($form) {
  498. drupal_add_tabledrag('upload-attachments', 'order', 'sibling', 'upload-weight');
  499. $output = drupal_render($form);
  500. return $output;
  501. }
  502. function upload_load($node) {
  503. $files = array();
  504. if ($node->vid) {
  505. $result = db_query('SELECT * FROM {files} f INNER JOIN {upload} r ON f.fid = r.fid WHERE r.vid = %d ORDER BY r.weight, f.fid', $node->vid);
  506. while ($file = db_fetch_object($result)) {
  507. $files[$file->fid] = $file;
  508. }
  509. }
  510. return $files;
  511. }
  512. /**
  513. * Menu-callback for JavaScript-based uploads.
  514. */
  515. function upload_js() {
  516. $cached_form_state = array();
  517. $files = array();
  518. // Load the form from the Form API cache.
  519. if (!($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($cached_form['#node']) || !isset($cached_form['attachments'])) {
  520. form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
  521. $output = theme('status_messages');
  522. print drupal_to_js(array('status' => TRUE, 'data' => $output));
  523. exit();
  524. }
  525. $form_state = array('values' => $_POST);
  526. // Handle new uploads, and merge tmp files into node-files.
  527. upload_node_form_submit($cached_form, $form_state);
  528. if(!empty($form_state['values']['files'])) {
  529. foreach ($form_state['values']['files'] as $fid => $file) {
  530. if (isset($cached_form['#node']->files[$fid])) {
  531. $files[$fid] = $cached_form['#node']->files[$fid];
  532. }
  533. }
  534. }
  535. $node = $cached_form['#node'];
  536. $node->files = $files;
  537. $form = _upload_form($node);
  538. unset($cached_form['attachments']['wrapper']['new']);
  539. $cached_form['attachments']['wrapper'] = array_merge($cached_form['attachments']['wrapper'], $form);
  540. $cached_form['attachments']['#collapsed'] = FALSE;
  541. form_set_cache($_POST['form_build_id'], $cached_form, $cached_form_state);
  542. foreach ($files as $fid => $file) {
  543. if (is_numeric($fid)) {
  544. $form['files'][$fid]['description']['#default_value'] = $form_state['values']['files'][$fid]['description'];
  545. $form['files'][$fid]['list']['#default_value'] = !empty($form_state['values']['files'][$fid]['list']);
  546. $form['files'][$fid]['remove']['#default_value'] = !empty($form_state['values']['files'][$fid]['remove']);
  547. $form['files'][$fid]['weight']['#default_value'] = $form_state['values']['files'][$fid]['weight'];
  548. }
  549. }
  550. // Render the form for output.
  551. $form += array(
  552. '#post' => $_POST,
  553. '#programmed' => FALSE,
  554. '#tree' => FALSE,
  555. '#parents' => array(),
  556. );
  557. $empty_form_state = array();
  558. $data = &$form;
  559. $data['__drupal_alter_by_ref'] = array(&$empty_form_state);
  560. drupal_alter('form', $data, 'upload_js');
  561. $form_state = array('submitted' => FALSE);
  562. $form = form_builder('upload_js', $form, $form_state);
  563. $output = theme('status_messages') . drupal_render($form);
  564. // We send the updated file attachments form.
  565. // Don't call drupal_json(). ahah.js uses an iframe and
  566. // the header output by drupal_json() causes problems in some browsers.
  567. print drupal_to_js(array('status' => TRUE, 'data' => $output));
  568. exit;
  569. }