Merge pull request #14222 from mfb/debug-var
[civicrm-core.git] / CRM / Core / BAO / LocationType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id$
33 *
34 */
35 class CRM_Core_BAO_LocationType extends CRM_Core_DAO_LocationType {
36
37 /**
38 * Static holder for the default LT.
39 * @var int
40 */
41 public static $_defaultLocationType = NULL;
42 public static $_billingLocationType = NULL;
43
44 /**
45 * Class constructor.
46 */
47 public function __construct() {
48 parent::__construct();
49 }
50
51 /**
52 * Fetch object based on array of properties.
53 *
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $defaults
57 * (reference ) an assoc array to hold the flattened values.
58 *
59 * @return CRM_Core_BAO_LocaationType|null
60 * object on success, null otherwise
61 */
62 public static function retrieve(&$params, &$defaults) {
63 $locationType = new CRM_Core_DAO_LocationType();
64 $locationType->copyValues($params);
65 if ($locationType->find(TRUE)) {
66 CRM_Core_DAO::storeValues($locationType, $defaults);
67 return $locationType;
68 }
69 return NULL;
70 }
71
72 /**
73 * Update the is_active flag in the db.
74 *
75 * @param int $id
76 * Id of the database record.
77 * @param bool $is_active
78 * Value we want to set the is_active field.
79 *
80 * @return bool
81 * true if we found and updated the object, else false
82 */
83 public static function setIsActive($id, $is_active) {
84 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
85 }
86
87 /**
88 * Retrieve the default location_type.
89 *
90 * @return object
91 * The default location type object on success,
92 * null otherwise
93 */
94 public static function &getDefault() {
95 if (self::$_defaultLocationType == NULL) {
96 $params = ['is_default' => 1];
97 $defaults = [];
98 self::$_defaultLocationType = self::retrieve($params, $defaults);
99 }
100 return self::$_defaultLocationType;
101 }
102
103 /**
104 * Get ID of billing location type.
105 *
106 * @return int
107 */
108 public static function getBilling() {
109 if (self::$_billingLocationType == NULL) {
110 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', [], 'validate');
111 self::$_billingLocationType = array_search('Billing', $locationTypes);
112 }
113 return self::$_billingLocationType;
114 }
115
116 /**
117 * Add a Location Type.
118 *
119 * @param array $params
120 * Reference array contains the values submitted by the form.
121 *
122 *
123 * @return object
124 */
125 public static function create(&$params) {
126 if (empty($params['id'])) {
127 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
128 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
129 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
130 }
131
132 $locationType = new CRM_Core_DAO_LocationType();
133 $locationType->copyValues($params);
134 if (!empty($params['is_default'])) {
135 $query = "UPDATE civicrm_location_type SET is_default = 0";
136 CRM_Core_DAO::executeQuery($query);
137 }
138
139 $locationType->save();
140 return $locationType;
141 }
142
143 /**
144 * Delete location Types.
145 *
146 * @param int $locationTypeId
147 * ID of the location type to be deleted.
148 *
149 */
150 public static function del($locationTypeId) {
151 $entity = ['address', 'phone', 'email', 'im'];
152 //check dependencies
153 foreach ($entity as $key) {
154 if ($key == 'im') {
155 $name = strtoupper($key);
156 }
157 else {
158 $name = ucfirst($key);
159 }
160 $baoString = 'CRM_Core_BAO_' . $name;
161 $object = new $baoString();
162 $object->location_type_id = $locationTypeId;
163 $object->delete();
164 }
165
166 $locationType = new CRM_Core_DAO_LocationType();
167 $locationType->id = $locationTypeId;
168 $locationType->delete();
169 }
170
171 }