tripal_core.files.api.inc

Provides an application programming interface (API) for managing files within the Tripal data directory structure.

File

tripal_core/api/tripal_core.files.api.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for managing files within
  5. * the Tripal data directory structure.
  6. */
  7. /**
  8. * @defgroup tripal_files_api Tripal Files API
  9. * @ingroup tripal_core_api
  10. * @{
  11. * Provides an application programming interface (API) for managing files within
  12. * the Tripal data directory structure.
  13. * @}
  14. *
  15. */
  16. /**
  17. * Creates a directory for a module in the Drupal's public files directory.
  18. *
  19. * Previously it was recommended that this function be called during
  20. * installation of the module in the .install file. However this causes
  21. * permission problems if the module is installed via drush with a
  22. * user account that is not the same as the web user. Therefore, this
  23. * function should not be called in a location accessiblve via a drush
  24. * command. The tripal_get_files_dir() and tripal_get_files_stream()
  25. * will automatically create the directory if it doesn't exist so there is
  26. * little need to call this function directly.
  27. *
  28. * @param $module_name
  29. * the name of the module being installed
  30. * @param $path
  31. * Optional sub-path to create
  32. *
  33. * @ingroup tripal_files_api
  34. */
  35. function tripal_create_files_dir($module_name, $path = FALSE) {
  36. // if the path is not supplied then assume they want to create the base files directory
  37. // for the specified module
  38. if (!$path) {
  39. // make the data directory for this module
  40. $data_dir = tripal_get_files_dir() . "/$module_name";
  41. if (!file_prepare_directory($data_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  42. $message = "Cannot create directory $data_dir. This module may not " .
  43. "behave correctly without this directory. Please create " .
  44. "the directory manually or fix the problem and reinstall.";
  45. drupal_set_message(check_plain(t($message)), 'error');
  46. tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
  47. }
  48. }
  49. else {
  50. // make sure the module data directory exists, we make a recursive call
  51. // but without the path
  52. tripal_create_files_dir($module_name);
  53. // now make sure the sub dir exists
  54. $sub_dir = tripal_get_files_dir() . '/' . $module_name . '/' . $path;
  55. if (!file_prepare_directory($sub_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  56. $message = "Can not create directory $sub_dir. ";
  57. drupal_set_message(check_plain(t($message)), 'error');
  58. tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
  59. }
  60. }
  61. }
  62. /**
  63. * Retreives the Drupal relative directory for a Tripal module.
  64. *
  65. * Each Tripal module has a unique data directory which was created using the
  66. * tripal_create_files_dir function during installation. This function
  67. * retrieves the directory path.
  68. *
  69. * @param $module_name
  70. * (Optional) The name of the module.
  71. *
  72. * @returns
  73. * The path within the Drupal installation where the data directory resides
  74. *
  75. * @ingroup tripal_files_api
  76. */
  77. function tripal_get_files_dir($module_name = FALSE) {
  78. // Build the directory path.
  79. $data_dir = variable_get('file_public_path', conf_path() . '/files/tripal');
  80. // If a module name is provided then append the module directory.
  81. if ($module_name) {
  82. $data_dir .= "/$module_name";
  83. // Make sure the directory exists.
  84. tripal_create_files_dir($module_name);
  85. }
  86. return $data_dir;
  87. }
  88. /**
  89. * Retreives the Drupal stream (e.g. public://...) for a Tripal module.
  90. *
  91. * Each Tripal module has a unique data directory which was created using the
  92. * tripal_create_files_dir function during installation. This function
  93. * retrieves the directory path.
  94. *
  95. * @param $module_name
  96. * (Optional) The name of the module.
  97. *
  98. * @returns
  99. * The path within the Drupal installation where the data directory resides
  100. *
  101. * @ingroup tripal_files_api
  102. */
  103. function tripal_get_files_stream($module_name = FALSE) {
  104. // Build the directory path.
  105. $stream = 'public://tripal';
  106. // If a module name is provided then append the module directory.
  107. if ($module_name) {
  108. $stream .= "/$module_name";
  109. // Make sure the directory exists.
  110. tripal_create_files_dir($module_name);
  111. }
  112. return $stream;
  113. }