Merge pull request #18997 from eileenmcnaughton/trans
[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 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * This class contains functions for managing Status Preferences.
19 */
20 class CRM_Core_BAO_StatusPreference extends CRM_Core_DAO_StatusPreference {
21
22 /**
23 * Create or update a Status Preference entry.
24 *
25 * @param array $params
26 *
27 * @return CRM_Core_DAO_StatusPreference
28 * @throws CRM_Core_Exception
29 */
30 public static function create($params) {
31 $statusPreference = new CRM_Core_DAO_StatusPreference();
32
33 // Default severity level to ignore is 0 (DEBUG).
34 $params['ignore_severity'] = $params['ignore_severity'] ?? 0;
35
36 // Severity can be either text ('critical') or an integer <= 7.
37 // It's a magic number, but based on PSR-3 standards.
38 if (!CRM_Utils_Rule::integer($params['ignore_severity'])) {
39 $params['ignore_severity'] = CRM_Utils_Check::severityMap($params['ignore_severity']);
40 }
41 if ($params['ignore_severity'] > 7) {
42 throw new CRM_Core_Exception(ts('You can not pass a severity level higher than 7.'));
43 }
44 // If severity is now blank, you have an invalid severity string.
45 if (is_null($params['ignore_severity'])) {
46 throw new CRM_Core_Exception(ts('Invalid string passed as severity level.'));
47 }
48
49 // Set default domain when creating (or updating by name)
50 if (empty($params['id']) && empty($params['domain_id'])) {
51 $params['domain_id'] = CRM_Core_Config::domainID();
52 }
53
54 // Enforce unique status pref names. Update if a duplicate name is found in the same domain.
55 if (empty($params['id']) && !empty($params['name'])) {
56 $statusPreference->domain_id = $params['domain_id'];
57 $statusPreference->name = $params['name'];
58 $statusPreference->find(TRUE);
59 }
60
61 $op = $statusPreference->id ? 'edit' : 'create';
62 CRM_Utils_Hook::pre($op, 'StatusPreference', $statusPreference->id, $params);
63
64 $statusPreference->copyValues($params);
65 $statusPreference->save();
66
67 CRM_Utils_Hook::post($op, 'StatusPreference', $statusPreference->id, $statusPreference);
68
69 return $statusPreference;
70 }
71
72 }