From f0f49b459c2225f36cd55c38ff43013080a3ac27 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 6 Mar 2015 16:49:58 -0800 Subject: [PATCH] CRM_Utils_Check - Add Message::$level. Only harass admins about WARNING or higher. --- CRM/Utils/Check.php | 28 +++++++++++++++++++++++++--- CRM/Utils/Check/Message.php | 29 ++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/CRM/Utils/Check.php b/CRM/Utils/Check.php index 3e59c4f6c1..0332175d7c 100644 --- a/CRM/Utils/Check.php +++ b/CRM/Utils/Check.php @@ -60,9 +60,14 @@ class CRM_Utils_Check { /** * Execute "checkAll". * - * @param array|NULL $messages list of CRM_Utils_Check_Message; or NULL if the default list should be fetched + * @param array|NULL $messages + * List of CRM_Utils_Check_Message; or NULL if the default list should be fetched. + * @param array|string|callable $filter + * Restrict messages using a callback filter. + * By default, only show warnings and errors. + * Set TRUE to show all messages. */ - public function showPeriodicAlerts($messages = NULL) { + public function showPeriodicAlerts($messages = NULL, $filter = array(__CLASS__, 'isImportantAlert')) { if (CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityAlert', NULL, TRUE) ) { @@ -77,12 +82,29 @@ class CRM_Utils_Check { $messages = $this->checkAll(); } foreach ($messages as $message) { - CRM_Core_Session::setStatus($message->getMessage(), $message->getTitle()); + if ($filter === TRUE || call_user_func($filter, $message)) { + CRM_Core_Session::setStatus($message->getMessage(), $message->getTitle()); + } } } } } + /** + * Determine if a message is important enough to harass the administrator about. + * + * @param CRM_Utils_Check_Message $message + * @return bool + */ + protected static function isImportantAlert($message) { + return in_array($message->getLevel(), array( + \Psr\Log\LogLevel::WARNING, + \Psr\Log\LogLevel::ALERT, + \Psr\Log\LogLevel::CRITICAL, + \Psr\Log\LogLevel::EMERGENCY, + )); + } + /** * Throw an exception if any of the checks fail. * diff --git a/CRM/Utils/Check/Message.php b/CRM/Utils/Check/Message.php index fc9dd35697..af45898057 100644 --- a/CRM/Utils/Check/Message.php +++ b/CRM/Utils/Check/Message.php @@ -48,15 +48,29 @@ class CRM_Utils_Check_Message { */ private $title; + /** + * @var string + * @see Psr\Log\LogLevel + */ + private $level; + /** * @param string $name - * @param $message - * @param $title + * Symbolic name for the check. + * @param string $message + * Printable message (short or long). + * @param string $title + * Printable message (short). + * @param string $level + * The severity of the message. Use PSR-3 log levels. + * + * @see Psr\Log\LogLevel */ - public function __construct($name, $message, $title) { + public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING) { $this->name = $name; $this->message = $message; $this->title = $title; + $this->level = $level; } /** @@ -80,6 +94,14 @@ class CRM_Utils_Check_Message { return $this->title; } + /** + * @return string + * @see Psr\Log\LogLevel + */ + public function getLevel() { + return $this->level; + } + /** * @return array */ @@ -88,6 +110,7 @@ class CRM_Utils_Check_Message { 'name' => $this->name, 'message' => $this->message, 'title' => $this->title, + 'level' => $this->level, ); } -- 2.25.1