file_entity.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. require_once dirname(__FILE__) . '/file_entity.pages.inc';
  3. /**
  4. * @file
  5. * Administrative interface for file type configuration.
  6. */
  7. /**
  8. * Administrative form for browsing files and performing operations on them.
  9. */
  10. function file_entity_admin_files($form, &$form_state) {
  11. if (!empty($form_state['values']['operation'])) {
  12. // The form is being rebuilt because an operation requiring confirmation
  13. // was selected. Hand off to the callback of the operation at this point
  14. // which should return a confirm form.
  15. $operation = $form_state['values']['operation'];
  16. $files = array_keys(array_filter($form_state['values']['files']));
  17. return call_user_func_array($operation['callback'], array($files));
  18. }
  19. $limit = variable_get('file_entity_admin_files_limit', 50);
  20. // Build the 'Update options' form.
  21. $form['options'] = array(
  22. '#type' => 'fieldset',
  23. '#title' => t('Update options'),
  24. '#attributes' => array('class' => array('container-inline')),
  25. );
  26. $options = array();
  27. $form['#operations'] = module_invoke_all('file_operations_info');
  28. drupal_alter('file_entity_file_operations', $form['#operations']);
  29. foreach ($form['#operations'] as $operation => $array) {
  30. $options[$operation] = $array['label'];
  31. }
  32. $form['options']['operation'] = array(
  33. '#type' => 'select',
  34. '#title' => t('Operation'),
  35. '#title_display' => 'invisible',
  36. '#options' => $options,
  37. );
  38. $form['options']['submit'] = array(
  39. '#type' => 'submit',
  40. '#value' => t('Update'),
  41. );
  42. // Build the sortable table header.
  43. $header = array(
  44. 'title' => array('data' => t('Title'), 'field' => 'f.filename'),
  45. 'type' => array('data' => t('Type'), 'field' => 'f.filemime'),
  46. 'size' => array('data' => t('Size'), 'field' => 'f.filesize'),
  47. 'author' => array('data' => t('Author'), 'field' => 'u.name'),
  48. 'timestamp' => array('data' => t('Updated'), 'field' => 'f.timestamp', 'sort' => 'desc'),
  49. 'operations' => array('data' => t('Operations')),
  50. );
  51. $query = db_select('file_managed', 'f')->extend('PagerDefault')->extend('TableSort');
  52. $query->join('users', 'u', 'f.uid = u.uid');
  53. $query->fields('f', array('fid'));
  54. $query->fields('u', array('uid'));
  55. $query->condition('f.status', FILE_STATUS_PERMANENT);
  56. $query->limit($limit);
  57. $query->orderByHeader($header);
  58. foreach (array_keys(file_entity_get_hidden_stream_wrappers()) as $name) {
  59. $query->condition('f.uri', $name . '%', 'NOT LIKE');
  60. }
  61. $result = $query->execute()->fetchAllKeyed();
  62. $files = file_load_multiple(array_keys($result));
  63. $form['#files'] = $files;
  64. // This is used to add usernames to the table below.
  65. $accounts = user_load_multiple(array_values($result));
  66. $destination = drupal_get_destination();
  67. $options = array();
  68. foreach ($files as $file) {
  69. $options[$file->fid] = array(
  70. 'title' => theme('file_entity_file_link', array('file' => $file)),
  71. 'type' => check_plain($file->filemime),
  72. 'size' => format_size($file->filesize),
  73. 'author' => theme('username', array('account' => $accounts[$file->uid])),
  74. 'timestamp' => format_date($file->timestamp, 'short'),
  75. );
  76. $options[$file->fid]['operations'] = array(
  77. 'data' => array(
  78. '#theme' => 'links__file_operations',
  79. '#links' => array(),
  80. '#attributes' => array('class' => array('links', 'inline')),
  81. ),
  82. );
  83. $options[$file->fid]['operations']['data']['#links']['edit'] = array(
  84. 'title' => t('Edit'),
  85. 'href' => 'file/' . $file->fid . '/edit',
  86. 'query' => $destination,
  87. );
  88. $options[$file->fid]['operations']['data']['#links']['delete'] = array(
  89. 'title' => t('Delete'),
  90. 'href' => 'file/' . $file->fid . '/delete',
  91. 'query' => $destination,
  92. );
  93. }
  94. $form['files'] = array(
  95. '#type' => 'tableselect',
  96. '#header' => $header,
  97. '#options' => $options,
  98. '#empty' => t('No files available.'),
  99. '#attributes' => array('class' => array('file-display-table', 'clear')),
  100. );
  101. $form['pager'] = array('#markup' => theme('pager', array('tags' => NULL)));
  102. return $form;
  103. }
  104. /**
  105. * Validate file_entity_admin_files form submissions.
  106. *
  107. * Check if any files have been selected to perform the chosen
  108. * 'Update option' on.
  109. */
  110. function file_entity_admin_files_validate($form, &$form_state) {
  111. // Error if there are no items to select.
  112. if (!is_array($form_state['values']['files']) || !count(array_filter($form_state['values']['files']))) {
  113. form_set_error('', t('No files selected.'));
  114. }
  115. }
  116. /**
  117. * Submit handler for file_entity_admin_files.
  118. */
  119. function file_entity_admin_files_submit($form, &$form_state) {
  120. $operations = $form['#operations'];
  121. $operation = $operations[$form_state['values']['operation']];
  122. // In the case of an operation which needs confirmation, rebuild the form.
  123. if (!empty($operation['confirm'])) {
  124. $form_state['rebuild'] = TRUE;
  125. $form_state['values']['operation'] = $operation;
  126. return;
  127. }
  128. // Filter out unchecked nodes
  129. $files = array_filter($form_state['values']['files']);
  130. if ($function = $operation['callback']) {
  131. // Add in callback arguments if present.
  132. if (isset($operation['callback arguments'])) {
  133. $args = array_merge(array($files), $operation['callback arguments']);
  134. }
  135. else {
  136. $args = array($files);
  137. }
  138. call_user_func_array($function, $args);
  139. cache_clear_all();
  140. }
  141. }
  142. /**
  143. * Displays the file type admin overview page.
  144. */
  145. function file_entity_list_types_page() {
  146. $types = file_info_file_types();
  147. $entity_info = entity_get_info('file');
  148. $field_ui = module_exists('field_ui');
  149. $header = array(
  150. array('data' => t('Name')),
  151. array('data' => t('Operations'), 'colspan' => $field_ui ? '3' : '1'),
  152. );
  153. $rows = array();
  154. foreach ($types as $type => $info) {
  155. $row = array(array('data' => theme('file_entity_file_type_overview', $info)));
  156. $path = isset($entity_info['bundles'][$type]['admin']['real path']) ? $entity_info['bundles'][$type]['admin']['real path'] : NULL;
  157. if ($field_ui) {
  158. $row[] = array('data' => isset($path) ? l(t('manage fields'), $path . '/fields') : '');
  159. $row[] = array('data' => isset($path) ? l(t('manage display'), $path . '/display') : '');
  160. }
  161. $row[] = array('data' => isset($path) ? l(t('manage file display'), $path . '/file-display') : '');
  162. $rows[] = $row;
  163. }
  164. $build['file_type_table'] = array(
  165. '#theme' => 'table',
  166. '#header' => $header,
  167. '#rows' => $rows,
  168. '#empty' => t('No file types available.'),
  169. );
  170. return $build;
  171. }
  172. /**
  173. * Form callback; presents file display settings for a given view mode.
  174. */
  175. function file_entity_file_display_form($form, &$form_state, $file_type, $view_mode) {
  176. $form['#file_type'] = $file_type;
  177. $form['#view_mode'] = $view_mode;
  178. $form['#tree'] = TRUE;
  179. $form['#attached']['js'][] = drupal_get_path('module', 'file_entity') . '/file_entity.admin.js';
  180. // Retrieve available formatters for this file type and load all configured
  181. // filters for existing text formats.
  182. $formatters = file_info_formatter_types();
  183. foreach ($formatters as $name => $formatter) {
  184. if (isset($formatter['file types']) && !in_array($file_type, $formatter['file types'])) {
  185. unset($formatters[$name]);
  186. }
  187. }
  188. $current_displays = file_displays_load($file_type, $view_mode, TRUE);
  189. foreach ($current_displays as $name => $display) {
  190. $current_displays[$name] = (array) $display;
  191. }
  192. // Formatter status.
  193. $form['displays']['status'] = array(
  194. '#type' => 'item',
  195. '#title' => t('Enabled displays'),
  196. '#prefix' => '<div id="file-displays-status-wrapper">',
  197. '#suffix' => '</div>',
  198. );
  199. $i=0;
  200. foreach ($formatters as $name => $formatter) {
  201. $form['displays']['status'][$name] = array(
  202. '#type' => 'checkbox',
  203. '#title' => check_plain($formatter['label']),
  204. '#default_value' => !empty($current_displays[$name]['status']),
  205. '#description' => isset($formatter['description']) ? filter_xss($formatter['description']) : NULL,
  206. '#parents' => array('displays', $name, 'status'),
  207. '#weight' => (isset($formatter['weight']) ? $formatter['weight'] : 0) + ($i / 1000),
  208. );
  209. $i++;
  210. }
  211. // Formatter order (tabledrag).
  212. $form['displays']['order'] = array(
  213. '#type' => 'item',
  214. '#title' => t('Display precedence order'),
  215. '#theme' => 'file_entity_file_display_order',
  216. );
  217. foreach ($formatters as $name => $formatter) {
  218. $form['displays']['order'][$name]['label'] = array(
  219. '#markup' => check_plain($formatter['label']),
  220. );
  221. $form['displays']['order'][$name]['weight'] = array(
  222. '#type' => 'weight',
  223. '#title' => t('Weight for @title', array('@title' => $formatter['label'])),
  224. '#title_display' => 'invisible',
  225. '#delta' => 50,
  226. '#default_value' => isset($current_displays[$name]['weight']) ? $current_displays[$name]['weight'] : 0,
  227. '#parents' => array('displays', $name, 'weight'),
  228. );
  229. $form['displays']['order'][$name]['#weight'] = $form['displays']['order'][$name]['weight']['#default_value'];
  230. }
  231. // Formatter settings.
  232. $form['display_settings_title'] = array(
  233. '#type' => 'item',
  234. '#title' => t('Display settings'),
  235. );
  236. $form['display_settings'] = array(
  237. '#type' => 'vertical_tabs',
  238. );
  239. $i=0;
  240. foreach ($formatters as $name => $formatter) {
  241. if (isset($formatter['settings callback']) && ($function = $formatter['settings callback']) && function_exists($function)) {
  242. $defaults = !empty($formatter['default settings']) ? $formatter['default settings'] : array();
  243. $settings = !empty($current_displays[$name]['settings']) ? $current_displays[$name]['settings'] : array();
  244. $settings += $defaults;
  245. $settings_form = $function($form, $form_state, $settings, $name, $file_type, $view_mode);
  246. if (!empty($settings_form)) {
  247. $form['displays']['settings'][$name] = array(
  248. '#type' => 'fieldset',
  249. '#title' => check_plain($formatter['label']),
  250. '#parents' => array('displays', $name, 'settings'),
  251. '#group' => 'display_settings',
  252. '#weight' => (isset($formatter['weight']) ? $formatter['weight'] : 0) + ($i / 1000),
  253. ) + $settings_form;
  254. }
  255. }
  256. $i++;
  257. }
  258. $form['actions'] = array('#type' => 'actions');
  259. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  260. return $form;
  261. }
  262. /**
  263. * Process file display settings form submissions.
  264. */
  265. function file_entity_file_display_form_submit($form, &$form_state) {
  266. $file_type = $form['#file_type'];
  267. $view_mode = $form['#view_mode'];
  268. $displays = isset($form_state['values']['displays']) ? $form_state['values']['displays'] : array();
  269. $displays_original = file_displays_load($file_type, $view_mode, TRUE);
  270. foreach ($displays as $formatter_name => $display) {
  271. $display_original = isset($displays_original[$formatter_name]) ? $displays_original[$formatter_name] : file_display_new($file_type, $view_mode, $formatter_name);
  272. $display += (array) $display_original;
  273. file_display_save((object) $display);
  274. }
  275. drupal_set_message(t('Your settings have been saved.'));
  276. }
  277. /**
  278. * Returns HTML for a file type label and description for the file type admin overview page.
  279. */
  280. function theme_file_entity_file_type_overview($variables) {
  281. return check_plain($variables['label']) . '<div class="description">' . $variables['description'] . '</div>';
  282. }
  283. /**
  284. * Returns HTML for a file display's display order table.
  285. */
  286. function theme_file_entity_file_display_order($variables) {
  287. $element = $variables['element'];
  288. $rows = array();
  289. foreach (element_children($element, TRUE) as $name) {
  290. $element[$name]['weight']['#attributes']['class'][] = 'file-display-order-weight';
  291. $rows[] = array(
  292. 'data' => array(
  293. drupal_render($element[$name]['label']),
  294. drupal_render($element[$name]['weight']),
  295. ),
  296. 'class' => array('draggable'),
  297. );
  298. }
  299. $output = drupal_render_children($element);
  300. $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'file-displays-order')));
  301. drupal_add_tabledrag('file-displays-order', 'order', 'sibling', 'file-display-order-weight', NULL, NULL, TRUE);
  302. return $output;
  303. }