tripal_core.tripal.api.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for Tripal
  5. *
  6. * The Tripal API currently provides generic insert/update/select functions for all chado content as
  7. * well as some module specific functions that insert/update/delete/select specific chado content.
  8. *
  9. * This API is currently in its infancy and some necessary functions might be missing. If you find
  10. * a missing function that you think should be included go to the sourceforge feature request
  11. * page and request it's inclusion in the API. Such feature requests with a working function
  12. * definition will be given priority.
  13. */
  14. /**
  15. * @defgroup tripal_api Tripal API
  16. * @{
  17. * Provides an application programming interface (API) for Tripal
  18. *
  19. * The Tripal API currently provides generic insert/update/select functions for all chado content as
  20. * well as some module specific functions that insert/update/delete/select specific chado content.
  21. *
  22. * This API is currently in its infancy and some necessary functions might be missing. If you find
  23. * a missing function that you think should be included go to the sourceforge feature request
  24. * page and request it's inclusion in the API. Such feature requests with a working function
  25. * definition will be given priority.
  26. * @}
  27. */
  28. // Globals used by Tripals Error catching functions
  29. // Should match those defined by watchdog
  30. define('TRIPAL_CRITICAL',2);
  31. define('TRIPAL_ERROR',3);
  32. define('TRIPAL_WARNING',4);
  33. define('TRIPAL_NOTICE',5);
  34. define('TRIPAL_INFO',6);
  35. define('TRIPAL_DEBUG',7);
  36. /**
  37. * Provide better error notice for Tripal
  38. * @param $type
  39. * The catagory to which this message belongs. Can be any string, but the general
  40. * practice is to use the name of the module.
  41. * @param $message
  42. * The message to store in the log. Keep $message translatable by not concatenating
  43. * dynamic values into it! Variables in the message should be added by using placeholder
  44. * strings alongside the variables argument to declare the value of the placeholders.
  45. * See t() for documentation on how $message and $variables interact.
  46. * @param $variables
  47. * Array of variables to replace in the message on display or NULL if message is
  48. * already translated or not possible to translate.
  49. * @param $severity
  50. * The severity of the message; one of the following values:
  51. * - TRIPAL_CRITICAL: Critical conditions.
  52. * - TRIPAL_ERROR: Error conditions.
  53. * - TRIPAL_WARNING: Warning conditions.
  54. * - TRIPAL_NOTICE: (default) Normal but significant conditions.
  55. * - TRIPAL_INFO: Informational messages.
  56. * - TRIPAL_DEBUG: Debug-level messages.
  57. * @param $options
  58. * An array of options. Some available options include:
  59. * - print: prints the error message to the terminal screen. Useful when display is the command-line
  60. *
  61. * @ingroup tripal_api
  62. */
  63. function tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) {
  64. // Get human-readable severity string
  65. $severity_string = '';
  66. switch ($severity) {
  67. case TRIPAL_CRITICAL:
  68. $severity_string = 'CRITICAL';
  69. break;
  70. case TRIPAL_ERROR:
  71. $severity_string = 'ERROR';
  72. break;
  73. case TRIPAL_WARNING:
  74. $severity_string = 'WARNING';
  75. break;
  76. case TRIPAL_NOTICE:
  77. $severity_string = 'NOTICE';
  78. break;
  79. case TRIPAL_INFO:
  80. $severity_string = 'INFO';
  81. break;
  82. case TRIPAL_DEBUG:
  83. $severity_string = 'DEBUG';
  84. break;
  85. }
  86. // Send to watchdog
  87. try {
  88. watchdog($type, $message, $variables, $severity);
  89. }
  90. catch (Exception $e) {
  91. print "CRITICAL (TRIPAL_CORE): Unable to register error message with watchdog";
  92. $options['print'] = TRUE;
  93. }
  94. // If print option supplied then print directly to the screen
  95. if (isset($options['print'])) {
  96. if (sizeof($variables) > 0) {
  97. $message = str_replace(array_keys($variables), $variables, $message);
  98. }
  99. print $severity_string . ' (' . strtoupper($type) . '):' . $message . "\n";
  100. }
  101. }
  102. /**
  103. * Use this function to encapsulate text intended to be
  104. * visible only by the site administrator. A small tripal logo
  105. * appears alongside the text. Do not call this function directly, but
  106. * rather, use the theme() function:
  107. *
  108. * theme('tripal_admin_message', array('message' => $my_message));
  109. *
  110. * @param $message
  111. * The message to be displayed to the site administrator
  112. *
  113. * @ingroup tripal_api
  114. */
  115. function theme_tripal_admin_message($variables) {
  116. $message = $variables['message'];
  117. if (!user_access('access administration pages')) {
  118. return '';
  119. }
  120. return "
  121. <div class=\"tripal-site-admin-only\">
  122. <div class=\"tripal-site-admin-message\">$message</div>
  123. </div>";
  124. }