Update copyright date for 2020
[civicrm-core.git] / CRM / Utils / Check / Message.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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-2020
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 array
63 * actions which can be performed with this message
64 */
65 private $actions = [];
66
67 /**
68 * @var string
69 * crm-i css class
70 */
71 private $icon;
72
73 /**
74 * @var bool
75 * Has this message been suppressed?
76 */
77 private $isVisible;
78
79 /**
80 * @var bool|string
81 * Date this message is hidden until
82 */
83 private $hiddenUntil;
84
85 /**
86 * Class constructor.
87 *
88 * @param string $name
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.
96 * @param string $icon
97 *
98 * @see Psr\Log\LogLevel
99 *
100 */
101 public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) {
102 $this->name = $name;
103 $this->message = $message;
104 $this->title = $title;
105 $this->icon = $icon;
106 $this->setLevel($level);
107 }
108
109 /**
110 * Get name.
111 *
112 * @return string
113 */
114 public function getName() {
115 return $this->name;
116 }
117
118 /**
119 * Get message.
120 *
121 * @return string
122 */
123 public function getMessage() {
124 return $this->message;
125 }
126
127 /**
128 * @return string
129 */
130 public function getTitle() {
131 return $this->title;
132 }
133
134 /**
135 * Get severity level number.
136 *
137 * @return int
138 * @see Psr\Log\LogLevel
139 */
140 public function getLevel() {
141 return $this->level;
142 }
143
144 /**
145 * Get severity string.
146 *
147 * @return string
148 * @see Psr\Log\LogLevel
149 */
150 public function getSeverity() {
151 return CRM_Utils_Check::severityMap($this->level, TRUE);
152 }
153
154 /**
155 * Set optional additional help text.
156 *
157 * @param string $help
158 */
159 public function addHelp($help) {
160 $this->help = $help;
161 }
162
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[] = [
177 'title' => $title,
178 'confirm' => $confirmation,
179 'type' => $type,
180 'params' => $params,
181 ];
182 }
183
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
204 /**
205 * Convert to array.
206 *
207 * @return array
208 */
209 public function toArray() {
210 $array = [
211 'name' => $this->name,
212 'message' => $this->message,
213 'title' => $this->title,
214 'severity' => $this->getSeverity(),
215 'severity_id' => $this->level,
216 'is_visible' => (int) $this->isVisible(),
217 'icon' => $this->icon,
218 ];
219 if ($this->getHiddenUntil()) {
220 $array['hidden_until'] = $this->getHiddenUntil();
221 }
222 if (!empty($this->help)) {
223 $array['help'] = $this->help;
224 }
225 if (!empty($this->actions)) {
226 $array['actions'] = $this->actions;
227 }
228 return $array;
229 }
230
231 /**
232 * Get message visibility.
233 *
234 * @return bool
235 */
236 public function isVisible() {
237 if (!isset($this->isVisible)) {
238 $this->isVisible = !$this->checkStatusPreference();
239 }
240 return $this->isVisible;
241 }
242
243 /**
244 * Get date hidden until.
245 *
246 * @return string
247 */
248 public function getHiddenUntil() {
249 if (!isset($this->hiddenUntil)) {
250 $this->checkStatusPreference();
251 }
252 return $this->hiddenUntil;
253 }
254
255 /**
256 * Check if message has been hidden by the user.
257 *
258 * Also populates this->hiddenUntil property.
259 *
260 * @return bool
261 * TRUE means hidden, FALSE means visible.
262 * @throws \CiviCRM_API3_Exception
263 */
264 private function checkStatusPreference() {
265 $this->hiddenUntil = FALSE;
266 // Debug & info can't be hidden
267 if ($this->level < 2) {
268 return FALSE;
269 }
270 $statusPreferenceParams = [
271 'name' => $this->getName(),
272 'domain_id' => CRM_Core_Config::domainID(),
273 'sequential' => 1,
274 ];
275 // Check if there's a StatusPreference matching this name/domain.
276 $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams);
277 $prefs = CRM_Utils_Array::value('values', $statusPreference, []);
278 if ($prefs) {
279 // If so, compare severity to StatusPreference->severity.
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'];
284 $today = new DateTime();
285 $snoozeDate = new DateTime($prefs[0]['hush_until']);
286 return !($today > $snoozeDate);
287 }
288 else {
289 // Hidden indefinitely.
290 return TRUE;
291 }
292 }
293 }
294 return FALSE;
295 }
296
297 }