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