| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 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-2016 |
| 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 $inactiveNeeded Do we need inactive options ?. |
| 68 | * Do we need inactive options ?. |
| 69 | * |
| 70 | * @return array |
| 71 | * all active options for fieldId |
| 72 | */ |
| 73 | public static function getCustomOption( |
| 74 | $fieldID, |
| 75 | $inactiveNeeded = FALSE |
| 76 | ) { |
| 77 | $options = array(); |
| 78 | if (!$fieldID) { |
| 79 | return $options; |
| 80 | } |
| 81 | |
| 82 | $optionValues = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, array(), $inactiveNeeded ? 'get' : 'create'); |
| 83 | |
| 84 | foreach ((array) $optionValues as $value => $label) { |
| 85 | $options[] = array( |
| 86 | 'label' => $label, |
| 87 | 'value' => $value, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | return $options; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Wrapper for ajax option selector. |
| 96 | * |
| 97 | * @param array $params |
| 98 | * Associated array for params record id. |
| 99 | * |
| 100 | * @return array |
| 101 | * associated array of option list |
| 102 | * -rp = rowcount |
| 103 | * -page= offset |
| 104 | */ |
| 105 | static public function getOptionListSelector(&$params) { |
| 106 | $options = array(); |
| 107 | |
| 108 | $field = CRM_Core_BAO_CustomField::getFieldObject($params['fid']); |
| 109 | $defVal = CRM_Utils_Array::explodePadded($field->default_value); |
| 110 | |
| 111 | // format the params |
| 112 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
| 113 | $params['rowCount'] = $params['rp']; |
| 114 | |
| 115 | if (!$field->option_group_id) { |
| 116 | return $options; |
| 117 | } |
| 118 | $queryParams = array(1 => array($field->option_group_id, 'Integer')); |
| 119 | $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1"; |
| 120 | $params['total'] = CRM_Core_DAO::singleValueQuery($total, $queryParams); |
| 121 | |
| 122 | $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; |
| 123 | $orderBy = ' ORDER BY options.weight asc'; |
| 124 | |
| 125 | $query = "SELECT * FROM civicrm_option_value as options WHERE option_group_id = %1 {$orderBy} {$limit}"; |
| 126 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 127 | $links = CRM_Custom_Page_Option::actionLinks(); |
| 128 | |
| 129 | $fields = array('id', 'label', 'value'); |
| 130 | $config = CRM_Core_Config::singleton(); |
| 131 | while ($dao->fetch()) { |
| 132 | $options[$dao->id] = array(); |
| 133 | foreach ($fields as $k) { |
| 134 | $options[$dao->id][$k] = $dao->$k; |
| 135 | } |
| 136 | $action = array_sum(array_keys($links)); |
| 137 | $class = 'crm-entity'; |
| 138 | // update enable/disable links depending on custom_field properties. |
| 139 | if ($dao->is_active) { |
| 140 | $action -= CRM_Core_Action::ENABLE; |
| 141 | } |
| 142 | else { |
| 143 | $class .= ' disabled'; |
| 144 | $action -= CRM_Core_Action::DISABLE; |
| 145 | } |
| 146 | if (in_array($field->html_type, array('CheckBox', 'AdvMulti-Select', 'Multi-Select'))) { |
| 147 | if (in_array($dao->value, $defVal)) { |
| 148 | $options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />'; |
| 149 | } |
| 150 | else { |
| 151 | $options[$dao->id]['is_default'] = ''; |
| 152 | } |
| 153 | } |
| 154 | else { |
| 155 | if ($field->default_value == $dao->value) { |
| 156 | $options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />'; |
| 157 | } |
| 158 | else { |
| 159 | $options[$dao->id]['is_default'] = ''; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $options[$dao->id]['class'] = $dao->id . ',' . $class; |
| 164 | $options[$dao->id]['is_active'] = empty($dao->is_active) ? ts('No') : ts('Yes'); |
| 165 | $options[$dao->id]['links'] = CRM_Core_Action::formLink($links, |
| 166 | $action, |
| 167 | array( |
| 168 | 'id' => $dao->id, |
| 169 | 'fid' => $params['fid'], |
| 170 | 'gid' => $params['gid'], |
| 171 | ), |
| 172 | ts('more'), |
| 173 | FALSE, |
| 174 | 'customOption.row.actions', |
| 175 | 'customOption', |
| 176 | $dao->id |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | return $options; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Delete Option. |
| 185 | * |
| 186 | * @param $optionId integer |
| 187 | * option id |
| 188 | * |
| 189 | */ |
| 190 | public static function del($optionId) { |
| 191 | // get the customFieldID |
| 192 | $query = " |
| 193 | SELECT f.id as id, f.data_type as dataType |
| 194 | FROM civicrm_option_value v, |
| 195 | civicrm_option_group g, |
| 196 | civicrm_custom_field f |
| 197 | WHERE v.id = %1 |
| 198 | AND g.id = f.option_group_id |
| 199 | AND g.id = v.option_group_id"; |
| 200 | $params = array(1 => array($optionId, 'Integer')); |
| 201 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 202 | if ($dao->fetch()) { |
| 203 | if (in_array($dao->dataType, |
| 204 | array('Int', 'Float', 'Money', 'Boolean') |
| 205 | )) { |
| 206 | $value = 0; |
| 207 | } |
| 208 | else { |
| 209 | $value = ''; |
| 210 | } |
| 211 | $params = array( |
| 212 | 'optionId' => $optionId, |
| 213 | 'fieldId' => $dao->id, |
| 214 | 'value' => $value, |
| 215 | ); |
| 216 | // delete this value from the tables |
| 217 | self::updateCustomValues($params); |
| 218 | |
| 219 | // also delete this option value |
| 220 | $query = " |
| 221 | DELETE |
| 222 | FROM civicrm_option_value |
| 223 | WHERE id = %1"; |
| 224 | $params = array(1 => array($optionId, 'Integer')); |
| 225 | CRM_Core_DAO::executeQuery($query, $params); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @param array $params |
| 231 | * |
| 232 | * @throws Exception |
| 233 | */ |
| 234 | public static function updateCustomValues($params) { |
| 235 | $optionDAO = new CRM_Core_DAO_OptionValue(); |
| 236 | $optionDAO->id = $params['optionId']; |
| 237 | $optionDAO->find(TRUE); |
| 238 | $oldValue = $optionDAO->value; |
| 239 | |
| 240 | // get the table, column, html_type and data type for this field |
| 241 | $query = " |
| 242 | SELECT g.table_name as tableName , |
| 243 | f.column_name as columnName, |
| 244 | f.data_type as dataType, |
| 245 | f.html_type as htmlType |
| 246 | FROM civicrm_custom_group g, |
| 247 | civicrm_custom_field f |
| 248 | WHERE f.custom_group_id = g.id |
| 249 | AND f.id = %1"; |
| 250 | $queryParams = array(1 => array($params['fieldId'], 'Integer')); |
| 251 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 252 | if ($dao->fetch()) { |
| 253 | if ($dao->dataType == 'Money') { |
| 254 | $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']); |
| 255 | } |
| 256 | switch ($dao->htmlType) { |
| 257 | case 'Autocomplete-Select': |
| 258 | case 'Select': |
| 259 | case 'Radio': |
| 260 | $query = " |
| 261 | UPDATE {$dao->tableName} |
| 262 | SET {$dao->columnName} = %1 |
| 263 | WHERE id = %2"; |
| 264 | if ($dao->dataType == 'Auto-complete') { |
| 265 | $dataType = "String"; |
| 266 | } |
| 267 | else { |
| 268 | $dataType = $dao->dataType; |
| 269 | } |
| 270 | $queryParams = array( |
| 271 | 1 => array( |
| 272 | $params['value'], |
| 273 | $dataType, |
| 274 | ), |
| 275 | 2 => array( |
| 276 | $params['optionId'], |
| 277 | 'Integer', |
| 278 | ), |
| 279 | ); |
| 280 | break; |
| 281 | |
| 282 | case 'AdvMulti-Select': |
| 283 | case 'Multi-Select': |
| 284 | case 'CheckBox': |
| 285 | $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR; |
| 286 | $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR; |
| 287 | $query = " |
| 288 | UPDATE {$dao->tableName} |
| 289 | SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )"; |
| 290 | $queryParams = array( |
| 291 | 1 => array($oldString, 'String'), |
| 292 | 2 => array($newString, 'String'), |
| 293 | ); |
| 294 | break; |
| 295 | |
| 296 | default: |
| 297 | CRM_Core_Error::fatal(); |
| 298 | } |
| 299 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * When changing the value of an option this is called to update all corresponding custom data |
| 305 | * |
| 306 | * @param int $optionId |
| 307 | * @param string $newValue |
| 308 | */ |
| 309 | public static function updateValue($optionId, $newValue) { |
| 310 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 311 | $optionValue->id = $optionId; |
| 312 | $optionValue->find(TRUE); |
| 313 | $oldValue = $optionValue->value; |
| 314 | if ($oldValue == $newValue) { |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | $customField = new CRM_Core_DAO_CustomField(); |
| 319 | $customField->option_group_id = $optionValue->option_group_id; |
| 320 | $customField->find(); |
| 321 | while ($customField->fetch()) { |
| 322 | $customGroup = new CRM_Core_DAO_CustomGroup(); |
| 323 | $customGroup->id = $customField->custom_group_id; |
| 324 | $customGroup->find(TRUE); |
| 325 | if (CRM_Core_BAO_CustomField::isSerialized($customField)) { |
| 326 | $params = array( |
| 327 | 1 => array(CRM_Utils_Array::implodePadded($oldValue), 'String'), |
| 328 | 2 => array(CRM_Utils_Array::implodePadded($newValue), 'String'), |
| 329 | 3 => array('%' . CRM_Utils_Array::implodePadded($oldValue) . '%', 'String'), |
| 330 | ); |
| 331 | } |
| 332 | else { |
| 333 | $params = array( |
| 334 | 1 => array($oldValue, 'String'), |
| 335 | 2 => array($newValue, 'String'), |
| 336 | 3 => array($oldValue, 'String'), |
| 337 | ); |
| 338 | } |
| 339 | $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3"; |
| 340 | $customGroup->free(); |
| 341 | CRM_Core_DAO::executeQuery($sql, $params); |
| 342 | } |
| 343 | $customField->free(); |
| 344 | } |
| 345 | |
| 346 | } |