Commit | Line | Data |
---|---|---|
a2600a6d TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
a2600a6d | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
a2600a6d TO |
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 | ||
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 | ||
165aab59 CW |
61 | /** |
62 | * @var string | |
63 | * crm-i css class | |
64 | */ | |
65 | private $icon; | |
66 | ||
a82f003a CW |
67 | /** |
68 | * @var bool | |
69 | * Has this message been suppressed? | |
70 | */ | |
71 | private $isVisible; | |
72 | ||
7cf4759f CW |
73 | /** |
74 | * @var bool|string | |
75 | * Date this message is hidden until | |
76 | */ | |
77 | private $hiddenUntil; | |
78 | ||
5bc392e6 | 79 | /** |
70599df6 | 80 | * Class constructor. |
81 | * | |
100fef9d | 82 | * @param string $name |
f0f49b45 TO |
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 | |
3860d465 CW |
92 | * |
93 | * @throws \CRM_Core_Exception | |
5bc392e6 | 94 | */ |
165aab59 | 95 | public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) { |
a2600a6d TO |
96 | $this->name = $name; |
97 | $this->message = $message; | |
1248c859 | 98 | $this->title = $title; |
165aab59 | 99 | $this->icon = $icon; |
81756b44 | 100 | $this->setLevel($level); |
a2600a6d TO |
101 | } |
102 | ||
103 | /** | |
70599df6 | 104 | * Get name. |
105 | * | |
a2600a6d TO |
106 | * @return string |
107 | */ | |
00be9182 | 108 | public function getName() { |
a2600a6d TO |
109 | return $this->name; |
110 | } | |
111 | ||
112 | /** | |
70599df6 | 113 | * Get message. |
114 | * | |
a2600a6d TO |
115 | * @return string |
116 | */ | |
00be9182 | 117 | public function getMessage() { |
a2600a6d TO |
118 | return $this->message; |
119 | } | |
120 | ||
1248c859 TO |
121 | /** |
122 | * @return string | |
123 | */ | |
124 | public function getTitle() { | |
125 | return $this->title; | |
126 | } | |
127 | ||
f0f49b45 | 128 | /** |
fe8bdad8 | 129 | * Get severity level number. |
70599df6 | 130 | * |
fe8bdad8 | 131 | * @return int |
f0f49b45 TO |
132 | * @see Psr\Log\LogLevel |
133 | */ | |
134 | public function getLevel() { | |
135 | return $this->level; | |
136 | } | |
137 | ||
0ea9001d | 138 | /** |
fe8bdad8 CW |
139 | * Get severity string. |
140 | * | |
0ea9001d | 141 | * @return string |
fe8bdad8 | 142 | * @see Psr\Log\LogLevel |
1b366958 | 143 | */ |
0ea9001d | 144 | public function getSeverity() { |
fe8bdad8 | 145 | return CRM_Utils_Check::severityMap($this->level, TRUE); |
0ea9001d | 146 | } |
147 | ||
1b366958 | 148 | /** |
70599df6 | 149 | * Set optional additional help text. |
150 | * | |
097c681e | 151 | * @param string $help |
1b366958 AH |
152 | */ |
153 | public function addHelp($help) { | |
154 | $this->help = $help; | |
155 | } | |
156 | ||
81756b44 CW |
157 | /** |
158 | * Set severity level | |
159 | * | |
160 | * @param string|int $level | |
161 | * @throws \CRM_Core_Exception | |
162 | */ | |
163 | public function setLevel($level) { | |
164 | // Convert level to integer | |
165 | if (!CRM_Utils_Rule::positiveInteger($level)) { | |
166 | $level = CRM_Utils_Check::severityMap($level); | |
167 | } | |
168 | else { | |
169 | // Validate numeric input - this will throw an exception if invalid | |
170 | CRM_Utils_Check::severityMap($level, TRUE); | |
171 | } | |
172 | $this->level = $level; | |
173 | // Clear internal caches | |
174 | unset($this->isVisible, $this->hiddenUntil); | |
175 | } | |
176 | ||
a2600a6d | 177 | /** |
70599df6 | 178 | * Convert to array. |
179 | * | |
a2600a6d TO |
180 | * @return array |
181 | */ | |
00be9182 | 182 | public function toArray() { |
1b366958 | 183 | $array = array( |
a2600a6d TO |
184 | 'name' => $this->name, |
185 | 'message' => $this->message, | |
1248c859 | 186 | 'title' => $this->title, |
fe8bdad8 | 187 | 'severity' => $this->getSeverity(), |
d1fa280a | 188 | 'severity_id' => $this->level, |
5009ff2d | 189 | 'is_visible' => (int) $this->isVisible(), |
165aab59 | 190 | 'icon' => $this->icon, |
a2600a6d | 191 | ); |
7cf4759f CW |
192 | if ($this->getHiddenUntil()) { |
193 | $array['hidden_until'] = $this->getHiddenUntil(); | |
194 | } | |
1b366958 AH |
195 | if (!empty($this->help)) { |
196 | $array['help'] = $this->help; | |
197 | } | |
198 | return $array; | |
a2600a6d | 199 | } |
96025800 | 200 | |
70599df6 | 201 | /** |
a82f003a | 202 | * Get message visibility. |
70599df6 | 203 | * |
204 | * @return bool | |
205 | */ | |
e918c16d | 206 | public function isVisible() { |
a82f003a CW |
207 | if (!isset($this->isVisible)) { |
208 | $this->isVisible = !$this->checkStatusPreference(); | |
209 | } | |
210 | return $this->isVisible; | |
7cf4759f CW |
211 | } |
212 | ||
213 | /** | |
a82f003a CW |
214 | * Get date hidden until. |
215 | * | |
7cf4759f CW |
216 | * @return string |
217 | */ | |
218 | public function getHiddenUntil() { | |
219 | if (!isset($this->hiddenUntil)) { | |
220 | $this->checkStatusPreference(); | |
221 | } | |
222 | return $this->hiddenUntil; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Check if message is visible or has been hidden by the user. | |
227 | * | |
a82f003a CW |
228 | * Also populates this->hiddenUntil property. |
229 | * | |
7cf4759f CW |
230 | * @return bool |
231 | * TRUE means hidden, FALSE means visible. | |
232 | * @throws \CiviCRM_API3_Exception | |
233 | */ | |
234 | private function checkStatusPreference() { | |
235 | $this->hiddenUntil = FALSE; | |
236 | $statusPreferenceParams = array( | |
237 | 'name' => $this->getName(), | |
238 | 'domain_id' => CRM_Core_Config::domainID(), | |
239 | ); | |
240 | // Check if there's a StatusPreference matching this name/domain. | |
241 | $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams); | |
242 | $spid = FALSE; | |
243 | if (isset($statusPreference['id'])) { | |
244 | $spid = $statusPreference['id']; | |
245 | } | |
246 | if ($spid) { | |
247 | // If so, compare severity to StatusPreference->severity. | |
fe8bdad8 | 248 | if ($this->level <= $statusPreference['values'][$spid]['ignore_severity']) { |
7cf4759f CW |
249 | // A hush or a snooze has been set. Find out which. |
250 | if (isset($statusPreference['values'][$spid]['hush_until'])) { | |
251 | // Snooze is set. | |
252 | $this->hiddenUntil = $statusPreference['values'][$spid]['hush_until']; | |
253 | $today = new DateTime(); | |
254 | $snoozeDate = new DateTime($statusPreference['values'][$spid]['hush_until']); | |
255 | if ($today > $snoozeDate) { | |
256 | // Snooze is expired. | |
257 | return FALSE; | |
258 | } | |
259 | else { | |
260 | // Snooze is active. | |
261 | return TRUE; | |
262 | } | |
263 | } | |
264 | else { | |
265 | // Hush. | |
266 | return TRUE; | |
267 | } | |
268 | } | |
269 | } | |
270 | return FALSE; | |
46a903fb NM |
271 | } |
272 | ||
a2600a6d | 273 | } |