a few comment fixes
[civicrm-core.git] / CRM / Core / BAO / LocationType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_BAO_LocationType extends CRM_Core_DAO_LocationType {
36
37 /**
100fef9d 38 * Static holder for the default LT
6a488035
TO
39 */
40 static $_defaultLocationType = NULL;
41 static $_billingLocationType = NULL;
42
43 /**
100fef9d 44 * Class constructor
6a488035 45 */
00be9182 46 public function __construct() {
6a488035
TO
47 parent::__construct();
48 }
49
50 /**
c490a46a 51 * Fetch object based on array of properties
6a488035 52 *
6a0b768e
TO
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.
6a488035 57 *
16b10e64
CW
58 * @return CRM_Core_BAO_LocaationType|null
59 * object on success, null otherwise
6a488035 60 */
00be9182 61 public static function retrieve(&$params, &$defaults) {
6a488035
TO
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 /**
100fef9d 72 * Update the is_active flag in the db
6a488035 73 *
6a0b768e
TO
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.
6a488035 78 *
a6c01b45
CW
79 * @return Object
80 * DAO object on sucess, null otherwise
6a488035 81 *
6a488035 82 */
00be9182 83 public static function setIsActive($id, $is_active) {
6a488035
TO
84 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
85 }
86
87 /**
100fef9d 88 * Retrieve the default location_type
6a488035 89 *
a6c01b45
CW
90 * @return object
91 * The default location type object on success,
6a488035 92 * null otherwise
6a488035 93 */
00be9182 94 public static function &getDefault() {
6a488035
TO
95 if (self::$_defaultLocationType == NULL) {
96 $params = array('is_default' => 1);
97 $defaults = array();
98 self::$_defaultLocationType = self::retrieve($params, $defaults);
99 }
100 return self::$_defaultLocationType;
101 }
102
d424ffde 103 /**
6a488035 104 * Get ID of billing location type
d424ffde 105 *
df8d3074 106 * @return int
6a488035 107 */
00be9182 108 public static function getBilling() {
6a488035 109 if (self::$_billingLocationType == NULL) {
180409a4 110 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
6a488035
TO
111 self::$_billingLocationType = array_search('Billing', $locationTypes);
112 }
113 return self::$_billingLocationType;
114 }
115
116 /**
100fef9d 117 * Add a Location Type
6a488035 118 *
6a0b768e
TO
119 * @param array $params
120 * Reference array contains the values submitted by the form.
dd244018 121 *
6a488035
TO
122 *
123 * @return object
124 */
00be9182 125 public static function create(&$params) {
6a488035
TO
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 // action is taken depending upon the mode
131 $locationType = new CRM_Core_DAO_LocationType();
132 $locationType->copyValues($params);
133
134 if ($params['is_default']) {
135 $query = "UPDATE civicrm_location_type SET is_default = 0";
136 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
137 }
138
139 $locationType->save();
140 return $locationType;
141 }
142
143 /**
100fef9d 144 * Delete location Types
6a488035 145 *
6a0b768e
TO
146 * @param int $locationTypeId
147 * ID of the location type to be deleted.
6a488035 148 *
6a488035 149 */
00be9182 150 public static function del($locationTypeId) {
6a488035
TO
151 $entity = array('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 }
4d5c2eb5 160 $baoString = 'CRM_Core_BAO_' . $name;
161 $object = new $baoString();
6a488035
TO
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 }
96025800 170
6a488035 171}