827613da60caf47cccad1274d1e2e9a75f4b7607
[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
69 * Has this message been suppressed?
70 */
71 private $isVisible;
72
73 /**
74 * @var bool|string
75 * Date this message is hidden until
76 */
77 private $hiddenUntil;
78
79 /**
80 * Class constructor.
81 *
82 * @param string $name
83 * Symbolic name for the check.
84 * @param string $message
85 * Printable message (short or long).
86 * @param string $title
87 * Printable message (short).
88 * @param string $level
89 * The severity of the message. Use PSR-3 log levels.
90 *
91 * @see Psr\Log\LogLevel
92 *
93 * @throws \CRM_Core_Exception
94 */
95 public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) {
96 $this->name = $name;
97 $this->message = $message;
98 $this->title = $title;
99 // Convert level to integer
100 if (!CRM_Utils_Rule::positiveInteger($level)) {
101 $level = CRM_Utils_Check::severityMap($level);
102 }
103 else {
104 // Validate numeric input - this will throw an exception if invalid
105 CRM_Utils_Check::severityMap($level, TRUE);
106 }
107 $this->level = $level;
108 $this->icon = $icon;
109 }
110
111 /**
112 * Get name.
113 *
114 * @return string
115 */
116 public function getName() {
117 return $this->name;
118 }
119
120 /**
121 * Get message.
122 *
123 * @return string
124 */
125 public function getMessage() {
126 return $this->message;
127 }
128
129 /**
130 * @return string
131 */
132 public function getTitle() {
133 return $this->title;
134 }
135
136 /**
137 * Get severity level number.
138 *
139 * @return int
140 * @see Psr\Log\LogLevel
141 */
142 public function getLevel() {
143 return $this->level;
144 }
145
146 /**
147 * Get severity string.
148 *
149 * @return string
150 * @see Psr\Log\LogLevel
151 */
152 public function getSeverity() {
153 return CRM_Utils_Check::severityMap($this->level, TRUE);
154 }
155
156 /**
157 * Set optional additional help text.
158 *
159 * @param string $help
160 */
161 public function addHelp($help) {
162 $this->help = $help;
163 }
164
165 /**
166 * Convert to array.
167 *
168 * @return array
169 */
170 public function toArray() {
171 $array = array(
172 'name' => $this->name,
173 'message' => $this->message,
174 'title' => $this->title,
175 'severity' => $this->getSeverity(),
176 'severity_id' => $this->level,
177 'is_visible' => (int) $this->isVisible(),
178 'icon' => $this->icon,
179 );
180 if ($this->getHiddenUntil()) {
181 $array['hidden_until'] = $this->getHiddenUntil();
182 }
183 if (!empty($this->help)) {
184 $array['help'] = $this->help;
185 }
186 return $array;
187 }
188
189 /**
190 * Get message visibility.
191 *
192 * @return bool
193 */
194 public function isVisible() {
195 if (!isset($this->isVisible)) {
196 $this->isVisible = !$this->checkStatusPreference();
197 }
198 return $this->isVisible;
199 }
200
201 /**
202 * Get date hidden until.
203 *
204 * @return string
205 */
206 public function getHiddenUntil() {
207 if (!isset($this->hiddenUntil)) {
208 $this->checkStatusPreference();
209 }
210 return $this->hiddenUntil;
211 }
212
213 /**
214 * Check if message is visible or has been hidden by the user.
215 *
216 * Also populates this->hiddenUntil property.
217 *
218 * @return bool
219 * TRUE means hidden, FALSE means visible.
220 * @throws \CiviCRM_API3_Exception
221 */
222 private function checkStatusPreference() {
223 $this->hiddenUntil = FALSE;
224 $statusPreferenceParams = array(
225 'name' => $this->getName(),
226 'domain_id' => CRM_Core_Config::domainID(),
227 );
228 // Check if there's a StatusPreference matching this name/domain.
229 $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams);
230 $spid = FALSE;
231 if (isset($statusPreference['id'])) {
232 $spid = $statusPreference['id'];
233 }
234 if ($spid) {
235 // If so, compare severity to StatusPreference->severity.
236 if ($this->level <= $statusPreference['values'][$spid]['ignore_severity']) {
237 // A hush or a snooze has been set. Find out which.
238 if (isset($statusPreference['values'][$spid]['hush_until'])) {
239 // Snooze is set.
240 $this->hiddenUntil = $statusPreference['values'][$spid]['hush_until'];
241 $today = new DateTime();
242 $snoozeDate = new DateTime($statusPreference['values'][$spid]['hush_until']);
243 if ($today > $snoozeDate) {
244 // Snooze is expired.
245 return FALSE;
246 }
247 else {
248 // Snooze is active.
249 return TRUE;
250 }
251 }
252 else {
253 // Hush.
254 return TRUE;
255 }
256 }
257 }
258 return FALSE;
259 }
260
261 }