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