3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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)
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.
44 class CRM_Core_BAO_Setting
extends CRM_Core_DAO_Setting
{
47 * various predefined settings that have been migrated to the setting table
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;
66 * Checks whether an item is present in the in-memory cache table
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)
74 * @return boolean true if item is already in cache
78 static function inCache(
87 if (!isset(self
::$_cache)) {
88 self
::$_cache = array();
91 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
95 ($force ||
!isset(self
::$_cache[$cacheKey]))
98 // check in civi cache if present (typically memcache)
99 $globalCache = CRM_Utils_Cache
::singleton();
100 $result = $globalCache->get($cacheKey);
103 self
::$_cache[$cacheKey] = $result;
107 return isset(self
::$_cache[$cacheKey]) ?
$cacheKey : NULL;
110 * Allow key o be cleared
111 * @param string $cacheKey
113 static function flushCache($cacheKey){
114 unset(self
::$_cache[$cacheKey]);
115 $globalCache = CRM_Utils_Cache
::singleton();
116 $globalCache->delete($cacheKey);
119 static function setCache($values,
125 if (!isset(self
::$_cache)) {
126 self
::$_cache = array();
129 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
131 self
::$_cache[$cacheKey] = $values;
133 $globalCache = CRM_Utils_Cache
::singleton();
134 $result = $globalCache->set($cacheKey, $values);
139 static function dao($group,
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
152 $dao = new CRM_Core_DAO_Setting();
154 $dao->group_name
= $group;
156 $dao->component_id
= $componentID;
157 if (empty($domainID)) {
158 $dao->domain_id
= CRM_Core_Config
::domainID();
161 $dao->domain_id
= $domainID;
165 $dao->contact_id
= $contactID;
176 * Retrieve the value of a setting from the DB table
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
185 * @return object The data if present in the setting table, else null
189 static function getItem(
193 $defaultValue = NULL,
198 if (NULL !== ($override = self
::getOverride($group, $name, NULL))) {
202 if (empty($domainID)) {
203 $domainID = CRM_Core_Config
::domainID();
205 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, TRUE, $domainID);
207 $dao = self
::dao($group, NULL, $componentID, $contactID, $domainID);
211 while ($dao->fetch()) {
212 if (NULL !== ($override = self
::getOverride($group, $dao->name
, NULL))) {
213 $values[$dao->name
] = $override;
215 elseif ($dao->value
) {
216 $values[$dao->name
] = unserialize($dao->value
);
219 $values[$dao->name
] = NULL;
224 $cacheKey = self
::setCache($values, $group, $componentID, $contactID, $domainID);
226 return $name ? CRM_Utils_Array
::value($name, self
::$_cache[$cacheKey], $defaultValue) : self
::$_cache[$cacheKey];
230 * Store multiple items in the setting table
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
238 static function getItems(&$params, $domains = NULL, $settingsToReturn) {
239 $originalDomain = CRM_Core_Config
::domainID();
240 if (empty($domains)) {
241 $domains[] = $originalDomain;
243 if (!empty($settingsToReturn) && !is_array($settingsToReturn)) {
244 $settingsToReturn = array($settingsToReturn);
246 $reloadConfig = FALSE;
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);
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
265 $configKey = CRM_Utils_Array
::value('config_key', $fields['values'][$name], $name);
266 if(isset($config->$configKey)){
267 $setting = $config->$configKey;
272 CRM_Core_BAO_Setting
::getItem(
273 $fields['values'][$name]['group_name'],
275 CRM_Utils_Array
::value('component_id', $params),
277 CRM_Utils_Array
::value('contact_id', $params),
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;
287 CRM_Core_BAO_Domain
::resetDomain();
293 * Store an item in the setting table
295 * _setItem() is the common logic shared by setItem() and setItems().
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
307 static function setItem(
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]);
323 return self
::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID);
327 * Store an item in a setting table
329 * _setItem() is the common logic shared by setItem() and setItems().
331 * @param array $metadata metadata describing this field
335 * @param null $componentID
336 * @param null $contactID
337 * @param null $createdID
338 * @param null $domainID
340 static function _setItem(
350 if (empty($domainID)) {
351 $domainID = CRM_Core_Config
::domainID();
354 $dao = self
::dao($group, $name, $componentID, $contactID, $domainID);
357 if (isset($metadata['on_change'])) {
358 foreach ($metadata['on_change'] as $callback) {
359 call_user_func($callback, unserialize($dao->value
), $value, $metadata);
363 if (CRM_Utils_System
::isNull($value)) {
364 $dao->value
= 'null';
367 $dao->value
= serialize($value);
370 $dao->created_date
= date('Ymdhis');
373 $dao->created_id
= $createdID;
376 $session = CRM_Core_Session
::singleton();
377 $createdID = $session->get('userID');
380 // ensure that this is a valid contact id (for session inconsistency rules)
381 $cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
387 $dao->created_id
= $session->get('userID');
395 // also save in cache if needed
396 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, FALSE, $domainID);
398 self
::$_cache[$cacheKey][$name] = $value;
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
411 * _setItem() is the common logic shared by setItem() and setItems().
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
419 static function setItems(&$params, $domains = NULL) {
420 $originalDomain = CRM_Core_Config
::domainID();
421 if (empty($domains)) {
422 $domains[] = $originalDomain;
424 $reloadConfig = FALSE;
425 $fields = $config_keys = array();
426 $fieldsToSet = self
::validateSettingsInput($params, $fields);
428 foreach ($fieldsToSet as $settingField => &$settingValue) {
429 self
::validateSetting($settingValue, $fields['values'][$settingField]);
432 foreach ($domains as $domainID) {
433 if($domainID != CRM_Core_Config
::domainID()){
434 $reloadConfig = TRUE;
435 CRM_Core_BAO_Domain
::setDomain($domainID);
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],
443 $fields['values'][$name]['group_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),
451 if(!empty($fields['values'][$name]['prefetch'])){
452 if(!empty($fields['values'][$name]['config_key'])){
453 $name = $fields['values'][$name]['config_key'];
455 $config_keys[$name] = $value;
457 $result[$domainID][$name] = $value;
460 CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
463 if(!empty($config_keys)){
464 CRM_Core_BAO_ConfigSetting
::create($config_keys);
467 CRM_Core_BAO_Domain
::resetDomain();
475 * gets metadata about the settings fields (from getfields) based on the fields being passed in
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
482 * @return array $fieldstoset name => value array of the fields to be set (with extraneous removed)
484 static function validateSettingsInput($params, &$fields, $createMode = TRUE) {
485 $group = CRM_Utils_Array
::value('group', $params);
487 $ignoredParams = array(
505 $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE));
506 $getFieldsParams = array('version' => 3);
507 if (count($settingParams) ==1) {
508 // ie we are only setting one field - we'll pass it into getfields for efficiency
509 list($name) = array_keys($settingParams);
510 $getFieldsParams['name'] = $name;
512 $fields = civicrm_api('setting','getfields', $getFieldsParams);
513 $invalidParams = (array_diff_key($settingParams, $fields['values']));
514 if (!empty($invalidParams)) {
515 throw new api_Exception(implode(',', $invalidParams) . " not valid settings");
517 if (!empty($settingParams)) {
518 $filteredFields = array_intersect_key($settingParams, $fields['values']);
521 // no filters so we are interested in all for get mode. In create mode this means nothing to set
522 $filteredFields = $createMode ?
array() : $fields['values'];
524 return $filteredFields;
528 * Validate & convert settings input
530 * @value mixed value of the setting to be set
531 * @fieldSpec array Metadata for given field (drawn from the xml)
533 static function validateSetting(&$value, $fieldSpec) {
534 if($fieldSpec['type'] == 'String' && is_array($value)){
535 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,$value) . CRM_Core_DAO
::VALUE_SEPARATOR
;
537 if (empty($fieldSpec['validate_callback'])) {
541 list($class,$fn) = explode('::',$fieldSpec['validate_callback']);
542 if (!$class::$fn($value,$fieldSpec)) {
543 throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}");
549 * Validate & convert settings input - translate True False to 0 or 1
551 * @value mixed value of the setting to be set
552 * @fieldSpec array Metadata for given field (drawn from the xml)
554 static function validateBoolSetting(&$value, $fieldSpec) {
555 if (!CRM_Utils_Rule
::boolean($value)) {
556 throw new api_Exception("Boolean value required for {$fieldSpec['name']}");
568 * Delete some or all of the items in the settings table
570 * @param string $group The group name of the entries to be deleted
571 * @param string $name The name of the setting to be deleted
572 * @param int $componentID The optional component ID (so componenets can share the same name space)
578 static function deleteItem($group, $name = NULL, $componentID = NULL, $contactID = NULL) {
579 $dao = self::dao($group, $name, $componentID, $contactID);
582 // also reset memory cache if any
583 CRM_Utils_System::flushCache();
585 $cacheKey = self::inCache($group, $name, $componentID, $contactID, FALSE);
588 unset(self::$_cache[$cacheKey][$name]);
591 unset(self::$_cache[$cacheKey]);
597 * This provides information about the setting - similar to the fields concept for DAO information.
598 * As the setting is serialized code creating validation setting input needs to know the data type
599 * This also helps move information out of the form layer into the data layer where people can interact with
600 * it via the API or other mechanisms. In order to keep this consistent it is important the form layer
603 * Note that this function should never be called when using the runtime getvalue function. Caching works
604 * around the expectation it will be called during setting administration
606 * Function is intended for configuration rather than runtime access to settings
608 * The following params will filter the result. If none are passed all settings will be returns
610 * @params string $name Name of specific setting e.g customCSSURL
611 * @params integer $componentID id of relevant component.
613 * @return array $result - the following information as appropriate for each setting
617 * - add (CiviCRM version added)
623 static function getSettingSpecification(
629 $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile;
630 foreach ($filters as $filterField => $filterString) {
631 $cacheString .= "_{$filterField}_{$filterString}";
634 // the caching into 'All' seems to be a duplicate of caching to
635 // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook
636 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Specs', $cacheString, $componentID);
637 if ($settingsMetadata === NULL) {
638 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Spec', 'All', $componentID);
639 if (empty($settingsMetadata)) {
640 global $civicrm_root;
641 $metaDataFolders = array($civicrm_root. '/settings');
642 CRM_Utils_Hook
::alterSettingsFolders($metaDataFolders);
643 $settingsMetadata = self
::loadSettingsMetaDataFolders($metaDataFolders);
644 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All', $componentID);
649 CRM_Utils_Hook
::alterSettingsMetaData($settingsMetadata, $domainID, $profile);
650 self
::_filterSettingsSpecification($filters, $settingsMetadata);
653 // this is a bit 'heavy' if you are using hooks but this function
654 // is expected to only be called during setting administration
655 // it should not be called by 'getvalue' or 'getitem
656 CRM_Core_BAO_Cache
::setItem(
658 'CiviCRM setting Specs',
663 return $settingsMetadata;
668 * Load the settings files defined in a series of folders
669 * @param array $metaDataFolders list of folder paths
672 public static function loadSettingsMetaDataFolders($metaDataFolders) {
673 $settingsMetadata = array();
674 $loadedFolders = array();
675 foreach ($metaDataFolders as $metaDataFolder) {
676 $realFolder = realpath($metaDataFolder);
677 if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) {
678 $loadedFolders[$realFolder] = TRUE;
679 $settingsMetadata = $settingsMetadata + self
::loadSettingsMetaData($metaDataFolder);
682 return $settingsMetadata;
686 * Load up settings metadata from files
688 static function loadSettingsMetadata($metaDataFolder) {
689 $settingMetaData = array();
690 $settingsFiles = CRM_Utils_File
::findFiles($metaDataFolder, '*.setting.php');
691 foreach ($settingsFiles as $file) {
692 $settings = include $file;
693 $settingMetaData = array_merge($settingMetaData, $settings);
695 CRM_Core_BAO_Cache
::setItem($settingMetaData,'CiviCRM setting Spec', 'All');
696 return $settingMetaData;
700 * Filter the settings metadata according to filters passed in. This is a convenience filter
701 * and allows selective reverting / filling of settings
703 * @param array $filters Filters to match against data
704 * @param array $settingSpec metadata to filter
706 static function _filterSettingsSpecification($filters, &$settingSpec) {
707 if (empty($filters)) {
710 else if (array_keys($filters) == array('name')) {
711 $settingSpec = array($filters['name'] => CRM_Utils_Array
::value($filters['name'], $settingSpec, ''));
715 foreach ($settingSpec as $field => $fieldValues) {
716 if (array_intersect_assoc($fieldValues, $filters) != $filters) {
717 unset($settingSpec[$field]);
725 * Look for any missing settings and convert them from config or load default as appropriate
726 * This should be run from GenCode & also from upgrades to add any new defaults.
728 * Multisites have often been overlooked in upgrade scripts so can be expected to be missing
729 * a number of settings
731 static function updateSettingsFromMetaData() {
734 'domain_id' => 'all',
735 'filters' => array('prefetch' => 0),
737 $existing = civicrm_api('setting', 'get', $apiParams);
739 if (!empty($existing['values'])) {
740 $allSettings = civicrm_api('setting', 'getfields', array('version' => 3));
741 foreach ($existing['values'] as $domainID => $domainSettings) {
742 CRM_Core_BAO_Domain
::setDomain($domainID);
743 $missing = array_diff_key($allSettings['values'], $domainSettings);
744 foreach ($missing as $name => $settings) {
745 self
::convertConfigToSetting($name, $domainID);
747 CRM_Core_BAO_Domain
::resetDomain();
753 * move an item from being in the config array to being stored as a setting
754 * remove from config - as appropriate based on metadata
756 * Note that where the key name is being changed the 'legacy_key' will give us the old name
758 static function convertConfigToSetting($name, $domainID = NULL) {
759 // we have to force this here in case more than one domain is in play.
760 // whenever there is a possibility of more than one domain we must force it
761 $config = CRM_Core_Config
::singleton();
762 if (empty($domainID)) {
763 $domainID= CRM_Core_Config
::domainID();
765 $domain = new CRM_Core_DAO_Domain();
766 $domain->id
= $domainID;
768 if ($domain->config_backend
) {
769 $values = unserialize($domain->config_backend
);
773 $spec = self
::getSettingSpecification(NULL, array('name' => $name), $domainID);
774 $configKey = CRM_Utils_Array
::value('config_key', $spec[$name], CRM_Utils_Array
::value('legacy_key', $spec[$name], $name));
775 //if the key is set to config_only we don't need to do anything
776 if(empty($spec[$name]['config_only'])){
777 if (!empty($values[$configKey])) {
778 civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID));
781 civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID));
784 if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) {
785 unset($values[$configKey]);
786 $domain->config_backend
= serialize($values);
788 unset($config->$configKey);
793 static function valueOptions($group,
798 $returnField = 'name',
799 $returnNameANDLabels = FALSE,
802 $optionValue = self
::getItem($group, $name);
804 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, $returnField);
806 //enabled name => label require for new contact edit form, CRM-4605
807 if ($returnNameANDLabels) {
808 $names = $labels = $nameAndLabels = array();
809 if ($returnField == 'name') {
810 $names = $groupValues;
811 $labels = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'label');
814 $labels = $groupValues;
815 $names = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'name');
819 $returnValues = array();
820 foreach ($groupValues as $gn => $gv) {
821 $returnValues[$gv] = 0;
824 if ($optionValue && !empty($groupValues)) {
825 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
826 substr($optionValue, 1, -1)
829 if (!empty($dbValues)) {
830 foreach ($groupValues as $key => $val) {
831 if (in_array($key, $dbValues)) {
832 $returnValues[$val] = 1;
833 if ($returnNameANDLabels) {
834 $nameAndLabels[$names[$key]] = $labels[$key];
840 return ($returnNameANDLabels) ?
$nameAndLabels : $returnValues;
843 static function setValueOption($group,
853 elseif (is_array($value)) {
854 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, FALSE, NULL, $keyField);
857 foreach ($groupValues as $key => $val) {
858 if (CRM_Utils_Array
::value($val, $value)) {
863 if (!empty($cbValues)) {
864 $optionValue = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
865 array_keys($cbValues)
866 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
873 $optionValue = $value;
876 self
::setItem($optionValue, $group, $name);
879 static function fixAndStoreDirAndURL(&$params, $domainID = NULL) {
880 if (self
::isUpgradeFromPreFourOneAlpha1()) {
884 if (empty($domainID)) {
885 $domainID = CRM_Core_Config
::domainID();
888 SELECT name, group_name
891 AND ( group_name = %2
895 1 => array($domainID, 'Integer'),
896 2 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
897 3 => array(self
::URL_PREFERENCES_NAME
, 'String'),
900 $dirParams = array();
901 $urlParams = array();
903 $dao = CRM_Core_DAO
::executeQuery($sql,
909 // trap exceptions as error
913 if (is_a($dao, 'DB_Error')) {
914 if (CRM_Core_Config
::isUpgradeMode()) {
915 // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue
919 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
924 while ($dao->fetch()) {
925 if (!isset($params[$dao->name
])) {
928 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
929 $dirParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
932 $urlParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
934 unset($params[$dao->name
]);
937 if (!empty($dirParams)) {
938 self
::storeDirectoryOrURLPreferences($dirParams,
939 self
::DIRECTORY_PREFERENCES_NAME
943 if (!empty($urlParams)) {
944 self
::storeDirectoryOrURLPreferences($urlParams,
945 self
::URL_PREFERENCES_NAME
950 static function storeDirectoryOrURLPreferences(&$params, $group) {
951 foreach ($params as $name => $value) {
952 // always try to store relative directory or url from CMS root
953 $value = ($group == self
::DIRECTORY_PREFERENCES_NAME
) ? CRM_Utils_File
::relativeDirectory($value) : CRM_Utils_System
::relativeURL($value);
955 self
::setItem($value, $group, $name);
959 static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
960 if (CRM_Core_Config
::isUpgradeMode()) {
961 $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') ?
TRUE : FALSE;
962 // hack to set the resource base url so that js/ css etc is loaded correctly
964 $params['userFrameworkResourceURL'] = CRM_Utils_File
::addTrailingSlash(CIVICRM_UF_BASEURL
, '/') . str_replace('administrator', '', CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name'));
966 if (self
::isUpgradeFromPreFourOneAlpha1()) {
972 $config = CRM_Core_Config
::singleton();
976 SELECT name, group_name, value
978 WHERE ( group_name = %1
982 $sqlParams = array(1 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
983 2 => array(self
::URL_PREFERENCES_NAME
, 'String'),
984 3 => array(CRM_Core_Config
::domainID(), 'Integer'),
987 $dao = CRM_Core_DAO
::executeQuery($sql,
993 // trap exceptions as error
997 if (is_a($dao, 'DB_Error')) {
998 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
1002 while ($dao->fetch()) {
1003 $value = self
::getOverride($dao->group_name
, $dao->name
, NULL);
1004 if ($value === NULL && $dao->value
) {
1005 $value = unserialize($dao->value
);
1006 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
1007 $value = CRM_Utils_File
::absoluteDirectory($value);
1010 // CRM-7622: we need to remove the language part
1011 $value = CRM_Utils_System
::absoluteURL($value, TRUE);
1014 // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available
1015 if (!isset($value) && CRM_Utils_Array
::value($dao->name
, $params)) {
1016 $value = $params[$dao->name
];
1018 $params[$dao->name
] = $value;
1021 $config->{$dao->name
} = $value;
1027 * Determine what, if any, overrides have been provided
1030 * @return mixed, NULL or an overriden value
1032 protected static function getOverride($group, $name, $default) {
1033 global $civicrm_setting;
1034 if ($group && $name && isset($civicrm_setting[$group][$name])) {
1035 return $civicrm_setting[$group][$name];
1043 * civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade
1047 static function isUpgradeFromPreFourOneAlpha1() {
1048 if (CRM_Core_Config
::isUpgradeMode()) {
1049 $currentVer = CRM_Core_BAO_Domain
::version();
1050 if (version_compare($currentVer, '4.1.alpha1') < 0) {