tripal_core.files.api.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * This function is typically used in the '.install' file for a Tripal module
  18. * Each module should call this function during installation to create
  19. * the module data directory which is sites/default/files/tripal/[module_name].
  20. * This directory can then be used by the module for storing files.
  21. *
  22. * @param $module_name
  23. * the name of the module being installed
  24. * @param $path
  25. * Optional sub-path to create
  26. *
  27. * @ingroup tripal_files_api
  28. */
  29. function tripal_create_files_dir($module_name, $path = FALSE) {
  30. // if the path is not supplied then assume they want to create the base files directory
  31. // for the specified module
  32. if (!$path) {
  33. // make the data directory for this module
  34. $data_dir = tripal_get_files_dir() . "/$module_name";
  35. if (!file_prepare_directory($data_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  36. $message = "Cannot create directory $data_dir. This module may not " .
  37. "behave correctly without this directory. Please create " .
  38. "the directory manually or fix the problem and reinstall.";
  39. drupal_set_message(check_plain(t($message)), 'error');
  40. tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
  41. }
  42. }
  43. else {
  44. // make sure the module data directory exists, we make a recursive call
  45. // but without the path
  46. tripal_create_files_dir($module_name);
  47. // now make sure the sub dir exists
  48. $sub_dir = tripal_get_files_dir() . '/' . $module_name . '/' . $path;
  49. print "$sub_dir\n";
  50. if (!file_prepare_directory($sub_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  51. $message = "Can not create directory $sub_dir. ";
  52. drupal_set_message(check_plain(t($message)), 'error');
  53. tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
  54. }
  55. }
  56. }
  57. /**
  58. * Each Tripal module has a unique data directory which was created using the
  59. * tripal_create_files_dir function during installation. This function
  60. * retrieves the directory path.
  61. *
  62. * @param $module_name
  63. * (Optional) The name of the module.
  64. *
  65. * @returns
  66. * The path within the Drupal installation where the data directory resides
  67. *
  68. * @ingroup tripal_files_api
  69. */
  70. function tripal_get_files_dir($module_name = FALSE) {
  71. $data_dir = variable_get('file_public_path', conf_path() . '/files/tripal');
  72. if ($module_name) {
  73. $data_dir .= "/$module_name";
  74. }
  75. return $data_dir;
  76. }