tripal_core_files.api.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Files API
  5. *
  6. * This file provides the API to help Tripal modules to storing files
  7. * in a consistent directory strcuture.
  8. *
  9. * @defgroup tripal_files_api Files API
  10. * @ingroup tripal_core_api
  11. * @{
  12. * Provides an application programming interface (API) for managing files within
  13. * the Tripal data directory structure.
  14. *
  15. * @}
  16. *
  17. */
  18. /**
  19. * This function is a substitute for Drupal 6's file_directory_path()
  20. * function which no longer exists in Drupal 7. However, the function
  21. * call is useful because it's more intitutive. So this wrapper was created
  22. * to mimic the behavior of the old function.
  23. *
  24. * @return
  25. * the public directory where tripal files are housed: sites/default/files/tripal
  26. */
  27. function tripal_file_directory_path() {
  28. return variable_get('file_public_path', conf_path() . '/files/tripal');
  29. }
  30. /**
  31. * This function is typically used in the '.install' file for a Tripal module
  32. * Each module should call this function during installation to create
  33. * the module data directory which is sites/default/files/tripal/[module_name]
  34. * for default Drupal settings. This directory can then be used by the module
  35. * for storing files.
  36. *
  37. * @param $module_name
  38. * the name of the module being installed.
  39. *
  40. * @returns
  41. * nothing
  42. *
  43. * @ingroup tripal_files_api
  44. */
  45. function tripal_create_moddir($module_name) {
  46. // make the data directory for this module
  47. $data_dir = tripal_file_directory_path() . "/$module_name";
  48. if (!file_prepare_directory($data_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  49. $message = "Cannot create directory $data_dir. This module may not " .
  50. "behave correctly without this directory. Please create " .
  51. "the directory manually or fix the problem and reinstall.";
  52. drupal_set_message(check_plain(t($message)), 'error');
  53. watchdog('tripal_core', $message, array(), WATCHDOG_ERROR);
  54. }
  55. }
  56. /**
  57. * This function creates directories inside of the module's
  58. * Data directory.
  59. *
  60. * @param $module_name
  61. * @param $path
  62. */
  63. function tripal_create_mod_subdir($module_name, $path) {
  64. // make sure the module data directory exists
  65. tripal_create_moddir($module_name);
  66. // now make sure the sub dir exists
  67. $sub_dir = tripal_file_directory_path() . $module_name . $path;
  68. if (!file_prepare_directory($sub_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  69. $message = "Cannot create directory $sub_dir. ";
  70. drupal_set_message(check_plain(t($message)), 'error');
  71. watchdog('tripal_core', $message, array(), WATCHDOG_ERROR);
  72. }
  73. }
  74. /**
  75. * Each Tripal module has a unique data directory which was creatd using the
  76. * tripal_create_moddir function during installation. This function
  77. * retrieves the directory path.
  78. *
  79. * @param $module_name
  80. * The name of the module
  81. *
  82. * @returns
  83. * The path within the Drupal installation where the data directory resides
  84. *
  85. * @ingroup tripal_files_api
  86. */
  87. function tripal_get_moddir($module_name) {
  88. $data_dir = tripal_file_directory_path() . "/$module_name";
  89. return $data_dir;
  90. }