tripal_core.files.api.inc 2.5 KB

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