tripal_core.tripal.api.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Display messages to tripal administrators. This can be used instead of
  104. * drupal_set_message when you want to target tripal administrators.
  105. *
  106. * @param $message
  107. * The message to be displayed to the tripal administrators
  108. * @param $importance
  109. * The level of importance for this message. In the future this will be used to allow
  110. * administrators to filter some of these messages. It can be one of the following:
  111. * - TRIPAL_CRITICAL: Critical conditions.
  112. * - TRIPAL_ERROR: Error conditions.
  113. * - TRIPAL_WARNING: Warning conditions.
  114. * - TRIPAL_NOTICE: Normal but significant conditions.
  115. * - TRIPAL_INFO: (default) Informational messages.
  116. * - TRIPAL_DEBUG: Debug-level messages.
  117. * @param $options
  118. * Any options to apply to the current message. Supported options include:
  119. * - return_html: return HTML instead of setting a drupal message. This can be
  120. * used to place a tripal message in a particular place in the page.
  121. * The default is FALSE.
  122. */
  123. function tripal_set_message($message, $importance = TRIPAL_INFO, $options = array()) {
  124. global $user;
  125. // Only show the message to the administrator user (uid=1)
  126. // thus if the current user is not the administrator then return nothing
  127. if ($user->uid > 1) {
  128. return '';
  129. }
  130. // set defaults
  131. $options['return_html'] = (isset($options['return_html'])) ? $options['return_html'] : FALSE;
  132. // Get human-readable severity string
  133. $importance_string = '';
  134. switch ($importance) {
  135. case TRIPAL_CRITICAL:
  136. $importance_string = 'CRITICAL';
  137. break;
  138. case TRIPAL_ERROR:
  139. $importance_string = 'ERROR';
  140. break;
  141. case TRIPAL_WARNING:
  142. $importance_string = 'WARNING';
  143. break;
  144. case TRIPAL_NOTICE:
  145. $importance_string = 'NOTICE';
  146. break;
  147. case TRIPAL_INFO:
  148. $importance_string = 'INFO';
  149. break;
  150. case TRIPAL_DEBUG:
  151. $importance_string = 'DEBUG';
  152. break;
  153. }
  154. // Mark-up the Message
  155. $full_message =
  156. '<div class="tripal-site-admin-message">'
  157. . '<span class="tripal-serverity-string ' . strtolower($importance_string) . '">' . $importance_string . ': </span>'
  158. . $message
  159. . '</div>';
  160. // Handle whether to return the HTML & let the caller deal with it
  161. // or to use drupal_set_message to put it near the top of the page & let the theme deal with it
  162. if ($options['return_html']) {
  163. return '<div class="messages tripal-site-admin-only">' . $full_message . '</div>';
  164. }
  165. else {
  166. drupal_set_message($full_message, 'tripal-site-admin-only');
  167. }
  168. }