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