From 7d0291603da8e94aaa61735dc94167c248ceea5a Mon Sep 17 00:00:00 2001 From: Nikki Murray Date: Wed, 14 Oct 2015 13:16:28 -0400 Subject: [PATCH] cache on page load, get max visible severity, show appropriate footer --- CRM/Core/Invoke.php | 39 +++++++++++++++++++++++++++++++-- CRM/Utils/Check.php | 26 +++++++++++++++++----- CRM/Utils/Check/Message.php | 2 +- templates/CRM/common/footer.tpl | 4 ++-- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/CRM/Core/Invoke.php b/CRM/Core/Invoke.php index 24c0ecf77e..7c38eb6eb3 100644 --- a/CRM/Core/Invoke.php +++ b/CRM/Core/Invoke.php @@ -352,8 +352,43 @@ class CRM_Core_Invoke { if (CRM_Core_Config::isUpgradeMode()) { return; } - $statusSeverity = 0; - $statusMessage = ts('System status OK'); + // check date of last cache and compare to today's date + $systemCheckDate = Civi::cache()->get('systemCheckDate'); + if ($systemCheckDate > strtotime("one day ago")) { + $statusSeverity = Civi::cache()->get('systemCheckSeverity'); + } + // calls helper function in CRM_Utils_Check + if (empty($statusSeverity)) { + $statusSeverity = CRM_Utils_Check::checkAll(TRUE); + } + switch ($statusSeverity) { + case 7: + $statusMessage = ts('System Status: Emergency'); + break; + + case 6: + $statusMessage = ts('System Status: Alert'); + break; + + case 5: + $statusMessage = ts('System Status: Critical'); + break; + + case 4: + $statusMessage = ts('System Status: Error'); + break; + + case 3: + $statusMessage = ts('System Status: Warning'); + break; + + case 2: + $statusMessage = ts('System Status: Notice'); + break; + + default: + $statusMessage = ts('System Status: Ok'); + } // TODO: get status from CRM_Utils_Check, if cached $template->assign('footer_status_severity', $statusSeverity); $template->assign('footer_status_message', $statusMessage); diff --git a/CRM/Utils/Check.php b/CRM/Utils/Check.php index 265141187b..1783774757 100644 --- a/CRM/Utils/Check.php +++ b/CRM/Utils/Check.php @@ -32,7 +32,7 @@ */ class CRM_Utils_Check { // How often to run checks and notify admins about issues. - const CHECK_TIMER = 86400; + const CHECK_TIMER = 86400; /** * We only need one instance of this object, so we use the @@ -108,7 +108,7 @@ class CRM_Utils_Check { * @param CRM_Utils_Check_Message $b * @return int */ - public function severitySort($a, $b) { + public static function severitySort($a, $b) { $aSeverity = $a->getSeverity(); $bSeverity = $b->getSeverity(); if ($aSeverity == $bSeverity) { @@ -177,11 +177,14 @@ class CRM_Utils_Check { * We might even expose the results of these checks on the Wordpress * plugin status page or the Drupal admin/reports/status path. * + * @param bool $max + * Whether to return just the maximum non-hushed severity + * * @return array * Array of messages * @link https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_requirements */ - public function checkAll() { + public static function checkAll($max = FALSE) { $checks = array(); $checks[] = new CRM_Utils_Check_Security(); $checks[] = new CRM_Utils_Check_Env(); @@ -210,7 +213,20 @@ class CRM_Utils_Check { } uasort($messages, array(__CLASS__, 'severitySort')); - return $messages; + $maxSeverity = 1; + foreach ($messages as $message) { + if (!$message->isVisible()) { + continue; + } + $maxSeverity = max(1, $message->getLevel()); + break; + } + + Civi::cache()->set('systemCheckSeverity', $maxSeverity); + $timestamp = time(); + Civi::cache()->set('systemCheckDate', $timestamp); + + return ($max) ? $maxSeverity : $messages; } /** @@ -219,7 +235,7 @@ class CRM_Utils_Check { * @return bool * TRUE means hush/snooze, FALSE means display. */ - public function checkHushSnooze($message) { + public static function checkHushSnooze($message) { $statusPreferenceParams = array( 'name' => $message->getName(), 'domain_id' => CRM_Core_Config::domainID(), diff --git a/CRM/Utils/Check/Message.php b/CRM/Utils/Check/Message.php index 2711813a98..dbaf7ed92b 100644 --- a/CRM/Utils/Check/Message.php +++ b/CRM/Utils/Check/Message.php @@ -47,7 +47,7 @@ class CRM_Utils_Check_Message { private $title; /** - * @var string + * @var int * @see Psr\Log\LogLevel */ private $level; diff --git a/templates/CRM/common/footer.tpl b/templates/CRM/common/footer.tpl index 5f32c2b98c..a61bfebee1 100644 --- a/templates/CRM/common/footer.tpl +++ b/templates/CRM/common/footer.tpl @@ -31,8 +31,8 @@