3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Utils_Check_Message
{
35 * @see Psr\Log\LogLevel
41 * help text (to be presented separately from the message)
47 * actions which can be performed with this message
49 private $actions = [];
59 * Has this message been suppressed?
65 * Date this message is hidden until
73 * Symbolic name for the check.
74 * @param string $message
75 * Printable message (short or long).
76 * @param string $title
77 * Printable message (short).
78 * @param string $level
79 * The severity of the message. Use PSR-3 log levels.
82 * @see Psr\Log\LogLevel
85 public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel
::WARNING
, $icon = NULL) {
87 $this->message
= $message;
88 $this->title
= $title;
90 $this->setLevel($level);
98 public function getName() {
107 public function getMessage() {
108 return $this->message
;
114 public function getTitle() {
119 * Get severity level number.
122 * @see Psr\Log\LogLevel
124 public function getLevel() {
129 * Get severity string.
132 * @see Psr\Log\LogLevel
134 public function getSeverity() {
135 return CRM_Utils_Check
::severityMap($this->level
, TRUE);
139 * Set optional additional help text.
141 * @param string $help
143 public function addHelp($help) {
148 * Set optional additional actions text.
150 * @param string $title
151 * Text displayed on the status message as a link or button.
152 * @param string $confirmation
153 * Optional confirmation message before performing action
154 * @param string $type
155 * Currently supports: api3 or href
156 * @param array $params
157 * Params to be passed to CRM.api3 or CRM.url depending on type
159 public function addAction($title, $confirmation, $type, $params) {
162 'confirm' => $confirmation,
171 * @param string|int $level
172 * @throws \CRM_Core_Exception
174 public function setLevel($level) {
175 // Convert level to integer
176 if (!CRM_Utils_Rule
::positiveInteger($level)) {
177 $level = CRM_Utils_Check
::severityMap($level);
180 // Validate numeric input - this will throw an exception if invalid
181 CRM_Utils_Check
::severityMap($level, TRUE);
183 $this->level
= $level;
184 // Clear internal caches
185 unset($this->isVisible
, $this->hiddenUntil
);
193 public function toArray() {
195 'name' => $this->name
,
196 'message' => $this->message
,
197 'title' => $this->title
,
198 'severity' => $this->getSeverity(),
199 'severity_id' => $this->level
,
200 'is_visible' => (int) $this->isVisible(),
201 'icon' => $this->icon
,
203 if ($this->getHiddenUntil()) {
204 $array['hidden_until'] = $this->getHiddenUntil();
206 if (!empty($this->help
)) {
207 $array['help'] = $this->help
;
209 if (!empty($this->actions
)) {
210 $array['actions'] = $this->actions
;
216 * Get message visibility.
220 public function isVisible() {
221 if (!isset($this->isVisible
)) {
222 $this->isVisible
= !$this->checkStatusPreference();
224 return $this->isVisible
;
228 * Get date hidden until.
232 public function getHiddenUntil() {
233 if (!isset($this->hiddenUntil
)) {
234 $this->checkStatusPreference();
236 return $this->hiddenUntil
;
240 * Check if message has been hidden by the user.
242 * Also populates this->hiddenUntil property.
245 * TRUE means hidden, FALSE means visible.
246 * @throws \CiviCRM_API3_Exception
248 private function checkStatusPreference() {
249 $this->hiddenUntil
= FALSE;
250 // Debug & info can't be hidden
251 if ($this->level
< 2) {
254 $statusPreferenceParams = [
255 'name' => $this->getName(),
256 'domain_id' => CRM_Core_Config
::domainID(),
259 // Check if there's a StatusPreference matching this name/domain.
260 $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams);
261 $prefs = CRM_Utils_Array
::value('values', $statusPreference, []);
263 // If so, compare severity to StatusPreference->severity.
264 if ($this->level
<= $prefs[0]['ignore_severity']) {
265 if (isset($prefs[0]['hush_until'])) {
267 $this->hiddenUntil
= $prefs[0]['hush_until'];
268 $today = new DateTime();
269 $snoozeDate = new DateTime($prefs[0]['hush_until']);
270 return !($today > $snoozeDate);
273 // Hidden indefinitely.