System.check api - add severity_id to output
[civicrm-core.git] / CRM / Utils / Check / Message.php
CommitLineData
a2600a6d
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
a2600a6d 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
a2600a6d
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
a2600a6d
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
a2600a6d
TO
32 */
33class CRM_Utils_Check_Message {
34 /**
35 * @var string
36 */
37 private $name;
38
39 /**
40 * @var string
41 */
42 private $message;
43
1248c859
TO
44 /**
45 * @var string
46 */
47 private $title;
48
f0f49b45 49 /**
7d029160 50 * @var int
f0f49b45
TO
51 * @see Psr\Log\LogLevel
52 */
53 private $level;
54
1b366958
AH
55 /**
56 * @var string
57 * help text (to be presented separately from the message)
58 */
59 private $help;
60
165aab59
CW
61 /**
62 * @var string
63 * crm-i css class
64 */
65 private $icon;
66
7cf4759f
CW
67 /**
68 * @var bool|string
69 * Date this message is hidden until
70 */
71 private $hiddenUntil;
72
5bc392e6 73 /**
70599df6 74 * Class constructor.
75 *
100fef9d 76 * @param string $name
f0f49b45
TO
77 * Symbolic name for the check.
78 * @param string $message
79 * Printable message (short or long).
80 * @param string $title
81 * Printable message (short).
82 * @param string $level
83 * The severity of the message. Use PSR-3 log levels.
84 *
85 * @see Psr\Log\LogLevel
5bc392e6 86 */
165aab59 87 public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) {
a2600a6d
TO
88 $this->name = $name;
89 $this->message = $message;
1248c859 90 $this->title = $title;
70c711fe
J
91 // Handle non-integer severity levels.
92 if (!CRM_Utils_Rule::integer($level)) {
93 $level = CRM_Utils_Check::severityMap($level);
94 }
f0f49b45 95 $this->level = $level;
165aab59 96 $this->icon = $icon;
a2600a6d
TO
97 }
98
99 /**
70599df6 100 * Get name.
101 *
a2600a6d
TO
102 * @return string
103 */
00be9182 104 public function getName() {
a2600a6d
TO
105 return $this->name;
106 }
107
108 /**
70599df6 109 * Get message.
110 *
a2600a6d
TO
111 * @return string
112 */
00be9182 113 public function getMessage() {
a2600a6d
TO
114 return $this->message;
115 }
116
1248c859
TO
117 /**
118 * @return string
119 */
120 public function getTitle() {
121 return $this->title;
122 }
123
f0f49b45 124 /**
70599df6 125 * Get level.
126 *
f0f49b45
TO
127 * @return string
128 * @see Psr\Log\LogLevel
129 */
130 public function getLevel() {
131 return $this->level;
132 }
133
0ea9001d 134 /**
135 * Alias for Level
136 * @return string
1b366958 137 */
0ea9001d 138 public function getSeverity() {
139 return $this->getLevel();
140 }
141
1b366958 142 /**
70599df6 143 * Set optional additional help text.
144 *
097c681e 145 * @param string $help
1b366958
AH
146 */
147 public function addHelp($help) {
148 $this->help = $help;
149 }
150
a2600a6d 151 /**
70599df6 152 * Convert to array.
153 *
a2600a6d
TO
154 * @return array
155 */
00be9182 156 public function toArray() {
1b366958 157 $array = array(
a2600a6d
TO
158 'name' => $this->name,
159 'message' => $this->message,
1248c859 160 'title' => $this->title,
d1fa280a
CW
161 'severity' => CRM_Utils_Check::severityMap($this->level, TRUE),
162 'severity_id' => $this->level,
5009ff2d 163 'is_visible' => (int) $this->isVisible(),
165aab59 164 'icon' => $this->icon,
a2600a6d 165 );
7cf4759f
CW
166 if ($this->getHiddenUntil()) {
167 $array['hidden_until'] = $this->getHiddenUntil();
168 }
1b366958
AH
169 if (!empty($this->help)) {
170 $array['help'] = $this->help;
171 }
172 return $array;
a2600a6d 173 }
96025800 174
70599df6 175 /**
7cf4759f 176 * Return message visibility.
70599df6 177 *
178 * @return bool
179 */
e918c16d 180 public function isVisible() {
7cf4759f
CW
181 return !$this->checkStatusPreference();
182 }
183
184 /**
185 * @return string
186 */
187 public function getHiddenUntil() {
188 if (!isset($this->hiddenUntil)) {
189 $this->checkStatusPreference();
190 }
191 return $this->hiddenUntil;
192 }
193
194 /**
195 * Check if message is visible or has been hidden by the user.
196 *
197 * @return bool
198 * TRUE means hidden, FALSE means visible.
199 * @throws \CiviCRM_API3_Exception
200 */
201 private function checkStatusPreference() {
202 $this->hiddenUntil = FALSE;
203 $statusPreferenceParams = array(
204 'name' => $this->getName(),
205 'domain_id' => CRM_Core_Config::domainID(),
206 );
207 // Check if there's a StatusPreference matching this name/domain.
208 $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams);
209 $spid = FALSE;
210 if (isset($statusPreference['id'])) {
211 $spid = $statusPreference['id'];
212 }
213 if ($spid) {
214 // If so, compare severity to StatusPreference->severity.
215 $severity = $this->getSeverity();
216 if ($severity <= $statusPreference['values'][$spid]['ignore_severity']) {
217 // A hush or a snooze has been set. Find out which.
218 if (isset($statusPreference['values'][$spid]['hush_until'])) {
219 // Snooze is set.
220 $this->hiddenUntil = $statusPreference['values'][$spid]['hush_until'];
221 $today = new DateTime();
222 $snoozeDate = new DateTime($statusPreference['values'][$spid]['hush_until']);
223 if ($today > $snoozeDate) {
224 // Snooze is expired.
225 return FALSE;
226 }
227 else {
228 // Snooze is active.
229 return TRUE;
230 }
231 }
232 else {
233 // Hush.
234 return TRUE;
235 }
236 }
237 }
238 return FALSE;
46a903fb
NM
239 }
240
a2600a6d 241}