Merge pull request #23939 from civicrm/5.51
[civicrm-core.git] / CRM / Core / BAO / LocationType.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 */
17 class CRM_Core_BAO_LocationType extends CRM_Core_DAO_LocationType implements \Civi\Core\HookInterface {
18
19 /**
20 * @var CRM_Core_DAO_LocationType|null
21 */
22 public static $_defaultLocationType = NULL;
23
24 /**
25 * @var int|null
26 */
27 public static $_billingLocationType = NULL;
28
29 /**
30 * Retrieve DB object and copy to defaults array.
31 *
32 * @param array $params
33 * Array of criteria values.
34 * @param array $defaults
35 * Array to be populated with found values.
36 *
37 * @return self|null
38 * The DAO object, if found.
39 *
40 * @deprecated
41 */
42 public static function retrieve($params, &$defaults) {
43 return self::commonRetrieve(self::class, $params, $defaults);
44 }
45
46 /**
47 * Update the is_active flag in the db.
48 *
49 * @param int $id
50 * Id of the database record.
51 * @param bool $is_active
52 * Value we want to set the is_active field.
53 *
54 * @return bool
55 * true if we found and updated the object, else false
56 */
57 public static function setIsActive($id, $is_active) {
58 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
59 }
60
61 /**
62 * Retrieve the default location_type.
63 *
64 * @return CRM_Core_DAO_LocationType|null
65 * The default location type object on success,
66 * null otherwise
67 */
68 public static function &getDefault() {
69 if (self::$_defaultLocationType == NULL) {
70 $params = ['is_default' => 1];
71 $defaults = [];
72 self::$_defaultLocationType = self::retrieve($params, $defaults);
73 }
74 return self::$_defaultLocationType;
75 }
76
77 /**
78 * Get ID of billing location type.
79 *
80 * @return int
81 */
82 public static function getBilling() {
83 if (self::$_billingLocationType == NULL) {
84 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', [], 'validate');
85 self::$_billingLocationType = array_search('Billing', $locationTypes);
86 }
87 return self::$_billingLocationType;
88 }
89
90 /**
91 * Add a Location Type.
92 *
93 * @param array $params
94 * Reference array contains the values submitted by the form.
95 *
96 *
97 * @return object
98 */
99 public static function create(&$params) {
100 if (empty($params['id'])) {
101 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
102 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
103 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
104 }
105
106 $locationType = new CRM_Core_DAO_LocationType();
107 $locationType->copyValues($params);
108 if (!empty($params['is_default'])) {
109 $query = "UPDATE civicrm_location_type SET is_default = 0";
110 CRM_Core_DAO::executeQuery($query);
111 }
112
113 $locationType->save();
114 return $locationType;
115 }
116
117 /**
118 * Delete location Types.
119 *
120 * @param int $locationTypeId
121 * @deprecated
122 */
123 public static function del($locationTypeId) {
124 static::deleteRecord(['id' => $locationTypeId]);
125 }
126
127 /**
128 * Callback for hook_civicrm_pre().
129 * @param \Civi\Core\Event\PreEvent $event
130 * @throws CRM_Core_Exception
131 */
132 public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
133 // When deleting a location type, delete related records
134 if ($event->action === 'delete') {
135 foreach (['Address', 'IM', 'Email', 'Phone'] as $entity) {
136 civicrm_api4($entity, 'delete', [
137 'checkPermissions' => FALSE,
138 'where' => [['location_type_id', '=', $event->id]],
139 ]);
140 }
141 }
142 }
143
144 }