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