Merge pull request #16853 from colemanw/bool3
[civicrm-core.git] / CRM / Core / BAO / StatusPreference.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class contains functions for managing Status Preferences.
22 */
23 class CRM_Core_BAO_StatusPreference extends CRM_Core_DAO_StatusPreference {
24
25 /**
26 * Create or update a Status Preference entry.
27 *
28 * @param array $params
29 *
30 * @return array
31 */
32 public static function create($params) {
33 $statusPreference = new CRM_Core_BAO_StatusPreference();
34
35 // Default severity level to ignore is 0 (DEBUG).
36 if (!isset($params['ignore_severity'])) {
37 $params['ignore_severity'] = 0;
38 }
39 // Severity can be either text ('critical') or an integer <= 7.
40 // It's a magic number, but based on PSR-3 standards.
41 if (!CRM_Utils_Rule::integer($params['ignore_severity'])) {
42 $params['ignore_severity'] = CRM_Utils_Check::severityMap($params['ignore_severity']);
43 }
44 if ($params['ignore_severity'] > 7) {
45 CRM_Core_Error::fatal(ts('You can not pass a severity level higher than 7.'));
46 }
47 // If severity is now blank, you have an invalid severity string.
48 if (is_null($params['ignore_severity'])) {
49 CRM_Core_Error::fatal(ts('Invalid string passed as severity level.'));
50 }
51
52 // Check if this StatusPreference already exists.
53 if (empty($params['id']) && !empty($params['name'])) {
54 $statusPreference->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
55 $statusPreference->name = $params['name'];
56
57 $statusPreference->find(TRUE);
58 }
59
60 $statusPreference->copyValues($params);
61
62 $edit = (bool) $statusPreference->id;
63 if ($edit) {
64 CRM_Utils_Hook::pre('edit', 'StatusPreference', $statusPreference->id, $statusPreference);
65 }
66 else {
67 CRM_Utils_Hook::pre('create', 'StatusPreference', NULL, $statusPreference);
68 }
69
70 $statusPreference->save();
71
72 if ($edit) {
73 CRM_Utils_Hook::post('edit', 'StatusPreference', $statusPreference->id, $statusPreference);
74 }
75 else {
76 CRM_Utils_Hook::post('create', 'StatusPreference', NULL, $statusPreference);
77 }
78
79 return $statusPreference;
80 }
81
82 }