| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 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 | * 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 |
| 48 | * |
| 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 |
| 51 | * |
| 52 | * @return object CRM_Core_BAO_CustomOption object |
| 53 | * @access public |
| 54 | * @static |
| 55 | */ |
| 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); |
| 61 | return $customOption; |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns all active options ordered by weight for a given field |
| 68 | * |
| 69 | * @param int $fieldId field whose options are needed |
| 70 | * @param boolean $inactiveNeeded do we need inactive options ? |
| 71 | * |
| 72 | * @return array $customOption all active options for fieldId |
| 73 | * @static |
| 74 | */ |
| 75 | static function getCustomOption( |
| 76 | $fieldID, |
| 77 | $inactiveNeeded = FALSE |
| 78 | ) { |
| 79 | $options = array(); |
| 80 | if (!$fieldID) { |
| 81 | return $options; |
| 82 | } |
| 83 | |
| 84 | $field = CRM_Core_BAO_CustomField::getFieldObject($fieldID); |
| 85 | |
| 86 | // get the option group id |
| 87 | $optionGroupID = $field->option_group_id; |
| 88 | if (!$optionGroupID) { |
| 89 | return $options; |
| 90 | } |
| 91 | |
| 92 | $optionValues = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroupID); |
| 93 | |
| 94 | foreach ($optionValues as $id => $value) { |
| 95 | if (!$inactiveNeeded && empty($value['is_active'])) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | $options[$id] = array(); |
| 100 | $options[$id]['id'] = $id; |
| 101 | $options[$id]['label'] = $value['label']; |
| 102 | $options[$id]['value'] = $value['value']; |
| 103 | } |
| 104 | |
| 105 | CRM_Utils_Hook::customFieldOptions($fieldID, $options, TRUE); |
| 106 | |
| 107 | return $options; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the option label for a custom field with a specific value. Handles all |
| 112 | * custom field data and html types |
| 113 | * |
| 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) |
| 118 | * |
| 119 | * @return string the label to display for this custom field |
| 120 | * @static |
| 121 | * @access public |
| 122 | */ |
| 123 | static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) { |
| 124 | if (!$fieldId) { |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | if (!$htmlType || !$dataType) { |
| 129 | $sql = " |
| 130 | SELECT html_type, data_type |
| 131 | FROM civicrm_custom_field |
| 132 | WHERE id = %1 |
| 133 | "; |
| 134 | $params = array(1 => array($fieldId, 'Integer')); |
| 135 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 136 | if ($dao->fetch()) { |
| 137 | $htmlType = $dao->html_type; |
| 138 | $dataType = $dao->data_type; |
| 139 | } |
| 140 | else { |
| 141 | CRM_Core_Error::fatal(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | $options = NULL; |
| 146 | switch ($htmlType) { |
| 147 | case 'CheckBox': |
| 148 | case 'Multi-Select': |
| 149 | case 'AdvMulti-Select': |
| 150 | case 'Select': |
| 151 | case 'Radio': |
| 152 | case 'Autocomplete-Select': |
| 153 | if (!in_array($dataType, array( |
| 154 | 'Boolean', 'ContactReference'))) { |
| 155 | $options = self::valuesByID($fieldId); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return CRM_Core_BAO_CustomField::getDisplayValueCommon($value, |
| 160 | $options, |
| 161 | $htmlType, |
| 162 | $dataType |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Function to delete Option |
| 168 | * |
| 169 | * param $optionId integer option id |
| 170 | * |
| 171 | * @static |
| 172 | * @access public |
| 173 | */ |
| 174 | static function del($optionId) { |
| 175 | // get the customFieldID |
| 176 | $query = " |
| 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 |
| 181 | WHERE v.id = %1 |
| 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); |
| 186 | if ($dao->fetch()) { |
| 187 | if (in_array($dao->dataType, |
| 188 | array('Int', 'Float', 'Money', 'Boolean') |
| 189 | )) { |
| 190 | $value = 0; |
| 191 | } |
| 192 | else { |
| 193 | $value = ''; |
| 194 | } |
| 195 | $params = array( |
| 196 | 'optionId' => $optionId, |
| 197 | 'fieldId' => $dao->id, |
| 198 | 'value' => $value, |
| 199 | ); |
| 200 | // delete this value from the tables |
| 201 | self::updateCustomValues($params); |
| 202 | |
| 203 | // also delete this option value |
| 204 | $query = " |
| 205 | DELETE |
| 206 | FROM civicrm_option_value |
| 207 | WHERE id = %1"; |
| 208 | $params = array(1 => array($optionId, 'Integer')); |
| 209 | CRM_Core_DAO::executeQuery($query, $params); |
| 210 | } |
| 211 | } |
| 212 | |
| 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; |
| 218 | |
| 219 | // get the table, column, html_type and data type for this field |
| 220 | $query = " |
| 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 |
| 228 | AND f.id = %1"; |
| 229 | $queryParams = array(1 => array($params['fieldId'], 'Integer')); |
| 230 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 231 | if ($dao->fetch()) { |
| 232 | if ($dao->dataType == 'Money') { |
| 233 | $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']); |
| 234 | } |
| 235 | switch ($dao->htmlType) { |
| 236 | case 'Autocomplete-Select': |
| 237 | case 'Select': |
| 238 | case 'Radio': |
| 239 | $query = " |
| 240 | UPDATE {$dao->tableName} |
| 241 | SET {$dao->columnName} = %1 |
| 242 | WHERE id = %2"; |
| 243 | if ($dao->dataType == 'Auto-complete') { |
| 244 | $dataType = "String"; |
| 245 | } |
| 246 | else { |
| 247 | $dataType = $dao->dataType; |
| 248 | } |
| 249 | $queryParams = array( |
| 250 | 1 => array($params['value'], |
| 251 | $dataType, |
| 252 | ), |
| 253 | 2 => array( |
| 254 | $params['optionId'], |
| 255 | 'Integer', |
| 256 | ), |
| 257 | ); |
| 258 | break; |
| 259 | |
| 260 | case 'AdvMulti-Select': |
| 261 | case 'Multi-Select': |
| 262 | case 'CheckBox': |
| 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; |
| 265 | $query = " |
| 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'), |
| 270 | ); |
| 271 | break; |
| 272 | |
| 273 | default: |
| 274 | CRM_Core_Error::fatal(); |
| 275 | } |
| 276 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static function valuesByID($customFieldID, $optionGroupID = NULL) { |
| 281 | if (!$optionGroupID) { |
| 282 | $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', |
| 283 | $customFieldID, |
| 284 | 'option_group_id' |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | $options = $optionGroupID ? CRM_Core_OptionGroup::valuesByID($optionGroupID) : array(); |
| 289 | |
| 290 | CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE); |
| 291 | |
| 292 | return $options; |
| 293 | } |
| 294 | } |
| 295 | |