file_entity.pages.inc

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

Supports file operations including View, Edit, and Delete.

Functions

Namesort descending Description
file_entity_add_upload Form callback for adding a file via an upload form.
file_entity_add_upload_multiple Form for uploading multiple files.
file_entity_add_upload_multiple_submit Submit handler for the multiple upload form.
file_entity_add_upload_step_fields Generate form fields for the fourth step in the add file wizard.
file_entity_add_upload_step_filetype Generate form fields for the second step in the add file wizard.
file_entity_add_upload_step_scheme Generate form fields for the third step in the add file wizard.
file_entity_add_upload_step_upload Generate form fields for the first step in the add file wizard.
file_entity_add_upload_submit Submit handler for the add file form.
file_entity_delete_form Page callback: Form constructor for the file deletion confirmation form.
file_entity_delete_form_submit Form submission handler for file_entity_delete_form().
file_entity_download_page Menu callback; download a single file entity.
file_entity_edit Page callback: Form constructor for the file edit form.
file_entity_edit_delete_submit Form submission handler for the 'Delete' button for file_entity_edit().
file_entity_edit_submit Form submission handler for the 'Save' button for file_entity_edit().
file_entity_edit_validate Form validation handler for file_entity_edit().
file_entity_get_filetype_candidates Get the candidate filetypes for a given file.
file_entity_get_upload_validators Retrieves the upload validators for a file.
file_entity_multiple_delete_form Form constructor for file deletion confirmation form.
file_entity_multiple_delete_form_submit Form submission handler for file_entity_multiple_delete_form().
file_entity_page_delete Deprecated Page callback for the file deletion confirmation form.
file_entity_page_edit Deprecated Page callback for the file edit form.
file_entity_upload_archive_form
file_entity_upload_archive_form_submit Upload a file.
file_entity_upload_destination_uri Determines the upload location for the file add upload form.
file_entity_usage_page Page callback to show file usage information.
file_entity_view_page Menu callback; view a single file entity.

File

