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 * 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
70 * @param boolean $inactiveNeeded do we need inactive options ?
72 * @internal param int $fieldId field whose options are needed
73 * @return array $customOption all active options for fieldId
76 static function getCustomOption(
78 $inactiveNeeded = FALSE
85 $field = CRM_Core_BAO_CustomField
::getFieldObject($fieldID);
87 // get the option group id
88 $optionGroupID = $field->option_group_id
;
89 if (!$optionGroupID) {
93 $optionValues = CRM_Core_BAO_OptionValue
::getOptionValuesArray($optionGroupID);
95 foreach ($optionValues as $id => $value) {
96 if (!$inactiveNeeded && empty($value['is_active'])) {
100 $options[$id] = array();
101 $options[$id]['id'] = $id;
102 $options[$id]['label'] = $value['label'];
103 $options[$id]['value'] = $value['value'];
106 CRM_Utils_Hook
::customFieldOptions($fieldID, $options, TRUE);
112 * Returns the option label for a custom field with a specific value. Handles all
113 * custom field data and html types
115 * @param $fieldId int the custom field ID
116 * @pram $value string the value (typically from the DB) of this custom field
118 * @param $htmlType string the html type of the field (optional)
119 * @param $dataType string the data type of the field (optional)
121 * @return string the label to display for this custom field
125 static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) {
130 if (!$htmlType ||
!$dataType) {
132 SELECT html_type, data_type
133 FROM civicrm_custom_field
136 $params = array(1 => array($fieldId, 'Integer'));
137 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
139 $htmlType = $dao->html_type
;
140 $dataType = $dao->data_type
;
143 CRM_Core_Error
::fatal();
151 case 'AdvMulti-Select':
154 case 'Autocomplete-Select':
155 if (!in_array($dataType, array(
156 'Boolean', 'ContactReference'))) {
157 $options = self
::valuesByID($fieldId);
161 return CRM_Core_BAO_CustomField
::getDisplayValueCommon($value,
169 * Function to delete Option
171 * param $optionId integer option id
176 static function del($optionId) {
177 // get the customFieldID
179 SELECT f.id as id, f.data_type as dataType
180 FROM civicrm_option_value v,
181 civicrm_option_group g,
182 civicrm_custom_field f
184 AND g.id = f.option_group_id
185 AND g.id = v.option_group_id";
186 $params = array(1 => array($optionId, 'Integer'));
187 $dao = CRM_Core_DAO
::executeQuery($query, $params);
189 if (in_array($dao->dataType
,
190 array('Int', 'Float', 'Money', 'Boolean')
198 'optionId' => $optionId,
199 'fieldId' => $dao->id
,
202 // delete this value from the tables
203 self
::updateCustomValues($params);
205 // also delete this option value
208 FROM civicrm_option_value
210 $params = array(1 => array($optionId, 'Integer'));
211 CRM_Core_DAO
::executeQuery($query, $params);
220 static function updateCustomValues($params) {
221 $optionDAO = new CRM_Core_DAO_OptionValue();
222 $optionDAO->id
= $params['optionId'];
223 $optionDAO->find(TRUE);
224 $oldValue = $optionDAO->value
;
226 // get the table, column, html_type and data type for this field
228 SELECT g.table_name as tableName ,
229 f.column_name as columnName,
230 f.data_type as dataType,
231 f.html_type as htmlType
232 FROM civicrm_custom_group g,
233 civicrm_custom_field f
234 WHERE f.custom_group_id = g.id
236 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
237 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
239 if ($dao->dataType
== 'Money') {
240 $params['value'] = CRM_Utils_Rule
::cleanMoney($params['value']);
242 switch ($dao->htmlType
) {
243 case 'Autocomplete-Select':
247 UPDATE {$dao->tableName}
248 SET {$dao->columnName} = %1
250 if ($dao->dataType
== 'Auto-complete') {
251 $dataType = "String";
254 $dataType = $dao->dataType
;
256 $queryParams = array(
257 1 => array($params['value'],
267 case 'AdvMulti-Select':
270 $oldString = CRM_Core_DAO
::VALUE_SEPARATOR
. $oldValue . CRM_Core_DAO
::VALUE_SEPARATOR
;
271 $newString = CRM_Core_DAO
::VALUE_SEPARATOR
. $params['value'] . CRM_Core_DAO
::VALUE_SEPARATOR
;
273 UPDATE {$dao->tableName}
274 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
275 $queryParams = array(1 => array($oldString, 'String'),
276 2 => array($newString, 'String'),
281 CRM_Core_Error
::fatal();
283 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
288 * @param $customFieldID
289 * @param null $optionGroupID
293 static function valuesByID($customFieldID, $optionGroupID = NULL) {
294 if (!$optionGroupID) {
295 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
301 $options = $optionGroupID ? CRM_Core_OptionGroup
::valuesByID($optionGroupID) : array();
303 CRM_Utils_Hook
::customFieldOptions($customFieldID, $options, FALSE);