3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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'];
140 $params['sort'] = CRM_Utils_Array
::value('sortBy', $params);
142 $field = CRM_Core_BAO_CustomField
::getFieldObject($params['fid']);
144 // get the option group id
145 $optionGroupID = $field->option_group_id
;
146 if (!$optionGroupID) {
149 $queryParams = array(1 => array($optionGroupID, 'Integer'));
150 $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1";
151 $params['total'] = CRM_Core_DAO
::singleValueQuery($total, $queryParams);
153 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
155 $orderBy = ' ORDER BY options.weight asc';
156 if (!empty($params['sort'])) {
157 $orderBy = ' ORDER BY ' . CRM_Utils_Type
::escape($params['sort'], 'String');
160 $query = "SELECT * FROM civicrm_option_value as options WHERE option_group_id = %1 {$orderBy} {$limit}";
161 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
162 $links = CRM_Custom_Page_Option
::actionLinks();
164 $fields = array('id', 'label', 'value', 'weight');
165 $config = CRM_Core_Config
::singleton();
166 while ($dao->fetch()) {
167 $options[$dao->id
] = array();
168 foreach ($fields as $k) {
169 $options[$dao->id
][$k] = $dao->$k;
171 $action = array_sum(array_keys($links));
172 $class = 'crm-entity';
173 // update enable/disable links depending on custom_field properties.
174 if ($dao->is_active
) {
175 $action -= CRM_Core_Action
::ENABLE
;
178 $class .= ' disabled';
179 $action -= CRM_Core_Action
::DISABLE
;
181 if ($fieldHtmlType == 'CheckBox' ||
182 $fieldHtmlType == 'AdvMulti-Select' ||
183 $fieldHtmlType == 'Multi-Select'
185 if (in_array($dao->value
, $defVal)) {
186 $options[$dao->id
]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
189 $options[$dao->id
]['is_default'] = '';
193 if ($defaultValue == $dao->value
) {
194 $options[$dao->id
]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
197 $options[$dao->id
]['is_default'] = '';
201 $options[$dao->id
]['class'] = $dao->id
. ',' . $class;
202 $options[$dao->id
]['is_active'] = !empty($dao->is_active
) ?
'Yes' : 'No';
203 $options[$dao->id
]['links'] = CRM_Core_Action
::formLink($links,
207 'fid' => $params['fid'],
208 'gid' => $params['gid'],
212 'customOption.row.actions',
217 // Add order changing widget to selector
218 $returnURL = CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
219 "reset=1&action=browse&gid={$params['gid']}&fid={$params['fid']}"
221 $filter = "option_group_id = {$optionGroupID}";
222 CRM_Utils_Weight
::addOrder($options, 'CRM_Core_DAO_OptionValue',
223 'id', $returnURL, $filter
230 * Returns the option label for a custom field with a specific value. Handles all
231 * custom field data and html types
233 * @param int $fieldId
234 * the custom field ID.
235 * @pram $value string the value (typically from the DB) of this custom field
237 * @param string $htmlType
238 * the html type of the field (optional).
239 * @param string $dataType
240 * the data type of the field (optional).
243 * the label to display for this custom field
245 public static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) {
250 if (!$htmlType ||
!$dataType) {
252 SELECT html_type, data_type
253 FROM civicrm_custom_field
256 $params = array(1 => array($fieldId, 'Integer'));
257 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
259 $htmlType = $dao->html_type
;
260 $dataType = $dao->data_type
;
263 CRM_Core_Error
::fatal();
271 case 'AdvMulti-Select':
274 case 'Autocomplete-Select':
275 if (!in_array($dataType, array(
280 $options = self
::valuesByID($fieldId);
284 return CRM_Core_BAO_CustomField
::getDisplayValueCommon($value,
294 * @param $optionId integer
298 public static function del($optionId) {
299 // get the customFieldID
301 SELECT f.id as id, f.data_type as dataType
302 FROM civicrm_option_value v,
303 civicrm_option_group g,
304 civicrm_custom_field f
306 AND g.id = f.option_group_id
307 AND g.id = v.option_group_id";
308 $params = array(1 => array($optionId, 'Integer'));
309 $dao = CRM_Core_DAO
::executeQuery($query, $params);
311 if (in_array($dao->dataType
,
312 array('Int', 'Float', 'Money', 'Boolean')
320 'optionId' => $optionId,
321 'fieldId' => $dao->id
,
324 // delete this value from the tables
325 self
::updateCustomValues($params);
327 // also delete this option value
330 FROM civicrm_option_value
332 $params = array(1 => array($optionId, 'Integer'));
333 CRM_Core_DAO
::executeQuery($query, $params);
338 * @param array $params
342 public static function updateCustomValues($params) {
343 $optionDAO = new CRM_Core_DAO_OptionValue();
344 $optionDAO->id
= $params['optionId'];
345 $optionDAO->find(TRUE);
346 $oldValue = $optionDAO->value
;
348 // get the table, column, html_type and data type for this field
350 SELECT g.table_name as tableName ,
351 f.column_name as columnName,
352 f.data_type as dataType,
353 f.html_type as htmlType
354 FROM civicrm_custom_group g,
355 civicrm_custom_field f
356 WHERE f.custom_group_id = g.id
358 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
359 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
361 if ($dao->dataType
== 'Money') {
362 $params['value'] = CRM_Utils_Rule
::cleanMoney($params['value']);
364 switch ($dao->htmlType
) {
365 case 'Autocomplete-Select':
369 UPDATE {$dao->tableName}
370 SET {$dao->columnName} = %1
372 if ($dao->dataType
== 'Auto-complete') {
373 $dataType = "String";
376 $dataType = $dao->dataType
;
378 $queryParams = array(
390 case 'AdvMulti-Select':
393 $oldString = CRM_Core_DAO
::VALUE_SEPARATOR
. $oldValue . CRM_Core_DAO
::VALUE_SEPARATOR
;
394 $newString = CRM_Core_DAO
::VALUE_SEPARATOR
. $params['value'] . CRM_Core_DAO
::VALUE_SEPARATOR
;
396 UPDATE {$dao->tableName}
397 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
398 $queryParams = array(
399 1 => array($oldString, 'String'),
400 2 => array($newString, 'String'),
405 CRM_Core_Error
::fatal();
407 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
412 * @param int $customFieldID
413 * @param int $optionGroupID
417 public static function valuesByID($customFieldID, $optionGroupID = NULL) {
418 if (!$optionGroupID) {
419 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
425 $options = $optionGroupID ? CRM_Core_OptionGroup
::valuesByID($optionGroupID) : array();
427 CRM_Utils_Hook
::customFieldOptions($customFieldID, $options, FALSE);