sites/all/modules/ulmus/file_entity/file_entity.pages.inc
View source
  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. drupal_set_title($file->filename);
  11. $uri = entity_uri('file', $file);
  12. // Set the file path as the canonical URL to prevent duplicate content.
  13. drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
  14. // Set the non-aliased path as a default shortlink.
  15. drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);
  16. return file_view($file, 'full');
  17. }
  18. /**
  19. * Menu callback; download a single file entity.
  20. */
  21. function file_entity_download_page($file) {
  22. // Ensure there is a valid token to download this file.
  23. if (!variable_get('file_entity_allow_insecure_download', FALSE)) {
  24. if (!isset($_GET['token']) || $_GET['token'] !== file_entity_get_download_token($file)) {
  25. return MENU_ACCESS_DENIED;
  26. }
  27. }
  28. // If the file does not exist it can cause problems with file_transfer().
  29. if (!is_file($file->uri)) {
  30. return MENU_NOT_FOUND;
  31. }
  32. $headers = array(
  33. 'Content-Type' => mime_header_encode($file->filemime),
  34. 'Content-Disposition' => 'attachment; filename="' . $file->filename . '"',
  35. 'Content-Length' => $file->filesize,
  36. 'Content-Transfer-Encoding' => 'binary',
  37. 'Pragma' => 'no-cache',
  38. 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
  39. 'Expires' => '0',
  40. );
  41. // Let other modules alter the download headers.
  42. drupal_alter('file_download_headers', $headers, $file);
  43. // Let other modules know the file is being downloaded.
  44. module_invoke_all('file_transfer', $file->uri, $headers);
  45. file_transfer($file->uri, $headers);
  46. }
  47. /**
  48. * Form callback for adding a file via an upload form.
  49. *
  50. * This is a multi step form which has 1-3 pages:
  51. * - Upload file
  52. * - Choose filetype
  53. * If there is only one candidate (based on mimetype) we will skip this step.
  54. * - Edit fields
  55. * Skip this step if there are no fields on this entity type.
  56. */
  57. function file_entity_add_upload($form, &$form_state, array $options = array()) {
  58. $step = (isset($form_state['step']) && in_array($form_state['step'], array(1, 2, 3, 4))) ? $form_state['step'] : 1;
  59. $form['#step'] = $step;
  60. switch ($step) {
  61. case 1:
  62. return file_entity_add_upload_step_upload($form, $form_state, $options);
  63. case 2:
  64. return file_entity_add_upload_step_filetype($form, $form_state, $options);
  65. case 3:
  66. return file_entity_add_upload_step_scheme($form, $form_state, $options);
  67. case 4:
  68. return file_entity_add_upload_step_fields($form, $form_state, $options);
  69. }
  70. }
  71. /**
  72. * Generate form fields for the first step in the add file wizard.
  73. */
  74. function file_entity_add_upload_step_upload($form, &$form_state, array $options = array()) {
  75. $form['upload'] = array(
  76. '#type' => 'managed_file',
  77. '#title' => t('Upload a new file'),
  78. '#upload_location' => file_entity_upload_destination_uri($options),
  79. '#upload_validators' => file_entity_get_upload_validators($options),
  80. '#progress_indicator' => 'bar',
  81. '#required' => TRUE,
  82. '#pre_render' => array('file_managed_file_pre_render', 'file_entity_upload_validators_pre_render'),
  83. '#default_value' => isset($form_state['storage']['upload']) ? $form_state['storage']['upload'] : NULL,
  84. );
  85. $form['actions'] = array('#type' => 'actions');
  86. $form['actions']['next'] = array(
  87. '#type' => 'submit',
  88. '#value' => t('Next'),
  89. );
  90. form_load_include($form_state, 'inc', 'file_entity', 'file_entity.pages');
  91. return $form;
  92. }
  93. /**
  94. * Generate form fields for the second step in the add file wizard.
  95. */
  96. function file_entity_add_upload_step_filetype($form, &$form_state, array $options = array()) {
  97. $file = file_load($form_state['storage']['upload']);
  98. $form['type'] = array(
  99. '#type' => 'radios',
  100. '#title' => t('File type'),
  101. '#options' => file_entity_get_filetype_candidates($file),
  102. '#default_value' => isset($form_state['storage']['type']) ? $form_state['storage']['type'] : NULL,
  103. '#required' => TRUE,
  104. );
  105. $form['actions'] = array('#type' => 'actions');
  106. $form['actions']['previous'] = array(
  107. '#type' => 'submit',
  108. '#value' => t('Previous'),
  109. '#limit_validation_errors' => array(),
  110. '#submit' => array('file_entity_add_upload_submit'),
  111. );
  112. $form['actions']['next'] = array(
  113. '#type' => 'submit',
  114. '#value' => t('Next'),
  115. );
  116. return $form;
  117. }
  118. /**
  119. * Generate form fields for the third step in the add file wizard.
  120. */
  121. function file_entity_add_upload_step_scheme($form, &$form_state, array $options = array()) {
  122. $file = file_load($form_state['storage']['upload']);
  123. $options = array();
  124. foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $info) {
  125. $options[$scheme] = check_plain($info['description']);
  126. }
  127. $form['scheme'] = array(
  128. '#type' => 'radios',
  129. '#title' => t('Destination'),
  130. '#options' => $options,
  131. '#default_value' => isset($form_state['storage']['scheme']) ? $form_state['storage']['scheme'] : file_default_scheme(),
  132. '#required' => TRUE,
  133. );
  134. $form['actions'] = array('#type' => 'actions');
  135. $form['actions']['previous'] = array(
  136. '#type' => 'submit',
  137. '#value' => t('Previous'),
  138. '#limit_validation_errors' => array(),
  139. '#submit' => array('file_entity_add_upload_submit'),
  140. );
  141. $form['actions']['next'] = array(
  142. '#type' => 'submit',
  143. '#value' => t('Next'),
  144. );
  145. return $form;
  146. }
  147. /**
  148. * Generate form fields for the fourth step in the add file wizard.
  149. */
  150. function file_entity_add_upload_step_fields($form, &$form_state, array $options = array()) {
  151. // Load the file and overwrite the filetype set on the previous screen.
  152. $file = file_load($form_state['storage']['upload']);
  153. $file->type = $form_state['storage']['type'];
  154. // Let users modify the filename here.
  155. $form['filename'] = array(
  156. '#type' => 'textfield',
  157. '#title' => t('Name'),
  158. '#default_value' => $file->filename,
  159. '#required' => TRUE,
  160. '#maxlength' => 255,
  161. '#weight' => -10,
  162. );
  163. // Add fields.
  164. field_attach_form('file', $file, $form, $form_state);
  165. $form['actions'] = array('#type' => 'actions');
  166. $form['actions']['previous'] = array(
  167. '#type' => 'submit',
  168. '#value' => t('Previous'),
  169. '#limit_validation_errors' => array(),
  170. '#submit' => array('file_entity_add_upload_submit'),
  171. );
  172. $form['actions']['submit'] = array(
  173. '#type' => 'submit',
  174. '#value' => t('Save'),
  175. );
  176. return $form;
  177. }
  178. /**
  179. * Page callback to show file usage information.
  180. */
  181. function file_entity_usage_page($file) {
  182. $rows = array();
  183. $occured_entities = array();
  184. foreach (file_usage_list($file) as $module => $usage) {
  185. $info = system_get_info('module', $module);
  186. // There are cases, where actual entitiy doesen't exist.
  187. // We have to handle this.
  188. foreach ($usage as $entity_type => $entity_ids) {
  189. $entity_info = entity_get_info($entity_type);
  190. $entities = empty($entity_info) ? NULL : entity_load($entity_type, array_keys($entity_ids));
  191. foreach ($entity_ids as $entity_id => $count) {
  192. // If some other module already added this entity just sum all counts.
  193. if (isset($occured_entities[$entity_type][$entity_id])) {
  194. $rows[$occured_entities[$entity_type][$entity_id]][2] += $count;
  195. continue;
  196. }
  197. $label = empty($entities[$entity_id]) ? $module : entity_label($entity_type, $entities[$entity_id]);
  198. $entity_uri = empty($entities[$entity_id]) ? NULL : entity_uri($entity_type, $entities[$entity_id]);
  199. // Some entities do not have URL.
  200. if (empty($entity_uri)) {
  201. $rows[] = array(check_plain($label), $entity_type, $module, $count);
  202. }
  203. else {
  204. $uri = $entity_uri['path'];
  205. $rows[] = array(l($label, $uri), $entity_type, $module, $count);
  206. }
  207. $occured_entities[$entity_type][$entity_id] = count($rows) - 1;
  208. }
  209. }
  210. }
  211. $header[] = array(
  212. 'data' => t('Entity'),
  213. );
  214. $header[] = array(
  215. 'data' => t('Entity type'),
  216. );
  217. $header[] = array(
  218. 'data' => t('Registering module'),
  219. );
  220. $header[] = array(
  221. 'data' => t('Use count'),
  222. );
  223. $build['usage_table'] = array(
  224. '#theme' => 'table',
  225. '#header' => $header,
  226. '#rows' => $rows,
  227. '#caption' => t('This table lists all of the places where @filename is used.',
  228. array('@filename' => $file->filename)),
  229. '#empty' => t('This file is not currently used.'),
  230. );
  231. return $build;
  232. }
  233. /**
  234. * Get the candidate filetypes for a given file.
  235. *
  236. * Only filetypes for which the user has access to create entities are returned.
  237. *
  238. * @param array $file
  239. * An upload file array from form_state.
  240. *
  241. * @return array
  242. * An array of file type bundles that support the file's mime type.
  243. */
  244. function file_entity_get_filetype_candidates($file) {
  245. $types = module_invoke_all('file_type', $file);
  246. drupal_alter('file_type', $types, $file);
  247. $candidates = array();
  248. foreach ($types as $type) {
  249. $file->type = $type;
  250. if (file_entity_access('create', $file)) {
  251. $candidates[$type] = file_entity_type_get_name($file);
  252. }
  253. }
  254. return $candidates;
  255. }
  256. /**
  257. * Submit handler for the add file form.
  258. */
  259. function file_entity_add_upload_submit($form, &$form_state) {
  260. $form_state['storage'] = isset($form_state['storage']) ? $form_state['storage'] : array();
  261. $form_state['storage'] = array_merge($form_state['storage'], $form_state['values']);
  262. // This var is set to TRUE when we are ready to save the file.
  263. $save = FALSE;
  264. $trigger = $form_state['triggering_element']['#id'];
  265. $steps_to_check = array(2, 3);
  266. if ($trigger == 'edit-previous') {
  267. // If the previous button was hit,
  268. // the step checking order should be reversed 3, 2.
  269. $steps_to_check = array_reverse($steps_to_check);
  270. }
  271. foreach ($steps_to_check as $step) {
  272. // Check if we can skip step 2 and 3.
  273. if (($form['#step'] == $step - 1 && $trigger == 'edit-next') || ($form['#step'] == $step + 1 && $trigger == 'edit-previous')) {
  274. $file = file_load($form_state['storage']['upload']);
  275. if ($step == 2) {
  276. // Check if we can skip step 2.
  277. $candidates = file_entity_get_filetype_candidates($file);
  278. if (count($candidates) == 1) {
  279. $candidates_keys = array_keys($candidates);
  280. // There is only one possible filetype for this file.
  281. // Skip the second page.
  282. $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
  283. $form_state['storage']['type'] = reset($candidates_keys);
  284. }
  285. elseif (variable_get('file_entity_file_upload_wizard_skip_file_type', FALSE)) {
  286. // Do not assign the file a file type.
  287. $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
  288. $form_state['storage']['type'] = FILE_TYPE_NONE;
  289. }
  290. }
  291. else {
  292. // Check if we can skip step 3.
  293. $schemes = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE);
  294. if (count($schemes) == 1) {
  295. // There is only one possible stream wrapper for this file.
  296. // Skip the third page.
  297. $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
  298. $form_state['storage']['scheme'] = key($schemes);
  299. }
  300. elseif (variable_get('file_entity_file_upload_wizard_skip_scheme', FALSE)) {
  301. // Assign the file the default scheme.
  302. $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
  303. $form_state['storage']['scheme'] = file_default_scheme();
  304. }
  305. }
  306. }
  307. }
  308. // We have the filetype, check if we can skip step 4.
  309. if (($form['#step'] == 3 && $trigger == 'edit-next')) {
  310. $file = file_load($form_state['storage']['upload']);
  311. if (!field_info_instances('file', $form_state['storage']['type'])) {
  312. // This filetype doesn't have fields, save the file.
  313. $save = TRUE;
  314. }
  315. elseif (variable_get('file_entity_file_upload_wizard_skip_fields', FALSE)) {
  316. // Save the file with blanks fields.
  317. $save = TRUE;
  318. }
  319. }
  320. // Form id's can vary depending on how many other forms are displayed, so we
  321. // need to do string comparissons. e.g edit-submit--2.
  322. if (strpos($trigger, 'edit-next') !== FALSE) {
  323. $form_state['step'] = $form['#step'] + 1;
  324. }
  325. elseif (strpos($trigger, 'edit-previous') !== FALSE) {
  326. $form_state['step'] = $form['#step'] - 1;
  327. }
  328. elseif (strpos($trigger, 'edit-submit') !== FALSE) {
  329. $save = TRUE;
  330. }
  331. if ($save) {
  332. $file = file_load($form_state['storage']['upload']);
  333. if ($file) {
  334. if (file_uri_scheme($file->uri) != $form_state['storage']['scheme']) {
  335. if ($moved_file = file_move($file, $form_state['storage']['scheme'] . '://' . file_uri_target($file->uri), FILE_EXISTS_RENAME)) {
  336. // Only re-assign the file object if file_move() did not fail.
  337. $file = $moved_file;
  338. }
  339. }
  340. $file->type = $form_state['storage']['type'];
  341. $file->display = TRUE;
  342. // Change the file from temporary to permanent.
  343. $file->status = FILE_STATUS_PERMANENT;
  344. // Save the form fields.
  345. // Keep in mind that the values for the Field API fields must be in
  346. // $form_state['values'] and not in ['storage']. This is true as long as
  347. // the fields are on the last page of the multi step form.
  348. entity_form_submit_build_entity('file', $file, $form, $form_state);
  349. file_save($file);
  350. $form_state['file'] = $file;
  351. drupal_set_message(t('@type %name was uploaded.', array('@type' => file_entity_type_get_name($file), '%name' => $file->filename)));
  352. }
  353. else {
  354. drupal_set_message(t('An error occurred and no file was uploaded.'), 'error');
  355. return;
  356. }
  357. // Figure out destination.
  358. if (isset($_GET['destination'])) {
  359. $destination = drupal_get_destination();
  360. unset($_GET['destination']);
  361. }
  362. elseif (user_access('administer files')) {
  363. $destination = array('destination' => 'admin/content/file');
  364. }
  365. else {
  366. $destination = array('destination' => 'file/' . $file->fid);
  367. }
  368. $form_state['redirect'] = $destination['destination'];
  369. }
  370. else {
  371. $form_state['rebuild'] = TRUE;
  372. }
  373. // Clear the page and block caches.
  374. cache_clear_all();
  375. }
  376. /**
  377. * Determines the upload location for the file add upload form.
  378. *
  379. * @param array $params
  380. * An array of parameters from the media browser.
  381. * @param array $data
  382. * (optional) An array of token objects to pass to token_replace().
  383. *
  384. * @return string
  385. * A file directory URI with tokens replaced.
  386. *
  387. * @see token_replace()
  388. */
  389. function file_entity_upload_destination_uri(array $params, array $data = array()) {
  390. $params += array(
  391. 'uri_scheme' => file_default_scheme(),
  392. 'file_directory' => '',
  393. );
  394. $destination = trim($params['file_directory'], '/');
  395. // Replace tokens.
  396. $destination = token_replace($destination, $data);
  397. return $params['uri_scheme'] . '://' . $destination;
  398. }
  399. /**
  400. * Form for uploading multiple files.
  401. */
  402. function file_entity_add_upload_multiple($form, &$form_state, $params = array()) {
  403. $form = file_entity_add_upload($form, $form_state, $params);
  404. unset($form['upload']['#title']);
  405. // The validators will be set from plupload anyway. This isn't pretty,
  406. // but don't allow it to show up twice.
  407. unset($form['upload']['#description']);
  408. $form['upload']['#type'] = 'plupload';
  409. // Ensure that we call the plupload_element_pre_render function.
  410. // If it isn't called, it doesn't set the JS settings that transfers the
  411. // list of allowed file extentions to the PLUpload widget.
  412. // We override the 'file_entity_upload_validators_pre_render' setting if it
  413. // exists, because both pre-render hooks adds the upload-help with list of
  414. // allowed file extensions.
  415. $index = array_search('file_entity_upload_validators_pre_render', $form['upload']['#pre_render']);
  416. if ($index !== FALSE) {
  417. $form['upload']['#pre_render'][$index] = 'plupload_element_pre_render';
  418. }
  419. else {
  420. $form['upload']['#pre_render'][] = 'plupload_element_pre_render';
  421. }
  422. $form['submit']['#value'] = t('Start upload');
  423. return $form;
  424. }
  425. /**
  426. * Submit handler for the multiple upload form.
  427. */
  428. function file_entity_add_upload_multiple_submit($form, &$form_state) {
  429. $upload_location = !empty($form['upload']['#upload_location']) ?
  430. $form['upload']['#upload_location'] . '/' :
  431. variable_get('file_default_scheme', 'public') . '://';
  432. // Ensure writable destination directory for the files.
  433. file_prepare_directory($upload_location, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  434. // We can't use file_save_upload() because of
  435. // http://www.jacobsingh.name/content/tight-coupling-no-not.
  436. foreach ($form_state['values']['upload'] as $uploaded_file) {
  437. if ($uploaded_file['status'] == 'done') {
  438. $source = $uploaded_file['tmppath'];
  439. $destination = file_stream_wrapper_uri_normalize($upload_location . $uploaded_file['name']);
  440. // Rename it to its original name, and put it in its final home.
  441. // Note - not using file_move here because if we call file_get_mime
  442. // (in file_uri_to_object) while it has a .tmp extension, it horks.
  443. $destination = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME);
  444. $file = file_uri_to_object($destination);
  445. $file->status = FILE_STATUS_PERMANENT;
  446. file_save($file);
  447. $saved_files[] = $file;
  448. $form_state['files'][$file->fid] = $file;
  449. }
  450. else {
  451. // @todo: move this to element validate or something.
  452. form_set_error('pud', t('The specified file %name could not be uploaded.', array('%name' => $uploaded_file['name'])));
  453. }
  454. }
  455. // Redirect to the file edit page.
  456. if (file_entity_access('update', $file) && module_exists('multiform') && module_exists('media')) {
  457. $destination = array();
  458. if (isset($_GET['destination'])) {
  459. $destination = drupal_get_destination();
  460. unset($_GET['destination']);
  461. }
  462. elseif (user_access('administer files')) {
  463. $destination = array('destination' => 'admin/content/file');
  464. }
  465. $form_state['redirect'] = array('admin/content/file/edit-multiple/' . implode(' ', array_keys($form_state['files'])), array('query' => $destination));
  466. }
  467. else {
  468. $form_state['redirect'] = user_access('administer files') ? 'admin/content/file' : '<front>';
  469. }
  470. // Clear the page and block caches.
  471. cache_clear_all();
  472. }
  473. /**
  474. * Page callback: Form constructor for the file edit form.
  475. *
  476. * Path: file/%file/edit
  477. *
  478. * @param object $file
  479. * A file object from file_load().
  480. *
  481. * @see file_entity_menu()
  482. *
  483. * @todo Rename this form to file_edit_form to ease into core.
  484. */
  485. function file_entity_edit($form, &$form_state, $file) {
  486. drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => $file->type, '@title' => $file->filename)), PASS_THROUGH);
  487. $form_state['file'] = $file;
  488. $form['#attributes']['class'][] = 'file-form';
  489. if (!empty($file->type)) {
  490. $form['#attributes']['class'][] = 'file-' . $file->type . '-form';
  491. }
  492. // Basic file information.
  493. // These elements are just values so they are not even sent to the client.
  494. foreach (array('fid', 'type', 'uid', 'timestamp') as $key) {
  495. $form[$key] = array(
  496. '#type' => 'value',
  497. '#value' => isset($file->$key) ? $file->$key : NULL,
  498. );
  499. }
  500. $form['filename'] = array(
  501. '#type' => 'textfield',
  502. '#title' => t('Name'),
  503. '#default_value' => $file->filename,
  504. '#required' => TRUE,
  505. '#maxlength' => 255,
  506. '#weight' => -10,
  507. );
  508. // Add a 'replace this file' upload field if the file is writeable.
  509. if (file_entity_file_is_writeable($file)) {
  510. // Set up replacement file validation.
  511. $replacement_options = array();
  512. // The replacement file must have the same extension as the original file.
  513. $replacement_options['file_extensions'] = pathinfo($file->uri, PATHINFO_EXTENSION);
  514. $form['replace_upload'] = array(
  515. '#type' => 'file',
  516. '#title' => t('Replace file'),
  517. '#description' => t('This file will replace the existing file. This action cannot be undone.'),
  518. '#upload_validators' => file_entity_get_upload_validators($replacement_options),
  519. '#pre_render' => array('file_entity_upload_validators_pre_render'),
  520. );
  521. }
  522. $form['preview'] = file_view_file($file, 'preview');
  523. $form['additional_settings'] = array(
  524. '#type' => 'vertical_tabs',
  525. '#weight' => 99,
  526. );
  527. // File destination information for administrators.
  528. $form['destination'] = array(
  529. '#type' => 'fieldset',
  530. '#access' => user_access('administer files') && file_entity_file_is_writeable($file),
  531. '#title' => t('Destination'),
  532. '#collapsible' => TRUE,
  533. '#collapsed' => TRUE,
  534. '#group' => 'additional_settings',
  535. '#attributes' => array(
  536. 'class' => array('file-form-destination'),
  537. ),
  538. '#attached' => array(
  539. 'js' => array(
  540. drupal_get_path('module', 'file_entity') . '/file_entity.js',
  541. ),
  542. ),
  543. );
  544. $options = array();
  545. foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $info) {
  546. $options[$scheme] = check_plain($info['name']);
  547. }
  548. $form['destination']['scheme'] = array(
  549. '#type' => 'radios',
  550. '#title' => t('Destination'),
  551. '#options' => $options,
  552. '#default_value' => file_uri_scheme($file->uri),
  553. );
  554. // File user information for administrators.
  555. $form['user'] = array(
  556. '#type' => 'fieldset',
  557. '#access' => user_access('administer files'),
  558. '#title' => t('User information'),
  559. '#collapsible' => TRUE,
  560. '#collapsed' => TRUE,
  561. '#group' => 'additional_settings',
  562. '#attributes' => array(
  563. 'class' => array('file-form-user'),
  564. ),
  565. '#attached' => array(
  566. 'js' => array(
  567. drupal_get_path('module', 'file_entity') . '/file_entity.js',
  568. array(
  569. 'type' => 'setting',
  570. 'data' => array('anonymous' => variable_get('anonymous', t('Anonymous'))),
  571. ),
  572. ),
  573. ),
  574. '#weight' => 90,
  575. );
  576. $form['user']['name'] = array(
  577. '#type' => 'textfield',
  578. '#title' => t('Associated with'),
  579. '#maxlength' => 60,
  580. '#autocomplete_path' => 'user/autocomplete',
  581. '#default_value' => !empty($file->uid) ? user_load($file->uid)->name : '',
  582. '#weight' => -1,
  583. '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
  584. );
  585. // Add the buttons.
  586. $form['actions'] = array('#type' => 'actions');
  587. $form['actions']['submit'] = array(
  588. '#type' => 'submit',
  589. '#value' => t('Save'),
  590. '#weight' => 5,
  591. '#submit' => array('file_entity_edit_submit'),
  592. );
  593. $form['actions']['delete'] = array(
  594. '#type' => 'submit',
  595. '#value' => t('Delete'),
  596. '#weight' => 10,
  597. '#submit' => array('file_entity_edit_delete_submit'),
  598. '#access' => file_entity_access('delete', $file),
  599. );
  600. // Build the URL for the cancel button taking into account that there might be
  601. // a "destination" that includes query string variables.
  602. $parameters = drupal_get_query_parameters();
  603. $destination = isset($parameters['destination']) ? $parameters['destination'] : 'file/' . $file->fid;
  604. $url = drupal_parse_url($destination);
  605. $form['actions']['cancel'] = array(
  606. '#type' => 'link',
  607. '#title' => t('Cancel'),
  608. '#href' => $url['path'],
  609. '#options' => array('query' => $url['query']),
  610. '#weight' => 15,
  611. );
  612. $langcode = function_exists('entity_language') ? entity_language('file', $file) : NULL;
  613. field_attach_form('file', $file, $form, $form_state, $langcode);
  614. return $form;
  615. }
  616. /**
  617. * Form validation handler for file_entity_edit().
  618. */
  619. function file_entity_edit_validate($form, &$form_state) {
  620. $file = (object) $form_state['values'];
  621. // Validate the "associated user" field.
  622. if (!empty($file->name) && !($account = user_load_by_name($file->name))) {
  623. // The use of empty() is mandatory in the context of usernames
  624. // as the empty string denotes the anonymous user. In case we
  625. // are dealing with an anonymous user we set the user ID to 0.
  626. form_set_error('name', t('The username %name does not exist.', array('%name' => $file->name)));
  627. }
  628. // Handle the replacement file if uploaded.
  629. if (isset($form_state['values']['replace_upload'])) {
  630. // Save the file as a temporary file.
  631. $file = file_save_upload('replace_upload', $form['replace_upload']['#upload_validators']);
  632. if (!empty($file)) {
  633. // Put the temporary file in form_values so we can save it on submit.
  634. $form_state['values']['replace_upload'] = $file;
  635. }
  636. elseif ($file === FALSE) {
  637. // File uploaded failed.
  638. form_set_error('replace_upload', t('The replacement file could not be uploaded.'));
  639. }
  640. }
  641. // Run entity form validation.
  642. entity_form_field_validate('file', $form, $form_state);
  643. }
  644. /**
  645. * Form submission handler for the 'Save' button for file_entity_edit().
  646. */
  647. function file_entity_edit_submit($form, &$form_state) {
  648. $file = $form_state['file'];
  649. // Check if a replacement file has been uploaded.
  650. if (!empty($form_state['values']['replace_upload'])) {
  651. $replacement = $form_state['values']['replace_upload'];
  652. // Move file from temp to permanent home.
  653. file_unmanaged_copy($replacement->uri, $file->uri, FILE_EXISTS_REPLACE);
  654. }
  655. // Run entity form submit handling and save the file.
  656. entity_form_submit_build_entity('file', $file, $form, $form_state);
  657. // A user might assign the associated user by entering a user name in the file
  658. // edit form, which we then need to translate to a user ID.
  659. if (isset($file->name)) {
  660. // The use of isset() is mandatory in the context of user IDs, because
  661. // user ID 0 denotes the anonymous user.
  662. if ($user = user_load_by_name($file->name)) {
  663. $file->uid = $user->uid;
  664. }
  665. else {
  666. // Anonymous user.
  667. $file->uid = 0;
  668. }
  669. }
  670. elseif ($file->uid) {
  671. $user = user_load($file->uid);
  672. $file->name = $user->name;
  673. }
  674. if (file_uri_scheme($file->uri) != $form_state['values']['scheme']) {
  675. if ($moved_file = file_move($file, $form_state['values']['scheme'] . '://' . file_uri_target($file->uri), FILE_EXISTS_RENAME)) {
  676. // Only re-assign the file object if file_move() did not fail.
  677. $file = $moved_file;
  678. }
  679. }
  680. file_save($file);
  681. $args = array(
  682. '@type' => file_entity_type_get_name($file),
  683. '%title' => entity_label('file', $file),
  684. );
  685. watchdog('file', '@type: updated %title.', $args);
  686. drupal_set_message(t('@type %title has been updated.', $args));
  687. $form_state['redirect'] = 'file/' . $file->fid;
  688. // Clear the page and block caches.
  689. cache_clear_all();
  690. }
  691. /**
  692. * Form submission handler for the 'Delete' button for file_entity_edit().
  693. */
  694. function file_entity_edit_delete_submit($form, &$form_state) {
  695. $fid = $form_state['values']['fid'];
  696. $destination = array();
  697. if (isset($_GET['destination'])) {
  698. $destination = drupal_get_destination();
  699. unset($_GET['destination']);
  700. }
  701. $form_state['redirect'] = array('file/' . $fid . '/delete', array('query' => $destination));
  702. // Clear the page and block caches.
  703. cache_clear_all();
  704. }
  705. /**
  706. * Page callback: Form constructor for the file deletion confirmation form.
  707. *
  708. * Path: file/%file/delete
  709. *
  710. * @param object $file
  711. * A file object from file_load().
  712. *
  713. * @see file_entity_menu()
  714. */
  715. function file_entity_delete_form($form, &$form_state, $file) {
  716. $form_state['file'] = $file;
  717. $form['fid'] = array(
  718. '#type' => 'value',
  719. '#value' => $file->fid,
  720. );
  721. $description = t('This action cannot be undone.');
  722. if ($references = file_usage_list($file)) {
  723. $description .= ' ' . t('This file is currently in use and may cause problems if deleted.');
  724. }
  725. return confirm_form($form,
  726. t('Are you sure you want to delete the file %title?', array(
  727. '%title' => entity_label('file', $file),
  728. )),
  729. 'file/' . $file->fid,
  730. $description,
  731. t('Delete')
  732. );
  733. }
  734. /**
  735. * Form submission handler for file_entity_delete_form().
  736. */
  737. function file_entity_delete_form_submit($form, &$form_state) {
  738. if ($form_state['values']['confirm'] && $file = file_load($form_state['values']['fid'])) {
  739. // Use file_delete_multiple() rather than file_delete() since we want to
  740. // avoid unwanted validation and usage checking.
  741. file_delete_multiple(array($file->fid));
  742. $args = array(
  743. '@type' => file_entity_type_get_name($file),
  744. '%title' => entity_label('file', $file),
  745. );
  746. watchdog('file', '@type: deleted %title.', $args);
  747. drupal_set_message(t('@type %title has been deleted.', $args));
  748. }
  749. $form_state['redirect'] = '<front>';
  750. // Clear the page and block caches.
  751. cache_clear_all();
  752. }
  753. /**
  754. * Form constructor for file deletion confirmation form.
  755. *
  756. * @param array $files
  757. * An array of file objects.
  758. */
  759. function file_entity_multiple_delete_form($form, &$form_state, array $files) {
  760. $form['files'] = array(
  761. '#prefix' => '<ul>',
  762. '#suffix' => '</ul>',
  763. '#tree' => TRUE,
  764. );
  765. $files_have_usage = FALSE;
  766. foreach ($files as $fid => $file) {
  767. $title = entity_label('file', $file);
  768. $usage = file_usage_list($file);
  769. if (!empty($usage)) {
  770. $files_have_usage = TRUE;
  771. $title = t('@title (in use)', array('@title' => $title));
  772. }
  773. else {
  774. $title = check_plain($title);
  775. }
  776. $form['files'][$fid] = array(
  777. '#type' => 'hidden',
  778. '#value' => $fid,
  779. '#prefix' => '<li>',
  780. '#suffix' => $title . "</li>\n",
  781. );
  782. }
  783. $form['operation'] = array(
  784. '#type' => 'hidden',
  785. '#value' => 'delete',
  786. );
  787. $description = t('This action cannot be undone.');
  788. if ($files_have_usage) {
  789. $description .= ' ' . t('Some of the files are currently in use and may cause problems if deleted.');
  790. }
  791. return confirm_form(
  792. $form,
  793. format_plural(count($files), 'Are you sure you want to delete this file?', 'Are you sure you want to delete these files?'),
  794. 'admin/content/file',
  795. $description,
  796. t('Delete')
  797. );
  798. }
  799. /**
  800. * Form submission handler for file_entity_multiple_delete_form().
  801. */
  802. function file_entity_multiple_delete_form_submit($form, &$form_state) {
  803. if ($form_state['values']['confirm'] && $fids = array_keys($form_state['values']['files'])) {
  804. file_delete_multiple($fids);
  805. $count = count($fids);
  806. watchdog('file', 'Deleted @count files.', array('@count' => $count));
  807. drupal_set_message(format_plural($count, 'Deleted one file.', 'Deleted @count files.'));
  808. }
  809. $form_state['redirect'] = 'admin/content/file';
  810. // Clear the page and block caches.
  811. cache_clear_all();
  812. }
  813. /**
  814. * Page callback for the file edit form.
  815. *
  816. * @deprecated
  817. * Use drupal_get_form('file_entity_edit')
  818. */
  819. function file_entity_page_edit($file) {
  820. return drupal_get_form('file_entity_edit', $file);
  821. }
  822. /**
  823. * Page callback for the file deletion confirmation form.
  824. *
  825. * @deprecated
  826. * Use drupal_get_form('file_entity_delete_form')
  827. */
  828. function file_entity_page_delete($file) {
  829. return drupal_get_form('file_entity_delete_form');
  830. }
  831. /**
  832. * Retrieves the upload validators for a file.
  833. *
  834. * @param array $options
  835. * (optional) An array of options for file validation.
  836. *
  837. * @return array
  838. * An array suitable for passing to file_save_upload() or for a managed_file
  839. * or upload element's '#upload_validators' property.
  840. */
  841. function file_entity_get_upload_validators(array $options = array()) {
  842. // Set up file upload validators.
  843. $validators = array();
  844. // Validate file extensions. If there are no file extensions in $params and
  845. // there are no Media defaults, there is no file extension validation.
  846. if (!empty($options['file_extensions'])) {
  847. $validators['file_validate_extensions'] = array($options['file_extensions']);
  848. }
  849. else {
  850. $validators['file_validate_extensions'] = array(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'));
  851. }
  852. // Cap the upload size according to the system or user defined limit.
  853. $max_filesize = parse_size(file_upload_max_size());
  854. $file_entity_max_filesize = parse_size(variable_get('file_entity_max_filesize', ''));
  855. // If the user defined a size limit, use the smaller of the two.
  856. if (!empty($file_entity_max_filesize)) {
  857. $max_filesize = min($max_filesize, $file_entity_max_filesize);
  858. }
  859. if (!empty($options['max_filesize']) && $options['max_filesize'] < $max_filesize) {
  860. $max_filesize = parse_size($options['max_filesize']);
  861. }
  862. // There is always a file size limit due to the PHP server limit.
  863. $validators['file_validate_size'] = array($max_filesize);
  864. // Add image validators.
  865. $options += array('min_resolution' => 0, 'max_resolution' => 0);
  866. if ($options['min_resolution'] || $options['max_resolution']) {
  867. $validators['file_validate_image_resolution'] = array($options['max_resolution'], $options['min_resolution']);
  868. }
  869. // Add other custom upload validators from options.
  870. if (!empty($options['upload_validators'])) {
  871. $validators += $options['upload_validators'];
  872. }
  873. return $validators;
  874. }
  875. function file_entity_upload_archive_form($form, &$form_state) {
  876. $options = array(
  877. 'file_extensions' => archiver_get_extensions(),
  878. );
  879. $form['upload'] = array(
  880. '#type' => 'managed_file',
  881. '#title' => t('Upload an archive file'),
  882. '#upload_location' => NULL, // Upload to the temporary directory.
  883. '#upload_validators' => file_entity_get_upload_validators($options),
  884. '#progress_indicator' => 'bar',
  885. '#required' => TRUE,
  886. '#pre_render' => array('file_managed_file_pre_render', 'file_entity_upload_validators_pre_render'),
  887. );
  888. $form['pattern'] = array(
  889. '#type' => 'textfield',
  890. '#title' => t('Pattern'),
  891. '#description' => t('Only files matching this pattern will be imported. For example, to import all jpg and gif files, the pattern would be <em>*.jpg|*.gif</em>. Use <em>.*</em> to extract all files in the archive.'),
  892. '#default_value' => '.*',
  893. '#required' => TRUE,
  894. );
  895. $form['actions'] = array('#type' => 'actions');
  896. $form['actions']['submit'] = array(
  897. '#type' => 'submit',
  898. '#value' => t('Submit'),
  899. );
  900. form_load_include($form_state, 'inc', 'file_entity', 'file_entity.pages');
  901. return $form;
  902. }
  903. /**
  904. * Upload a file.
  905. */
  906. function file_entity_upload_archive_form_submit($form, &$form_state) {
  907. $form_state['files'] = array();
  908. if ($archive = file_load($form_state['values']['upload'])) {
  909. if ($archiver = archiver_get_archiver($archive->uri)) {
  910. $files = $archiver->listContents();
  911. $extract_dir = file_default_scheme() . '://' . pathinfo($archive->filename, PATHINFO_FILENAME);
  912. $extract_dir = file_destination($extract_dir, FILE_EXISTS_RENAME);
  913. if (!file_prepare_directory($extract_dir, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY)) {
  914. throw new Exception(t('Unable to prepar, e directory %dir for extraction.', array('%dir' => $extract_dir)));
  915. }
  916. $archiver->extract($extract_dir);
  917. $pattern = '/' . $form_state['values']['pattern'] . '/';
  918. if ($files = file_scan_directory($extract_dir, $pattern)) {
  919. foreach ($files as $file) {
  920. $file->status = FILE_STATUS_PERMANENT;
  921. $file->uid = $archive->uid;
  922. file_save($file);
  923. $form_state['files'][$file->fid] = $file;
  924. }
  925. }
  926. drupal_set_message(t('Extracted %file and added @count new files.', array('%file' => $archive->filename, '@count' => count($files))));
  927. }
  928. else {
  929. throw new Exception(t('Cannot extract %file, not a valid archive.', array('%file' => $archive->uri)));
  930. }
  931. }
  932. // Redirect to the file edit page.
  933. if (file_entity_access('edit') && module_exists('multiform')) {
  934. $destination = array('destination' => 'admin/content/file');
  935. if (isset($_GET['destination'])) {
  936. $destination = drupal_get_destination();
  937. unset($_GET['destination']);
  938. }
  939. $form_state['redirect'] = array('admin/content/file/edit-multiple/' . implode(' ', array_keys($form_state['files'])), array('query' => $destination));
  940. }
  941. else {
  942. $form_state['redirect'] = 'admin/content/file';
  943. }
  944. }
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.