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