Merge pull request #23210 from eileenmcnaughton/cancel
[civicrm-core.git] / CRM / Core / BAO / LocationType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035 16 */
ee44263e 17class CRM_Core_BAO_LocationType extends CRM_Core_DAO_LocationType implements \Civi\Core\HookInterface {
6a488035
TO
18
19 /**
6707a0d7 20 * @var CRM_Core_DAO_LocationType|null
6a488035 21 */
518fa0ee 22 public static $_defaultLocationType = NULL;
6707a0d7
BT
23
24 /**
25 * @var int|null
26 */
518fa0ee 27 public static $_billingLocationType = NULL;
6a488035 28
6a488035 29 /**
4f940304 30 * Retrieve DB object and copy to defaults array.
6a488035 31 *
6a0b768e 32 * @param array $params
4f940304 33 * Array of criteria values.
6a0b768e 34 * @param array $defaults
4f940304 35 * Array to be populated with found values.
6a488035 36 *
4f940304
CW
37 * @return self|null
38 * The DAO object, if found.
39 *
40 * @deprecated
6a488035 41 */
4f940304
CW
42 public static function retrieve($params, &$defaults) {
43 return self::commonRetrieve(self::class, $params, $defaults);
6a488035
TO
44 }
45
46 /**
fe482240 47 * Update the is_active flag in the db.
6a488035 48 *
6a0b768e
TO
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.
6a488035 53 *
8a4fede3 54 * @return bool
55 * true if we found and updated the object, else false
6a488035 56 */
00be9182 57 public static function setIsActive($id, $is_active) {
6a488035
TO
58 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
59 }
60
61 /**
fe482240 62 * Retrieve the default location_type.
6a488035 63 *
3fd42bb5 64 * @return CRM_Core_DAO_LocationType|null
a6c01b45 65 * The default location type object on success,
6a488035 66 * null otherwise
6a488035 67 */
00be9182 68 public static function &getDefault() {
6a488035 69 if (self::$_defaultLocationType == NULL) {
be2fb01f
CW
70 $params = ['is_default' => 1];
71 $defaults = [];
6a488035
TO
72 self::$_defaultLocationType = self::retrieve($params, $defaults);
73 }
74 return self::$_defaultLocationType;
75 }
76
d424ffde 77 /**
fe482240 78 * Get ID of billing location type.
d424ffde 79 *
df8d3074 80 * @return int
6a488035 81 */
00be9182 82 public static function getBilling() {
6a488035 83 if (self::$_billingLocationType == NULL) {
be2fb01f 84 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', [], 'validate');
6a488035
TO
85 self::$_billingLocationType = array_search('Billing', $locationTypes);
86 }
87 return self::$_billingLocationType;
88 }
89
90 /**
fe482240 91 * Add a Location Type.
6a488035 92 *
6a0b768e
TO
93 * @param array $params
94 * Reference array contains the values submitted by the form.
dd244018 95 *
6a488035
TO
96 *
97 * @return object
98 */
00be9182 99 public static function create(&$params) {
9859f345 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 }
6a488035 105
6a488035
TO
106 $locationType = new CRM_Core_DAO_LocationType();
107 $locationType->copyValues($params);
9859f345 108 if (!empty($params['is_default'])) {
6a488035 109 $query = "UPDATE civicrm_location_type SET is_default = 0";
33621c4f 110 CRM_Core_DAO::executeQuery($query);
6a488035
TO
111 }
112
113 $locationType->save();
114 return $locationType;
115 }
116
117 /**
fe482240 118 * Delete location Types.
6a488035 119 *
6a0b768e 120 * @param int $locationTypeId
47babea7 121 * @deprecated
6a488035 122 */
00be9182 123 public static function del($locationTypeId) {
47babea7
CW
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 ]);
6a488035 140 }
6a488035 141 }
6a488035 142 }
96025800 143
6a488035 144}