Merge pull request #19225 from colemanw/select2Tweak
[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 {
18
19 /**
20 * Static holder for the default LT.
21 * @var int
22 */
23 public static $_defaultLocationType = NULL;
24 public static $_billingLocationType = NULL;
25
26 /**
27 * Class constructor.
28 */
29 public function __construct() {
30 parent::__construct();
31 }
32
33 /**
34 * Fetch object based on array of properties.
35 *
36 * @param array $params
37 * (reference ) an assoc array of name/value pairs.
38 * @param array $defaults
39 * (reference ) an assoc array to hold the flattened values.
40 *
41 * @return CRM_Core_BAO_LocaationType|null
42 * object on success, null otherwise
43 */
44 public static function retrieve(&$params, &$defaults) {
45 $locationType = new CRM_Core_DAO_LocationType();
46 $locationType->copyValues($params);
47 if ($locationType->find(TRUE)) {
48 CRM_Core_DAO::storeValues($locationType, $defaults);
49 return $locationType;
50 }
51 return NULL;
52 }
53
54 /**
55 * Update the is_active flag in the db.
56 *
57 * @param int $id
58 * Id of the database record.
59 * @param bool $is_active
60 * Value we want to set the is_active field.
61 *
62 * @return bool
63 * true if we found and updated the object, else false
64 */
65 public static function setIsActive($id, $is_active) {
66 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
67 }
68
69 /**
70 * Retrieve the default location_type.
71 *
72 * @return object
73 * The default location type object on success,
74 * null otherwise
75 */
76 public static function &getDefault() {
77 if (self::$_defaultLocationType == NULL) {
78 $params = ['is_default' => 1];
79 $defaults = [];
80 self::$_defaultLocationType = self::retrieve($params, $defaults);
81 }
82 return self::$_defaultLocationType;
83 }
84
85 /**
86 * Get ID of billing location type.
87 *
88 * @return int
89 */
90 public static function getBilling() {
91 if (self::$_billingLocationType == NULL) {
92 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', [], 'validate');
93 self::$_billingLocationType = array_search('Billing', $locationTypes);
94 }
95 return self::$_billingLocationType;
96 }
97
98 /**
99 * Add a Location Type.
100 *
101 * @param array $params
102 * Reference array contains the values submitted by the form.
103 *
104 *
105 * @return object
106 */
107 public static function create(&$params) {
108 if (empty($params['id'])) {
109 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
110 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
111 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
112 }
113
114 $locationType = new CRM_Core_DAO_LocationType();
115 $locationType->copyValues($params);
116 if (!empty($params['is_default'])) {
117 $query = "UPDATE civicrm_location_type SET is_default = 0";
118 CRM_Core_DAO::executeQuery($query);
119 }
120
121 $locationType->save();
122 return $locationType;
123 }
124
125 /**
126 * Delete location Types.
127 *
128 * @param int $locationTypeId
129 * ID of the location type to be deleted.
130 *
131 */
132 public static function del($locationTypeId) {
133 $entity = ['address', 'phone', 'email', 'im'];
134 //check dependencies
135 foreach ($entity as $key) {
136 if ($key == 'im') {
137 $name = strtoupper($key);
138 }
139 else {
140 $name = ucfirst($key);
141 }
142 $baoString = 'CRM_Core_BAO_' . $name;
143 $object = new $baoString();
144 $object->location_type_id = $locationTypeId;
145 $object->delete();
146 }
147
148 $locationType = new CRM_Core_DAO_LocationType();
149 $locationType->id = $locationTypeId;
150 $locationType->delete();
151 }
152
153 }