file_entity.admin.inc

  1. cis7 sites/all/modules/ulmus/file_entity/file_entity.admin.inc
  2. cle7 sites/all/modules/ulmus/file_entity/file_entity.admin.inc
  3. ecd7 sites/all/modules/ulmus/file_entity/file_entity.admin.inc
  4. elmsmedia7 sites/all/modules/ulmus/file_entity/file_entity.admin.inc
  5. harmony7 sites/all/modules/ulmus/file_entity/file_entity.admin.inc
  6. icor7 sites/all/modules/ulmus/file_entity/file_entity.admin.inc
  7. meedjum_blog7 sites/all/modules/ulmus/file_entity/file_entity.admin.inc
  8. mooc7 sites/all/modules/ulmus/file_entity/file_entity.admin.inc

File administration and module settings UI.

Functions

Namesort descending Description
file_entity_admin_file Menu callback: file administration.
file_entity_admin_files Form builder: Builds the file administration overview.
file_entity_admin_files_submit Process file_entity_admin_files form submissions.
file_entity_admin_files_validate Validate file_entity_admin_files form submissions.
file_entity_build_filter_query Apply filters for file administration filters based on session.
file_entity_file_display_form Form callback; presents file display settings for a given view mode.
file_entity_file_display_form_submit Process file display settings form submissions.
file_entity_file_type_form Form constructor for the file type settings form.
file_entity_file_type_form_submit Form submission handler for file_entity_file_type_form().
file_entity_file_type_form_validate Form validation handler for file_entity_file_type_form().
file_entity_filter_form Return form for file administration filters.
file_entity_filter_form_submit Process result from file administration filter form.
file_entity_list_types_page Displays the file type admin overview page.
file_entity_mass_update Make mass update of files.
file_entity_multiple_delete_confirm File entity delete confirmation.
file_entity_multiple_delete_confirm_submit Submit handler for delete confirmation.
file_entity_settings_form Form callback for file_entity settings.
file_entity_type_delete_confirm Menu callback; delete a single file type.
file_entity_type_delete_confirm_submit Process file type delete confirm submissions.
file_entity_type_disable_confirm Menu callback; disable a single file type.
file_entity_type_disable_confirm_submit Process file type disable confirm submissions.
file_entity_type_enable_confirm Menu callback; disable a single file type.
file_entity_type_enable_confirm_submit Process file type disable confirm submissions.
file_entity_type_revert_confirm Menu callback; revert a single file type.
file_entity_type_revert_confirm_submit Process file type delete confirm submissions.
file_filters List file administration filters that can be applied.
theme_file_entity_file_display_order Returns HTML for a file display's display order table.
theme_file_entity_file_type_overview Returns HTML for the file type overview page.
_file_entity_mass_update_batch_finished File Mass Update Batch 'finished' callback.
_file_entity_mass_update_batch_process File Mass Update Batch operation.
_file_entity_mass_update_helper File Mass Update - helper function.

File

