3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 bool|int $load if true, load from local cache (typically memcache)
74 * @param null $domainID
77 * @return boolean true if item is already in cache
81 static function inCache(
90 if (!isset(self
::$_cache)) {
91 self
::$_cache = array();
94 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
98 ($force ||
!isset(self
::$_cache[$cacheKey]))
101 // check in civi cache if present (typically memcache)
102 $globalCache = CRM_Utils_Cache
::singleton();
103 $result = $globalCache->get($cacheKey);
106 self
::$_cache[$cacheKey] = $result;
110 return isset(self
::$_cache[$cacheKey]) ?
$cacheKey : NULL;
113 * Allow key o be cleared
114 * @param string $cacheKey
116 static function flushCache($cacheKey){
117 unset(self
::$_cache[$cacheKey]);
118 $globalCache = CRM_Utils_Cache
::singleton();
119 $globalCache->delete($cacheKey);
122 static function setCache($values,
128 if (!isset(self
::$_cache)) {
129 self
::$_cache = array();
132 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
134 self
::$_cache[$cacheKey] = $values;
136 $globalCache = CRM_Utils_Cache
::singleton();
137 $result = $globalCache->set($cacheKey, $values);
142 static function dao($group,
148 if (self
::isUpgradeFromPreFourOneAlpha1()) {
149 // civicrm_setting table is not going to be present. For now we'll just
150 // return a dummy object
151 $dao = new CRM_Core_DAO_Domain();
152 $dao->id
= -1; // so ->find() doesn't fetch any data later on
155 $dao = new CRM_Core_DAO_Setting();
157 $dao->group_name
= $group;
159 $dao->component_id
= $componentID;
160 if (empty($domainID)) {
161 $dao->domain_id
= CRM_Core_Config
::domainID();
164 $dao->domain_id
= $domainID;
168 $dao->contact_id
= $contactID;
179 * Retrieve the value of a setting from the DB table
181 * @param string $group (required) The group name of the item
182 * @param string $name (required) The name under which this item is stored
183 * @param int $componentID The optional component ID (so componenets can share the same name space)
184 * @param string $defaultValue The default value to return for this setting if not present in DB
185 * @param int $contactID If set, this is a contactID specific setting, else its a global setting
188 * @return object The data if present in the setting table, else null
192 static function getItem(
196 $defaultValue = NULL,
201 if (NULL !== ($override = self
::getOverride($group, $name, NULL))) {
205 if (empty($domainID)) {
206 $domainID = CRM_Core_Config
::domainID();
208 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, TRUE, $domainID);
210 $dao = self
::dao($group, NULL, $componentID, $contactID, $domainID);
214 while ($dao->fetch()) {
215 if (NULL !== ($override = self
::getOverride($group, $dao->name
, NULL))) {
216 $values[$dao->name
] = $override;
218 elseif ($dao->value
) {
219 $values[$dao->name
] = unserialize($dao->value
);
222 $values[$dao->name
] = NULL;
227 $cacheKey = self
::setCache($values, $group, $componentID, $contactID, $domainID);
229 return $name ? CRM_Utils_Array
::value($name, self
::$_cache[$cacheKey], $defaultValue) : self
::$_cache[$cacheKey];
233 * Store multiple items in the setting table
235 * @param array $params (required) An api formatted array of keys and values
236 * @param null $domains
237 * @param $settingsToReturn
239 * @domains array an array of domains to get settings for. Default is the current domain
244 static function getItems(&$params, $domains = NULL, $settingsToReturn) {
245 $originalDomain = CRM_Core_Config
::domainID();
246 if (empty($domains)) {
247 $domains[] = $originalDomain;
249 if (!empty($settingsToReturn) && !is_array($settingsToReturn)) {
250 $settingsToReturn = array($settingsToReturn);
252 $reloadConfig = FALSE;
254 $fields = $result = array();
255 $fieldsToGet = self
::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE);
256 foreach ($domains as $domainID) {
257 if($domainID != CRM_Core_Config
::domainID()){
258 $reloadConfig = TRUE;
259 CRM_Core_BAO_Domain
::setDomain($domainID);
261 $config = CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
262 $result[$domainID] = array();
263 foreach ($fieldsToGet as $name => $value) {
264 if(!empty($fields['values'][$name]['prefetch'])){
265 if(isset($params['filters']) && isset($params['filters']['prefetch'])
266 && $params['filters']['prefetch'] == 0){
267 // we are filtering out the prefetches from the return array
271 $configKey = CRM_Utils_Array
::value('config_key', $fields['values'][$name], $name);
272 if(isset($config->$configKey)){
273 $setting = $config->$configKey;
278 CRM_Core_BAO_Setting
::getItem(
279 $fields['values'][$name]['group_name'],
281 CRM_Utils_Array
::value('component_id', $params),
283 CRM_Utils_Array
::value('contact_id', $params),
287 if (!is_null($setting)) {
288 // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones
289 // e.g for revert of fill actions
290 $result[$domainID][$name] = $setting;
293 CRM_Core_BAO_Domain
::resetDomain();
299 * Store an item in the setting table
301 * _setItem() is the common logic shared by setItem() and setItems().
303 * @param object $value (required) The value that will be serialized and stored
304 * @param string $group (required) The group name of the item
305 * @param string $name (required) The name of the setting
306 * @param int $componentID The optional component ID (so componenets can share the same name space)
307 * @param null $contactID
308 * @param int $createdID An optional ID to assign the creator to. If not set, retrieved from session
310 * @param null $domainID
316 static function setItem(
326 $fieldsToSet = self
::validateSettingsInput(array($name => $value), $fields);
327 //We haven't traditionally validated inputs to setItem, so this breaks things.
328 //foreach ($fieldsToSet as $settingField => &$settingValue) {
329 // self::validateSetting($settingValue, $fields['values'][$settingField]);
332 return self
::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID);
336 * Store an item in a setting table
338 * _setItem() is the common logic shared by setItem() and setItems().
340 * @param array $metadata metadata describing this field
344 * @param null $componentID
345 * @param null $contactID
346 * @param null $createdID
347 * @param null $domainID
349 static function _setItem(
359 if (empty($domainID)) {
360 $domainID = CRM_Core_Config
::domainID();
363 $dao = self
::dao($group, $name, $componentID, $contactID, $domainID);
366 if (isset($metadata['on_change'])) {
367 foreach ($metadata['on_change'] as $callback) {
368 call_user_func($callback, unserialize($dao->value
), $value, $metadata);
372 if (CRM_Utils_System
::isNull($value)) {
373 $dao->value
= 'null';
376 $dao->value
= serialize($value);
379 $dao->created_date
= date('Ymdhis');
382 $dao->created_id
= $createdID;
385 $session = CRM_Core_Session
::singleton();
386 $createdID = $session->get('userID');
389 // ensure that this is a valid contact id (for session inconsistency rules)
390 $cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
396 $dao->created_id
= $session->get('userID');
404 // also save in cache if needed
405 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, FALSE, $domainID);
407 self
::$_cache[$cacheKey][$name] = $value;
412 * Store multiple items in the setting table. Note that this will also store config keys
413 * the storage is determined by the metdata and is affected by
414 * 'name' setting's name
415 * 'prefetch' = store in config
416 * 'config_only' = don't store in settings
417 * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict
418 * 'legacy_key' = rename from config or setting with this name
420 * _setItem() is the common logic shared by setItem() and setItems().
422 * @param array $params (required) An api formatted array of keys and values
423 * @domains array an array of domains to get settings for. Default is the current domain
428 static function setItems(&$params, $domains = NULL) {
429 $originalDomain = CRM_Core_Config
::domainID();
430 if (empty($domains)) {
431 $domains[] = $originalDomain;
433 $reloadConfig = FALSE;
434 $fields = $config_keys = array();
435 $fieldsToSet = self
::validateSettingsInput($params, $fields);
437 foreach ($fieldsToSet as $settingField => &$settingValue) {
438 self
::validateSetting($settingValue, $fields['values'][$settingField]);
441 foreach ($domains as $domainID) {
442 if($domainID != CRM_Core_Config
::domainID()){
443 $reloadConfig = TRUE;
444 CRM_Core_BAO_Domain
::setDomain($domainID);
446 $result[$domainID] = array();
447 foreach ($fieldsToSet as $name => $value) {
448 if(empty($fields['values'][$name]['config_only'])){
449 CRM_Core_BAO_Setting
::_setItem(
450 $fields['values'][$name],
452 $fields['values'][$name]['group_name'],
454 CRM_Utils_Array
::value('component_id', $params),
455 CRM_Utils_Array
::value('contact_id', $params),
456 CRM_Utils_Array
::value('created_id', $params),
460 if(!empty($fields['values'][$name]['prefetch'])){
461 if(!empty($fields['values'][$name]['config_key'])){
462 $name = $fields['values'][$name]['config_key'];
464 $config_keys[$name] = $value;
466 $result[$domainID][$name] = $value;
469 CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
472 if(!empty($config_keys)){
473 CRM_Core_BAO_ConfigSetting
::create($config_keys);
476 CRM_Core_BAO_Domain
::resetDomain();
484 * gets metadata about the settings fields (from getfields) based on the fields being passed in
486 * This function filters on the fields like 'version' & 'debug' that are not settings
488 * @param array $params Parameters as passed into API
489 * @param array $fields empty array to be populated with fields metadata
490 * @param bool $createMode
492 * @throws api_Exception
493 * @return array $fieldstoset name => value array of the fields to be set (with extraneous removed)
495 static function validateSettingsInput($params, &$fields, $createMode = TRUE) {
496 $group = CRM_Utils_Array
::value('group', $params);
498 $ignoredParams = array(
516 $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE));
517 $getFieldsParams = array('version' => 3);
518 if (count($settingParams) ==1) {
519 // ie we are only setting one field - we'll pass it into getfields for efficiency
520 list($name) = array_keys($settingParams);
521 $getFieldsParams['name'] = $name;
523 $fields = civicrm_api3('setting','getfields', $getFieldsParams);
524 $invalidParams = (array_diff_key($settingParams, $fields['values']));
525 if (!empty($invalidParams)) {
526 throw new api_Exception(implode(',', $invalidParams) . " not valid settings");
528 if (!empty($settingParams)) {
529 $filteredFields = array_intersect_key($settingParams, $fields['values']);
532 // no filters so we are interested in all for get mode. In create mode this means nothing to set
533 $filteredFields = $createMode ?
array() : $fields['values'];
535 return $filteredFields;
539 * Validate & convert settings input
541 * @value mixed value of the setting to be set
542 * @fieldSpec array Metadata for given field (drawn from the xml)
544 static function validateSetting(&$value, $fieldSpec) {
545 if($fieldSpec['type'] == 'String' && is_array($value)){
546 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,$value) . CRM_Core_DAO
::VALUE_SEPARATOR
;
548 if (empty($fieldSpec['validate_callback'])) {
552 list($class,$fn) = explode('::',$fieldSpec['validate_callback']);
553 if (!$class::$fn($value,$fieldSpec)) {
554 throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}");
560 * Validate & convert settings input - translate True False to 0 or 1
562 * @value mixed value of the setting to be set
563 * @fieldSpec array Metadata for given field (drawn from the xml)
565 static function validateBoolSetting(&$value, $fieldSpec) {
566 if (!CRM_Utils_Rule
::boolean($value)) {
567 throw new api_Exception("Boolean value required for {$fieldSpec['name']}");
579 * Delete some or all of the items in the settings table
581 * @param string $group The group name of the entries to be deleted
582 * @param string $name The name of the setting to be deleted
583 * @param int $componentID The optional component ID (so componenets can share the same name space)
589 static function deleteItem($group, $name = NULL, $componentID = NULL, $contactID = NULL) {
590 $dao = self::dao($group, $name, $componentID, $contactID);
593 // also reset memory cache if any
594 CRM_Utils_System::flushCache();
596 $cacheKey = self::inCache($group, $name, $componentID, $contactID, FALSE);
599 unset(self::$_cache[$cacheKey][$name]);
602 unset(self::$_cache[$cacheKey]);
608 * This provides information about the setting - similar to the fields concept for DAO information.
609 * As the setting is serialized code creating validation setting input needs to know the data type
610 * This also helps move information out of the form layer into the data layer where people can interact with
611 * it via the API or other mechanisms. In order to keep this consistent it is important the form layer
614 * Note that this function should never be called when using the runtime getvalue function. Caching works
615 * around the expectation it will be called during setting administration
617 * Function is intended for configuration rather than runtime access to settings
619 * The following params will filter the result. If none are passed all settings will be returns
621 * @params string $name Name of specific setting e.g customCSSURL
622 * @params integer $componentID id of relevant component.
624 * @return array $result - the following information as appropriate for each setting
628 * - add (CiviCRM version added)
634 static function getSettingSpecification(
640 $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile;
641 foreach ($filters as $filterField => $filterString) {
642 $cacheString .= "_{$filterField}_{$filterString}";
645 // the caching into 'All' seems to be a duplicate of caching to
646 // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook
647 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Specs', $cacheString, $componentID);
648 if ($settingsMetadata === NULL) {
649 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Spec', 'All', $componentID);
650 if (empty($settingsMetadata)) {
651 global $civicrm_root;
652 $metaDataFolders = array($civicrm_root. '/settings');
653 CRM_Utils_Hook
::alterSettingsFolders($metaDataFolders);
654 $settingsMetadata = self
::loadSettingsMetaDataFolders($metaDataFolders);
655 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All', $componentID);
660 CRM_Utils_Hook
::alterSettingsMetaData($settingsMetadata, $domainID, $profile);
661 self
::_filterSettingsSpecification($filters, $settingsMetadata);
664 // this is a bit 'heavy' if you are using hooks but this function
665 // is expected to only be called during setting administration
666 // it should not be called by 'getvalue' or 'getitem
667 CRM_Core_BAO_Cache
::setItem(
669 'CiviCRM setting Specs',
674 return $settingsMetadata;
679 * Load the settings files defined in a series of folders
680 * @param array $metaDataFolders list of folder paths
683 public static function loadSettingsMetaDataFolders($metaDataFolders) {
684 $settingsMetadata = array();
685 $loadedFolders = array();
686 foreach ($metaDataFolders as $metaDataFolder) {
687 $realFolder = realpath($metaDataFolder);
688 if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) {
689 $loadedFolders[$realFolder] = TRUE;
690 $settingsMetadata = $settingsMetadata + self
::loadSettingsMetaData($metaDataFolder);
693 return $settingsMetadata;
697 * Load up settings metadata from files
699 static function loadSettingsMetadata($metaDataFolder) {
700 $settingMetaData = array();
701 $settingsFiles = CRM_Utils_File
::findFiles($metaDataFolder, '*.setting.php');
702 foreach ($settingsFiles as $file) {
703 $settings = include $file;
704 $settingMetaData = array_merge($settingMetaData, $settings);
706 CRM_Core_BAO_Cache
::setItem($settingMetaData,'CiviCRM setting Spec', 'All');
707 return $settingMetaData;
711 * Filter the settings metadata according to filters passed in. This is a convenience filter
712 * and allows selective reverting / filling of settings
714 * @param array $filters Filters to match against data
715 * @param array $settingSpec metadata to filter
717 static function _filterSettingsSpecification($filters, &$settingSpec) {
718 if (empty($filters)) {
721 else if (array_keys($filters) == array('name')) {
722 $settingSpec = array($filters['name'] => CRM_Utils_Array
::value($filters['name'], $settingSpec, ''));
726 foreach ($settingSpec as $field => $fieldValues) {
727 if (array_intersect_assoc($fieldValues, $filters) != $filters) {
728 unset($settingSpec[$field]);
736 * Look for any missing settings and convert them from config or load default as appropriate
737 * This should be run from GenCode & also from upgrades to add any new defaults.
739 * Multisites have often been overlooked in upgrade scripts so can be expected to be missing
740 * a number of settings
742 static function updateSettingsFromMetaData() {
745 'domain_id' => 'all',
746 'filters' => array('prefetch' => 0),
748 $existing = civicrm_api('setting', 'get', $apiParams);
750 if (!empty($existing['values'])) {
751 $allSettings = civicrm_api('setting', 'getfields', array('version' => 3));
752 foreach ($existing['values'] as $domainID => $domainSettings) {
753 CRM_Core_BAO_Domain
::setDomain($domainID);
754 $missing = array_diff_key($allSettings['values'], $domainSettings);
755 foreach ($missing as $name => $settings) {
756 self
::convertConfigToSetting($name, $domainID);
758 CRM_Core_BAO_Domain
::resetDomain();
764 * move an item from being in the config array to being stored as a setting
765 * remove from config - as appropriate based on metadata
767 * Note that where the key name is being changed the 'legacy_key' will give us the old name
769 static function convertConfigToSetting($name, $domainID = NULL) {
770 // we have to force this here in case more than one domain is in play.
771 // whenever there is a possibility of more than one domain we must force it
772 $config = CRM_Core_Config
::singleton();
773 if (empty($domainID)) {
774 $domainID= CRM_Core_Config
::domainID();
776 $domain = new CRM_Core_DAO_Domain();
777 $domain->id
= $domainID;
779 if ($domain->config_backend
) {
780 $values = unserialize($domain->config_backend
);
784 $spec = self
::getSettingSpecification(NULL, array('name' => $name), $domainID);
785 $configKey = CRM_Utils_Array
::value('config_key', $spec[$name], CRM_Utils_Array
::value('legacy_key', $spec[$name], $name));
786 //if the key is set to config_only we don't need to do anything
787 if(empty($spec[$name]['config_only'])){
788 if (!empty($values[$configKey])) {
789 civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID));
792 civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID));
795 if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) {
796 unset($values[$configKey]);
797 $domain->config_backend
= serialize($values);
799 unset($config->$configKey);
804 static function valueOptions($group,
809 $returnField = 'name',
810 $returnNameANDLabels = FALSE,
813 $optionValue = self
::getItem($group, $name);
815 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, $returnField);
817 //enabled name => label require for new contact edit form, CRM-4605
818 if ($returnNameANDLabels) {
819 $names = $labels = $nameAndLabels = array();
820 if ($returnField == 'name') {
821 $names = $groupValues;
822 $labels = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'label');
825 $labels = $groupValues;
826 $names = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'name');
830 $returnValues = array();
831 foreach ($groupValues as $gn => $gv) {
832 $returnValues[$gv] = 0;
835 if ($optionValue && !empty($groupValues)) {
836 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
837 substr($optionValue, 1, -1)
840 if (!empty($dbValues)) {
841 foreach ($groupValues as $key => $val) {
842 if (in_array($key, $dbValues)) {
843 $returnValues[$val] = 1;
844 if ($returnNameANDLabels) {
845 $nameAndLabels[$names[$key]] = $labels[$key];
851 return ($returnNameANDLabels) ?
$nameAndLabels : $returnValues;
854 static function setValueOption($group,
864 elseif (is_array($value)) {
865 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, FALSE, NULL, $keyField);
868 foreach ($groupValues as $key => $val) {
869 if (!empty($value[$val])) {
874 if (!empty($cbValues)) {
875 $optionValue = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
876 array_keys($cbValues)
877 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
884 $optionValue = $value;
887 self
::setItem($optionValue, $group, $name);
890 static function fixAndStoreDirAndURL(&$params, $domainID = NULL) {
891 if (self
::isUpgradeFromPreFourOneAlpha1()) {
895 if (empty($domainID)) {
896 $domainID = CRM_Core_Config
::domainID();
899 SELECT name, group_name
902 AND ( group_name = %2
906 1 => array($domainID, 'Integer'),
907 2 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
908 3 => array(self
::URL_PREFERENCES_NAME
, 'String'),
911 $dirParams = array();
912 $urlParams = array();
914 $dao = CRM_Core_DAO
::executeQuery($sql,
920 // trap exceptions as error
924 if (is_a($dao, 'DB_Error')) {
925 if (CRM_Core_Config
::isUpgradeMode()) {
926 // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue
930 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
935 while ($dao->fetch()) {
936 if (!isset($params[$dao->name
])) {
939 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
940 $dirParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
943 $urlParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
945 unset($params[$dao->name
]);
948 if (!empty($dirParams)) {
949 self
::storeDirectoryOrURLPreferences($dirParams,
950 self
::DIRECTORY_PREFERENCES_NAME
954 if (!empty($urlParams)) {
955 self
::storeDirectoryOrURLPreferences($urlParams,
956 self
::URL_PREFERENCES_NAME
961 static function storeDirectoryOrURLPreferences(&$params, $group) {
962 foreach ($params as $name => $value) {
963 // always try to store relative directory or url from CMS root
964 $value = ($group == self
::DIRECTORY_PREFERENCES_NAME
) ? CRM_Utils_File
::relativeDirectory($value) : CRM_Utils_System
::relativeURL($value);
966 self
::setItem($value, $group, $name);
970 static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
971 if (CRM_Core_Config
::isUpgradeMode()) {
972 $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') ?
TRUE : FALSE;
973 // hack to set the resource base url so that js/ css etc is loaded correctly
975 $params['userFrameworkResourceURL'] = CRM_Utils_File
::addTrailingSlash(CIVICRM_UF_BASEURL
, '/') . str_replace('administrator', '', CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name'));
977 if (self
::isUpgradeFromPreFourOneAlpha1()) {
983 $config = CRM_Core_Config
::singleton();
987 SELECT name, group_name, value
989 WHERE ( group_name = %1
993 $sqlParams = array(1 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
994 2 => array(self
::URL_PREFERENCES_NAME
, 'String'),
995 3 => array(CRM_Core_Config
::domainID(), 'Integer'),
998 $dao = CRM_Core_DAO
::executeQuery($sql,
1004 // trap exceptions as error
1008 if (is_a($dao, 'DB_Error')) {
1009 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
1013 while ($dao->fetch()) {
1014 $value = self
::getOverride($dao->group_name
, $dao->name
, NULL);
1015 if ($value === NULL && $dao->value
) {
1016 $value = unserialize($dao->value
);
1017 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
1018 $value = CRM_Utils_File
::absoluteDirectory($value);
1021 // CRM-7622: we need to remove the language part
1022 $value = CRM_Utils_System
::absoluteURL($value, TRUE);
1025 // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available
1026 if (!isset($value) && !empty($params[$dao->name
])) {
1027 $value = $params[$dao->name
];
1029 $params[$dao->name
] = $value;
1032 $config->{$dao->name
} = $value;
1038 * Determine what, if any, overrides have been provided
1045 * @return mixed, NULL or an overriden value
1047 protected static function getOverride($group, $name, $default) {
1048 global $civicrm_setting;
1049 if ($group && $name && isset($civicrm_setting[$group][$name])) {
1050 return $civicrm_setting[$group][$name];
1058 * civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade
1062 static function isUpgradeFromPreFourOneAlpha1() {
1063 if (CRM_Core_Config
::isUpgradeMode()) {
1064 $currentVer = CRM_Core_BAO_Domain
::version();
1065 if (version_compare($currentVer, '4.1.alpha1') < 0) {