tripal.notice.api.inc

Provides an application programming interface (API) for improved user notifications. These API functions can be used to set messages for end-users, administrators, or simple logging.

File

tripal/api/tripal.notice.api.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for improved user
  5. * notifications. These API functions can be used to set messages for
  6. * end-users, administrators, or simple logging.
  7. */
  8. /**
  9. * @defgroup tripal_notify_api Notify
  10. * @ingroup tripal_api
  11. * @{
  12. * Provides an application programming interface (API) for improved user
  13. * notivications. These API functions can be used to set messages for
  14. * end-users, administrators, or simple logging.
  15. *
  16. * @}
  17. */
  18. // Globals used by Tripals Error catching functions
  19. // Should match those defined by watchdog
  20. define('TRIPAL_CRITICAL',2);
  21. define('TRIPAL_ERROR',3);
  22. define('TRIPAL_WARNING',4);
  23. define('TRIPAL_NOTICE',5);
  24. define('TRIPAL_INFO',6);
  25. define('TRIPAL_DEBUG',7);
  26. /**
  27. * Provide better error notice for Tripal.
  28. *
  29. * If the environment variable 'TRIPAL_DEBUG' is set to 1 then this function
  30. * will add backtrace information to the message.
  31. *
  32. * @param $type
  33. * The catagory to which this message belongs. Can be any string, but the
  34. * general practice is to use the name of the module.
  35. * @param $severity
  36. * The severity of the message; one of the following values:
  37. * - TRIPAL_CRITICAL: Critical conditions.
  38. * - TRIPAL_ERROR: Error conditions.
  39. * - TRIPAL_WARNING: Warning conditions.
  40. * - TRIPAL_NOTICE: (default) Normal but significant conditions.
  41. * - TRIPAL_INFO: Informational messages.
  42. * - TRIPAL_DEBUG: Debug-level messages.
  43. * @param $message
  44. * The message to store in the log. Keep $message translatable by not
  45. * concatenating dynamic values into it! Variables in the message should be
  46. * added by using placeholder strings alongside the variables argument to
  47. * declare the value of the placeholders. See t() for documentation on how
  48. * $message and $variables interact.
  49. * @param $variables
  50. * Array of variables to replace in the message on display or NULL if message
  51. * is already translated or not possible to translate.
  52. * @param $options
  53. * An array of options. Some available options include:
  54. * - print: prints the error message to the terminal screen. Useful when
  55. * display is the command-line
  56. *
  57. * @ingroup tripal_notify_api
  58. */
  59. function tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) {
  60. $suppress = getenv('TRIPAL_SUPPRESS_ERRORS');
  61. if(strtolower($suppress) === 'true') return;
  62. // Get human-readable severity string
  63. $severity_string = '';
  64. switch ($severity) {
  65. case TRIPAL_CRITICAL:
  66. $severity_string = 'CRITICAL';
  67. break;
  68. case TRIPAL_ERROR:
  69. $severity_string = 'ERROR';
  70. break;
  71. case TRIPAL_WARNING:
  72. $severity_string = 'WARNING';
  73. break;
  74. case TRIPAL_NOTICE:
  75. $severity_string = 'NOTICE';
  76. break;
  77. case TRIPAL_INFO:
  78. $severity_string = 'INFO';
  79. break;
  80. case TRIPAL_DEBUG:
  81. $severity_string = 'DEBUG';
  82. break;
  83. }
  84. // If we are not set to return debugging information and the severity string
  85. // is debug then don't report the error.
  86. if (($severity == TRIPAL_DEBUG) AND (getenv('TRIPAL_DEBUG') != 1)) {
  87. return FALSE;
  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): Unable to register error message with watchdog: " . $e->getMessage(). "\n.";
  106. $options['print'] = TRUE;
  107. }
  108. // Format the message for printing (either to the screen, log or both).
  109. if (sizeof($variables) > 0) {
  110. $print_message = str_replace(array_keys($variables), $variables, $message);
  111. }
  112. else {
  113. $print_message = $message;
  114. }
  115. // If print option supplied then print directly to the screen.
  116. if (isset($options['print'])) {
  117. print $severity_string . ' (' . strtoupper($type) . '): ' . $print_message . "\n";
  118. }
  119. // Print to the Tripal error log but only if the severity is not info.
  120. if (($severity != TRIPAL_INFO)) {
  121. tripal_log('[' . strtoupper($type) . '] ' . $print_message . "\n", $severity_string);
  122. }
  123. }
  124. /**
  125. * Display messages to tripal administrators.
  126. *
  127. * This can be used instead of drupal_set_message when you want to target
  128. * tripal administrators.
  129. *
  130. * @param $message
  131. * The message to be displayed to the tripal administrators.
  132. * @param $importance
  133. * The level of importance for this message. In the future this will be used
  134. * to allow administrators to filter some of these messages. It can be one of
  135. * the following:
  136. * - TRIPAL_CRITICAL: Critical conditions.
  137. * - TRIPAL_ERROR: Error conditions.
  138. * - TRIPAL_WARNING: Warning conditions.
  139. * - TRIPAL_NOTICE: Normal but significant conditions.
  140. * - TRIPAL_INFO: (default) Informational messages.
  141. * - TRIPAL_DEBUG: Debug-level messages.
  142. * @param $options
  143. * Any options to apply to the current message. Supported options include:
  144. * - return_html: return HTML instead of setting a drupal message. This can
  145. * be used to place a tripal message in a particular place in the page.
  146. * The default is FALSE.
  147. *
  148. * @ingroup tripal_notify_api
  149. */
  150. function tripal_set_message($message, $importance = TRIPAL_INFO, $options = array()) {
  151. global $user;
  152. // Only show the message to the users with 'view dev helps' permission.
  153. if (!user_access('view dev helps')) {
  154. return '';
  155. }
  156. // Set defaults.
  157. $options['return_html'] = (isset($options['return_html'])) ? $options['return_html'] : FALSE;
  158. // Get human-readable severity string.
  159. $importance_string = '';
  160. switch ($importance) {
  161. case TRIPAL_CRITICAL:
  162. $importance_string = 'CRITICAL';
  163. break;
  164. case TRIPAL_ERROR:
  165. $importance_string = 'ERROR';
  166. break;
  167. case TRIPAL_WARNING:
  168. $importance_string = 'WARNING';
  169. break;
  170. case TRIPAL_NOTICE:
  171. $importance_string = 'NOTICE';
  172. break;
  173. case TRIPAL_INFO:
  174. $importance_string = 'INFO';
  175. break;
  176. case TRIPAL_DEBUG:
  177. $importance_string = 'DEBUG';
  178. break;
  179. }
  180. // Mark-up the Message.
  181. $full_message =
  182. '<div class="tripal-site-admin-message">' .
  183. '<span class="tripal-severity-string ' . strtolower($importance_string) . '">' . $importance_string . ': </span>' .
  184. $message .
  185. '</div>';
  186. // Handle whether to return the HTML & let the caller deal with it
  187. // or to use drupal_set_message to put it near the top of the page & let the
  188. // theme deal with it.
  189. if ($options['return_html']) {
  190. return '<div class="messages tripal-site-admin-only">' . $full_message . '</div>';
  191. }
  192. else {
  193. drupal_set_message($full_message, 'tripal-site-admin-only');
  194. }
  195. }
  196. /**
  197. * File-based logging for Tripal.
  198. *
  199. * @param $message
  200. * The message to be logged. Need not contain date/time information.
  201. * @param $log_type
  202. * The type of log. Should be one of 'error' or 'job' although other types
  203. * are supported.
  204. * @param $options
  205. * An array of options where the following keys are supported:
  206. * - first_progress_bar: this sohuld be used for the first log call for a
  207. * progress bar.
  208. * - is_progress_bar: this option should be used for all but the first print
  209. * of a progress bar to allow it all to be printed on the same line
  210. * without intervening date prefixes.
  211. * @return
  212. * The number of bytes that were written to the file, or FALSE on failure.
  213. *
  214. * @ingroup tripal_notify_api
  215. */
  216. function tripal_log($message, $type = 'error', $options = array()) {
  217. global $base_url;
  218. $prefix = '[site ' . $base_url . '] [TRIPAL ' . strtoupper(check_plain($type)) . '] ';
  219. if (!isset($options['is_progress_bar'])) {
  220. $message = $prefix . str_replace("\n", "", trim($message));
  221. }
  222. if (isset($options['first_progress_bar'])) {
  223. $message = $prefix . trim($message);
  224. }
  225. return error_log($message);
  226. }