sites/all/modules/ulmus/file_entity/file_entity.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * File administration and module settings UI.
  5. */
  6. require_once dirname(__FILE__) . '/file_entity.pages.inc';
  7. /**
  8. * List file administration filters that can be applied.
  9. */
  10. function file_filters() {
  11. $visible_steam_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
  12. $options = array();
  13. foreach ($visible_steam_wrappers as $scheme => $information) {
  14. $options[$scheme] = check_plain($information['name']);
  15. }
  16. $filters['uri'] = array(
  17. 'title' => t('scheme'),
  18. 'options' => array(
  19. '[any]' => t('any'),
  20. ) + $options,
  21. );
  22. $filters['type'] = array(
  23. 'title' => t('type'),
  24. 'options' => array(
  25. '[any]' => t('any'),
  26. ) + file_entity_type_get_names(),
  27. );
  28. return $filters;
  29. }
  30. /**
  31. * Apply filters for file administration filters based on session.
  32. *
  33. * @param object $query
  34. * A SelectQuery to which the filters should be applied.
  35. */
  36. function file_entity_build_filter_query(SelectQueryInterface $query) {
  37. // Build query.
  38. $filter_data = isset($_SESSION['file_entity_overview_filter']) ? $_SESSION['file_entity_overview_filter'] : array();
  39. foreach ($filter_data as $index => $filter) {
  40. list($key, $value) = $filter;
  41. switch ($key) {
  42. case 'uri':
  43. $query->condition('fm.' . $key, $value . '%', 'LIKE');
  44. break;
  45. case 'type':
  46. $query->condition('fm.' . $key, $value);
  47. break;
  48. }
  49. }
  50. }
  51. /**
  52. * Return form for file administration filters.
  53. */
  54. function file_entity_filter_form() {
  55. $session = isset($_SESSION['file_entity_overview_filter']) ? $_SESSION['file_entity_overview_filter'] : array();
  56. $filters = file_filters();
  57. $i = 0;
  58. $form['filters'] = array(
  59. '#type' => 'fieldset',
  60. '#title' => t('Show only items where'),
  61. '#theme' => 'exposed_filters__file_entity',
  62. );
  63. foreach ($session as $filter) {
  64. list($type, $value) = $filter;
  65. if ($type == 'term') {
  66. // Load term name from DB rather than search and parse options array.
  67. $value = module_invoke('taxonomy', 'term_load', $value);
  68. $value = $value->name;
  69. }
  70. else {
  71. $value = $filters[$type]['options'][$value];
  72. }
  73. $t_args = array('%property' => $filters[$type]['title'], '%value' => $value);
  74. if ($i++) {
  75. $form['filters']['current'][] = array('#markup' => t('and where %property is %value', $t_args));
  76. }
  77. else {
  78. $form['filters']['current'][] = array('#markup' => t('where %property is %value', $t_args));
  79. }
  80. if (in_array($type, array('type', 'uri'))) {
  81. // Remove the option if it is already being filtered on.
  82. unset($filters[$type]);
  83. }
  84. }
  85. $form['filters']['status'] = array(
  86. '#type' => 'container',
  87. '#attributes' => array('class' => array('clearfix')),
  88. '#prefix' => ($i ? '<div class="additional-filters">' . t('and where') . '</div>' : ''),
  89. );
  90. $form['filters']['status']['filters'] = array(
  91. '#type' => 'container',
  92. '#attributes' => array('class' => array('filters')),
  93. );
  94. foreach ($filters as $key => $filter) {
  95. $form['filters']['status']['filters'][$key] = array(
  96. '#type' => 'select',
  97. '#options' => $filter['options'],
  98. '#title' => $filter['title'],
  99. '#default_value' => '[any]',
  100. );
  101. }
  102. $form['filters']['status']['actions'] = array(
  103. '#type' => 'actions',
  104. '#attributes' => array('class' => array('container-inline')),
  105. );
  106. if (count($filters)) {
  107. $form['filters']['status']['actions']['submit'] = array(
  108. '#type' => 'submit',
  109. '#value' => count($session) ? t('Refine') : t('Filter'),
  110. );
  111. }
  112. if (count($session)) {
  113. $form['filters']['status']['actions']['undo'] = array('#type' => 'submit', '#value' => t('Undo'));
  114. $form['filters']['status']['actions']['reset'] = array('#type' => 'submit', '#value' => t('Reset'));
  115. }
  116. drupal_add_js('misc/form.js');
  117. return $form;
  118. }
  119. /**
  120. * Process result from file administration filter form.
  121. */
  122. function file_entity_filter_form_submit($form, &$form_state) {
  123. $filters = file_filters();
  124. switch ($form_state['values']['op']) {
  125. case t('Filter'):
  126. case t('Refine'):
  127. // Apply every filter that has a choice selected other than 'any'.
  128. foreach ($filters as $filter => $options) {
  129. if (isset($form_state['values'][$filter]) && $form_state['values'][$filter] != '[any]') {
  130. // Flatten the options array to accommodate hierarchical/nested
  131. // options.
  132. $flat_options = form_options_flatten($filters[$filter]['options']);
  133. // Only accept valid selections offered on the dropdown, block bad
  134. // input.
  135. if (isset($flat_options[$form_state['values'][$filter]])) {
  136. $_SESSION['file_entity_overview_filter'][] = array($filter, $form_state['values'][$filter]);
  137. }
  138. }
  139. }
  140. break;
  141. case t('Undo'):
  142. array_pop($_SESSION['file_entity_overview_filter']);
  143. break;
  144. case t('Reset'):
  145. $_SESSION['file_entity_overview_filter'] = array();
  146. break;
  147. }
  148. }
  149. /**
  150. * Make mass update of files.
  151. *
  152. * Change all files in the $files array to update them with the field values in
  153. * $updates.
  154. *
  155. * IMPORTANT NOTE: This function is intended to work when called
  156. * from a form submit handler. Calling it outside of the form submission
  157. * process may not work correctly.
  158. *
  159. * @param array $files
  160. * Array of file fids to update.
  161. * @param array $updates
  162. * Array of key/value pairs with file field names and the
  163. * value to update that field to.
  164. */
  165. function file_entity_mass_update($files, $updates) {
  166. // We use batch processing to prevent timeout when updating a large number
  167. // of files.
  168. if (count($files) > 10) {
  169. $batch = array(
  170. 'operations' => array(
  171. array(
  172. '_file_entity_mass_update_batch_process',
  173. array($files, $updates),
  174. ),
  175. ),
  176. 'finished' => '_file_entity_mass_update_batch_finished',
  177. 'title' => t('Processing'),
  178. // We use a single multi-pass operation, so the default
  179. // 'Remaining x of y operations' message will be confusing here.
  180. 'progress_message' => '',
  181. 'error_message' => t('The update has encountered an error.'),
  182. // The operations do not live in the .module file, so we need to
  183. // tell the batch engine which file to load before calling them.
  184. 'file' => drupal_get_path('module', 'file_entity') . '/file_entity.admin.inc',
  185. );
  186. batch_set($batch);
  187. }
  188. else {
  189. foreach ($files as $fid) {
  190. _file_entity_mass_update_helper($fid, $updates);
  191. }
  192. drupal_set_message(t('The update has been performed.'));
  193. }
  194. }
  195. /**
  196. * File Mass Update - helper function.
  197. */
  198. function _file_entity_mass_update_helper($fid, $updates) {
  199. $file = file_load($fid);
  200. // For efficiency manually save the original file before applying any changes.
  201. $file->original = clone $file;
  202. foreach ($updates as $name => $value) {
  203. $file->$name = $value;
  204. }
  205. file_save($file);
  206. return $file;
  207. }
  208. /**
  209. * File Mass Update Batch operation.
  210. */
  211. function _file_entity_mass_update_batch_process($files, $updates, &$context) {
  212. if (!isset($context['sandbox']['progress'])) {
  213. $context['sandbox']['progress'] = 0;
  214. $context['sandbox']['max'] = count($files);
  215. $context['sandbox']['files'] = $files;
  216. }
  217. // Process files by groups of 5.
  218. $count = min(5, count($context['sandbox']['files']));
  219. for ($i = 1; $i <= $count; $i++) {
  220. // For each fid, load the file, reset the values, and save it.
  221. $fid = array_shift($context['sandbox']['files']);
  222. $file = _file_entity_mass_update_helper($fid, $updates);
  223. // Store result for post-processing in the finished callback.
  224. $context['results'][] = l($file->filename, 'file/' . $file->fid);
  225. // Update our progress information.
  226. $context['sandbox']['progress']++;
  227. }
  228. // Inform the batch engine that we are not finished,
  229. // and provide an estimation of the completion level we reached.
  230. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  231. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  232. }
  233. }
  234. /**
  235. * File Mass Update Batch 'finished' callback.
  236. */
  237. function _file_entity_mass_update_batch_finished($success, $results, $operations) {
  238. if ($success) {
  239. drupal_set_message(t('The update has been performed.'));
  240. }
  241. else {
  242. drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
  243. $message = format_plural(count($results), '1 item successfully processed:', '@count items successfully processed:');
  244. $message .= theme('item_list', array('items' => $results));
  245. drupal_set_message($message);
  246. }
  247. }
  248. /**
  249. * Menu callback: file administration.
  250. */
  251. function file_entity_admin_file($form, $form_state) {
  252. if (isset($form_state['values']['operation']) && $form_state['values']['operation'] == 'delete') {
  253. return file_entity_multiple_delete_confirm($form, $form_state, array_filter($form_state['values']['files']));
  254. }
  255. $form['filter'] = file_entity_filter_form();
  256. $form['#submit'][] = 'file_entity_filter_form_submit';
  257. $form['admin'] = file_entity_admin_files();
  258. return $form;
  259. }
  260. /**
  261. * Form builder: Builds the file administration overview.
  262. */
  263. function file_entity_admin_files() {
  264. $admin_access = user_access('administer files');
  265. // Build the 'Update options' form.
  266. $form['options'] = array(
  267. '#type' => 'fieldset',
  268. '#title' => t('Update options'),
  269. '#attributes' => array('class' => array('container-inline')),
  270. '#access' => $admin_access,
  271. );
  272. $options = array();
  273. foreach (module_invoke_all('file_operations') as $operation => $array) {
  274. $options[$operation] = $array['label'];
  275. }
  276. $form['options']['operation'] = array(
  277. '#type' => 'select',
  278. '#title' => t('Operation'),
  279. '#title_display' => 'invisible',
  280. '#options' => $options,
  281. '#default_value' => 'approve',
  282. );
  283. $form['options']['submit'] = array(
  284. '#type' => 'submit',
  285. '#value' => t('Update'),
  286. '#validate' => array('file_entity_admin_files_validate'),
  287. '#submit' => array('file_entity_admin_files_submit'),
  288. );
  289. // Build the sortable table header.
  290. $header = array(
  291. 'title' => array('data' => t('Title'), 'field' => 'fm.filename'),
  292. 'type' => array('data' => t('Type'), 'field' => 'fm.type'),
  293. 'size' => array('data' => t('Size'), 'field' => 'fm.filesize'),
  294. 'author' => t('Author'),
  295. 'timestamp' => array(
  296. 'data' => t('Updated'),
  297. 'field' => 'fm.timestamp',
  298. 'sort' => 'desc'),
  299. 'usage' => array('data' => t('Used in'), 'field' => 'total_count'),
  300. 'operations' => array('data' => t('Operations')),
  301. );
  302. $query = db_select('file_managed', 'fm')->extend('PagerDefault')->extend('TableSort');
  303. $query->leftJoin('file_usage', 'fu', 'fm.fid = fu.fid');
  304. $query->groupBy('fm.fid');
  305. $query->addExpression('SUM(fu.count)', 'total_count');
  306. file_entity_build_filter_query($query);
  307. $result = $query
  308. ->fields('fm', array('fid', 'uid'))
  309. ->limit(50)
  310. ->orderByHeader($header)
  311. ->addTag('file_access')
  312. ->execute()
  313. ->fetchAllAssoc('fid');
  314. $files = file_load_multiple(array_keys($result));
  315. $uids = array();
  316. foreach ($files as $file) {
  317. $uids[] = $file->uid;
  318. }
  319. $accounts = !empty($uids) ? user_load_multiple(array_unique($uids)) : array();
  320. // Prepare the list of files.
  321. $destination = drupal_get_destination();
  322. $options = array();
  323. foreach ($files as $file) {
  324. $file_type = file_type_load($file->type);
  325. $options[$file->fid] = array(
  326. 'title' => array(
  327. 'data' => array(
  328. '#type' => 'link',
  329. '#title' => $file->filename,
  330. '#href' => 'file/' . $file->fid,
  331. ),
  332. ),
  333. 'type' => $file_type ? check_plain($file_type->label) : FILE_TYPE_NONE,
  334. 'size' => format_size($file->filesize),
  335. 'author' => theme('username', array('account' => $accounts[$file->uid])),
  336. 'timestamp' => format_date($file->timestamp, 'short'),
  337. 'usage' => format_plural((int) $result[$file->fid]->total_count, '1 place', '@count places'),
  338. );
  339. // Show a warning for files that do not exist.
  340. if (@!is_file($file->uri)) {
  341. $options[$file->fid]['#attributes']['class'][] = 'error';
  342. if (!file_stream_wrapper_get_instance_by_uri($file->uri)) {
  343. $options[$file->fid]['#attributes']['title'] = t('The stream wrapper for @scheme files is missing.', array('@scheme' => file_uri_scheme($file->uri)));
  344. }
  345. else {
  346. $options[$file->fid]['#attributes']['title'] = t('The file does not exist.');
  347. }
  348. }
  349. // Build a list of all the accessible operations for the current file.
  350. $operations = array();
  351. if (file_entity_access('update', $file)) {
  352. // Convert the usage count to a link.
  353. $options[$file->fid]['usage'] = l($options[$file->fid]['usage'], 'file/' . $file->fid . '/usage');
  354. $operations['edit'] = array(
  355. 'title' => t('Edit'),
  356. 'href' => 'file/' . $file->fid . '/edit',
  357. 'query' => $destination,
  358. );
  359. }
  360. if (file_entity_access('delete', $file)) {
  361. $operations['delete'] = array(
  362. 'title' => t('Delete'),
  363. 'href' => 'file/' . $file->fid . '/delete',
  364. 'query' => $destination,
  365. );
  366. }
  367. $options[$file->fid]['operations'] = array();
  368. if (count($operations) > 1) {
  369. // Render an unordered list of operations links.
  370. $options[$file->fid]['operations'] = array(
  371. 'data' => array(
  372. '#theme' => 'links__file_entity_operations',
  373. '#links' => $operations,
  374. '#attributes' => array('class' => array('links', 'inline')),
  375. ),
  376. );
  377. }
  378. elseif (!empty($operations)) {
  379. // Render the first and only operation as a link.
  380. $link = reset($operations);
  381. $options[$file->fid]['operations'] = array(
  382. 'data' => array(
  383. '#type' => 'link',
  384. '#title' => $link['title'],
  385. '#href' => $link['href'],
  386. '#options' => array('query' => $link['query']),
  387. ),
  388. );
  389. }
  390. }
  391. // Only use a tableselect when the current user is able to perform any
  392. // operations.
  393. if ($admin_access) {
  394. $form['files'] = array(
  395. '#type' => 'tableselect',
  396. '#header' => $header,
  397. '#options' => $options,
  398. '#empty' => t('No files available.'),
  399. );
  400. }
  401. // Otherwise, use a simple table.
  402. else {
  403. $form['files'] = array(
  404. '#theme' => 'table',
  405. '#header' => $header,
  406. '#rows' => $options,
  407. '#empty' => t('No files available.'),
  408. );
  409. }
  410. $form['pager'] = array('#markup' => theme('pager'));
  411. return $form;
  412. }
  413. /**
  414. * Validate file_entity_admin_files form submissions.
  415. *
  416. * Check if any files have been selected to perform the chosen
  417. * 'Update option' on.
  418. */
  419. function file_entity_admin_files_validate($form, &$form_state) {
  420. // Error if there are no items to select.
  421. if (!is_array($form_state['values']['files']) || !count(array_filter($form_state['values']['files']))) {
  422. form_set_error('', t('No items selected.'));
  423. }
  424. }
  425. /**
  426. * Process file_entity_admin_files form submissions.
  427. *
  428. * Execute the chosen 'Update option' on the selected files.
  429. */
  430. function file_entity_admin_files_submit($form, &$form_state) {
  431. $operations = module_invoke_all('file_operations');
  432. $operation = $operations[$form_state['values']['operation']];
  433. // Filter out unchecked files.
  434. $files = array_filter($form_state['values']['files']);
  435. if ($function = $operation['callback']) {
  436. // Add in callback arguments if present.
  437. if (isset($operation['callback arguments'])) {
  438. $args = array_merge(array($files), $operation['callback arguments']);
  439. }
  440. else {
  441. $args = array($files);
  442. }
  443. call_user_func_array($function, $args);
  444. cache_clear_all();
  445. }
  446. else {
  447. // We need to rebuild the form to go to a second step. For example, to
  448. // show the confirmation form for the deletion of files.
  449. $form_state['rebuild'] = TRUE;
  450. }
  451. }
  452. /**
  453. * File entity delete confirmation.
  454. */
  455. function file_entity_multiple_delete_confirm($form, &$form_state, $files) {
  456. $form['files'] = array(
  457. '#prefix' => '<ul>',
  458. '#suffix' => '</ul>',
  459. '#tree' => TRUE,
  460. );
  461. // array_filter returns only elements with TRUE values.
  462. foreach ($files as $fid => $value) {
  463. $filename = db_query('SELECT filename FROM {file_managed} WHERE fid = :fid', array(':fid' => $fid))->fetchField();
  464. $form['files'][$fid] = array(
  465. '#type' => 'hidden',
  466. '#value' => $fid,
  467. '#prefix' => '<li>',
  468. '#suffix' => check_plain($filename) . "</li>\n",
  469. );
  470. }
  471. $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
  472. $form['#submit'][] = 'file_entity_multiple_delete_confirm_submit';
  473. $confirm_question = format_plural(count($files),
  474. 'Are you sure you want to delete this item?',
  475. 'Are you sure you want to delete these items?');
  476. return confirm_form($form,
  477. $confirm_question,
  478. 'admin/content/file', t('This action cannot be undone.'),
  479. t('Delete'), t('Cancel'));
  480. }
  481. /**
  482. * Submit handler for delete confirmation.
  483. */
  484. function file_entity_multiple_delete_confirm_submit($form, &$form_state) {
  485. if ($form_state['values']['confirm']) {
  486. file_delete_multiple(array_keys($form_state['values']['files']));
  487. $count = count($form_state['values']['files']);
  488. watchdog('file_entity', 'Deleted @count files.', array('@count' => $count));
  489. drupal_set_message(format_plural($count, 'Deleted 1 file.', 'Deleted @count files.'));
  490. }
  491. $form_state['redirect'] = 'admin/content/file';
  492. }
  493. /**
  494. * Displays the file type admin overview page.
  495. */
  496. function file_entity_list_types_page() {
  497. $file_entity_info = entity_get_info('file');
  498. $field_ui = module_exists('field_ui');
  499. $colspan = $field_ui ? 5 : 3;
  500. $header = array(
  501. array('data' => t('Name')),
  502. array('data' => t('Operations'), 'colspan' => $colspan),
  503. array('data' => t('Status')),
  504. );
  505. $rows = array();
  506. $weight = 0;
  507. $types = file_type_load_all(TRUE);
  508. $count = count($types);
  509. foreach ($types as $type) {
  510. $weight++;
  511. $row = array(
  512. array(
  513. 'data' => theme('file_entity_file_type_overview',
  514. array(
  515. 'label' => $type->label,
  516. 'description' => $type->description,
  517. )
  518. ),
  519. ),
  520. );
  521. $path = isset($file_entity_info['bundles'][$type->type]['admin']['real path']) ? $file_entity_info['bundles'][$type->type]['admin']['real path'] : NULL;
  522. if (empty($type->disabled) && isset($path)) {
  523. $row[] = array('data' => l(t('edit file type'), $path . '/edit'));
  524. if ($field_ui) {
  525. $row[] = array('data' => l(t('manage fields'), $path . '/fields'));
  526. $row[] = array('data' => l(t('manage display'), $path . '/display'));
  527. }
  528. $row[] = array('data' => l(t('manage file display'), $path . '/file-display'));
  529. }
  530. else {
  531. $row += array_fill(1, $colspan - 1, '');
  532. }
  533. $admin_path = 'admin/structure/file-types/manage/' . $type->type;
  534. switch ($type->ctools_type) {
  535. // Configuration is in code.
  536. case 'Default':
  537. if (!empty($type->disabled)) {
  538. $row[] = l(t('enable'), $admin_path . '/enable');
  539. }
  540. else {
  541. $row[] = l(t('disable'), $admin_path . '/disable');
  542. }
  543. break;
  544. // Configuration is in DB.
  545. case 'Normal':
  546. if (!empty($type->disabled)) {
  547. $status = l(t('enable'), $admin_path . '/enable');
  548. }
  549. else {
  550. $status = l(t('disable'), $admin_path . '/disable');
  551. }
  552. $row[] = $status . ' | ' . l(t('delete'), $admin_path . '/delete');
  553. break;
  554. // Configuration is in code, but overridden in DB.
  555. case 'Overridden':
  556. if (!empty($type->disabled)) {
  557. $row[] = l(t('enable'), $admin_path . '/enable');
  558. }
  559. else {
  560. $row[] = l(t('disable'), $admin_path . '/disable') . ' | ' . l(t('revert'), $admin_path . '/revert');
  561. }
  562. break;
  563. }
  564. if (!empty($type->disabled)) {
  565. $row[] = t('Disabled');
  566. $rows[$weight + $count] = array('data' => $row, 'class' => array('ctools-export-ui-disabled'));
  567. }
  568. else {
  569. $row[] = $type->ctools_type;
  570. $rows[$weight] = array('data' => $row);
  571. }
  572. }
  573. // Move disabled items to the bottom.
  574. ksort($rows);
  575. $build['file_type_table'] = array(
  576. '#theme' => 'table',
  577. '#header' => $header,
  578. '#rows' => $rows,
  579. '#empty' => t('No file types available.'),
  580. '#attached' => array(
  581. 'css' => array(drupal_get_path('module', 'ctools') . '/css/export-ui-list.css'),
  582. ),
  583. );
  584. return $build;
  585. }
  586. /**
  587. * Form callback; presents file display settings for a given view mode.
  588. */
  589. function file_entity_file_display_form($form, &$form_state, $file_type, $view_mode) {
  590. $form['#file_type'] = $file_type->type;
  591. $form['#view_mode'] = $view_mode;
  592. $form['#tree'] = TRUE;
  593. $form['#attached']['js'][] = drupal_get_path('module', 'file_entity') . '/file_entity.admin.js';
  594. // Retrieve available formatters for this file type and load all configured
  595. // filters for existing text formats.
  596. $formatters = file_info_formatter_types();
  597. foreach ($formatters as $name => $formatter) {
  598. if (!empty($formatter['hidden'])) {
  599. unset($formatters[$name]);
  600. }
  601. if (isset($formatter['mime types'])) {
  602. if (file_entity_match_mimetypes($formatter['mime types'], $file_type->mimetypes)) {
  603. continue;
  604. }
  605. unset($formatters[$name]);
  606. }
  607. }
  608. $current_displays = file_displays_load($file_type->type, $view_mode, TRUE);
  609. foreach ($current_displays as $name => $display) {
  610. $current_displays[$name] = (array) $display;
  611. }
  612. // Formatter status.
  613. $form['displays']['status'] = array(
  614. '#type' => 'item',
  615. '#title' => t('Enabled displays'),
  616. '#prefix' => '<div id="file-displays-status-wrapper">',
  617. '#suffix' => '</div>',
  618. );
  619. $i = 0;
  620. foreach ($formatters as $name => $formatter) {
  621. $form['displays']['status'][$name] = array(
  622. '#type' => 'checkbox',
  623. '#title' => check_plain($formatter['label']),
  624. '#default_value' => !empty($current_displays[$name]['status']),
  625. '#description' => isset($formatter['description']) ? filter_xss($formatter['description']) : NULL,
  626. '#parents' => array('displays', $name, 'status'),
  627. '#weight' => (isset($formatter['weight']) ? $formatter['weight'] : 0) + ($i / 1000),
  628. );
  629. $i++;
  630. }
  631. // Formatter order (tabledrag).
  632. $form['displays']['order'] = array(
  633. '#type' => 'item',
  634. '#title' => t('Display precedence order'),
  635. '#theme' => 'file_entity_file_display_order',
  636. );
  637. foreach ($formatters as $name => $formatter) {
  638. $form['displays']['order'][$name]['label'] = array(
  639. '#markup' => check_plain($formatter['label']),
  640. );
  641. $form['displays']['order'][$name]['weight'] = array(
  642. '#type' => 'weight',
  643. '#title' => t('Weight for @title', array('@title' => $formatter['label'])),
  644. '#title_display' => 'invisible',
  645. '#delta' => 50,
  646. '#default_value' => isset($current_displays[$name]['weight']) ? $current_displays[$name]['weight'] : 0,
  647. '#parents' => array('displays', $name, 'weight'),
  648. );
  649. $form['displays']['order'][$name]['#weight'] = $form['displays']['order'][$name]['weight']['#default_value'];
  650. }
  651. // Formatter settings.
  652. $form['display_settings_title'] = array(
  653. '#type' => 'item',
  654. '#title' => t('Display settings'),
  655. );
  656. $form['display_settings'] = array(
  657. '#type' => 'vertical_tabs',
  658. );
  659. $i = 0;
  660. foreach ($formatters as $name => $formatter) {
  661. if (isset($formatter['settings callback']) && ($function = $formatter['settings callback']) && function_exists($function)) {
  662. $defaults = !empty($formatter['default settings']) ? $formatter['default settings'] : array();
  663. $settings = !empty($current_displays[$name]['settings']) ? $current_displays[$name]['settings'] : array();
  664. $settings += $defaults;
  665. $settings_form = $function($form, $form_state, $settings, $name, $file_type->type, $view_mode);
  666. if (!empty($settings_form)) {
  667. $form['displays']['settings'][$name] = array(
  668. '#type' => 'fieldset',
  669. '#title' => check_plain($formatter['label']),
  670. '#parents' => array('displays', $name, 'settings'),
  671. '#group' => 'display_settings',
  672. '#weight' => (isset($formatter['weight']) ? $formatter['weight'] : 0) + ($i / 1000),
  673. ) + $settings_form;
  674. }
  675. }
  676. $i++;
  677. }
  678. $form['actions'] = array('#type' => 'actions');
  679. $form['actions']['submit'] = array(
  680. '#type' => 'submit',
  681. '#value' => t('Save configuration'),
  682. );
  683. return $form;
  684. }
  685. /**
  686. * Process file display settings form submissions.
  687. */
  688. function file_entity_file_display_form_submit($form, &$form_state) {
  689. $file_type = $form['#file_type'];
  690. $view_mode = $form['#view_mode'];
  691. $displays = isset($form_state['values']['displays']) ? $form_state['values']['displays'] : array();
  692. $displays_original = file_displays_load($file_type, $view_mode, TRUE);
  693. foreach ($displays as $formatter_name => $display) {
  694. $display_original = isset($displays_original[$formatter_name]) ? $displays_original[$formatter_name] : file_display_new($file_type, $view_mode, $formatter_name);
  695. $display += (array) $display_original;
  696. file_display_save((object) $display);
  697. }
  698. drupal_set_message(t('Your settings have been saved.'));
  699. }
  700. /**
  701. * Returns HTML for the file type overview page.
  702. *
  703. * Specifically, this returns HTML for a file type label and description.
  704. */
  705. function theme_file_entity_file_type_overview($variables) {
  706. return check_plain($variables['label']) . '<div class="description">' . $variables['description'] . '</div>';
  707. }
  708. /**
  709. * Returns HTML for a file display's display order table.
  710. */
  711. function theme_file_entity_file_display_order($variables) {
  712. $element = $variables['element'];
  713. $rows = array();
  714. foreach (element_children($element, TRUE) as $name) {
  715. $element[$name]['weight']['#attributes']['class'][] = 'file-display-order-weight';
  716. $rows[] = array(
  717. 'data' => array(
  718. drupal_render($element[$name]['label']),
  719. drupal_render($element[$name]['weight']),
  720. ),
  721. 'class' => array('draggable'),
  722. );
  723. }
  724. $output = drupal_render_children($element);
  725. $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'file-displays-order')));
  726. drupal_add_tabledrag('file-displays-order', 'order', 'sibling', 'file-display-order-weight', NULL, NULL, TRUE);
  727. return $output;
  728. }
  729. /**
  730. * Form constructor for the file type settings form.
  731. *
  732. * @param object $type
  733. * The file type.
  734. *
  735. * @see file_entity_file_type_form_validate()
  736. * @see file_entity_file_type_form_submit()
  737. */
  738. function file_entity_file_type_form($form, &$form_state, $type = NULL) {
  739. if (!isset($type->type)) {
  740. // This is a new type.
  741. $type = (object) array(
  742. 'type' => '',
  743. 'label' => '',
  744. 'description' => '',
  745. 'mimetypes' => array(),
  746. );
  747. }
  748. $form['#file_type'] = $type;
  749. $form['label'] = array(
  750. '#type' => 'textfield',
  751. '#title' => t('Name'),
  752. '#description' => t('This is the human readable name of the file type.'),
  753. '#required' => TRUE,
  754. '#default_value' => $type->label,
  755. );
  756. $form['type'] = array(
  757. '#type' => 'machine_name',
  758. '#default_value' => $type->type,
  759. '#maxlength' => 255,
  760. '#disabled' => (bool) $type->type,
  761. '#machine_name' => array(
  762. 'exists' => 'file_type_load',
  763. 'source' => array('label'),
  764. ),
  765. '#description' => t('A unique machine-readable name for this file type. It must only contain lowercase letters, numbers, and underscores.'),
  766. );
  767. $form['description'] = array(
  768. '#type' => 'textarea',
  769. '#title' => t('Description'),
  770. '#description' => t('This is the description of the file type.'),
  771. '#default_value' => $type->description,
  772. );
  773. $form['mimetypes'] = array(
  774. '#type' => 'textarea',
  775. '#title' => t('Mimetypes'),
  776. '#description' => t('Enter one mimetype per line.'),
  777. '#default_value' => implode("\n", $type->mimetypes),
  778. );
  779. include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc';
  780. $mimetypes = file_mimetype_mapping();
  781. $form['mimetype_mapping'] = array(
  782. '#type' => 'fieldset',
  783. '#title' => t('Mimetype List'),
  784. '#collapsible' => TRUE,
  785. '#collapsed' => TRUE,
  786. );
  787. $form['mimetype_mapping']['mapping'] = array(
  788. '#theme' => 'item_list',
  789. '#items' => $mimetypes['mimetypes'],
  790. );
  791. $form['actions'] = array('#type' => 'actions');
  792. $form['actions']['submit'] = array(
  793. '#type' => 'submit',
  794. '#value' => t('Save'),
  795. );
  796. if (!empty($type->type)) {
  797. $form['actions']['delete'] = array(
  798. '#type' => 'submit',
  799. '#value' => t('Delete'),
  800. );
  801. }
  802. return $form;
  803. }
  804. /**
  805. * Form validation handler for file_entity_file_type_form().
  806. *
  807. * @see file_entity_file_type_form_submit()
  808. */
  809. function file_entity_file_type_form_validate($form, &$form_state) {
  810. include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc';
  811. $mimetype_mapping = file_mimetype_mapping();
  812. $valid_mimetypes = $mimetype_mapping['mimetypes'];
  813. $submitted_mimetypes = array_filter(array_map('trim', explode("\n", $form_state['values']['mimetypes'])));
  814. $invalid_mimetypes = array();
  815. foreach ($submitted_mimetypes as $mimetype) {
  816. if (!file_entity_match_mimetypes($mimetype, $valid_mimetypes)) {
  817. $invalid_mimetypes[] = $mimetype;
  818. }
  819. }
  820. foreach ($invalid_mimetypes as $mimetype) {
  821. form_set_error('mimetypes', t('The mimetype %mimetype is not a valid mimetype.', array('%mimetype' => $mimetype)));
  822. }
  823. }
  824. /**
  825. * Form submission handler for file_entity_file_type_form().
  826. *
  827. * @see file_entity_file_type_form_validate()
  828. */
  829. function file_entity_file_type_form_submit($form, &$form_state) {
  830. if (!empty($form['#file_type']->type)) {
  831. $type = file_type_load($form['#file_type']->type);
  832. }
  833. else {
  834. $type = (object) array(
  835. 'type' => $form_state['values']['type'],
  836. );
  837. }
  838. if ($form_state['values']['op'] == t('Delete')) {
  839. $form_state['redirect'] = 'admin/structure/file-types/manage/' . $type->type . '/delete';
  840. return;
  841. }
  842. $type->label = $form_state['values']['label'];
  843. $type->description = $form_state['values']['description'];
  844. $type->mimetypes = array_filter(array_map('trim', explode("\n", $form_state['values']['mimetypes'])));
  845. file_type_save($type);
  846. drupal_set_message(t('The file type %type has been updated.', array('%type' => $type->label)));
  847. $form_state['redirect'] = 'admin/structure/file-types';
  848. }
  849. /**
  850. * Menu callback; disable a single file type.
  851. */
  852. function file_entity_type_enable_confirm($form, &$form_state, $type) {
  853. $form['type'] = array('#type' => 'value', '#value' => $type->type);
  854. $form['label'] = array('#type' => 'value', '#value' => $type->label);
  855. $message = t('Are you sure you want to enable the file type %type?', array('%type' => $type->label));
  856. return confirm_form($form, $message, 'admin/structure/file-types', '', t('Enable'));
  857. }
  858. /**
  859. * Process file type disable confirm submissions.
  860. */
  861. function file_entity_type_enable_confirm_submit($form, &$form_state) {
  862. file_type_enable($form_state['values']['type']);
  863. $t_args = array('%label' => $form_state['values']['label']);
  864. drupal_set_message(t('The file type %label has been enabled.', $t_args));
  865. watchdog('file_entity', 'Enabled file type %label.', $t_args, WATCHDOG_NOTICE);
  866. $form_state['redirect'] = 'admin/structure/file-types';
  867. return;
  868. }
  869. /**
  870. * Menu callback; disable a single file type.
  871. */
  872. function file_entity_type_disable_confirm($form, &$form_state, $type) {
  873. $form['type'] = array('#type' => 'value', '#value' => $type->type);
  874. $form['label'] = array('#type' => 'value', '#value' => $type->label);
  875. $message = t('Are you sure you want to disable the file type %type?', array('%type' => $type->label));
  876. $caption = '';
  877. $num_files = db_query("SELECT COUNT(*) FROM {file_managed} WHERE type = :type", array(':type' => $type->type))->fetchField();
  878. if ($num_files) {
  879. $caption .= '<p>' . format_plural($num_files, '%type is used by 1 file on
  880. your site. If you disable this file type, you will not be able to edit
  881. the %type file and it may not display correctly.', '%type is used by
  882. @count files on your site. If you remove %type, you will not be able to
  883. edit the %type file and it may not display correctly.',
  884. array('%type' => $type->label)) . '</p>';
  885. }
  886. return confirm_form($form, $message, 'admin/structure/file-types', $caption, t('Disable'));
  887. }
  888. /**
  889. * Process file type disable confirm submissions.
  890. */
  891. function file_entity_type_disable_confirm_submit($form, &$form_state) {
  892. file_type_disable($form_state['values']['type']);
  893. $t_args = array('%label' => $form_state['values']['label']);
  894. drupal_set_message(t('The file type %label has been disabled.', $t_args));
  895. watchdog('file_entity', 'Disabled file type %label.', $t_args, WATCHDOG_NOTICE);
  896. $form_state['redirect'] = 'admin/structure/file-types';
  897. return;
  898. }
  899. /**
  900. * Menu callback; revert a single file type.
  901. */
  902. function file_entity_type_revert_confirm($form, &$form_state, $type) {
  903. $form['type'] = array('#type' => 'value', '#value' => $type->type);
  904. $form['label'] = array('#type' => 'value', '#value' => $type->label);
  905. $message = t('Are you sure you want to revert the file type %type?', array('%type' => $type->label));
  906. return confirm_form($form, $message, 'admin/structure/file-types', '', t('Revert'));
  907. }
  908. /**
  909. * Process file type delete confirm submissions.
  910. */
  911. function file_entity_type_revert_confirm_submit($form, &$form_state) {
  912. // @NOTE deleting the file_type from the DB actually reverts it to code.
  913. file_type_delete($form_state['values']['type']);
  914. $t_args = array('%label' => $form_state['values']['label']);
  915. drupal_set_message(t('The file type %label has been reverted.', $t_args));
  916. watchdog('file_entity', 'Reverted file type %label.', $t_args, WATCHDOG_NOTICE);
  917. $form_state['redirect'] = 'admin/structure/file-types';
  918. return;
  919. }
  920. /**
  921. * Menu callback; delete a single file type.
  922. */
  923. function file_entity_type_delete_confirm($form, &$form_state, $type) {
  924. $form['type'] = array('#type' => 'value', '#value' => $type->type);
  925. $form['label'] = array('#type' => 'value', '#value' => $type->label);
  926. $message = t('Are you sure you want to delete the file type %type?', array('%type' => $type->label));
  927. $caption = '';
  928. $num_files = db_query("SELECT COUNT(*) FROM {file_managed} WHERE type = :type", array(':type' => $type->type))->fetchField();
  929. if ($num_files) {
  930. $caption .= '<p>' . format_plural($num_files, '%type is used by 1 file on your site. If you remove this file type, you will not be able to edit the %type file and it may not display correctly.', '%type is used by @count pieces of file on your site. If you remove %type, you will not be able to edit the %type file and it may not display correctly.', array('%type' => $type->label)) . '</p>';
  931. }
  932. $caption .= '<p>' . t('This action cannot be undone.') . '</p>';
  933. return confirm_form($form, $message, 'admin/structure/file-types', $caption, t('Delete'));
  934. }
  935. /**
  936. * Process file type delete confirm submissions.
  937. */
  938. function file_entity_type_delete_confirm_submit($form, &$form_state) {
  939. file_type_delete($form_state['values']['type']);
  940. $t_args = array('%label' => $form_state['values']['label']);
  941. drupal_set_message(t('The file type %label has been deleted.', $t_args));
  942. watchdog('file_entity', 'Deleted file type %label.', $t_args, WATCHDOG_NOTICE);
  943. $form_state['redirect'] = 'admin/structure/file-types';
  944. return;
  945. }
  946. /**
  947. * Form callback for file_entity settings.
  948. */
  949. function file_entity_settings_form($form, &$form_state) {
  950. $form['file_entity_max_filesize'] = array(
  951. '#type' => 'textfield',
  952. '#title' => t('Maximum upload size'),
  953. '#default_value' => variable_get('file_entity_max_filesize', ''),
  954. '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current max limit <strong>%limit</strong>).', array('%limit' => format_size(file_upload_max_size()))),
  955. '#size' => 10,
  956. '#element_validate' => array('_file_generic_settings_max_filesize'),
  957. );
  958. $form['file_entity_default_allowed_extensions'] = array(
  959. '#type' => 'textfield',
  960. '#title' => t('Default allowed file extensions'),
  961. '#default_value' => variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'),
  962. '#description' => t('Separate extensions with a space or comma and do not include the leading dot.'),
  963. '#maxlength' => NULL,
  964. );
  965. $form['file_entity_alt'] = array(
  966. '#type' => 'textfield',
  967. '#title' => t('Alt attribute'),
  968. '#description' => t('The text to use as value for the <em>img</em> tag <em>alt</em> attribute.'),
  969. '#default_value' => variable_get('file_entity_alt', '[file:field_file_image_alt_text]'),
  970. );
  971. $form['file_entity_title'] = array(
  972. '#type' => 'textfield',
  973. '#title' => t('Title attribute'),
  974. '#description' => t('The text to use as value for the <em>img</em> tag <em>title</em> attribute.'),
  975. '#default_value' => variable_get('file_entity_title', '[file:field_file_image_title_text]'),
  976. );
  977. // Provide default token values.
  978. if (module_exists('token')) {
  979. $form['token_help'] = array(
  980. '#theme' => 'token_tree',
  981. '#token_types' => array('file'),
  982. '#dialog' => TRUE,
  983. );
  984. $form['file_entity_alt']['#description'] .= t('This field supports tokens.');
  985. $form['file_entity_title']['#description'] .= t('This field supports tokens.');
  986. }
  987. $form['file_upload_wizard'] = array(
  988. '#type' => 'fieldset',
  989. '#title' => t('File upload wizard'),
  990. '#collapsible' => TRUE,
  991. '#collapsed' => FALSE,
  992. '#description' => t('Configure the steps available when uploading a new file.'),
  993. );
  994. $form['file_upload_wizard']['file_entity_file_upload_wizard_skip_file_type'] = array(
  995. '#type' => 'checkbox',
  996. '#title' => t('Skip filetype selection.'),
  997. '#default_value' => variable_get('file_entity_file_upload_wizard_skip_file_type', FALSE),
  998. '#description' => t('The file type selection step is only available if the uploaded file falls into two or more file types. If this step is skipped, files with no available file type or two or more file types will not be assigned a file type.'),
  999. );
  1000. $form['file_upload_wizard']['file_entity_file_upload_wizard_skip_scheme'] = array(
  1001. '#type' => 'checkbox',
  1002. '#title' => t('Skip scheme selection.'),
  1003. '#default_value' => variable_get('file_entity_file_upload_wizard_skip_scheme', FALSE),
  1004. '#description' => t('The scheme selection step is only available if two or more file destinations, such as public local files served by the webserver and private local files served by Drupal, are available. If this step is skipped, files will automatically be saved using the default download method.'),
  1005. );
  1006. $form['file_upload_wizard']['file_entity_file_upload_wizard_skip_fields'] = array(
  1007. '#type' => 'checkbox',
  1008. '#title' => t('Skip available fields.'),
  1009. '#default_value' => variable_get('file_entity_file_upload_wizard_skip_fields', FALSE),
  1010. '#description' => t('The field selection step is only available if the file type the file belongs to has any available fields. If this step is skipped, any fields on the file will be left blank.'),
  1011. );
  1012. return system_settings_form($form);
  1013. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.