tripal_core.tripal.api.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. If the environment variable
  38. * 'TRIPAL_DEBUG' is set to 1 then this function will add backtrace
  39. * information to the message.
  40. *
  41. * @param $type
  42. * The catagory to which this message belongs. Can be any string, but the general
  43. * practice is to use the name of the module.
  44. * @param $severity
  45. * The severity of the message; one of the following values:
  46. * - TRIPAL_CRITICAL: Critical conditions.
  47. * - TRIPAL_ERROR: Error conditions.
  48. * - TRIPAL_WARNING: Warning conditions.
  49. * - TRIPAL_NOTICE: (default) Normal but significant conditions.
  50. * - TRIPAL_INFO: Informational messages.
  51. * - TRIPAL_DEBUG: Debug-level messages.
  52. * @param $message
  53. * The message to store in the log. Keep $message translatable by not concatenating
  54. * dynamic values into it! Variables in the message should be added by using placeholder
  55. * strings alongside the variables argument to declare the value of the placeholders.
  56. * See t() for documentation on how $message and $variables interact.
  57. * @param $variables
  58. * Array of variables to replace in the message on display or NULL if message is
  59. * already translated or not possible to translate.
  60. * @param $options
  61. * An array of options. Some available options include:
  62. * - print: prints the error message to the terminal screen. Useful when display is the command-line
  63. *
  64. * @ingroup tripal_api
  65. */
  66. function tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) {
  67. // Get human-readable severity string
  68. $severity_string = '';
  69. switch ($severity) {
  70. case TRIPAL_CRITICAL:
  71. $severity_string = 'CRITICAL';
  72. break;
  73. case TRIPAL_ERROR:
  74. $severity_string = 'ERROR';
  75. break;
  76. case TRIPAL_WARNING:
  77. $severity_string = 'WARNING';
  78. break;
  79. case TRIPAL_NOTICE:
  80. $severity_string = 'NOTICE';
  81. break;
  82. case TRIPAL_INFO:
  83. $severity_string = 'INFO';
  84. break;
  85. case TRIPAL_DEBUG:
  86. $severity_string = 'DEBUG';
  87. break;
  88. }
  89. // get the backtrace and include in the error message, but only if the
  90. // TRIPAL_DEBUG environment variable is set.
  91. if (getenv('TRIPAL_DEBUG') == 1) {
  92. $backtrace = debug_backtrace();
  93. $message .= "\nBacktrace:\n";
  94. $i = 1;
  95. for ($i = 1; $i < count($backtrace); $i++) {
  96. $function = $backtrace[$i];
  97. $message .= " $i) " . $function['function'] . "\n";
  98. }
  99. }
  100. // Send to watchdog
  101. try {
  102. watchdog($type, $message, $variables, $severity);
  103. }
  104. catch (Exception $e) {
  105. print "CRITICAL (TRIPAL_CORE): Unable to register error message with watchdog: " . $e->getMessage(). "\n.";
  106. $options['print'] = TRUE;
  107. }
  108. // If print option supplied then print directly to the screen
  109. if (isset($options['print'])) {
  110. if (sizeof($variables) > 0) {
  111. $message = str_replace(array_keys($variables), $variables, $message);
  112. }
  113. print $severity_string . ' (' . strtoupper($type) . '): ' . $message . "\n";
  114. }
  115. }
  116. /**
  117. * Display messages to tripal administrators. This can be used instead of
  118. * drupal_set_message when you want to target tripal administrators.
  119. *
  120. * @param $message
  121. * The message to be displayed to the tripal administrators
  122. * @param $importance
  123. * The level of importance for this message. In the future this will be used to allow
  124. * administrators to filter some of these messages. It can be one of the following:
  125. * - TRIPAL_CRITICAL: Critical conditions.
  126. * - TRIPAL_ERROR: Error conditions.
  127. * - TRIPAL_WARNING: Warning conditions.
  128. * - TRIPAL_NOTICE: Normal but significant conditions.
  129. * - TRIPAL_INFO: (default) Informational messages.
  130. * - TRIPAL_DEBUG: Debug-level messages.
  131. * @param $options
  132. * Any options to apply to the current message. Supported options include:
  133. * - return_html: return HTML instead of setting a drupal message. This can be
  134. * used to place a tripal message in a particular place in the page.
  135. * The default is FALSE.
  136. */
  137. function tripal_set_message($message, $importance = TRIPAL_INFO, $options = array()) {
  138. global $user;
  139. // Only show the message to the administrator user (uid=1)
  140. // thus if the current user is not the administrator then return nothing
  141. if (!user_access('administer tripal')) {
  142. return '';
  143. }
  144. // set defaults
  145. $options['return_html'] = (isset($options['return_html'])) ? $options['return_html'] : FALSE;
  146. // Get human-readable severity string
  147. $importance_string = '';
  148. switch ($importance) {
  149. case TRIPAL_CRITICAL:
  150. $importance_string = 'CRITICAL';
  151. break;
  152. case TRIPAL_ERROR:
  153. $importance_string = 'ERROR';
  154. break;
  155. case TRIPAL_WARNING:
  156. $importance_string = 'WARNING';
  157. break;
  158. case TRIPAL_NOTICE:
  159. $importance_string = 'NOTICE';
  160. break;
  161. case TRIPAL_INFO:
  162. $importance_string = 'INFO';
  163. break;
  164. case TRIPAL_DEBUG:
  165. $importance_string = 'DEBUG';
  166. break;
  167. }
  168. // Mark-up the Message
  169. $full_message =
  170. '<div class="tripal-site-admin-message">'
  171. . '<span class="tripal-serverity-string ' . strtolower($importance_string) . '">' . $importance_string . ': </span>'
  172. . $message
  173. . '</div>';
  174. // Handle whether to return the HTML & let the caller deal with it
  175. // or to use drupal_set_message to put it near the top of the page & let the theme deal with it
  176. if ($options['return_html']) {
  177. return '<div class="messages tripal-site-admin-only">' . $full_message . '</div>';
  178. }
  179. else {
  180. drupal_set_message($full_message, 'tripal-site-admin-only');
  181. }
  182. }