tripal_core.files.api.inc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @defgroup tripal_files_api Files API
  4. * @ingroup tripal_core_api
  5. * @{
  6. * Provides an application programming interface (API) for managing files within
  7. * the Tripal data directory structure.
  8. * @}
  9. *
  10. */
  11. /**
  12. * This function is typically used in the '.install' file for a Tripal module
  13. * Each module should call this function during installation to create
  14. * the module data directory which is sites/default/files/tripal/[module_name]
  15. * for default Drupal settings. This directory can then be used by the module
  16. * for storing files.
  17. *
  18. * @param $module_name
  19. * the name of the module being installed
  20. * @param $path
  21. * Optional sub-path to create
  22. */
  23. function tripal_create_files_dir($module_name, $path = FALSE) {
  24. // if the path is not supplied then assume they want to create the base files directory
  25. // for the specified module
  26. if (!$path) {
  27. // make the data directory for this module
  28. $data_dir = tripal_get_files_dir() . "/$module_name";
  29. if (!file_prepare_directory($data_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  30. $message = "Cannot create directory $data_dir. This module may not " .
  31. "behave correctly without this directory. Please create " .
  32. "the directory manually or fix the problem and reinstall.";
  33. drupal_set_message(check_plain(t($message)), 'error');
  34. tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
  35. }
  36. }
  37. // make sure the module data directory exists
  38. tripal_create_files_dir($module_name);
  39. // now make sure the sub dir exists
  40. $sub_dir = tripal_get_files_dir() . $module_name . $path;
  41. if (!file_prepare_directory($sub_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  42. $message = "Cannot create directory $sub_dir. ";
  43. drupal_set_message(check_plain(t($message)), 'error');
  44. tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
  45. }
  46. }
  47. /**
  48. * Each Tripal module has a unique data directory which was creatd using the
  49. * tripal_create_files_dir function during installation. This function
  50. * retrieves the directory path.
  51. *
  52. * @param $module_name
  53. * (Optional) The name of the module.
  54. *
  55. * @returns
  56. * The path within the Drupal installation where the data directory resides
  57. *
  58. * @ingroup tripal_files_api
  59. */
  60. function tripal_get_files_dir($module_name = FALSE) {
  61. $data_dir = variable_get('file_public_path', conf_path() . '/files/tripal');
  62. if ($module_name) {
  63. $data_dir .= "/$module_name";
  64. }
  65. return $data_dir;
  66. }