3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Core_BAO_LocationType
extends CRM_Core_DAO_LocationType
implements \Civi\Core\HookInterface
{
20 * @var CRM_Core_DAO_LocationType|null
22 public static $_defaultLocationType = NULL;
27 public static $_billingLocationType = NULL;
30 * Retrieve DB object and copy to defaults array.
32 * @param array $params
33 * Array of criteria values.
34 * @param array $defaults
35 * Array to be populated with found values.
38 * The DAO object, if found.
42 public static function retrieve($params, &$defaults) {
43 return self
::commonRetrieve(self
::class, $params, $defaults);
47 * Update the is_active flag in the db.
50 * Id of the database record.
51 * @param bool $is_active
52 * Value we want to set the is_active field.
55 * true if we found and updated the object, else false
57 public static function setIsActive($id, $is_active) {
58 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
62 * Retrieve the default location_type.
64 * @return CRM_Core_DAO_LocationType|null
65 * The default location type object on success,
68 public static function &getDefault() {
69 if (self
::$_defaultLocationType == NULL) {
70 $params = ['is_default' => 1];
72 self
::$_defaultLocationType = self
::retrieve($params, $defaults);
74 return self
::$_defaultLocationType;
78 * Get ID of billing location type.
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);
87 return self
::$_billingLocationType;
91 * Add a Location Type.
93 * @param array $params
94 * Reference array contains the values submitted by the form.
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);
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);
113 $locationType->save();
114 return $locationType;
118 * Delete location Types.
120 * @param int $locationTypeId
123 public static function del($locationTypeId) {
124 static::deleteRecord(['id' => $locationTypeId]);
128 * Callback for hook_civicrm_pre().
129 * @param \Civi\Core\Event\PreEvent $event
130 * @throws CRM_Core_Exception
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
]],