tripal_core.files.api.inc 2.6 KB

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