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