tripal_core.files.api.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. print "$sub_dir\n";
  56. if (!file_prepare_directory($sub_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  57. $message = "Can not create directory $sub_dir. ";
  58. drupal_set_message(check_plain(t($message)), 'error');
  59. tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
  60. }
  61. }
  62. }
  63. /**
  64. * Retreives the Drupal relative directory for a Tripal module.
  65. *
  66. * Each Tripal module has a unique data directory which was created using the
  67. * tripal_create_files_dir function during installation. This function
  68. * retrieves the directory path.
  69. *
  70. * @param $module_name
  71. * (Optional) The name of the module.
  72. *
  73. * @returns
  74. * The path within the Drupal installation where the data directory resides
  75. *
  76. * @ingroup tripal_files_api
  77. */
  78. function tripal_get_files_dir($module_name = FALSE) {
  79. // Build the directory path.
  80. $data_dir = variable_get('file_public_path', conf_path() . '/files/tripal');
  81. // If a module name is provided then append the module directory.
  82. if ($module_name) {
  83. $data_dir .= "/$module_name";
  84. // Make sure the directory exists.
  85. tripal_create_files_dir($module_name, "/$module_name");
  86. }
  87. return $data_dir;
  88. }
  89. /**
  90. * Retreives the Drupal stream (e.g. public://...) for a Tripal module.
  91. *
  92. * Each Tripal module has a unique data directory which was created using the
  93. * tripal_create_files_dir function during installation. This function
  94. * retrieves the directory path.
  95. *
  96. * @param $module_name
  97. * (Optional) The name of the module.
  98. *
  99. * @returns
  100. * The path within the Drupal installation where the data directory resides
  101. *
  102. * @ingroup tripal_files_api
  103. */
  104. function tripal_get_files_stream($module_name = FALSE) {
  105. // Build the directory path.
  106. $stream = 'public://tripal';
  107. // If a module name is provided then append the module directory.
  108. if ($module_name) {
  109. $stream .= "/$module_name";
  110. // Make sure the directory exists.
  111. tripal_create_files_dir($module_name, "/$module_name");
  112. }
  113. return $stream;
  114. }