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\Test\HookInterface
{
20 * @var CRM_Core_DAO_LocationType|null
22 public static $_defaultLocationType = NULL;
27 public static $_billingLocationType = NULL;
30 * Fetch object based on array of properties.
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.
37 * @return CRM_Core_DAO_LocationType|null
38 * object on success, null otherwise
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);
51 * Update the is_active flag in the db.
54 * Id of the database record.
55 * @param bool $is_active
56 * Value we want to set the is_active field.
59 * true if we found and updated the object, else false
61 public static function setIsActive($id, $is_active) {
62 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
66 * Retrieve the default location_type.
68 * @return CRM_Core_DAO_LocationType|null
69 * The default location type object on success,
72 public static function &getDefault() {
73 if (self
::$_defaultLocationType == NULL) {
74 $params = ['is_default' => 1];
76 self
::$_defaultLocationType = self
::retrieve($params, $defaults);
78 return self
::$_defaultLocationType;
82 * Get ID of billing location type.
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);
91 return self
::$_billingLocationType;
95 * Add a Location Type.
97 * @param array $params
98 * Reference array contains the values submitted by the form.
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);
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);
117 $locationType->save();
118 return $locationType;
122 * Delete location Types.
124 * @param int $locationTypeId
127 public static function del($locationTypeId) {
128 static::deleteRecord(['id' => $locationTypeId]);
132 * Callback for hook_civicrm_pre().
133 * @param \Civi\Core\Event\PreEvent $event
134 * @throws CRM_Core_Exception
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
]],