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