Merge pull request #12053 from civicrm/5.1
[civicrm-core.git] / CRM / Core / BAO / StatusPreference.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 * $Id$
33 *
34 */
35
36 /**
37 * This class contains functions for managing Status Preferences.
38 */
39 class CRM_Core_BAO_StatusPreference extends CRM_Core_DAO_StatusPreference {
40
41 /**
42 * Create or update a Status Preference entry.
43 *
44 * @param array $params
45 *
46 * @return array
47 */
48 public static function create($params) {
49 $statusPreference = new CRM_Core_BAO_StatusPreference();
50
51 // Default severity level to ignore is 0 (DEBUG).
52 if (!isset($params['ignore_severity'])) {
53 $params['ignore_severity'] = 0;
54 }
55 // Severity can be either text ('critical') or an integer <= 7.
56 // It's a magic number, but based on PSR-3 standards.
57 if (!CRM_Utils_Rule::integer($params['ignore_severity'])) {
58 $params['ignore_severity'] = CRM_Utils_Check::severityMap($params['ignore_severity']);
59 }
60 if ($params['ignore_severity'] > 7) {
61 CRM_Core_Error::fatal(ts('You can not pass a severity level higher than 7.'));
62 }
63 // If severity is now blank, you have an invalid severity string.
64 if (is_null($params['ignore_severity'])) {
65 CRM_Core_Error::fatal(ts('Invalid string passed as severity level.'));
66 }
67
68 // Check if this StatusPreference already exists.
69 if (empty($params['id']) && CRM_Utils_Array::value('name', $params)) {
70 $statusPreference->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
71 $statusPreference->name = $params['name'];
72
73 $statusPreference->find(TRUE);
74 }
75
76 $statusPreference->copyValues($params);
77
78 $edit = ($statusPreference->id) ? TRUE : FALSE;
79 if ($edit) {
80 CRM_Utils_Hook::pre('edit', 'StatusPreference', $statusPreference->id, $statusPreference);
81 }
82 else {
83 CRM_Utils_Hook::pre('create', 'StatusPreference', NULL, $statusPreference);
84 }
85
86 $statusPreference->save();
87
88 if ($edit) {
89 CRM_Utils_Hook::post('edit', 'StatusPreference', $statusPreference->id, $statusPreference);
90 }
91 else {
92 CRM_Utils_Hook::post('create', 'StatusPreference', NULL, $statusPreference);
93 }
94
95 return $statusPreference;
96 }
97
98 }