System.check api - add severity_id to output
[civicrm-core.git] / CRM / Utils / Check / Message.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33 class CRM_Utils_Check_Message {
34 /**
35 * @var string
36 */
37 private $name;
38
39 /**
40 * @var string
41 */
42 private $message;
43
44 /**
45 * @var string
46 */
47 private $title;
48
49 /**
50 * @var int
51 * @see Psr\Log\LogLevel
52 */
53 private $level;
54
55 /**
56 * @var string
57 * help text (to be presented separately from the message)
58 */
59 private $help;
60
61 /**
62 * @var string
63 * crm-i css class
64 */
65 private $icon;
66
67 /**
68 * @var bool|string
69 * Date this message is hidden until
70 */
71 private $hiddenUntil;
72
73 /**
74 * Class constructor.
75 *
76 * @param string $name
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
86 */
87 public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) {
88 $this->name = $name;
89 $this->message = $message;
90 $this->title = $title;
91 // Handle non-integer severity levels.
92 if (!CRM_Utils_Rule::integer($level)) {
93 $level = CRM_Utils_Check::severityMap($level);
94 }
95 $this->level = $level;
96 $this->icon = $icon;
97 }
98
99 /**
100 * Get name.
101 *
102 * @return string
103 */
104 public function getName() {
105 return $this->name;
106 }
107
108 /**
109 * Get message.
110 *
111 * @return string
112 */
113 public function getMessage() {
114 return $this->message;
115 }
116
117 /**
118 * @return string
119 */
120 public function getTitle() {
121 return $this->title;
122 }
123
124 /**
125 * Get level.
126 *
127 * @return string
128 * @see Psr\Log\LogLevel
129 */
130 public function getLevel() {
131 return $this->level;
132 }
133
134 /**
135 * Alias for Level
136 * @return string
137 */
138 public function getSeverity() {
139 return $this->getLevel();
140 }
141
142 /**
143 * Set optional additional help text.
144 *
145 * @param string $help
146 */
147 public function addHelp($help) {
148 $this->help = $help;
149 }
150
151 /**
152 * Convert to array.
153 *
154 * @return array
155 */
156 public function toArray() {
157 $array = array(
158 'name' => $this->name,
159 'message' => $this->message,
160 'title' => $this->title,
161 'severity' => CRM_Utils_Check::severityMap($this->level, TRUE),
162 'severity_id' => $this->level,
163 'is_visible' => (int) $this->isVisible(),
164 'icon' => $this->icon,
165 );
166 if ($this->getHiddenUntil()) {
167 $array['hidden_until'] = $this->getHiddenUntil();
168 }
169 if (!empty($this->help)) {
170 $array['help'] = $this->help;
171 }
172 return $array;
173 }
174
175 /**
176 * Return message visibility.
177 *
178 * @return bool
179 */
180 public function isVisible() {
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;
239 }
240
241 }