Merge pull request #17253 from mattwire/utf8convertblocksize
[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 * @throws CRM_Core_Exception
32 */
33 public static function create($params) {
34 $statusPreference = new CRM_Core_BAO_StatusPreference();
35
36 // Default severity level to ignore is 0 (DEBUG).
37 if (!isset($params['ignore_severity'])) {
38 $params['ignore_severity'] = 0;
39 }
40 // Severity can be either text ('critical') or an integer <= 7.
41 // It's a magic number, but based on PSR-3 standards.
42 if (!CRM_Utils_Rule::integer($params['ignore_severity'])) {
43 $params['ignore_severity'] = CRM_Utils_Check::severityMap($params['ignore_severity']);
44 }
45 if ($params['ignore_severity'] > 7) {
46 throw new CRM_Core_Exception(ts('You can not pass a severity level higher than 7.'));
47 }
48 // If severity is now blank, you have an invalid severity string.
49 if (is_null($params['ignore_severity'])) {
50 throw new CRM_Core_Exception(ts('Invalid string passed as severity level.'));
51 }
52
53 // Check if this StatusPreference already exists.
54 if (empty($params['id']) && !empty($params['name'])) {
55 $statusPreference->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
56 $statusPreference->name = $params['name'];
57
58 $statusPreference->find(TRUE);
59 }
60
61 $statusPreference->copyValues($params);
62
63 $edit = (bool) $statusPreference->id;
64 if ($edit) {
65 CRM_Utils_Hook::pre('edit', 'StatusPreference', $statusPreference->id, $statusPreference);
66 }
67 else {
68 CRM_Utils_Hook::pre('create', 'StatusPreference', NULL, $statusPreference);
69 }
70
71 $statusPreference->save();
72
73 if ($edit) {
74 CRM_Utils_Hook::post('edit', 'StatusPreference', $statusPreference->id, $statusPreference);
75 }
76 else {
77 CRM_Utils_Hook::post('create', 'StatusPreference', NULL, $statusPreference);
78 }
79
80 return $statusPreference;
81 }
82
83 }