CRM-13823 - Add hidden_until property to messages
authorColeman Watts <coleman@civicrm.org>
Mon, 16 Nov 2015 17:54:46 +0000 (12:54 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 16 Nov 2015 17:54:46 +0000 (12:54 -0500)
CRM/Utils/Check.php
CRM/Utils/Check/Message.php

index b0ad2a14f4c51d52e19db3af954926077e23383e..25f7e4e2cb729534757804d06d4214fbf01b9936 100644 (file)
@@ -225,52 +225,4 @@ class CRM_Utils_Check {
     return ($max) ? $maxSeverity : $messages;
   }
 
-  /**
-   * Evaluate if a system check should be hushed/snoozed.
-   *
-   * @param CRM_Utils_Check_Message $message
-   *   The message to evaluate.
-   *
-   * @return bool
-   *   TRUE means hush/snooze, FALSE means display.
-   * @throws \CiviCRM_API3_Exception
-   */
-  public static function checkHushSnooze($message) {
-    $statusPreferenceParams = array(
-      'name' => $message->getName(),
-      'domain_id' => CRM_Core_Config::domainID(),
-    );
-    // Check if there's a StatusPreference matching this name/domain.
-    $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams);
-    $spid = FALSE;
-    if (isset($statusPreference['id'])) {
-      $spid = $statusPreference['id'];
-    }
-    if ($spid) {
-      // If so, compare severity to StatusPreference->severity.
-      $severity = $message->getSeverity();
-      if ($severity <= $statusPreference['values'][$spid]['ignore_severity']) {
-        // A hush or a snooze has been set.  Find out which.
-        if (isset($statusPreference['values'][$spid]['hush_until'])) {
-          // Snooze is set.
-          $today = new DateTime();
-          $snoozeDate = new DateTime($statusPreference['values'][$spid]['hush_until']);
-          if ($today > $snoozeDate) {
-            // Snooze is expired.
-            return FALSE;
-          }
-          else {
-            // Snooze is active.
-            return TRUE;
-          }
-        }
-        else {
-          // Hush.
-          return TRUE;
-        }
-      }
-    }
-    return FALSE;
-  }
-
 }
index ca4efbd3174c9c473b3a7d647bce4b105a865629..1e50c4e99aaf22a59818f741905d0b550267c4a5 100644 (file)
@@ -64,6 +64,12 @@ class CRM_Utils_Check_Message {
    */
   private $icon;
 
+  /**
+   * @var bool|string
+   *   Date this message is hidden until
+   */
+  private $hiddenUntil;
+
   /**
    * Class constructor.
    *
@@ -156,6 +162,9 @@ class CRM_Utils_Check_Message {
       'is_visible' => (int) $this->isVisible(),
       'icon' => $this->icon,
     );
+    if ($this->getHiddenUntil()) {
+      $array['hidden_until'] = $this->getHiddenUntil();
+    }
     if (!empty($this->help)) {
       $array['help'] = $this->help;
     }
@@ -163,12 +172,69 @@ class CRM_Utils_Check_Message {
   }
 
   /**
-   * Check if message is visible or has been hidden by the user.
+   * Return message visibility.
    *
    * @return bool
    */
   public function isVisible() {
-    return !CRM_Utils_Check::checkHushSnooze($this);
+    return !$this->checkStatusPreference();
+  }
+
+  /**
+   * @return string
+   */
+  public function getHiddenUntil() {
+    if (!isset($this->hiddenUntil)) {
+      $this->checkStatusPreference();
+    }
+    return $this->hiddenUntil;
+  }
+
+  /**
+   * Check if message is visible or has been hidden by the user.
+   *
+   * @return bool
+   *   TRUE means hidden, FALSE means visible.
+   * @throws \CiviCRM_API3_Exception
+   */
+  private function checkStatusPreference() {
+    $this->hiddenUntil = FALSE;
+    $statusPreferenceParams = array(
+      'name' => $this->getName(),
+      'domain_id' => CRM_Core_Config::domainID(),
+    );
+    // Check if there's a StatusPreference matching this name/domain.
+    $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams);
+    $spid = FALSE;
+    if (isset($statusPreference['id'])) {
+      $spid = $statusPreference['id'];
+    }
+    if ($spid) {
+      // If so, compare severity to StatusPreference->severity.
+      $severity = $this->getSeverity();
+      if ($severity <= $statusPreference['values'][$spid]['ignore_severity']) {
+        // A hush or a snooze has been set.  Find out which.
+        if (isset($statusPreference['values'][$spid]['hush_until'])) {
+          // Snooze is set.
+          $this->hiddenUntil = $statusPreference['values'][$spid]['hush_until'];
+          $today = new DateTime();
+          $snoozeDate = new DateTime($statusPreference['values'][$spid]['hush_until']);
+          if ($today > $snoozeDate) {
+            // Snooze is expired.
+            return FALSE;
+          }
+          else {
+            // Snooze is active.
+            return TRUE;
+          }
+        }
+        else {
+          // Hush.
+          return TRUE;
+        }
+      }
+    }
+    return FALSE;
   }
 
 }