3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * Business objects for managing custom data options.
40 class CRM_Core_BAO_CustomOption
{
43 * Takes a bunch of params that are needed to match certain criteria and
44 * retrieves the relevant objects. Typically the valid params are only
45 * contact_id. We'll tweak this function to be more full featured over a period
46 * of time. This is the inverse function of create. It also stores all the retrieved
47 * values in the default array
49 * @param array $params (reference ) an assoc array of name/value pairs
50 * @param array $defaults (reference ) an assoc array to hold the flattened values
52 * @return object CRM_Core_BAO_CustomOption object
56 static function retrieve(&$params, &$defaults) {
57 $customOption = new CRM_Core_DAO_OptionValue();
58 $customOption->copyValues($params);
59 if ($customOption->find(TRUE)) {
60 CRM_Core_DAO
::storeValues($customOption, $defaults);
67 * Returns all active options ordered by weight for a given field
69 * @param int $fieldId field whose options are needed
70 * @param boolean $inactiveNeeded do we need inactive options ?
72 * @return array $customOption all active options for fieldId
75 static function getCustomOption(
77 $inactiveNeeded = FALSE
84 $field = CRM_Core_BAO_CustomField
::getFieldObject($fieldID);
86 // get the option group id
87 $optionGroupID = $field->option_group_id
;
88 if (!$optionGroupID) {
92 $optionValues = CRM_Core_BAO_OptionValue
::getOptionValuesArray($optionGroupID);
94 foreach ($optionValues as $id => $value) {
95 if (!$inactiveNeeded && !CRM_Utils_Array
::value('is_active', $value)) {
99 $options[$id] = array();
100 $options[$id]['id'] = $id;
101 $options[$id]['label'] = $value['label'];
102 $options[$id]['value'] = $value['value'];
105 CRM_Utils_Hook
::customFieldOptions($fieldID, $options, TRUE);
111 * Returns the option label for a custom field with a specific value. Handles all
112 * custom field data and html types
114 * @param $fieldId int the custom field ID
115 * @pram $value string the value (typically from the DB) of this custom field
116 * @param $htmlType string the html type of the field (optional)
117 * @param $dataType string the data type of the field (optional)
119 * @return string the label to display for this custom field
123 static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) {
128 if (!$htmlType ||
!$dataType) {
130 SELECT html_type, data_type
131 FROM civicrm_custom_field
134 $params = array(1 => array($fieldId, 'Integer'));
135 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
137 $htmlType = $dao->html_type
;
138 $dataType = $dao->data_type
;
141 CRM_Core_Error
::fatal();
149 case 'AdvMulti-Select':
152 case 'Autocomplete-Select':
153 if (!in_array($dataType, array(
154 'Boolean', 'ContactReference'))) {
155 $options = self
::valuesByID($fieldId);
159 return CRM_Core_BAO_CustomField
::getDisplayValueCommon($value,
167 * Function to delete Option
169 * param $optionId integer option id
174 static function del($optionId) {
175 // get the customFieldID
177 SELECT f.id as id, f.data_type as dataType
178 FROM civicrm_option_value v,
179 civicrm_option_group g,
180 civicrm_custom_field f
182 AND g.id = f.option_group_id
183 AND g.id = v.option_group_id";
184 $params = array(1 => array($optionId, 'Integer'));
185 $dao = CRM_Core_DAO
::executeQuery($query, $params);
187 if (in_array($dao->dataType
,
188 array('Int', 'Float', 'Money', 'Boolean')
196 'optionId' => $optionId,
197 'fieldId' => $dao->id
,
200 // delete this value from the tables
201 self
::updateCustomValues($params);
203 // also delete this option value
206 FROM civicrm_option_value
208 $params = array(1 => array($optionId, 'Integer'));
209 CRM_Core_DAO
::executeQuery($query, $params);
213 static function updateCustomValues($params) {
214 $optionDAO = new CRM_Core_DAO_OptionValue();
215 $optionDAO->id
= $params['optionId'];
216 $optionDAO->find(TRUE);
217 $oldValue = $optionDAO->value
;
219 // get the table, column, html_type and data type for this field
221 SELECT g.table_name as tableName ,
222 f.column_name as columnName,
223 f.data_type as dataType,
224 f.html_type as htmlType
225 FROM civicrm_custom_group g,
226 civicrm_custom_field f
227 WHERE f.custom_group_id = g.id
229 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
230 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
232 if ($dao->dataType
== 'Money') {
233 $params['value'] = CRM_Utils_Rule
::cleanMoney($params['value']);
235 switch ($dao->htmlType
) {
236 case 'Autocomplete-Select':
240 UPDATE {$dao->tableName}
241 SET {$dao->columnName} = %1
243 if ($dao->dataType
== 'Auto-complete') {
244 $dataType = "String";
247 $dataType = $dao->dataType
;
249 $queryParams = array(
250 1 => array($params['value'],
260 case 'AdvMulti-Select':
263 $oldString = CRM_Core_DAO
::VALUE_SEPARATOR
. $oldValue . CRM_Core_DAO
::VALUE_SEPARATOR
;
264 $newString = CRM_Core_DAO
::VALUE_SEPARATOR
. $params['value'] . CRM_Core_DAO
::VALUE_SEPARATOR
;
266 UPDATE {$dao->tableName}
267 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
268 $queryParams = array(1 => array($oldString, 'String'),
269 2 => array($newString, 'String'),
274 CRM_Core_Error
::fatal();
276 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
280 static function &valuesByID($customFieldID, $optionGroupID = NULL) {
281 if (!$optionGroupID) {
282 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
288 $options = CRM_Core_OptionGroup
::valuesByID($optionGroupID);
290 CRM_Utils_Hook
::customFieldOptions($customFieldID, $options, FALSE);