Merge pull request #15825 from seamuslee001/dev_core_183_logging
[civicrm-core.git] / CRM / Utils / Check / Message.php
CommitLineData
a2600a6d
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
a2600a6d 5 | |
bc77d7c0
TO
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 |
a2600a6d 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
a2600a6d
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
a2600a6d
TO
16 */
17class CRM_Utils_Check_Message {
18 /**
19 * @var string
20 */
21 private $name;
22
23 /**
24 * @var string
25 */
26 private $message;
27
1248c859
TO
28 /**
29 * @var string
30 */
31 private $title;
32
f0f49b45 33 /**
7d029160 34 * @var int
f0f49b45
TO
35 * @see Psr\Log\LogLevel
36 */
37 private $level;
38
1b366958
AH
39 /**
40 * @var string
41 * help text (to be presented separately from the message)
42 */
43 private $help;
44
531b0c6c
CW
45 /**
46 * @var array
47 * actions which can be performed with this message
48 */
be2fb01f 49 private $actions = [];
531b0c6c 50
165aab59
CW
51 /**
52 * @var string
53 * crm-i css class
54 */
55 private $icon;
56
a82f003a
CW
57 /**
58 * @var bool
59 * Has this message been suppressed?
60 */
61 private $isVisible;
62
7cf4759f
CW
63 /**
64 * @var bool|string
65 * Date this message is hidden until
66 */
67 private $hiddenUntil;
68
5bc392e6 69 /**
70599df6 70 * Class constructor.
71 *
100fef9d 72 * @param string $name
f0f49b45
TO
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.
bf48aa29 80 * @param string $icon
f0f49b45
TO
81 *
82 * @see Psr\Log\LogLevel
3860d465 83 *
5bc392e6 84 */
165aab59 85 public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) {
a2600a6d
TO
86 $this->name = $name;
87 $this->message = $message;
1248c859 88 $this->title = $title;
165aab59 89 $this->icon = $icon;
81756b44 90 $this->setLevel($level);
a2600a6d
TO
91 }
92
93 /**
70599df6 94 * Get name.
95 *
a2600a6d
TO
96 * @return string
97 */
00be9182 98 public function getName() {
a2600a6d
TO
99 return $this->name;
100 }
101
102 /**
70599df6 103 * Get message.
104 *
a2600a6d
TO
105 * @return string
106 */
00be9182 107 public function getMessage() {
a2600a6d
TO
108 return $this->message;
109 }
110
1248c859
TO
111 /**
112 * @return string
113 */
114 public function getTitle() {
115 return $this->title;
116 }
117
f0f49b45 118 /**
fe8bdad8 119 * Get severity level number.
70599df6 120 *
fe8bdad8 121 * @return int
f0f49b45
TO
122 * @see Psr\Log\LogLevel
123 */
124 public function getLevel() {
125 return $this->level;
126 }
127
0ea9001d 128 /**
fe8bdad8
CW
129 * Get severity string.
130 *
0ea9001d 131 * @return string
fe8bdad8 132 * @see Psr\Log\LogLevel
1b366958 133 */
0ea9001d 134 public function getSeverity() {
fe8bdad8 135 return CRM_Utils_Check::severityMap($this->level, TRUE);
0ea9001d 136 }
137
1b366958 138 /**
70599df6 139 * Set optional additional help text.
140 *
097c681e 141 * @param string $help
1b366958
AH
142 */
143 public function addHelp($help) {
144 $this->help = $help;
145 }
146
531b0c6c
CW
147 /**
148 * Set optional additional actions text.
149 *
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
158 */
159 public function addAction($title, $confirmation, $type, $params) {
be2fb01f 160 $this->actions[] = [
531b0c6c
CW
161 'title' => $title,
162 'confirm' => $confirmation,
163 'type' => $type,
164 'params' => $params,
be2fb01f 165 ];
531b0c6c
CW
166 }
167
81756b44
CW
168 /**
169 * Set severity level
170 *
171 * @param string|int $level
172 * @throws \CRM_Core_Exception
173 */
174 public function setLevel($level) {
175 // Convert level to integer
176 if (!CRM_Utils_Rule::positiveInteger($level)) {
177 $level = CRM_Utils_Check::severityMap($level);
178 }
179 else {
180 // Validate numeric input - this will throw an exception if invalid
181 CRM_Utils_Check::severityMap($level, TRUE);
182 }
183 $this->level = $level;
184 // Clear internal caches
185 unset($this->isVisible, $this->hiddenUntil);
186 }
187
a2600a6d 188 /**
70599df6 189 * Convert to array.
190 *
a2600a6d
TO
191 * @return array
192 */
00be9182 193 public function toArray() {
be2fb01f 194 $array = [
a2600a6d
TO
195 'name' => $this->name,
196 'message' => $this->message,
1248c859 197 'title' => $this->title,
fe8bdad8 198 'severity' => $this->getSeverity(),
d1fa280a 199 'severity_id' => $this->level,
5009ff2d 200 'is_visible' => (int) $this->isVisible(),
165aab59 201 'icon' => $this->icon,
be2fb01f 202 ];
7cf4759f
CW
203 if ($this->getHiddenUntil()) {
204 $array['hidden_until'] = $this->getHiddenUntil();
205 }
1b366958
AH
206 if (!empty($this->help)) {
207 $array['help'] = $this->help;
208 }
531b0c6c
CW
209 if (!empty($this->actions)) {
210 $array['actions'] = $this->actions;
211 }
1b366958 212 return $array;
a2600a6d 213 }
96025800 214
70599df6 215 /**
a82f003a 216 * Get message visibility.
70599df6 217 *
218 * @return bool
219 */
e918c16d 220 public function isVisible() {
a82f003a
CW
221 if (!isset($this->isVisible)) {
222 $this->isVisible = !$this->checkStatusPreference();
223 }
224 return $this->isVisible;
7cf4759f
CW
225 }
226
227 /**
a82f003a
CW
228 * Get date hidden until.
229 *
7cf4759f
CW
230 * @return string
231 */
232 public function getHiddenUntil() {
233 if (!isset($this->hiddenUntil)) {
234 $this->checkStatusPreference();
235 }
236 return $this->hiddenUntil;
237 }
238
239 /**
c7fffdc1 240 * Check if message has been hidden by the user.
7cf4759f 241 *
a82f003a
CW
242 * Also populates this->hiddenUntil property.
243 *
7cf4759f
CW
244 * @return bool
245 * TRUE means hidden, FALSE means visible.
246 * @throws \CiviCRM_API3_Exception
247 */
248 private function checkStatusPreference() {
249 $this->hiddenUntil = FALSE;
eb94a576
J
250 // Debug & info can't be hidden
251 if ($this->level < 2) {
c7fffdc1
CW
252 return FALSE;
253 }
be2fb01f 254 $statusPreferenceParams = [
7cf4759f
CW
255 'name' => $this->getName(),
256 'domain_id' => CRM_Core_Config::domainID(),
c7fffdc1 257 'sequential' => 1,
be2fb01f 258 ];
7cf4759f
CW
259 // Check if there's a StatusPreference matching this name/domain.
260 $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams);
be2fb01f 261 $prefs = CRM_Utils_Array::value('values', $statusPreference, []);
c7fffdc1 262 if ($prefs) {
7cf4759f 263 // If so, compare severity to StatusPreference->severity.
c7fffdc1
CW
264 if ($this->level <= $prefs[0]['ignore_severity']) {
265 if (isset($prefs[0]['hush_until'])) {
266 // Time-based hush.
267 $this->hiddenUntil = $prefs[0]['hush_until'];
7cf4759f 268 $today = new DateTime();
c7fffdc1
CW
269 $snoozeDate = new DateTime($prefs[0]['hush_until']);
270 return !($today > $snoozeDate);
7cf4759f
CW
271 }
272 else {
c7fffdc1 273 // Hidden indefinitely.
7cf4759f
CW
274 return TRUE;
275 }
276 }
277 }
278 return FALSE;
46a903fb
NM
279 }
280
a2600a6d 281}