file_entity.pages.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @file
  4. * Supports file operations including View, Edit, and Delete.
  5. */
  6. /**
  7. * Menu callback; view a single file entity.
  8. */
  9. function file_entity_view_page($file) {
  10. // @todo Implement granular editorial access: http://drupal.org/node/696970.
  11. // In the meantime, protect information about private files from being
  12. // discovered by unprivileged users. File IDs are autoincrement, so one can
  13. // attempt discovery by trying to access different media/ID paths. See also
  14. // media_browser_list(). This logic potentially belongs within
  15. // media_access(), but that would require extending that function's
  16. // signature to accept a $file paramter, and this is temporary code anyway.
  17. if (!user_access('administer files') && (file_uri_scheme($file->uri) === 'private')) {
  18. return MENU_ACCESS_DENIED;
  19. }
  20. drupal_set_title($file->filename);
  21. return file_view($file, 'full');
  22. }
  23. /**
  24. * Menu callback; presents the Media editing form.
  25. */
  26. function file_entity_page_edit($file) {
  27. drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => $file->type, '@title' => $file->filename)), PASS_THROUGH);
  28. return drupal_get_form('file_entity_edit', $file);
  29. }
  30. /**
  31. * Form builder: Builds the edit file form.
  32. */
  33. function file_entity_edit($form, &$form_state, $file) {
  34. $form_state['file'] = $file;
  35. $form['#attributes']['class'][] = 'file-form';
  36. if (!empty($file->type)) {
  37. $form['#attributes']['class'][] = 'file-' . $file->type . '-form';
  38. }
  39. // Basic file information.
  40. // These elements are just values so they are not even sent to the client.
  41. foreach (array('fid', 'type', 'uid', 'timestamp') as $key) {
  42. $form[$key] = array(
  43. '#type' => 'value',
  44. '#value' => isset($file->$key) ? $file->$key : NULL,
  45. );
  46. }
  47. $form['filename'] = array(
  48. '#type' => 'textfield',
  49. '#title' => t('Name'),
  50. '#default_value' => $file->filename,
  51. '#required' => TRUE,
  52. '#maxlength' => 255,
  53. '#weight' => -10,
  54. );
  55. $form['preview'] = array(
  56. '#theme' => 'file_link',
  57. '#file' => $file,
  58. '#weight' => -5,
  59. );
  60. // Add the buttons.
  61. $form['actions'] = array('#type' => 'actions');
  62. $form['actions']['submit'] = array(
  63. '#type' => 'submit',
  64. '#value' => t('Save'),
  65. '#weight' => 5,
  66. '#submit' => array('file_entity_edit_submit'),
  67. );
  68. $form['actions']['delete'] = array(
  69. '#type' => 'submit',
  70. '#value' => t('Delete'),
  71. '#weight' => 10,
  72. '#submit' => array('file_entity_delete_submit'),
  73. );
  74. field_attach_form('file', $file, $form, $form_state);
  75. return $form;
  76. }
  77. /**
  78. * Form validation handler for the file entity edit form.
  79. */
  80. function file_entity_edit_validate($form, &$form_state) {
  81. entity_form_field_validate('file', $form, $form_state);
  82. }
  83. /**
  84. * Form submit handler for the media submit form.
  85. */
  86. function file_entity_edit_submit($form, &$form_state) {
  87. $file = $form_state['file'];
  88. entity_form_submit_build_entity('file', $file, $form, $form_state);
  89. file_save($file);
  90. $form_state['redirect'] = 'file/' . $file->fid;
  91. }
  92. /**
  93. * Menu callback; shows delete confirmation form.
  94. */
  95. function file_entity_page_delete($file) {
  96. drupal_set_title(t('<em>Delete @type</em> @title', array('@type' => $file->type, '@title' => $file->filename)), PASS_THROUGH);
  97. // Don't bother showing the form if the item is in use, since we won't allow
  98. // them to delete it anyway.
  99. $references = file_usage_list($file);
  100. if (!empty($references)) {
  101. return t('The file %title is in use and cannot be deleted.', array('%title' => $file->filename));
  102. }
  103. else {
  104. $files = array($file->fid => $file->fid);
  105. return drupal_get_form('file_entity_multiple_delete_confirm', $files, 'file/' . $file->fid);
  106. }
  107. }
  108. /**
  109. * Confirm form for the request to delete files.
  110. *
  111. * @param array $files
  112. * An array of file_ids to delete.
  113. */
  114. function file_entity_multiple_delete_confirm($form, &$form_state, $files, $redirect_path = NULL) {
  115. $form['files'] = array('#tree' => TRUE);
  116. $form['file_titles'] = array('#theme' => 'item_list');
  117. $files = file_load_multiple($files);
  118. foreach ($files as $fid => $file) {
  119. $form['files'][$fid] = array(
  120. '#type' => 'value',
  121. '#value' => $fid,
  122. );
  123. $form['file_titles']['#items'][] = check_plain($file->filename);
  124. }
  125. $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
  126. $confirm_question = format_plural(count($files),
  127. 'Are you sure you want to delete this file?',
  128. 'Are you sure you want to delete these files?');
  129. return confirm_form($form,
  130. $confirm_question,
  131. $redirect_path,
  132. t('This action cannot be undone.'),
  133. t('Delete'),
  134. t('Cancel'));
  135. }
  136. /**
  137. * Attempt to delete files and notify the user of the result.
  138. */
  139. function file_entity_multiple_delete_confirm_submit($form, &$form_state) {
  140. if ($form_state['values']['confirm']) {
  141. $files = file_load_multiple(array_keys($form_state['values']['files']));
  142. foreach ($files as $fid => $file) {
  143. $result = file_delete($file);
  144. if (is_array($result)) {
  145. drupal_set_message(t('The file @title is in use and cannot be deleted.', array('@title' => $file->filename)), 'warning');
  146. }
  147. elseif (!$result) {
  148. drupal_set_message(t('The file @title was not deleted due to an error.', array('@title' => $file->filename)), 'error');
  149. }
  150. else {
  151. $message = t('File @title was deleted', array('@title' => $file->filename));
  152. $form_state['redirect'] = user_access('administer files') ? 'admin/content/file' : '<front>';
  153. watchdog('file', $message);
  154. drupal_set_message($message);
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * Form submit handler for the Delete button on the media edit form.
  161. */
  162. function file_entity_delete_submit($form, &$form_state) {
  163. $fid = $form_state['values']['fid'];
  164. $destination = array();
  165. if (isset($_GET['destination'])) {
  166. $destination = drupal_get_destination();
  167. unset($_GET['destination']);
  168. }
  169. $form_state['redirect'] = array('file/' . $fid . '/delete', array('query' => $destination));
  170. }