3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 (reference ) an assoc array of name/value pairs
46 * @param array $defaults (reference ) an assoc array to hold the flattened values
48 * @return CRM_Core_BAO_CustomOption object
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);
63 * Returns all active options ordered by weight for a given field
65 * @param int $fieldID field whose options are needed
66 * @param boolean $inactiveNeeded do we need inactive options ?
68 * @return array $customOption all active options for fieldId
71 static function getCustomOption(
73 $inactiveNeeded = FALSE
80 $field = CRM_Core_BAO_CustomField
::getFieldObject($fieldID);
82 // get the option group id
83 $optionGroupID = $field->option_group_id
;
84 if (!$optionGroupID) {
88 $optionValues = CRM_Core_BAO_OptionValue
::getOptionValuesArray($optionGroupID);
90 foreach ($optionValues as $id => $value) {
91 if (!$inactiveNeeded && empty($value['is_active'])) {
95 $options[$id] = array();
96 $options[$id]['id'] = $id;
97 $options[$id]['label'] = $value['label'];
98 $options[$id]['value'] = $value['value'];
101 CRM_Utils_Hook
::customFieldOptions($fieldID, $options, TRUE);
107 * Returns the option label for a custom field with a specific value. Handles all
108 * custom field data and html types
110 * @param $fieldId int the custom field ID
111 * @pram $value string the value (typically from the DB) of this custom field
113 * @param $htmlType string the html type of the field (optional)
114 * @param $dataType string the data type of the field (optional)
116 * @return string the label to display for this custom field
120 static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) {
125 if (!$htmlType ||
!$dataType) {
127 SELECT html_type, data_type
128 FROM civicrm_custom_field
131 $params = array(1 => array($fieldId, 'Integer'));
132 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
134 $htmlType = $dao->html_type
;
135 $dataType = $dao->data_type
;
138 CRM_Core_Error
::fatal();
146 case 'AdvMulti-Select':
149 case 'Autocomplete-Select':
150 if (!in_array($dataType, array(
151 'Boolean', 'ContactReference'))) {
152 $options = self
::valuesByID($fieldId);
156 return CRM_Core_BAO_CustomField
::getDisplayValueCommon($value,
166 * param $optionId integer option id
171 static function del($optionId) {
172 // get the customFieldID
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
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);
184 if (in_array($dao->dataType
,
185 array('Int', 'Float', 'Money', 'Boolean')
193 'optionId' => $optionId,
194 'fieldId' => $dao->id
,
197 // delete this value from the tables
198 self
::updateCustomValues($params);
200 // also delete this option value
203 FROM civicrm_option_value
205 $params = array(1 => array($optionId, 'Integer'));
206 CRM_Core_DAO
::executeQuery($query, $params);
211 * @param array $params
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
;
221 // get the table, column, html_type and data type for this field
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
231 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
232 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
234 if ($dao->dataType
== 'Money') {
235 $params['value'] = CRM_Utils_Rule
::cleanMoney($params['value']);
237 switch ($dao->htmlType
) {
238 case 'Autocomplete-Select':
242 UPDATE {$dao->tableName}
243 SET {$dao->columnName} = %1
245 if ($dao->dataType
== 'Auto-complete') {
246 $dataType = "String";
249 $dataType = $dao->dataType
;
251 $queryParams = array(
252 1 => array($params['value'],
262 case 'AdvMulti-Select':
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
;
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'),
276 CRM_Core_Error
::fatal();
278 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
283 * @param int $customFieldID
284 * @param int $optionGroupID
288 static function valuesByID($customFieldID, $optionGroupID = NULL) {
289 if (!$optionGroupID) {
290 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
296 $options = $optionGroupID ? CRM_Core_OptionGroup
::valuesByID($optionGroupID) : array();
298 CRM_Utils_Hook
::customFieldOptions($customFieldID, $options, FALSE);