Merge pull request #12177 from eileenmcnaughton/test_monish
[civicrm-core.git] / CRM / Utils / Check / Message.php
CommitLineData
a2600a6d
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
a2600a6d 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
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
531b0c6c
CW
61 /**
62 * @var array
63 * actions which can be performed with this message
64 */
65 private $actions = array();
66
165aab59
CW
67 /**
68 * @var string
69 * crm-i css class
70 */
71 private $icon;
72
a82f003a
CW
73 /**
74 * @var bool
75 * Has this message been suppressed?
76 */
77 private $isVisible;
78
7cf4759f
CW
79 /**
80 * @var bool|string
81 * Date this message is hidden until
82 */
83 private $hiddenUntil;
84
5bc392e6 85 /**
70599df6 86 * Class constructor.
87 *
100fef9d 88 * @param string $name
f0f49b45
TO
89 * Symbolic name for the check.
90 * @param string $message
91 * Printable message (short or long).
92 * @param string $title
93 * Printable message (short).
94 * @param string $level
95 * The severity of the message. Use PSR-3 log levels.
bf48aa29 96 * @param string $icon
f0f49b45
TO
97 *
98 * @see Psr\Log\LogLevel
3860d465 99 *
5bc392e6 100 */
165aab59 101 public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) {
a2600a6d
TO
102 $this->name = $name;
103 $this->message = $message;
1248c859 104 $this->title = $title;
165aab59 105 $this->icon = $icon;
81756b44 106 $this->setLevel($level);
a2600a6d
TO
107 }
108
109 /**
70599df6 110 * Get name.
111 *
a2600a6d
TO
112 * @return string
113 */
00be9182 114 public function getName() {
a2600a6d
TO
115 return $this->name;
116 }
117
118 /**
70599df6 119 * Get message.
120 *
a2600a6d
TO
121 * @return string
122 */
00be9182 123 public function getMessage() {
a2600a6d
TO
124 return $this->message;
125 }
126
1248c859
TO
127 /**
128 * @return string
129 */
130 public function getTitle() {
131 return $this->title;
132 }
133
f0f49b45 134 /**
fe8bdad8 135 * Get severity level number.
70599df6 136 *
fe8bdad8 137 * @return int
f0f49b45
TO
138 * @see Psr\Log\LogLevel
139 */
140 public function getLevel() {
141 return $this->level;
142 }
143
0ea9001d 144 /**
fe8bdad8
CW
145 * Get severity string.
146 *
0ea9001d 147 * @return string
fe8bdad8 148 * @see Psr\Log\LogLevel
1b366958 149 */
0ea9001d 150 public function getSeverity() {
fe8bdad8 151 return CRM_Utils_Check::severityMap($this->level, TRUE);
0ea9001d 152 }
153
1b366958 154 /**
70599df6 155 * Set optional additional help text.
156 *
097c681e 157 * @param string $help
1b366958
AH
158 */
159 public function addHelp($help) {
160 $this->help = $help;
161 }
162
531b0c6c
CW
163 /**
164 * Set optional additional actions text.
165 *
166 * @param string $title
167 * Text displayed on the status message as a link or button.
168 * @param string $confirmation
169 * Optional confirmation message before performing action
170 * @param string $type
171 * Currently supports: api3 or href
172 * @param array $params
173 * Params to be passed to CRM.api3 or CRM.url depending on type
174 */
175 public function addAction($title, $confirmation, $type, $params) {
176 $this->actions[] = array(
177 'title' => $title,
178 'confirm' => $confirmation,
179 'type' => $type,
180 'params' => $params,
181 );
182 }
183
81756b44
CW
184 /**
185 * Set severity level
186 *
187 * @param string|int $level
188 * @throws \CRM_Core_Exception
189 */
190 public function setLevel($level) {
191 // Convert level to integer
192 if (!CRM_Utils_Rule::positiveInteger($level)) {
193 $level = CRM_Utils_Check::severityMap($level);
194 }
195 else {
196 // Validate numeric input - this will throw an exception if invalid
197 CRM_Utils_Check::severityMap($level, TRUE);
198 }
199 $this->level = $level;
200 // Clear internal caches
201 unset($this->isVisible, $this->hiddenUntil);
202 }
203
a2600a6d 204 /**
70599df6 205 * Convert to array.
206 *
a2600a6d
TO
207 * @return array
208 */
00be9182 209 public function toArray() {
1b366958 210 $array = array(
a2600a6d
TO
211 'name' => $this->name,
212 'message' => $this->message,
1248c859 213 'title' => $this->title,
fe8bdad8 214 'severity' => $this->getSeverity(),
d1fa280a 215 'severity_id' => $this->level,
5009ff2d 216 'is_visible' => (int) $this->isVisible(),
165aab59 217 'icon' => $this->icon,
a2600a6d 218 );
7cf4759f
CW
219 if ($this->getHiddenUntil()) {
220 $array['hidden_until'] = $this->getHiddenUntil();
221 }
1b366958
AH
222 if (!empty($this->help)) {
223 $array['help'] = $this->help;
224 }
531b0c6c
CW
225 if (!empty($this->actions)) {
226 $array['actions'] = $this->actions;
227 }
1b366958 228 return $array;
a2600a6d 229 }
96025800 230
70599df6 231 /**
a82f003a 232 * Get message visibility.
70599df6 233 *
234 * @return bool
235 */
e918c16d 236 public function isVisible() {
a82f003a
CW
237 if (!isset($this->isVisible)) {
238 $this->isVisible = !$this->checkStatusPreference();
239 }
240 return $this->isVisible;
7cf4759f
CW
241 }
242
243 /**
a82f003a
CW
244 * Get date hidden until.
245 *
7cf4759f
CW
246 * @return string
247 */
248 public function getHiddenUntil() {
249 if (!isset($this->hiddenUntil)) {
250 $this->checkStatusPreference();
251 }
252 return $this->hiddenUntil;
253 }
254
255 /**
c7fffdc1 256 * Check if message has been hidden by the user.
7cf4759f 257 *
a82f003a
CW
258 * Also populates this->hiddenUntil property.
259 *
7cf4759f
CW
260 * @return bool
261 * TRUE means hidden, FALSE means visible.
262 * @throws \CiviCRM_API3_Exception
263 */
264 private function checkStatusPreference() {
265 $this->hiddenUntil = FALSE;
eb94a576
J
266 // Debug & info can't be hidden
267 if ($this->level < 2) {
c7fffdc1
CW
268 return FALSE;
269 }
7cf4759f
CW
270 $statusPreferenceParams = array(
271 'name' => $this->getName(),
272 'domain_id' => CRM_Core_Config::domainID(),
c7fffdc1 273 'sequential' => 1,
7cf4759f
CW
274 );
275 // Check if there's a StatusPreference matching this name/domain.
276 $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams);
c7fffdc1
CW
277 $prefs = CRM_Utils_Array::value('values', $statusPreference, array());
278 if ($prefs) {
7cf4759f 279 // If so, compare severity to StatusPreference->severity.
c7fffdc1
CW
280 if ($this->level <= $prefs[0]['ignore_severity']) {
281 if (isset($prefs[0]['hush_until'])) {
282 // Time-based hush.
283 $this->hiddenUntil = $prefs[0]['hush_until'];
7cf4759f 284 $today = new DateTime();
c7fffdc1
CW
285 $snoozeDate = new DateTime($prefs[0]['hush_until']);
286 return !($today > $snoozeDate);
7cf4759f
CW
287 }
288 else {
c7fffdc1 289 // Hidden indefinitely.
7cf4759f
CW
290 return TRUE;
291 }
292 }
293 }
294 return FALSE;
46a903fb
NM
295 }
296
a2600a6d 297}