| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * BAO object for civicrm_setting table. This table is used to store civicrm settings that are not used |
| 38 | * very frequently (i.e. not on every page load) |
| 39 | * |
| 40 | * The group column is used for grouping together all settings that logically belong to the same set. |
| 41 | * Thus all settings in the same group are retrieved with one DB call and then cached for future needs. |
| 42 | * |
| 43 | */ |
| 44 | class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { |
| 45 | |
| 46 | /** |
| 47 | * various predefined settings that have been migrated to the setting table |
| 48 | */ |
| 49 | CONST |
| 50 | ADDRESS_STANDARDIZATION_PREFERENCES_NAME = 'Address Standardization Preferences', |
| 51 | CAMPAIGN_PREFERENCES_NAME = 'Campaign Preferences', |
| 52 | DIRECTORY_PREFERENCES_NAME = 'Directory Preferences', |
| 53 | EVENT_PREFERENCES_NAME = 'Event Preferences', |
| 54 | MAILING_PREFERENCES_NAME = 'Mailing Preferences', |
| 55 | CONTRIBUTE_PREFERENCES_NAME = 'Contribute Preferences', |
| 56 | MEMBER_PREFERENCES_NAME = 'Member Preferences', |
| 57 | MULTISITE_PREFERENCES_NAME = 'Multi Site Preferences', |
| 58 | PERSONAL_PREFERENCES_NAME = 'Personal Preferences', |
| 59 | SYSTEM_PREFERENCES_NAME = 'CiviCRM Preferences', |
| 60 | URL_PREFERENCES_NAME = 'URL Preferences', |
| 61 | LOCALIZATION_PREFERENCES_NAME = 'Localization Preferences', |
| 62 | SEARCH_PREFERENCES_NAME = 'Search Preferences'; |
| 63 | static $_cache = NULL; |
| 64 | |
| 65 | /** |
| 66 | * Checks whether an item is present in the in-memory cache table |
| 67 | * |
| 68 | * @param string $group (required) The group name of the item |
| 69 | * @param string $name (required) The name of the setting |
| 70 | * @param int $componentID The optional component ID (so componenets can share the same name space) |
| 71 | * @param int $contactID If set, this is a contactID specific setting, else its a global setting |
| 72 | * @param int $load if true, load from local cache (typically memcache) |
| 73 | * |
| 74 | * @return boolean true if item is already in cache |
| 75 | * @static |
| 76 | * @access public |
| 77 | */ |
| 78 | static function inCache( |
| 79 | $group, |
| 80 | $name, |
| 81 | $componentID = NULL, |
| 82 | $contactID = NULL, |
| 83 | $load = FALSE, |
| 84 | $domainID = NULL, |
| 85 | $force = FALSE |
| 86 | ) { |
| 87 | if (!isset(self::$_cache)) { |
| 88 | self::$_cache = array(); |
| 89 | } |
| 90 | |
| 91 | $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}"; |
| 92 | |
| 93 | if ( |
| 94 | $load && |
| 95 | ($force || !isset(self::$_cache[$cacheKey])) |
| 96 | ) { |
| 97 | |
| 98 | // check in civi cache if present (typically memcache) |
| 99 | $globalCache = CRM_Utils_Cache::singleton(); |
| 100 | $result = $globalCache->get($cacheKey); |
| 101 | if ($result) { |
| 102 | |
| 103 | self::$_cache[$cacheKey] = $result; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return isset(self::$_cache[$cacheKey]) ? $cacheKey : NULL; |
| 108 | } |
| 109 | /** |
| 110 | * Allow key o be cleared |
| 111 | * @param string $cacheKey |
| 112 | */ |
| 113 | static function flushCache($cacheKey){ |
| 114 | unset(self::$_cache[$cacheKey]); |
| 115 | $globalCache = CRM_Utils_Cache::singleton(); |
| 116 | $globalCache->delete($cacheKey); |
| 117 | } |
| 118 | |
| 119 | static function setCache($values, |
| 120 | $group, |
| 121 | $componentID = NULL, |
| 122 | $contactID = NULL, |
| 123 | $domainID = NULL |
| 124 | ) { |
| 125 | if (!isset(self::$_cache)) { |
| 126 | self::$_cache = array(); |
| 127 | } |
| 128 | |
| 129 | $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}"; |
| 130 | |
| 131 | self::$_cache[$cacheKey] = $values; |
| 132 | |
| 133 | $globalCache = CRM_Utils_Cache::singleton(); |
| 134 | $result = $globalCache->set($cacheKey, $values); |
| 135 | |
| 136 | return $cacheKey; |
| 137 | } |
| 138 | |
| 139 | static function dao($group, |
| 140 | $name = NULL, |
| 141 | $componentID = NULL, |
| 142 | $contactID = NULL, |
| 143 | $domainID = NULL |
| 144 | ) { |
| 145 | if (self::isUpgradeFromPreFourOneAlpha1()) { |
| 146 | // civicrm_setting table is not going to be present. For now we'll just |
| 147 | // return a dummy object |
| 148 | $dao = new CRM_Core_DAO_Domain(); |
| 149 | $dao->id = -1; // so ->find() doesn't fetch any data later on |
| 150 | return $dao; |
| 151 | } |
| 152 | $dao = new CRM_Core_DAO_Setting(); |
| 153 | |
| 154 | $dao->group_name = $group; |
| 155 | $dao->name = $name; |
| 156 | $dao->component_id = $componentID; |
| 157 | if (empty($domainID)) { |
| 158 | $dao->domain_id = CRM_Core_Config::domainID(); |
| 159 | } |
| 160 | else { |
| 161 | $dao->domain_id = $domainID; |
| 162 | } |
| 163 | |
| 164 | if ($contactID) { |
| 165 | $dao->contact_id = $contactID; |
| 166 | $dao->is_domain = 0; |
| 167 | } |
| 168 | else { |
| 169 | $dao->is_domain = 1; |
| 170 | } |
| 171 | |
| 172 | return $dao; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Retrieve the value of a setting from the DB table |
| 177 | * |
| 178 | * @param string $group (required) The group name of the item |
| 179 | * @param string $name (required) The name under which this item is stored |
| 180 | * @param int $componentID The optional component ID (so componenets can share the same name space) |
| 181 | * @param string $defaultValue The default value to return for this setting if not present in DB |
| 182 | * @param int $contactID If set, this is a contactID specific setting, else its a global setting |
| 183 | |
| 184 | * |
| 185 | * @return object The data if present in the setting table, else null |
| 186 | * @static |
| 187 | * @access public |
| 188 | */ |
| 189 | static function getItem( |
| 190 | $group, |
| 191 | $name = NULL, |
| 192 | $componentID = NULL, |
| 193 | $defaultValue = NULL, |
| 194 | $contactID = NULL, |
| 195 | $domainID = NULL |
| 196 | ) { |
| 197 | |
| 198 | if (NULL !== ($override = self::getOverride($group, $name, NULL))) { |
| 199 | return $override; |
| 200 | } |
| 201 | |
| 202 | if (empty($domainID)) { |
| 203 | $domainID = CRM_Core_Config::domainID(); |
| 204 | } |
| 205 | $cacheKey = self::inCache($group, $name, $componentID, $contactID, TRUE, $domainID); |
| 206 | if (!$cacheKey) { |
| 207 | $dao = self::dao($group, NULL, $componentID, $contactID, $domainID); |
| 208 | $dao->find(); |
| 209 | |
| 210 | $values = array(); |
| 211 | while ($dao->fetch()) { |
| 212 | if (NULL !== ($override = self::getOverride($group, $dao->name, NULL))) { |
| 213 | $values[$dao->name] = $override; |
| 214 | } |
| 215 | elseif ($dao->value) { |
| 216 | $values[$dao->name] = unserialize($dao->value); |
| 217 | } |
| 218 | else { |
| 219 | $values[$dao->name] = NULL; |
| 220 | } |
| 221 | } |
| 222 | $dao->free(); |
| 223 | |
| 224 | $cacheKey = self::setCache($values, $group, $componentID, $contactID, $domainID); |
| 225 | } |
| 226 | return $name ? CRM_Utils_Array::value($name, self::$_cache[$cacheKey], $defaultValue) : self::$_cache[$cacheKey]; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Store multiple items in the setting table |
| 231 | * |
| 232 | * @param array $params (required) An api formatted array of keys and values |
| 233 | * @domains array an array of domains to get settings for. Default is the current domain |
| 234 | * @return void |
| 235 | * @static |
| 236 | * @access public |
| 237 | */ |
| 238 | static function getItems(&$params, $domains = NULL, $settingsToReturn) { |
| 239 | $originalDomain = CRM_Core_Config::domainID(); |
| 240 | if (empty($domains)) { |
| 241 | $domains[] = $originalDomain; |
| 242 | } |
| 243 | if (!empty($settingsToReturn) && !is_array($settingsToReturn)) { |
| 244 | $settingsToReturn = array($settingsToReturn); |
| 245 | } |
| 246 | $reloadConfig = FALSE; |
| 247 | |
| 248 | $fields = $result = array(); |
| 249 | $fieldsToGet = self::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE); |
| 250 | foreach ($domains as $domainID) { |
| 251 | if($domainID != CRM_Core_Config::domainID()){ |
| 252 | $reloadConfig = TRUE; |
| 253 | CRM_Core_BAO_Domain::setDomain($domainID); |
| 254 | } |
| 255 | $config = CRM_Core_Config::singleton($reloadConfig, $reloadConfig); |
| 256 | $result[$domainID] = array(); |
| 257 | foreach ($fieldsToGet as $name => $value) { |
| 258 | if(!empty($fields['values'][$name]['prefetch'])){ |
| 259 | if(isset($params['filters']) && isset($params['filters']['prefetch']) |
| 260 | && $params['filters']['prefetch'] == 0){ |
| 261 | // we are filtering out the prefetches from the return array |
| 262 | // so we will skip |
| 263 | continue; |
| 264 | } |
| 265 | $configKey = CRM_Utils_Array::value('config_key', $fields['values'][$name], $name); |
| 266 | if(isset($config->$configKey)){ |
| 267 | $setting = $config->$configKey; |
| 268 | } |
| 269 | } |
| 270 | else { |
| 271 | $setting = |
| 272 | CRM_Core_BAO_Setting::getItem( |
| 273 | $fields['values'][$name]['group_name'], |
| 274 | $name, |
| 275 | CRM_Utils_Array::value('component_id', $params), |
| 276 | NULL, |
| 277 | CRM_Utils_Array::value('contact_id', $params), |
| 278 | $domainID |
| 279 | ); |
| 280 | } |
| 281 | if (!is_null($setting)) { |
| 282 | // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones |
| 283 | // e.g for revert of fill actions |
| 284 | $result[$domainID][$name] = $setting; |
| 285 | } |
| 286 | } |
| 287 | CRM_Core_BAO_Domain::resetDomain(); |
| 288 | } |
| 289 | return $result; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Store an item in the setting table |
| 294 | * |
| 295 | * _setItem() is the common logic shared by setItem() and setItems(). |
| 296 | * |
| 297 | * @param object $value (required) The value that will be serialized and stored |
| 298 | * @param string $group (required) The group name of the item |
| 299 | * @param string $name (required) The name of the setting |
| 300 | * @param int $componentID The optional component ID (so componenets can share the same name space) |
| 301 | * @param int $createdID An optional ID to assign the creator to. If not set, retrieved from session |
| 302 | * |
| 303 | * @return void |
| 304 | * @static |
| 305 | * @access public |
| 306 | */ |
| 307 | static function setItem( |
| 308 | $value, |
| 309 | $group, |
| 310 | $name, |
| 311 | $componentID = NULL, |
| 312 | $contactID = NULL, |
| 313 | $createdID = NULL, |
| 314 | $domainID = NULL |
| 315 | ) { |
| 316 | $fields = array(); |
| 317 | $fieldsToSet = self::validateSettingsInput(array($name => $value), $fields); |
| 318 | //We haven't traditionally validated inputs to setItem, so this breaks things. |
| 319 | //foreach ($fieldsToSet as $settingField => &$settingValue) { |
| 320 | // self::validateSetting($settingValue, $fields['values'][$settingField]); |
| 321 | //} |
| 322 | |
| 323 | return self::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Store an item in a setting table |
| 328 | * |
| 329 | * _setItem() is the common logic shared by setItem() and setItems(). |
| 330 | * |
| 331 | * @param array $metadata metadata describing this field |
| 332 | * @param $value |
| 333 | * @param $group |
| 334 | * @param $name |
| 335 | * @param null $componentID |
| 336 | * @param null $contactID |
| 337 | * @param null $createdID |
| 338 | * @param null $domainID |
| 339 | */ |
| 340 | static function _setItem( |
| 341 | $metadata, |
| 342 | $value, |
| 343 | $group, |
| 344 | $name, |
| 345 | $componentID = NULL, |
| 346 | $contactID = NULL, |
| 347 | $createdID = NULL, |
| 348 | $domainID = NULL |
| 349 | ) { |
| 350 | if (empty($domainID)) { |
| 351 | $domainID = CRM_Core_Config::domainID(); |
| 352 | } |
| 353 | |
| 354 | $dao = self::dao($group, $name, $componentID, $contactID, $domainID); |
| 355 | $dao->find(TRUE); |
| 356 | |
| 357 | if (isset($metadata['on_change'])) { |
| 358 | foreach ($metadata['on_change'] as $callback) { |
| 359 | call_user_func($callback, unserialize($dao->value), $value, $metadata); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if (CRM_Utils_System::isNull($value)) { |
| 364 | $dao->value = 'null'; |
| 365 | } |
| 366 | else { |
| 367 | $dao->value = serialize($value); |
| 368 | } |
| 369 | |
| 370 | $dao->created_date = date('Ymdhis'); |
| 371 | |
| 372 | if ($createdID) { |
| 373 | $dao->created_id = $createdID; |
| 374 | } |
| 375 | else { |
| 376 | $session = CRM_Core_Session::singleton(); |
| 377 | $createdID = $session->get('userID'); |
| 378 | |
| 379 | if ($createdID) { |
| 380 | // ensure that this is a valid contact id (for session inconsistency rules) |
| 381 | $cid = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
| 382 | $createdID, |
| 383 | 'id', |
| 384 | 'id' |
| 385 | ); |
| 386 | if ($cid) { |
| 387 | $dao->created_id = $session->get('userID'); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | $dao->save(); |
| 393 | $dao->free(); |
| 394 | |
| 395 | // also save in cache if needed |
| 396 | $cacheKey = self::inCache($group, $name, $componentID, $contactID, FALSE, $domainID); |
| 397 | if ($cacheKey) { |
| 398 | self::$_cache[$cacheKey][$name] = $value; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Store multiple items in the setting table. Note that this will also store config keys |
| 404 | * the storage is determined by the metdata and is affected by |
| 405 | * 'name' setting's name |
| 406 | * 'prefetch' = store in config |
| 407 | * 'config_only' = don't store in settings |
| 408 | * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict |
| 409 | * 'legacy_key' = rename from config or setting with this name |
| 410 | * |
| 411 | * _setItem() is the common logic shared by setItem() and setItems(). |
| 412 | * |
| 413 | * @param array $params (required) An api formatted array of keys and values |
| 414 | * @domains array an array of domains to get settings for. Default is the current domain |
| 415 | * @return void |
| 416 | * @static |
| 417 | * @access public |
| 418 | */ |
| 419 | static function setItems(&$params, $domains = NULL) { |
| 420 | $originalDomain = CRM_Core_Config::domainID(); |
| 421 | if (empty($domains)) { |
| 422 | $domains[] = $originalDomain; |
| 423 | } |
| 424 | $reloadConfig = FALSE; |
| 425 | $fields = $config_keys = array(); |
| 426 | $fieldsToSet = self::validateSettingsInput($params, $fields); |
| 427 | |
| 428 | foreach ($fieldsToSet as $settingField => &$settingValue) { |
| 429 | self::validateSetting($settingValue, $fields['values'][$settingField]); |
| 430 | } |
| 431 | |
| 432 | foreach ($domains as $domainID) { |
| 433 | if($domainID != CRM_Core_Config::domainID()){ |
| 434 | $reloadConfig = TRUE; |
| 435 | CRM_Core_BAO_Domain::setDomain($domainID); |
| 436 | } |
| 437 | $result[$domainID] = array(); |
| 438 | foreach ($fieldsToSet as $name => $value) { |
| 439 | if(empty($fields['values'][$name]['config_only'])){ |
| 440 | CRM_Core_BAO_Setting::_setItem( |
| 441 | $fields['values'][$name], |
| 442 | $value, |
| 443 | $fields['values'][$name]['group_name'], |
| 444 | $name, |
| 445 | CRM_Utils_Array::value('component_id', $params), |
| 446 | CRM_Utils_Array::value('contact_id', $params), |
| 447 | CRM_Utils_Array::value('created_id', $params), |
| 448 | $domainID |
| 449 | ); |
| 450 | } |
| 451 | if(!empty($fields['values'][$name]['prefetch'])){ |
| 452 | if(!empty($fields['values'][$name]['config_key'])){ |
| 453 | $name = $fields['values'][$name]['config_key']; |
| 454 | } |
| 455 | $config_keys[$name] = $value; |
| 456 | } |
| 457 | $result[$domainID][$name] = $value; |
| 458 | } |
| 459 | if($reloadConfig){ |
| 460 | CRM_Core_Config::singleton($reloadConfig, $reloadConfig); |
| 461 | } |
| 462 | |
| 463 | if(!empty($config_keys)){ |
| 464 | CRM_Core_BAO_ConfigSetting::create($config_keys); |
| 465 | } |
| 466 | if($reloadConfig){ |
| 467 | CRM_Core_BAO_Domain::resetDomain(); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return $result; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * gets metadata about the settings fields (from getfields) based on the fields being passed in |
| 476 | * |
| 477 | * This function filters on the fields like 'version' & 'debug' that are not settings |
| 478 | * @param array $params Parameters as passed into API |
| 479 | * @param array $fields empty array to be populated with fields metadata |
| 480 | * @param bool $createMode |
| 481 | * |
| 482 | * @return array $fieldstoset name => value array of the fields to be set (with extraneous removed) |
| 483 | */ |
| 484 | static function validateSettingsInput($params, &$fields, $createMode = TRUE) { |
| 485 | $group = CRM_Utils_Array::value('group', $params); |
| 486 | |
| 487 | $ignoredParams = array( |
| 488 | 'version', |
| 489 | 'id', |
| 490 | 'domain_id', |
| 491 | 'debug', |
| 492 | 'created_id', |
| 493 | 'component_id', |
| 494 | 'contact_id', |
| 495 | 'filters', |
| 496 | 'entity_id', |
| 497 | 'entity_table', |
| 498 | 'sequential', |
| 499 | 'api.has_parent', |
| 500 | 'IDS_request_uri', |
| 501 | 'IDS_user_agent', |
| 502 | 'check_permissions', |
| 503 | ); |
| 504 | $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE)); |
| 505 | $getFieldsParams = array('version' => 3); |
| 506 | if (count($settingParams) ==1) { |
| 507 | // ie we are only setting one field - we'll pass it into getfields for efficiency |
| 508 | list($name) = array_keys($settingParams); |
| 509 | $getFieldsParams['name'] = $name; |
| 510 | } |
| 511 | $fields = civicrm_api('setting','getfields', $getFieldsParams); |
| 512 | $invalidParams = (array_diff_key($settingParams, $fields['values'])); |
| 513 | if (!empty($invalidParams)) { |
| 514 | throw new api_Exception(implode(',', $invalidParams) . " not valid settings"); |
| 515 | } |
| 516 | if (!empty($settingParams)) { |
| 517 | $filteredFields = array_intersect_key($settingParams, $fields['values']); |
| 518 | } |
| 519 | else { |
| 520 | // no filters so we are interested in all for get mode. In create mode this means nothing to set |
| 521 | $filteredFields = $createMode ? array() : $fields['values']; |
| 522 | } |
| 523 | return $filteredFields; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Validate & convert settings input |
| 528 | * |
| 529 | * @value mixed value of the setting to be set |
| 530 | * @fieldSpec array Metadata for given field (drawn from the xml) |
| 531 | */ |
| 532 | static function validateSetting(&$value, $fieldSpec) { |
| 533 | if($fieldSpec['type'] == 'String' && is_array($value)){ |
| 534 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,$value) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 535 | } |
| 536 | if (empty($fieldSpec['validate_callback'])) { |
| 537 | return TRUE; |
| 538 | } |
| 539 | else { |
| 540 | list($class,$fn) = explode('::',$fieldSpec['validate_callback']); |
| 541 | if (!$class::$fn($value,$fieldSpec)) { |
| 542 | throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}"); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Validate & convert settings input - translate True False to 0 or 1 |
| 549 | * |
| 550 | * @value mixed value of the setting to be set |
| 551 | * @fieldSpec array Metadata for given field (drawn from the xml) |
| 552 | */ |
| 553 | static function validateBoolSetting(&$value, $fieldSpec) { |
| 554 | if (!CRM_Utils_Rule::boolean($value)) { |
| 555 | throw new api_Exception("Boolean value required for {$fieldSpec['name']}"); |
| 556 | } |
| 557 | if (!$value) { |
| 558 | $value = 0; |
| 559 | } |
| 560 | else { |
| 561 | $value = 1; |
| 562 | } |
| 563 | return TRUE; |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Delete some or all of the items in the settings table |
| 568 | * |
| 569 | * @param string $group The group name of the entries to be deleted |
| 570 | * @param string $name The name of the setting to be deleted |
| 571 | * @param int $componentID The optional component ID (so componenets can share the same name space) |
| 572 | * |
| 573 | * @return void |
| 574 | * @static |
| 575 | * @access public |
| 576 | * |
| 577 | static function deleteItem($group, $name = NULL, $componentID = NULL, $contactID = NULL) { |
| 578 | $dao = self::dao($group, $name, $componentID, $contactID); |
| 579 | $dao->delete(); |
| 580 | |
| 581 | // also reset memory cache if any |
| 582 | CRM_Utils_System::flushCache(); |
| 583 | |
| 584 | $cacheKey = self::inCache($group, $name, $componentID, $contactID, FALSE); |
| 585 | if ($cacheKey) { |
| 586 | if ($name) { |
| 587 | unset(self::$_cache[$cacheKey][$name]); |
| 588 | } |
| 589 | else { |
| 590 | unset(self::$_cache[$cacheKey]); |
| 591 | } |
| 592 | } |
| 593 | }*/ |
| 594 | |
| 595 | /** |
| 596 | * This provides information about the setting - similar to the fields concept for DAO information. |
| 597 | * As the setting is serialized code creating validation setting input needs to know the data type |
| 598 | * This also helps move information out of the form layer into the data layer where people can interact with |
| 599 | * it via the API or other mechanisms. In order to keep this consistent it is important the form layer |
| 600 | * also leverages it. |
| 601 | * |
| 602 | * Note that this function should never be called when using the runtime getvalue function. Caching works |
| 603 | * around the expectation it will be called during setting administration |
| 604 | * |
| 605 | * Function is intended for configuration rather than runtime access to settings |
| 606 | * |
| 607 | * The following params will filter the result. If none are passed all settings will be returns |
| 608 | * |
| 609 | * @params string $name Name of specific setting e.g customCSSURL |
| 610 | * @params integer $componentID id of relevant component. |
| 611 | * |
| 612 | * @return array $result - the following information as appropriate for each setting |
| 613 | * - name |
| 614 | * - type |
| 615 | * - default |
| 616 | * - add (CiviCRM version added) |
| 617 | * - is_domain |
| 618 | * - is_contact |
| 619 | * - description |
| 620 | * - help_text |
| 621 | */ |
| 622 | static function getSettingSpecification( |
| 623 | $componentID = NULL, |
| 624 | $filters = array(), |
| 625 | $domainID = NULL, |
| 626 | $profile = NULL |
| 627 | ) { |
| 628 | $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile; |
| 629 | foreach ($filters as $filterField => $filterString) { |
| 630 | $cacheString .= "_{$filterField}_{$filterString}"; |
| 631 | } |
| 632 | $cached = 1; |
| 633 | // the caching into 'All' seems to be a duplicate of caching to |
| 634 | // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook |
| 635 | $settingsMetadata = CRM_Core_BAO_Cache::getItem('CiviCRM setting Specs', $cacheString, $componentID); |
| 636 | if ($settingsMetadata === NULL) { |
| 637 | $settingsMetadata = CRM_Core_BAO_Cache::getItem('CiviCRM setting Spec', 'All', $componentID); |
| 638 | if (empty($settingsMetadata)) { |
| 639 | $settingsMetadata = array(); |
| 640 | global $civicrm_root; |
| 641 | $metaDataFolders = array($civicrm_root. '/settings'); |
| 642 | CRM_Utils_Hook::alterSettingsFolders($metaDataFolders); |
| 643 | foreach ($metaDataFolders as $metaDataFolder) { |
| 644 | $settingsMetadata = $settingsMetadata + self::loadSettingsMetaData($metaDataFolder); |
| 645 | } |
| 646 | CRM_Core_BAO_Cache::setItem($settingsMetadata,'CiviCRM setting Spec', 'All', $componentID); |
| 647 | } |
| 648 | $cached = 0; |
| 649 | } |
| 650 | |
| 651 | CRM_Utils_Hook::alterSettingsMetaData($settingsMetadata, $domainID, $profile); |
| 652 | self::_filterSettingsSpecification($filters, $settingsMetadata); |
| 653 | |
| 654 | if (!$cached) { |
| 655 | // this is a bit 'heavy' if you are using hooks but this function |
| 656 | // is expected to only be called during setting administration |
| 657 | // it should not be called by 'getvalue' or 'getitem |
| 658 | CRM_Core_BAO_Cache::setItem( |
| 659 | $settingsMetadata, |
| 660 | 'CiviCRM setting Specs', |
| 661 | $cacheString, |
| 662 | $componentID |
| 663 | ); |
| 664 | } |
| 665 | return $settingsMetadata; |
| 666 | |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Load up settings metadata from files |
| 671 | */ |
| 672 | static function loadSettingsMetadata($metaDataFolder) { |
| 673 | $settingMetaData = array(); |
| 674 | $settingsFiles = CRM_Utils_File::findFiles($metaDataFolder, '*.setting.php'); |
| 675 | foreach ($settingsFiles as $file) { |
| 676 | $settings = include $file; |
| 677 | $settingMetaData = array_merge($settingMetaData, $settings); |
| 678 | } |
| 679 | CRM_Core_BAO_Cache::setItem($settingMetaData,'CiviCRM setting Spec', 'All'); |
| 680 | return $settingMetaData; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Filter the settings metadata according to filters passed in. This is a convenience filter |
| 685 | * and allows selective reverting / filling of settings |
| 686 | * |
| 687 | * @param array $filters Filters to match against data |
| 688 | * @param array $settingSpec metadata to filter |
| 689 | */ |
| 690 | static function _filterSettingsSpecification($filters, &$settingSpec) { |
| 691 | if (empty($filters)) { |
| 692 | return; |
| 693 | } |
| 694 | else if (array_keys($filters) == array('name')) { |
| 695 | $settingSpec = array($filters['name'] => CRM_Utils_Array::value($filters['name'], $settingSpec, '')); |
| 696 | return; |
| 697 | } |
| 698 | else { |
| 699 | foreach ($settingSpec as $field => $fieldValues) { |
| 700 | if (array_intersect_assoc($fieldValues, $filters) != $filters) { |
| 701 | unset($settingSpec[$field]); |
| 702 | } |
| 703 | } |
| 704 | return; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Look for any missing settings and convert them from config or load default as appropriate |
| 710 | * This should be run from GenCode & also from upgrades to add any new defaults. |
| 711 | * |
| 712 | * Multisites have often been overlooked in upgrade scripts so can be expected to be missing |
| 713 | * a number of settings |
| 714 | */ |
| 715 | static function updateSettingsFromMetaData() { |
| 716 | $apiParams = array( |
| 717 | 'version' => 3, |
| 718 | 'domain_id' => 'all', |
| 719 | 'filters' => array('prefetch' => 0), |
| 720 | ); |
| 721 | $existing = civicrm_api('setting', 'get', $apiParams); |
| 722 | |
| 723 | if (!empty($existing['values'])) { |
| 724 | $allSettings = civicrm_api('setting', 'getfields', array('version' => 3)); |
| 725 | foreach ($existing['values'] as $domainID => $domainSettings) { |
| 726 | CRM_Core_BAO_Domain::setDomain($domainID); |
| 727 | $missing = array_diff_key($allSettings['values'], $domainSettings); |
| 728 | foreach ($missing as $name => $settings) { |
| 729 | self::convertConfigToSetting($name, $domainID); |
| 730 | } |
| 731 | CRM_Core_BAO_Domain::resetDomain(); |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * move an item from being in the config array to being stored as a setting |
| 738 | * remove from config - as appropriate based on metadata |
| 739 | * |
| 740 | * Note that where the key name is being changed the 'legacy_key' will give us the old name |
| 741 | */ |
| 742 | static function convertConfigToSetting($name, $domainID = NULL) { |
| 743 | // we have to force this here in case more than one domain is in play. |
| 744 | // whenever there is a possibility of more than one domain we must force it |
| 745 | $config = CRM_Core_Config::singleton(); |
| 746 | if (empty($domainID)) { |
| 747 | $domainID= CRM_Core_Config::domainID(); |
| 748 | } |
| 749 | $domain = new CRM_Core_DAO_Domain(); |
| 750 | $domain->id = $domainID; |
| 751 | $domain->find(TRUE); |
| 752 | if ($domain->config_backend) { |
| 753 | $values = unserialize($domain->config_backend); |
| 754 | } else { |
| 755 | $values = array(); |
| 756 | } |
| 757 | $spec = self::getSettingSpecification(NULL, array('name' => $name), $domainID); |
| 758 | $configKey = CRM_Utils_Array::value('config_key', $spec[$name], CRM_Utils_Array::value('legacy_key', $spec[$name], $name)); |
| 759 | //if the key is set to config_only we don't need to do anything |
| 760 | if(empty($spec[$name]['config_only'])){ |
| 761 | if (!empty($values[$configKey])) { |
| 762 | civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID)); |
| 763 | } |
| 764 | else { |
| 765 | civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID)); |
| 766 | } |
| 767 | |
| 768 | if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) { |
| 769 | unset($values[$configKey]); |
| 770 | $domain->config_backend = serialize($values); |
| 771 | $domain->save(); |
| 772 | unset($config->$configKey); |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | static function valueOptions($group, |
| 778 | $name, |
| 779 | $system = TRUE, |
| 780 | $userID = NULL, |
| 781 | $localize = FALSE, |
| 782 | $returnField = 'name', |
| 783 | $returnNameANDLabels = FALSE, |
| 784 | $condition = NULL |
| 785 | ) { |
| 786 | $optionValue = self::getItem($group, $name); |
| 787 | |
| 788 | $groupValues = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, $returnField); |
| 789 | |
| 790 | //enabled name => label require for new contact edit form, CRM-4605 |
| 791 | if ($returnNameANDLabels) { |
| 792 | $names = $labels = $nameAndLabels = array(); |
| 793 | if ($returnField == 'name') { |
| 794 | $names = $groupValues; |
| 795 | $labels = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, 'label'); |
| 796 | } |
| 797 | else { |
| 798 | $labels = $groupValues; |
| 799 | $names = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, 'name'); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | $returnValues = array(); |
| 804 | foreach ($groupValues as $gn => $gv) { |
| 805 | $returnValues[$gv] = 0; |
| 806 | } |
| 807 | |
| 808 | if ($optionValue && !empty($groupValues)) { |
| 809 | $dbValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 810 | substr($optionValue, 1, -1) |
| 811 | ); |
| 812 | |
| 813 | if (!empty($dbValues)) { |
| 814 | foreach ($groupValues as $key => $val) { |
| 815 | if (in_array($key, $dbValues)) { |
| 816 | $returnValues[$val] = 1; |
| 817 | if ($returnNameANDLabels) { |
| 818 | $nameAndLabels[$names[$key]] = $labels[$key]; |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | return ($returnNameANDLabels) ? $nameAndLabels : $returnValues; |
| 825 | } |
| 826 | |
| 827 | static function setValueOption($group, |
| 828 | $name, |
| 829 | $value, |
| 830 | $system = TRUE, |
| 831 | $userID = NULL, |
| 832 | $keyField = 'name' |
| 833 | ) { |
| 834 | if (empty($value)) { |
| 835 | $optionValue = NULL; |
| 836 | } |
| 837 | elseif (is_array($value)) { |
| 838 | $groupValues = CRM_Core_OptionGroup::values($name, FALSE, FALSE, FALSE, NULL, $keyField); |
| 839 | |
| 840 | $cbValues = array(); |
| 841 | foreach ($groupValues as $key => $val) { |
| 842 | if (!empty($value[$val])) { |
| 843 | $cbValues[$key] = 1; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | if (!empty($cbValues)) { |
| 848 | $optionValue = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 849 | array_keys($cbValues) |
| 850 | ) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 851 | } |
| 852 | else { |
| 853 | $optionValue = NULL; |
| 854 | } |
| 855 | } |
| 856 | else { |
| 857 | $optionValue = $value; |
| 858 | } |
| 859 | |
| 860 | self::setItem($optionValue, $group, $name); |
| 861 | } |
| 862 | |
| 863 | static function fixAndStoreDirAndURL(&$params, $domainID = NULL) { |
| 864 | if (self::isUpgradeFromPreFourOneAlpha1()) { |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | if (empty($domainID)) { |
| 869 | $domainID = CRM_Core_Config::domainID(); |
| 870 | } |
| 871 | $sql = " |
| 872 | SELECT name, group_name |
| 873 | FROM civicrm_setting |
| 874 | WHERE domain_id = %1 |
| 875 | AND ( group_name = %2 |
| 876 | OR group_name = %3 ) |
| 877 | "; |
| 878 | $sqlParams = array( |
| 879 | 1 => array($domainID, 'Integer'), |
| 880 | 2 => array(self::DIRECTORY_PREFERENCES_NAME, 'String'), |
| 881 | 3 => array(self::URL_PREFERENCES_NAME, 'String'), |
| 882 | ); |
| 883 | |
| 884 | $dirParams = array(); |
| 885 | $urlParams = array(); |
| 886 | |
| 887 | $dao = CRM_Core_DAO::executeQuery($sql, |
| 888 | $sqlParams, |
| 889 | TRUE, |
| 890 | NULL, |
| 891 | FALSE, |
| 892 | TRUE, |
| 893 | // trap exceptions as error |
| 894 | TRUE |
| 895 | ); |
| 896 | |
| 897 | if (is_a($dao, 'DB_Error')) { |
| 898 | if (CRM_Core_Config::isUpgradeMode()) { |
| 899 | // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue |
| 900 | return; |
| 901 | } |
| 902 | else { |
| 903 | echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n"; |
| 904 | exit(); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | while ($dao->fetch()) { |
| 909 | if (!isset($params[$dao->name])) { |
| 910 | continue; |
| 911 | } |
| 912 | if ($dao->group_name == self::DIRECTORY_PREFERENCES_NAME) { |
| 913 | $dirParams[$dao->name] = CRM_Utils_Array::value($dao->name, $params, ''); |
| 914 | } |
| 915 | else { |
| 916 | $urlParams[$dao->name] = CRM_Utils_Array::value($dao->name, $params, ''); |
| 917 | } |
| 918 | unset($params[$dao->name]); |
| 919 | } |
| 920 | |
| 921 | if (!empty($dirParams)) { |
| 922 | self::storeDirectoryOrURLPreferences($dirParams, |
| 923 | self::DIRECTORY_PREFERENCES_NAME |
| 924 | ); |
| 925 | } |
| 926 | |
| 927 | if (!empty($urlParams)) { |
| 928 | self::storeDirectoryOrURLPreferences($urlParams, |
| 929 | self::URL_PREFERENCES_NAME |
| 930 | ); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | static function storeDirectoryOrURLPreferences(&$params, $group) { |
| 935 | foreach ($params as $name => $value) { |
| 936 | // always try to store relative directory or url from CMS root |
| 937 | $value = ($group == self::DIRECTORY_PREFERENCES_NAME) ? CRM_Utils_File::relativeDirectory($value) : CRM_Utils_System::relativeURL($value); |
| 938 | |
| 939 | self::setItem($value, $group, $name); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) { |
| 944 | if (CRM_Core_Config::isUpgradeMode()) { |
| 945 | $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') ? TRUE : FALSE; |
| 946 | // hack to set the resource base url so that js/ css etc is loaded correctly |
| 947 | if ($isJoomla) { |
| 948 | $params['userFrameworkResourceURL'] = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/') . str_replace('administrator', '', CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name')); |
| 949 | } |
| 950 | if (self::isUpgradeFromPreFourOneAlpha1()) { |
| 951 | return; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | if ($setInConfig) { |
| 956 | $config = CRM_Core_Config::singleton(); |
| 957 | } |
| 958 | |
| 959 | $sql = " |
| 960 | SELECT name, group_name, value |
| 961 | FROM civicrm_setting |
| 962 | WHERE ( group_name = %1 |
| 963 | OR group_name = %2 ) |
| 964 | AND domain_id = %3 |
| 965 | "; |
| 966 | $sqlParams = array(1 => array(self::DIRECTORY_PREFERENCES_NAME, 'String'), |
| 967 | 2 => array(self::URL_PREFERENCES_NAME, 'String'), |
| 968 | 3 => array(CRM_Core_Config::domainID(), 'Integer'), |
| 969 | ); |
| 970 | |
| 971 | $dao = CRM_Core_DAO::executeQuery($sql, |
| 972 | $sqlParams, |
| 973 | TRUE, |
| 974 | NULL, |
| 975 | FALSE, |
| 976 | TRUE, |
| 977 | // trap exceptions as error |
| 978 | TRUE |
| 979 | ); |
| 980 | |
| 981 | if (is_a($dao, 'DB_Error')) { |
| 982 | echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n"; |
| 983 | exit(); |
| 984 | } |
| 985 | |
| 986 | while ($dao->fetch()) { |
| 987 | $value = self::getOverride($dao->group_name, $dao->name, NULL); |
| 988 | if ($value === NULL && $dao->value) { |
| 989 | $value = unserialize($dao->value); |
| 990 | if ($dao->group_name == self::DIRECTORY_PREFERENCES_NAME) { |
| 991 | $value = CRM_Utils_File::absoluteDirectory($value); |
| 992 | } |
| 993 | else { |
| 994 | // CRM-7622: we need to remove the language part |
| 995 | $value = CRM_Utils_System::absoluteURL($value, TRUE); |
| 996 | } |
| 997 | } |
| 998 | // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available |
| 999 | if (!isset($value) && !empty($params[$dao->name])) { |
| 1000 | $value = $params[$dao->name]; |
| 1001 | } |
| 1002 | $params[$dao->name] = $value; |
| 1003 | |
| 1004 | if ($setInConfig) { |
| 1005 | $config->{$dao->name} = $value; |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * Determine what, if any, overrides have been provided |
| 1012 | * for a setting. |
| 1013 | * |
| 1014 | * @return mixed, NULL or an overriden value |
| 1015 | */ |
| 1016 | protected static function getOverride($group, $name, $default) { |
| 1017 | global $civicrm_setting; |
| 1018 | if ($group && $name && isset($civicrm_setting[$group][$name])) { |
| 1019 | return $civicrm_setting[$group][$name]; |
| 1020 | } |
| 1021 | else { |
| 1022 | return $default; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade |
| 1028 | * |
| 1029 | * @return boolean |
| 1030 | */ |
| 1031 | static function isUpgradeFromPreFourOneAlpha1() { |
| 1032 | if (CRM_Core_Config::isUpgradeMode()) { |
| 1033 | $currentVer = CRM_Core_BAO_Domain::version(); |
| 1034 | if (version_compare($currentVer, '4.1.alpha1') < 0) { |
| 1035 | return TRUE; |
| 1036 | } |
| 1037 | } |
| 1038 | return FALSE; |
| 1039 | } |
| 1040 | } |