| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Business objects for managing custom data options. |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Core_BAO_CustomOption { |
| 41 | |
| 42 | /** |
| 43 | * Fetch object based on array of properties |
| 44 | * |
| 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. |
| 49 | * |
| 50 | * @return CRM_Core_BAO_CustomOption |
| 51 | */ |
| 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); |
| 57 | return $customOption; |
| 58 | } |
| 59 | return NULL; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns all active options ordered by weight for a given field |
| 64 | * |
| 65 | * @param int $fieldID |
| 66 | * Field whose options are needed. |
| 67 | * @param bool $inactiveNeededDo we need inactive options ?. |
| 68 | * Do we need inactive options ?. |
| 69 | * |
| 70 | * @return array |
| 71 | * all active options for fieldId |
| 72 | */ |
| 73 | static function getCustomOption( |
| 74 | $fieldID, |
| 75 | $inactiveNeeded = FALSE |
| 76 | ) { |
| 77 | $options = array(); |
| 78 | if (!$fieldID) { |
| 79 | return $options; |
| 80 | } |
| 81 | |
| 82 | $field = CRM_Core_BAO_CustomField::getFieldObject($fieldID); |
| 83 | |
| 84 | // get the option group id |
| 85 | $optionGroupID = $field->option_group_id; |
| 86 | if (!$optionGroupID) { |
| 87 | return $options; |
| 88 | } |
| 89 | |
| 90 | $optionValues = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroupID); |
| 91 | |
| 92 | foreach ($optionValues as $id => $value) { |
| 93 | if (!$inactiveNeeded && empty($value['is_active'])) { |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | $options[$id] = array(); |
| 98 | $options[$id]['id'] = $id; |
| 99 | $options[$id]['label'] = $value['label']; |
| 100 | $options[$id]['value'] = $value['value']; |
| 101 | } |
| 102 | |
| 103 | CRM_Utils_Hook::customFieldOptions($fieldID, $options, TRUE); |
| 104 | |
| 105 | return $options; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the option label for a custom field with a specific value. Handles all |
| 110 | * custom field data and html types |
| 111 | * |
| 112 | * @param int $fieldId |
| 113 | * the custom field ID. |
| 114 | * @pram $value string the value (typically from the DB) of this custom field |
| 115 | * @param $value |
| 116 | * @param string $htmlType |
| 117 | * the html type of the field (optional). |
| 118 | * @param string $dataType |
| 119 | * the data type of the field (optional). |
| 120 | * |
| 121 | * @return string |
| 122 | * the label to display for this custom field |
| 123 | */ |
| 124 | public static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) { |
| 125 | if (!$fieldId) { |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | if (!$htmlType || !$dataType) { |
| 130 | $sql = " |
| 131 | SELECT html_type, data_type |
| 132 | FROM civicrm_custom_field |
| 133 | WHERE id = %1 |
| 134 | "; |
| 135 | $params = array(1 => array($fieldId, 'Integer')); |
| 136 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 137 | if ($dao->fetch()) { |
| 138 | $htmlType = $dao->html_type; |
| 139 | $dataType = $dao->data_type; |
| 140 | } |
| 141 | else { |
| 142 | CRM_Core_Error::fatal(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | $options = NULL; |
| 147 | switch ($htmlType) { |
| 148 | case 'CheckBox': |
| 149 | case 'Multi-Select': |
| 150 | case 'AdvMulti-Select': |
| 151 | case 'Select': |
| 152 | case 'Radio': |
| 153 | case 'Autocomplete-Select': |
| 154 | if (!in_array($dataType, array( |
| 155 | 'Boolean', |
| 156 | 'ContactReference' |
| 157 | )) |
| 158 | ) { |
| 159 | $options = self::valuesByID($fieldId); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return CRM_Core_BAO_CustomField::getDisplayValueCommon($value, |
| 164 | $options, |
| 165 | $htmlType, |
| 166 | $dataType |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Delete Option |
| 172 | * |
| 173 | * @param $optionId integer |
| 174 | * option id |
| 175 | * |
| 176 | */ |
| 177 | public static function del($optionId) { |
| 178 | // get the customFieldID |
| 179 | $query = " |
| 180 | SELECT f.id as id, f.data_type as dataType |
| 181 | FROM civicrm_option_value v, |
| 182 | civicrm_option_group g, |
| 183 | civicrm_custom_field f |
| 184 | WHERE v.id = %1 |
| 185 | AND g.id = f.option_group_id |
| 186 | AND g.id = v.option_group_id"; |
| 187 | $params = array(1 => array($optionId, 'Integer')); |
| 188 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 189 | if ($dao->fetch()) { |
| 190 | if (in_array($dao->dataType, |
| 191 | array('Int', 'Float', 'Money', 'Boolean') |
| 192 | )) { |
| 193 | $value = 0; |
| 194 | } |
| 195 | else { |
| 196 | $value = ''; |
| 197 | } |
| 198 | $params = array( |
| 199 | 'optionId' => $optionId, |
| 200 | 'fieldId' => $dao->id, |
| 201 | 'value' => $value, |
| 202 | ); |
| 203 | // delete this value from the tables |
| 204 | self::updateCustomValues($params); |
| 205 | |
| 206 | // also delete this option value |
| 207 | $query = " |
| 208 | DELETE |
| 209 | FROM civicrm_option_value |
| 210 | WHERE id = %1"; |
| 211 | $params = array(1 => array($optionId, 'Integer')); |
| 212 | CRM_Core_DAO::executeQuery($query, $params); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param array $params |
| 218 | * |
| 219 | * @throws Exception |
| 220 | */ |
| 221 | public static function updateCustomValues($params) { |
| 222 | $optionDAO = new CRM_Core_DAO_OptionValue(); |
| 223 | $optionDAO->id = $params['optionId']; |
| 224 | $optionDAO->find(TRUE); |
| 225 | $oldValue = $optionDAO->value; |
| 226 | |
| 227 | // get the table, column, html_type and data type for this field |
| 228 | $query = " |
| 229 | SELECT g.table_name as tableName , |
| 230 | f.column_name as columnName, |
| 231 | f.data_type as dataType, |
| 232 | f.html_type as htmlType |
| 233 | FROM civicrm_custom_group g, |
| 234 | civicrm_custom_field f |
| 235 | WHERE f.custom_group_id = g.id |
| 236 | AND f.id = %1"; |
| 237 | $queryParams = array(1 => array($params['fieldId'], 'Integer')); |
| 238 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 239 | if ($dao->fetch()) { |
| 240 | if ($dao->dataType == 'Money') { |
| 241 | $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']); |
| 242 | } |
| 243 | switch ($dao->htmlType) { |
| 244 | case 'Autocomplete-Select': |
| 245 | case 'Select': |
| 246 | case 'Radio': |
| 247 | $query = " |
| 248 | UPDATE {$dao->tableName} |
| 249 | SET {$dao->columnName} = %1 |
| 250 | WHERE id = %2"; |
| 251 | if ($dao->dataType == 'Auto-complete') { |
| 252 | $dataType = "String"; |
| 253 | } |
| 254 | else { |
| 255 | $dataType = $dao->dataType; |
| 256 | } |
| 257 | $queryParams = array( |
| 258 | 1 => array( |
| 259 | $params['value'], |
| 260 | $dataType, |
| 261 | ), |
| 262 | 2 => array( |
| 263 | $params['optionId'], |
| 264 | 'Integer', |
| 265 | ), |
| 266 | ); |
| 267 | break; |
| 268 | |
| 269 | case 'AdvMulti-Select': |
| 270 | case 'Multi-Select': |
| 271 | case 'CheckBox': |
| 272 | $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR; |
| 273 | $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR; |
| 274 | $query = " |
| 275 | UPDATE {$dao->tableName} |
| 276 | SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )"; |
| 277 | $queryParams = array( |
| 278 | 1 => array($oldString, 'String'), |
| 279 | 2 => array($newString, 'String'), |
| 280 | ); |
| 281 | break; |
| 282 | |
| 283 | default: |
| 284 | CRM_Core_Error::fatal(); |
| 285 | } |
| 286 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @param int $customFieldID |
| 292 | * @param int $optionGroupID |
| 293 | * |
| 294 | * @return array |
| 295 | */ |
| 296 | public static function valuesByID($customFieldID, $optionGroupID = NULL) { |
| 297 | if (!$optionGroupID) { |
| 298 | $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', |
| 299 | $customFieldID, |
| 300 | 'option_group_id' |
| 301 | ); |
| 302 | } |
| 303 | |
| 304 | $options = $optionGroupID ? CRM_Core_OptionGroup::valuesByID($optionGroupID) : array(); |
| 305 | |
| 306 | CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE); |
| 307 | |
| 308 | return $options; |
| 309 | } |
| 310 | } |