3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * Business objects for managing custom data options.
40 class CRM_Core_BAO_CustomOption
{
43 * Fetch object based on array of properties.
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.
50 * @return CRM_Core_BAO_CustomOption
52 public 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);
63 * Returns all active options ordered by weight for a given field.
66 * Field whose options are needed.
67 * @param bool $inactiveNeeded Do we need inactive options ?.
68 * Do we need inactive options ?.
71 * all active options for fieldId
73 public static function getCustomOption(
75 $inactiveNeeded = FALSE
82 $field = CRM_Core_BAO_CustomField
::getFieldObject($fieldID);
84 // get the option group id
85 $optionGroupID = $field->option_group_id
;
86 if (!$optionGroupID) {
90 $optionValues = CRM_Core_BAO_OptionValue
::getOptionValuesArray($optionGroupID);
92 foreach ($optionValues as $id => $value) {
93 if (!$inactiveNeeded && empty($value['is_active'])) {
97 $options[$id] = array();
98 $options[$id]['id'] = $id;
99 $options[$id]['label'] = $value['label'];
100 $options[$id]['value'] = $value['value'];
103 CRM_Utils_Hook
::customFieldOptions($fieldID, $options, TRUE);
109 * wrapper for ajax option selector.
111 * @param array $params
112 * Associated array for params record id.
115 * associated array of option list
119 static public function getOptionListSelector(&$params) {
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
;
131 CRM_Core_Error
::fatal();
133 $defVal = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
134 substr($defaultValue, 1, -1)
138 $params['offset'] = ($params['page'] - 1) * $params['rp'];
139 $params['rowCount'] = $params['rp'];
141 $field = CRM_Core_BAO_CustomField
::getFieldObject($params['fid']);
143 // get the option group id
144 $optionGroupID = $field->option_group_id
;
145 if (!$optionGroupID) {
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);
152 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
153 $orderBy = ' ORDER BY options.weight asc';
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();
159 $fields = array('id', 'label', 'value');
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;
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
;
173 $class .= ' disabled';
174 $action -= CRM_Core_Action
::DISABLE
;
176 if ($fieldHtmlType == 'CheckBox' ||
177 $fieldHtmlType == 'AdvMulti-Select' ||
178 $fieldHtmlType == 'Multi-Select'
180 if (in_array($dao->value
, $defVal)) {
181 $options[$dao->id
]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
184 $options[$dao->id
]['is_default'] = '';
188 if ($defaultValue == $dao->value
) {
189 $options[$dao->id
]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
192 $options[$dao->id
]['is_default'] = '';
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,
202 'fid' => $params['fid'],
203 'gid' => $params['gid'],
207 'customOption.row.actions',
217 * Returns the option label for a custom field with a specific value. Handles all
218 * custom field data and html types
220 * @param int $fieldId
221 * the custom field ID.
222 * @pram $value string the value (typically from the DB) of this custom field
224 * @param string $htmlType
225 * the html type of the field (optional).
226 * @param string $dataType
227 * the data type of the field (optional).
230 * the label to display for this custom field
232 public static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) {
237 if (!$htmlType ||
!$dataType) {
239 SELECT html_type, data_type
240 FROM civicrm_custom_field
243 $params = array(1 => array($fieldId, 'Integer'));
244 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
246 $htmlType = $dao->html_type
;
247 $dataType = $dao->data_type
;
250 CRM_Core_Error
::fatal();
258 case 'AdvMulti-Select':
261 case 'Autocomplete-Select':
262 if (!in_array($dataType, array(
267 $options = self
::valuesByID($fieldId);
271 return CRM_Core_BAO_CustomField
::getDisplayValueCommon($value,
281 * @param $optionId integer
285 public static function del($optionId) {
286 // get the customFieldID
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
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);
298 if (in_array($dao->dataType
,
299 array('Int', 'Float', 'Money', 'Boolean')
307 'optionId' => $optionId,
308 'fieldId' => $dao->id
,
311 // delete this value from the tables
312 self
::updateCustomValues($params);
314 // also delete this option value
317 FROM civicrm_option_value
319 $params = array(1 => array($optionId, 'Integer'));
320 CRM_Core_DAO
::executeQuery($query, $params);
325 * @param array $params
329 public static function updateCustomValues($params) {
330 $optionDAO = new CRM_Core_DAO_OptionValue();
331 $optionDAO->id
= $params['optionId'];
332 $optionDAO->find(TRUE);
333 $oldValue = $optionDAO->value
;
335 // get the table, column, html_type and data type for this field
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
345 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
346 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
348 if ($dao->dataType
== 'Money') {
349 $params['value'] = CRM_Utils_Rule
::cleanMoney($params['value']);
351 switch ($dao->htmlType
) {
352 case 'Autocomplete-Select':
356 UPDATE {$dao->tableName}
357 SET {$dao->columnName} = %1
359 if ($dao->dataType
== 'Auto-complete') {
360 $dataType = "String";
363 $dataType = $dao->dataType
;
365 $queryParams = array(
377 case 'AdvMulti-Select':
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
;
383 UPDATE {$dao->tableName}
384 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
385 $queryParams = array(
386 1 => array($oldString, 'String'),
387 2 => array($newString, 'String'),
392 CRM_Core_Error
::fatal();
394 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
399 * @param int $customFieldID
400 * @param int $optionGroupID
404 public static function valuesByID($customFieldID, $optionGroupID = NULL) {
405 if (!$optionGroupID) {
406 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
412 $options = $optionGroupID ? CRM_Core_OptionGroup
::valuesByID($optionGroupID) : array();
414 CRM_Utils_Hook
::customFieldOptions($customFieldID, $options, FALSE);
420 * When changing the value of an option this is called to update all corresponding custom data
422 * @param int $optionId
423 * @param string $newValue
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) {
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)) {
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'),
450 1 => array($oldValue, 'String'),
451 2 => array($newValue, 'String'),
452 3 => array($oldValue, 'String'),
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);
459 $customField->free();