f289b2056ad6d0adb7a69241c90c58bad808364d
[civicrm-core.git] / CRM / Core / BAO / LocationType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 function __construct() {
47 parent::__construct();
48 }
49
50 /**
51 * Takes a bunch of params that are needed to match certain criteria and
52 * retrieves the relevant objects. Typically the valid params are only
53 * contact_id. We'll tweak this function to be more full featured over a period
54 * of time. This is the inverse function of create. It also stores all the retrieved
55 * values in the default array
56 *
57 * @param array $params (reference ) an assoc array of name/value pairs
58 * @param array $defaults (reference ) an assoc array to hold the flattened values
59 *
60 * @return object CRM_Core_BAO_LocaationType object on success, null otherwise
61 * @access public
62 * @static
63 */
64 static function retrieve(&$params, &$defaults) {
65 $locationType = new CRM_Core_DAO_LocationType();
66 $locationType->copyValues($params);
67 if ($locationType->find(TRUE)) {
68 CRM_Core_DAO::storeValues($locationType, $defaults);
69 return $locationType;
70 }
71 return NULL;
72 }
73
74 /**
75 * update the is_active flag in the db
76 *
77 * @param int $id id of the database record
78 * @param boolean $is_active value we want to set the is_active field
79 *
80 * @return Object DAO object on sucess, null otherwise
81 *
82 * @access public
83 * @static
84 */
85 static function setIsActive($id, $is_active) {
86 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
87 }
88
89 /**
90 * retrieve the default location_type
91 *
92 * @param NULL
93 *
94 * @return object The default location type object on success,
95 * null otherwise
96 * @static
97 * @access public
98 */
99 static function &getDefault() {
100 if (self::$_defaultLocationType == NULL) {
101 $params = array('is_default' => 1);
102 $defaults = array();
103 self::$_defaultLocationType = self::retrieve($params, $defaults);
104 }
105 return self::$_defaultLocationType;
106 }
107
108 /*
109 * Get ID of billing location type
110 * @return integer
111 */
112 static function getBilling() {
113 if (self::$_billingLocationType == NULL) {
114 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
115 self::$_billingLocationType = array_search('Billing', $locationTypes);
116 }
117 return self::$_billingLocationType;
118 }
119
120 /**
121 * Function to add a Location Type
122 *
123 * @param array $params reference array contains the values submitted by the form
124 * @param array $ids reference array contains the id
125 *
126 * @access public
127 * @static
128 *
129 * @return object
130 */
131 static function create(&$params) {
132 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
133 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
134 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
135
136 // action is taken depending upon the mode
137 $locationType = new CRM_Core_DAO_LocationType();
138 $locationType->copyValues($params);
139
140 if ($params['is_default']) {
141 $query = "UPDATE civicrm_location_type SET is_default = 0";
142 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
143 }
144
145 $locationType->save();
146 return $locationType;
147 }
148
149 /**
150 * Function to delete location Types
151 *
152 * @param int $locationTypeId ID of the location type to be deleted.
153 *
154 * @access public
155 * @static
156 */
157 static function del($locationTypeId) {
158 $entity = array('address', 'phone', 'email', 'im');
159 //check dependencies
160 foreach ($entity as $key) {
161 if ($key == 'im') {
162 $name = strtoupper($key);
163 }
164 else {
165 $name = ucfirst($key);
166 }
167 $baoString = 'CRM_Core_BAO_' . $name;
168 $object = new $baoString();
169 $object->location_type_id = $locationTypeId;
170 $object->delete();
171 }
172
173 $locationType = new CRM_Core_DAO_LocationType();
174 $locationType->id = $locationTypeId;
175 $locationType->delete();
176 }
177 }
178