Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-05-23-28-33
[civicrm-core.git] / CRM / Core / BAO / CustomOption.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
36 /**
37 * Business objects for managing custom data options.
38 *
39 */
40 class CRM_Core_BAO_CustomOption {
41
42 /**
43 * Fetch object based on array of properties
44 *
45 * @param array $params (reference ) an assoc array of name/value pairs
46 * @param array $defaults (reference ) an assoc array to hold the flattened values
47 *
48 * @return CRM_Core_BAO_CustomOption object
49 * @static
50 */
51 public static function retrieve(&$params, &$defaults) {
52 $customOption = new CRM_Core_DAO_OptionValue();
53 $customOption->copyValues($params);
54 if ($customOption->find(TRUE)) {
55 CRM_Core_DAO::storeValues($customOption, $defaults);
56 return $customOption;
57 }
58 return NULL;
59 }
60
61 /**
62 * Returns all active options ordered by weight for a given field
63 *
64 * @param int $fieldID field whose options are needed
65 * @param boolean $inactiveNeeded do we need inactive options ?
66 *
67 * @return array $customOption all active options for fieldId
68 * @static
69 */
70 static function getCustomOption(
71 $fieldID,
72 $inactiveNeeded = FALSE
73 ) {
74 $options = array();
75 if (!$fieldID) {
76 return $options;
77 }
78
79 $field = CRM_Core_BAO_CustomField::getFieldObject($fieldID);
80
81 // get the option group id
82 $optionGroupID = $field->option_group_id;
83 if (!$optionGroupID) {
84 return $options;
85 }
86
87 $optionValues = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroupID);
88
89 foreach ($optionValues as $id => $value) {
90 if (!$inactiveNeeded && empty($value['is_active'])) {
91 continue;
92 }
93
94 $options[$id] = array();
95 $options[$id]['id'] = $id;
96 $options[$id]['label'] = $value['label'];
97 $options[$id]['value'] = $value['value'];
98 }
99
100 CRM_Utils_Hook::customFieldOptions($fieldID, $options, TRUE);
101
102 return $options;
103 }
104
105 /**
106 * Returns the option label for a custom field with a specific value. Handles all
107 * custom field data and html types
108 *
109 * @param $fieldId int the custom field ID
110 * @pram $value string the value (typically from the DB) of this custom field
111 * @param $value
112 * @param $htmlType string the html type of the field (optional)
113 * @param $dataType string the data type of the field (optional)
114 *
115 * @return string the label to display for this custom field
116 * @static
117 */
118 public static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) {
119 if (!$fieldId) {
120 return NULL;
121 }
122
123 if (!$htmlType || !$dataType) {
124 $sql = "
125 SELECT html_type, data_type
126 FROM civicrm_custom_field
127 WHERE id = %1
128 ";
129 $params = array(1 => array($fieldId, 'Integer'));
130 $dao = CRM_Core_DAO::executeQuery($sql, $params);
131 if ($dao->fetch()) {
132 $htmlType = $dao->html_type;
133 $dataType = $dao->data_type;
134 }
135 else {
136 CRM_Core_Error::fatal();
137 }
138 }
139
140 $options = NULL;
141 switch ($htmlType) {
142 case 'CheckBox':
143 case 'Multi-Select':
144 case 'AdvMulti-Select':
145 case 'Select':
146 case 'Radio':
147 case 'Autocomplete-Select':
148 if (!in_array($dataType, array(
149 'Boolean', 'ContactReference'))) {
150 $options = self::valuesByID($fieldId);
151 }
152 }
153
154 return CRM_Core_BAO_CustomField::getDisplayValueCommon($value,
155 $options,
156 $htmlType,
157 $dataType
158 );
159 }
160
161 /**
162 * Delete Option
163 *
164 * param $optionId integer option id
165 *
166 * @static
167 */
168 public static function del($optionId) {
169 // get the customFieldID
170 $query = "
171 SELECT f.id as id, f.data_type as dataType
172 FROM civicrm_option_value v,
173 civicrm_option_group g,
174 civicrm_custom_field f
175 WHERE v.id = %1
176 AND g.id = f.option_group_id
177 AND g.id = v.option_group_id";
178 $params = array(1 => array($optionId, 'Integer'));
179 $dao = CRM_Core_DAO::executeQuery($query, $params);
180 if ($dao->fetch()) {
181 if (in_array($dao->dataType,
182 array('Int', 'Float', 'Money', 'Boolean')
183 )) {
184 $value = 0;
185 }
186 else {
187 $value = '';
188 }
189 $params = array(
190 'optionId' => $optionId,
191 'fieldId' => $dao->id,
192 'value' => $value,
193 );
194 // delete this value from the tables
195 self::updateCustomValues($params);
196
197 // also delete this option value
198 $query = "
199 DELETE
200 FROM civicrm_option_value
201 WHERE id = %1";
202 $params = array(1 => array($optionId, 'Integer'));
203 CRM_Core_DAO::executeQuery($query, $params);
204 }
205 }
206
207 /**
208 * @param array $params
209 *
210 * @throws Exception
211 */
212 public static function updateCustomValues($params) {
213 $optionDAO = new CRM_Core_DAO_OptionValue();
214 $optionDAO->id = $params['optionId'];
215 $optionDAO->find(TRUE);
216 $oldValue = $optionDAO->value;
217
218 // get the table, column, html_type and data type for this field
219 $query = "
220 SELECT g.table_name as tableName ,
221 f.column_name as columnName,
222 f.data_type as dataType,
223 f.html_type as htmlType
224 FROM civicrm_custom_group g,
225 civicrm_custom_field f
226 WHERE f.custom_group_id = g.id
227 AND f.id = %1";
228 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
229 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
230 if ($dao->fetch()) {
231 if ($dao->dataType == 'Money') {
232 $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']);
233 }
234 switch ($dao->htmlType) {
235 case 'Autocomplete-Select':
236 case 'Select':
237 case 'Radio':
238 $query = "
239 UPDATE {$dao->tableName}
240 SET {$dao->columnName} = %1
241 WHERE id = %2";
242 if ($dao->dataType == 'Auto-complete') {
243 $dataType = "String";
244 }
245 else {
246 $dataType = $dao->dataType;
247 }
248 $queryParams = array(
249 1 => array($params['value'],
250 $dataType,
251 ),
252 2 => array(
253 $params['optionId'],
254 'Integer',
255 ),
256 );
257 break;
258
259 case 'AdvMulti-Select':
260 case 'Multi-Select':
261 case 'CheckBox':
262 $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR;
263 $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR;
264 $query = "
265 UPDATE {$dao->tableName}
266 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
267 $queryParams = array(1 => array($oldString, 'String'),
268 2 => array($newString, 'String'),
269 );
270 break;
271
272 default:
273 CRM_Core_Error::fatal();
274 }
275 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
276 }
277 }
278
279 /**
280 * @param int $customFieldID
281 * @param int $optionGroupID
282 *
283 * @return array
284 */
285 public static function valuesByID($customFieldID, $optionGroupID = NULL) {
286 if (!$optionGroupID) {
287 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
288 $customFieldID,
289 'option_group_id'
290 );
291 }
292
293 $options = $optionGroupID ? CRM_Core_OptionGroup::valuesByID($optionGroupID) : array();
294
295 CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE);
296
297 return $options;
298 }
299 }