Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Business objects for managing custom data options. | |
38 | * | |
39 | */ | |
40 | class CRM_Core_BAO_CustomOption { | |
41 | ||
42 | /** | |
fe482240 | 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 | * |
16b10e64 | 50 | * @return CRM_Core_BAO_CustomOption |
6a488035 | 51 | */ |
00be9182 | 52 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
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 | /** | |
fe482240 | 63 | * Returns all active options ordered by weight for a given field. |
6a488035 | 64 | * |
6a0b768e TO |
65 | * @param int $fieldID |
66 | * Field whose options are needed. | |
317fceb4 | 67 | * @param bool $inactiveNeeded Do we need inactive options ?. |
6a0b768e | 68 | * Do we need inactive options ?. |
6a488035 | 69 | * |
a6c01b45 CW |
70 | * @return array |
71 | * all active options for fieldId | |
6a488035 | 72 | */ |
317fceb4 | 73 | public static function getCustomOption( |
6a488035 TO |
74 | $fieldID, |
75 | $inactiveNeeded = FALSE | |
76 | ) { | |
77 | $options = array(); | |
78 | if (!$fieldID) { | |
79 | return $options; | |
80 | } | |
81 | ||
82 | $field = CRM_Core_BAO_CustomField::getFieldObject($fieldID); | |
83 | ||
84 | // get the option group id | |
85 | $optionGroupID = $field->option_group_id; | |
86 | if (!$optionGroupID) { | |
87 | return $options; | |
88 | } | |
89 | ||
90 | $optionValues = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroupID); | |
91 | ||
92 | foreach ($optionValues as $id => $value) { | |
8cc574cf | 93 | if (!$inactiveNeeded && empty($value['is_active'])) { |
6a488035 TO |
94 | continue; |
95 | } | |
96 | ||
97 | $options[$id] = array(); | |
98 | $options[$id]['id'] = $id; | |
99 | $options[$id]['label'] = $value['label']; | |
100 | $options[$id]['value'] = $value['value']; | |
101 | } | |
102 | ||
103 | CRM_Utils_Hook::customFieldOptions($fieldID, $options, TRUE); | |
104 | ||
105 | return $options; | |
106 | } | |
107 | ||
57c9c217 | 108 | /** |
109 | * wrapper for ajax option selector. | |
110 | * | |
111 | * @param array $params | |
112 | * Associated array for params record id. | |
113 | * | |
114 | * @return array | |
115 | * associated array of option list | |
116 | * -rp = rowcount | |
117 | * -page= offset | |
118 | */ | |
119 | static public function getOptionListSelector(&$params) { | |
120 | ||
121 | $options = array(); | |
122 | ||
123 | //get the default value from custom fields | |
124 | $customFieldBAO = new CRM_Core_BAO_CustomField(); | |
125 | $customFieldBAO->id = $params['fid']; | |
126 | if ($customFieldBAO->find(TRUE)) { | |
127 | $defaultValue = $customFieldBAO->default_value; | |
128 | $fieldHtmlType = $customFieldBAO->html_type; | |
129 | } | |
130 | else { | |
131 | CRM_Core_Error::fatal(); | |
132 | } | |
133 | $defVal = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
134 | substr($defaultValue, 1, -1) | |
135 | ); | |
136 | ||
137 | // format the params | |
138 | $params['offset'] = ($params['page'] - 1) * $params['rp']; | |
139 | $params['rowCount'] = $params['rp']; | |
57c9c217 | 140 | |
141 | $field = CRM_Core_BAO_CustomField::getFieldObject($params['fid']); | |
142 | ||
143 | // get the option group id | |
144 | $optionGroupID = $field->option_group_id; | |
145 | if (!$optionGroupID) { | |
146 | return $options; | |
147 | } | |
148 | $queryParams = array(1 => array($optionGroupID, 'Integer')); | |
149 | $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1"; | |
150 | $params['total'] = CRM_Core_DAO::singleValueQuery($total, $queryParams); | |
151 | ||
152 | $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; | |
57c9c217 | 153 | $orderBy = ' ORDER BY options.weight asc'; |
57c9c217 | 154 | |
155 | $query = "SELECT * FROM civicrm_option_value as options WHERE option_group_id = %1 {$orderBy} {$limit}"; | |
156 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
157 | $links = CRM_Custom_Page_Option::actionLinks(); | |
158 | ||
46227b8d | 159 | $fields = array('id', 'label', 'value'); |
57c9c217 | 160 | $config = CRM_Core_Config::singleton(); |
161 | while ($dao->fetch()) { | |
162 | $options[$dao->id] = array(); | |
163 | foreach ($fields as $k) { | |
164 | $options[$dao->id][$k] = $dao->$k; | |
165 | } | |
166 | $action = array_sum(array_keys($links)); | |
167 | $class = 'crm-entity'; | |
168 | // update enable/disable links depending on custom_field properties. | |
169 | if ($dao->is_active) { | |
170 | $action -= CRM_Core_Action::ENABLE; | |
171 | } | |
172 | else { | |
173 | $class .= ' disabled'; | |
174 | $action -= CRM_Core_Action::DISABLE; | |
175 | } | |
176 | if ($fieldHtmlType == 'CheckBox' || | |
177 | $fieldHtmlType == 'AdvMulti-Select' || | |
178 | $fieldHtmlType == 'Multi-Select' | |
179 | ) { | |
180 | if (in_array($dao->value, $defVal)) { | |
181 | $options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />'; | |
182 | } | |
183 | else { | |
184 | $options[$dao->id]['is_default'] = ''; | |
185 | } | |
186 | } | |
187 | else { | |
188 | if ($defaultValue == $dao->value) { | |
189 | $options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />'; | |
190 | } | |
191 | else { | |
192 | $options[$dao->id]['is_default'] = ''; | |
193 | } | |
194 | } | |
195 | ||
196 | $options[$dao->id]['class'] = $dao->id . ',' . $class; | |
197 | $options[$dao->id]['is_active'] = !empty($dao->is_active) ? 'Yes' : 'No'; | |
198 | $options[$dao->id]['links'] = CRM_Core_Action::formLink($links, | |
199 | $action, | |
200 | array( | |
201 | 'id' => $dao->id, | |
202 | 'fid' => $params['fid'], | |
203 | 'gid' => $params['gid'], | |
204 | ), | |
205 | ts('more'), | |
206 | FALSE, | |
207 | 'customOption.row.actions', | |
208 | 'customOption', | |
209 | $dao->id | |
210 | ); | |
211 | } | |
57c9c217 | 212 | |
213 | return $options; | |
214 | } | |
215 | ||
6a488035 TO |
216 | /** |
217 | * Returns the option label for a custom field with a specific value. Handles all | |
218 | * custom field data and html types | |
219 | * | |
5a4f6742 CW |
220 | * @param int $fieldId |
221 | * the custom field ID. | |
6a488035 | 222 | * @pram $value string the value (typically from the DB) of this custom field |
dd244018 | 223 | * @param $value |
5a4f6742 CW |
224 | * @param string $htmlType |
225 | * the html type of the field (optional). | |
226 | * @param string $dataType | |
227 | * the data type of the field (optional). | |
6a488035 | 228 | * |
a6c01b45 CW |
229 | * @return string |
230 | * the label to display for this custom field | |
6a488035 | 231 | */ |
00be9182 | 232 | public static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) { |
6a488035 TO |
233 | if (!$fieldId) { |
234 | return NULL; | |
235 | } | |
236 | ||
237 | if (!$htmlType || !$dataType) { | |
238 | $sql = " | |
239 | SELECT html_type, data_type | |
240 | FROM civicrm_custom_field | |
241 | WHERE id = %1 | |
242 | "; | |
243 | $params = array(1 => array($fieldId, 'Integer')); | |
244 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
245 | if ($dao->fetch()) { | |
246 | $htmlType = $dao->html_type; | |
247 | $dataType = $dao->data_type; | |
248 | } | |
249 | else { | |
250 | CRM_Core_Error::fatal(); | |
251 | } | |
252 | } | |
253 | ||
254 | $options = NULL; | |
255 | switch ($htmlType) { | |
256 | case 'CheckBox': | |
257 | case 'Multi-Select': | |
258 | case 'AdvMulti-Select': | |
259 | case 'Select': | |
260 | case 'Radio': | |
261 | case 'Autocomplete-Select': | |
262 | if (!in_array($dataType, array( | |
353ffa53 | 263 | 'Boolean', |
317fceb4 | 264 | 'ContactReference', |
353ffa53 TO |
265 | )) |
266 | ) { | |
6a488035 TO |
267 | $options = self::valuesByID($fieldId); |
268 | } | |
269 | } | |
270 | ||
271 | return CRM_Core_BAO_CustomField::getDisplayValueCommon($value, | |
272 | $options, | |
273 | $htmlType, | |
274 | $dataType | |
275 | ); | |
276 | } | |
277 | ||
278 | /** | |
fe482240 | 279 | * Delete Option. |
6a488035 | 280 | * |
16b10e64 CW |
281 | * @param $optionId integer |
282 | * option id | |
6a488035 | 283 | * |
6a488035 | 284 | */ |
00be9182 | 285 | public static function del($optionId) { |
6a488035 TO |
286 | // get the customFieldID |
287 | $query = " | |
288 | SELECT f.id as id, f.data_type as dataType | |
289 | FROM civicrm_option_value v, | |
290 | civicrm_option_group g, | |
291 | civicrm_custom_field f | |
292 | WHERE v.id = %1 | |
293 | AND g.id = f.option_group_id | |
294 | AND g.id = v.option_group_id"; | |
295 | $params = array(1 => array($optionId, 'Integer')); | |
296 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
297 | if ($dao->fetch()) { | |
298 | if (in_array($dao->dataType, | |
353ffa53 TO |
299 | array('Int', 'Float', 'Money', 'Boolean') |
300 | )) { | |
6a488035 TO |
301 | $value = 0; |
302 | } | |
303 | else { | |
304 | $value = ''; | |
305 | } | |
306 | $params = array( | |
307 | 'optionId' => $optionId, | |
308 | 'fieldId' => $dao->id, | |
309 | 'value' => $value, | |
310 | ); | |
311 | // delete this value from the tables | |
312 | self::updateCustomValues($params); | |
313 | ||
314 | // also delete this option value | |
315 | $query = " | |
316 | DELETE | |
317 | FROM civicrm_option_value | |
318 | WHERE id = %1"; | |
319 | $params = array(1 => array($optionId, 'Integer')); | |
320 | CRM_Core_DAO::executeQuery($query, $params); | |
321 | } | |
322 | } | |
323 | ||
b5c2afd0 | 324 | /** |
c490a46a | 325 | * @param array $params |
b5c2afd0 EM |
326 | * |
327 | * @throws Exception | |
328 | */ | |
00be9182 | 329 | public static function updateCustomValues($params) { |
6a488035 TO |
330 | $optionDAO = new CRM_Core_DAO_OptionValue(); |
331 | $optionDAO->id = $params['optionId']; | |
332 | $optionDAO->find(TRUE); | |
333 | $oldValue = $optionDAO->value; | |
334 | ||
335 | // get the table, column, html_type and data type for this field | |
336 | $query = " | |
337 | SELECT g.table_name as tableName , | |
338 | f.column_name as columnName, | |
339 | f.data_type as dataType, | |
340 | f.html_type as htmlType | |
341 | FROM civicrm_custom_group g, | |
342 | civicrm_custom_field f | |
343 | WHERE f.custom_group_id = g.id | |
344 | AND f.id = %1"; | |
345 | $queryParams = array(1 => array($params['fieldId'], 'Integer')); | |
346 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
347 | if ($dao->fetch()) { | |
348 | if ($dao->dataType == 'Money') { | |
349 | $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']); | |
350 | } | |
351 | switch ($dao->htmlType) { | |
352 | case 'Autocomplete-Select': | |
353 | case 'Select': | |
354 | case 'Radio': | |
355 | $query = " | |
356 | UPDATE {$dao->tableName} | |
357 | SET {$dao->columnName} = %1 | |
358 | WHERE id = %2"; | |
359 | if ($dao->dataType == 'Auto-complete') { | |
360 | $dataType = "String"; | |
361 | } | |
362 | else { | |
363 | $dataType = $dao->dataType; | |
364 | } | |
365 | $queryParams = array( | |
353ffa53 TO |
366 | 1 => array( |
367 | $params['value'], | |
6a488035 TO |
368 | $dataType, |
369 | ), | |
370 | 2 => array( | |
371 | $params['optionId'], | |
372 | 'Integer', | |
373 | ), | |
374 | ); | |
375 | break; | |
376 | ||
377 | case 'AdvMulti-Select': | |
378 | case 'Multi-Select': | |
379 | case 'CheckBox': | |
380 | $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR; | |
381 | $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR; | |
353ffa53 | 382 | $query = " |
6a488035 TO |
383 | UPDATE {$dao->tableName} |
384 | SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )"; | |
2aa397bc | 385 | $queryParams = array( |
353ffa53 | 386 | 1 => array($oldString, 'String'), |
6a488035 TO |
387 | 2 => array($newString, 'String'), |
388 | ); | |
389 | break; | |
390 | ||
391 | default: | |
392 | CRM_Core_Error::fatal(); | |
393 | } | |
394 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
395 | } | |
396 | } | |
397 | ||
b5c2afd0 | 398 | /** |
100fef9d CW |
399 | * @param int $customFieldID |
400 | * @param int $optionGroupID | |
b5c2afd0 EM |
401 | * |
402 | * @return array | |
403 | */ | |
00be9182 | 404 | public static function valuesByID($customFieldID, $optionGroupID = NULL) { |
6a488035 TO |
405 | if (!$optionGroupID) { |
406 | $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
407 | $customFieldID, | |
408 | 'option_group_id' | |
409 | ); | |
410 | } | |
411 | ||
2fea9ed9 | 412 | $options = $optionGroupID ? CRM_Core_OptionGroup::valuesByID($optionGroupID) : array(); |
6a488035 TO |
413 | |
414 | CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE); | |
415 | ||
416 | return $options; | |
417 | } | |
96025800 | 418 | |
58eaa092 CW |
419 | /** |
420 | * When changing the value of an option this is called to update all corresponding custom data | |
421 | * | |
422 | * @param int $optionId | |
423 | * @param string $newValue | |
424 | */ | |
425 | public static function updateValue($optionId, $newValue) { | |
426 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
427 | $optionValue->id = $optionId; | |
428 | $optionValue->find(TRUE); | |
429 | $oldValue = $optionValue->value; | |
430 | if ($oldValue == $newValue) { | |
431 | return; | |
432 | } | |
433 | ||
434 | $customField = new CRM_Core_DAO_CustomField(); | |
435 | $customField->option_group_id = $optionValue->option_group_id; | |
436 | $customField->find(); | |
437 | while ($customField->fetch()) { | |
438 | $customGroup = new CRM_Core_DAO_CustomGroup(); | |
439 | $customGroup->id = $customField->custom_group_id; | |
440 | $customGroup->find(TRUE); | |
441 | if (CRM_Core_BAO_CustomField::isSerialized($customField)) { | |
442 | $params = array( | |
443 | 1 => array(CRM_Utils_Array::implodePadded($oldValue), 'String'), | |
444 | 2 => array(CRM_Utils_Array::implodePadded($newValue), 'String'), | |
445 | 3 => array('%' . CRM_Utils_Array::implodePadded($oldValue) . '%', 'String'), | |
446 | ); | |
447 | } | |
448 | else { | |
449 | $params = array( | |
450 | 1 => array($oldValue, 'String'), | |
451 | 2 => array($newValue, 'String'), | |
452 | 3 => array($oldValue, 'String'), | |
453 | ); | |
454 | } | |
455 | $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3"; | |
456 | $customGroup->free(); | |
457 | CRM_Core_DAO::executeQuery($sql, $params); | |
458 | } | |
459 | $customField->free(); | |
460 | } | |
461 | ||
6a488035 | 462 | } |