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