3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Utils_Check_Message
{
51 * @see Psr\Log\LogLevel
57 * help text (to be presented separately from the message)
63 * actions which can be performed with this message
65 private $actions = [];
75 * Has this message been suppressed?
81 * Date this message is hidden until
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.
98 * @see Psr\Log\LogLevel
101 public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel
::WARNING
, $icon = NULL) {
103 $this->message
= $message;
104 $this->title
= $title;
106 $this->setLevel($level);
114 public function getName() {
123 public function getMessage() {
124 return $this->message
;
130 public function getTitle() {
135 * Get severity level number.
138 * @see Psr\Log\LogLevel
140 public function getLevel() {
145 * Get severity string.
148 * @see Psr\Log\LogLevel
150 public function getSeverity() {
151 return CRM_Utils_Check
::severityMap($this->level
, TRUE);
155 * Set optional additional help text.
157 * @param string $help
159 public function addHelp($help) {
164 * Set optional additional actions text.
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
175 public function addAction($title, $confirmation, $type, $params) {
178 'confirm' => $confirmation,
187 * @param string|int $level
188 * @throws \CRM_Core_Exception
190 public function setLevel($level) {
191 // Convert level to integer
192 if (!CRM_Utils_Rule
::positiveInteger($level)) {
193 $level = CRM_Utils_Check
::severityMap($level);
196 // Validate numeric input - this will throw an exception if invalid
197 CRM_Utils_Check
::severityMap($level, TRUE);
199 $this->level
= $level;
200 // Clear internal caches
201 unset($this->isVisible
, $this->hiddenUntil
);
209 public function toArray() {
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
,
219 if ($this->getHiddenUntil()) {
220 $array['hidden_until'] = $this->getHiddenUntil();
222 if (!empty($this->help
)) {
223 $array['help'] = $this->help
;
225 if (!empty($this->actions
)) {
226 $array['actions'] = $this->actions
;
232 * Get message visibility.
236 public function isVisible() {
237 if (!isset($this->isVisible
)) {
238 $this->isVisible
= !$this->checkStatusPreference();
240 return $this->isVisible
;
244 * Get date hidden until.
248 public function getHiddenUntil() {
249 if (!isset($this->hiddenUntil
)) {
250 $this->checkStatusPreference();
252 return $this->hiddenUntil
;
256 * Check if message has been hidden by the user.
258 * Also populates this->hiddenUntil property.
261 * TRUE means hidden, FALSE means visible.
262 * @throws \CiviCRM_API3_Exception
264 private function checkStatusPreference() {
265 $this->hiddenUntil
= FALSE;
266 // Debug & info can't be hidden
267 if ($this->level
< 2) {
270 $statusPreferenceParams = [
271 'name' => $this->getName(),
272 'domain_id' => CRM_Core_Config
::domainID(),
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, []);
279 // If so, compare severity to StatusPreference->severity.
280 if ($this->level
<= $prefs[0]['ignore_severity']) {
281 if (isset($prefs[0]['hush_until'])) {
283 $this->hiddenUntil
= $prefs[0]['hush_until'];
284 $today = new DateTime();
285 $snoozeDate = new DateTime($prefs[0]['hush_until']);
286 return !($today > $snoozeDate);
289 // Hidden indefinitely.