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
187 * @param null $domainID
189 * @return object The data if present in the setting table, else null
193 static function getItem(
197 $defaultValue = NULL,
202 if (NULL !== ($override = self
::getOverride($group, $name, NULL))) {
206 if (empty($domainID)) {
207 $domainID = CRM_Core_Config
::domainID();
209 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, TRUE, $domainID);
211 $dao = self
::dao($group, NULL, $componentID, $contactID, $domainID);
215 while ($dao->fetch()) {
216 if (NULL !== ($override = self
::getOverride($group, $dao->name
, NULL))) {
217 $values[$dao->name
] = $override;
219 elseif ($dao->value
) {
220 $values[$dao->name
] = unserialize($dao->value
);
223 $values[$dao->name
] = NULL;
228 $cacheKey = self
::setCache($values, $group, $componentID, $contactID, $domainID);
230 return $name ? CRM_Utils_Array
::value($name, self
::$_cache[$cacheKey], $defaultValue) : self
::$_cache[$cacheKey];
234 * Store multiple items in the setting table
236 * @param array $params (required) An api formatted array of keys and values
237 * @param null $domains
238 * @param $settingsToReturn
240 * @domains array an array of domains to get settings for. Default is the current domain
245 static function getItems(&$params, $domains = NULL, $settingsToReturn) {
246 $originalDomain = CRM_Core_Config
::domainID();
247 if (empty($domains)) {
248 $domains[] = $originalDomain;
250 if (!empty($settingsToReturn) && !is_array($settingsToReturn)) {
251 $settingsToReturn = array($settingsToReturn);
253 $reloadConfig = FALSE;
255 $fields = $result = array();
256 $fieldsToGet = self
::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE);
257 foreach ($domains as $domainID) {
258 if($domainID != CRM_Core_Config
::domainID()){
259 $reloadConfig = TRUE;
260 CRM_Core_BAO_Domain
::setDomain($domainID);
262 $config = CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
263 $result[$domainID] = array();
264 foreach ($fieldsToGet as $name => $value) {
265 if(!empty($fields['values'][$name]['prefetch'])){
266 if(isset($params['filters']) && isset($params['filters']['prefetch'])
267 && $params['filters']['prefetch'] == 0){
268 // we are filtering out the prefetches from the return array
272 $configKey = CRM_Utils_Array
::value('config_key', $fields['values'][$name], $name);
273 if(isset($config->$configKey)){
274 $setting = $config->$configKey;
279 CRM_Core_BAO_Setting
::getItem(
280 $fields['values'][$name]['group_name'],
282 CRM_Utils_Array
::value('component_id', $params),
284 CRM_Utils_Array
::value('contact_id', $params),
288 if (!is_null($setting)) {
289 // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones
290 // e.g for revert of fill actions
291 $result[$domainID][$name] = $setting;
294 CRM_Core_BAO_Domain
::resetDomain();
300 * Store an item in the setting table
302 * _setItem() is the common logic shared by setItem() and setItems().
304 * @param object $value (required) The value that will be serialized and stored
305 * @param string $group (required) The group name of the item
306 * @param string $name (required) The name of the setting
307 * @param int $componentID The optional component ID (so componenets can share the same name space)
308 * @param null $contactID
309 * @param int $createdID An optional ID to assign the creator to. If not set, retrieved from session
311 * @param null $domainID
317 static function setItem(
327 $fieldsToSet = self
::validateSettingsInput(array($name => $value), $fields);
328 //We haven't traditionally validated inputs to setItem, so this breaks things.
329 //foreach ($fieldsToSet as $settingField => &$settingValue) {
330 // self::validateSetting($settingValue, $fields['values'][$settingField]);
333 return self
::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID);
337 * Store an item in a setting table
339 * _setItem() is the common logic shared by setItem() and setItems().
341 * @param array $metadata metadata describing this field
345 * @param null $componentID
346 * @param null $contactID
347 * @param null $createdID
348 * @param null $domainID
350 static function _setItem(
360 if (empty($domainID)) {
361 $domainID = CRM_Core_Config
::domainID();
364 $dao = self
::dao($group, $name, $componentID, $contactID, $domainID);
367 if (isset($metadata['on_change'])) {
368 foreach ($metadata['on_change'] as $callback) {
369 call_user_func($callback, unserialize($dao->value
), $value, $metadata);
373 if (CRM_Utils_System
::isNull($value)) {
374 $dao->value
= 'null';
377 $dao->value
= serialize($value);
380 $dao->created_date
= date('Ymdhis');
383 $dao->created_id
= $createdID;
386 $session = CRM_Core_Session
::singleton();
387 $createdID = $session->get('userID');
390 // ensure that this is a valid contact id (for session inconsistency rules)
391 $cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
397 $dao->created_id
= $session->get('userID');
405 // also save in cache if needed
406 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, FALSE, $domainID);
408 self
::$_cache[$cacheKey][$name] = $value;
413 * Store multiple items in the setting table. Note that this will also store config keys
414 * the storage is determined by the metdata and is affected by
415 * 'name' setting's name
416 * 'prefetch' = store in config
417 * 'config_only' = don't store in settings
418 * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict
419 * 'legacy_key' = rename from config or setting with this name
421 * _setItem() is the common logic shared by setItem() and setItems().
423 * @param array $params (required) An api formatted array of keys and values
424 * @param null $domains
426 * @throws api_Exception
427 * @domains array an array of domains to get settings for. Default is the current domain
432 static function setItems(&$params, $domains = NULL) {
433 $originalDomain = CRM_Core_Config
::domainID();
434 if (empty($domains)) {
435 $domains[] = $originalDomain;
437 $reloadConfig = FALSE;
438 $fields = $config_keys = array();
439 $fieldsToSet = self
::validateSettingsInput($params, $fields);
441 foreach ($fieldsToSet as $settingField => &$settingValue) {
442 self
::validateSetting($settingValue, $fields['values'][$settingField]);
445 foreach ($domains as $domainID) {
446 if($domainID != CRM_Core_Config
::domainID()){
447 $reloadConfig = TRUE;
448 CRM_Core_BAO_Domain
::setDomain($domainID);
450 $result[$domainID] = array();
451 foreach ($fieldsToSet as $name => $value) {
452 if(empty($fields['values'][$name]['config_only'])){
453 CRM_Core_BAO_Setting
::_setItem(
454 $fields['values'][$name],
456 $fields['values'][$name]['group_name'],
458 CRM_Utils_Array
::value('component_id', $params),
459 CRM_Utils_Array
::value('contact_id', $params),
460 CRM_Utils_Array
::value('created_id', $params),
464 if(!empty($fields['values'][$name]['prefetch'])){
465 if(!empty($fields['values'][$name]['config_key'])){
466 $name = $fields['values'][$name]['config_key'];
468 $config_keys[$name] = $value;
470 $result[$domainID][$name] = $value;
473 CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
476 if(!empty($config_keys)){
477 CRM_Core_BAO_ConfigSetting
::create($config_keys);
480 CRM_Core_BAO_Domain
::resetDomain();
488 * gets metadata about the settings fields (from getfields) based on the fields being passed in
490 * This function filters on the fields like 'version' & 'debug' that are not settings
492 * @param array $params Parameters as passed into API
493 * @param array $fields empty array to be populated with fields metadata
494 * @param bool $createMode
496 * @throws api_Exception
497 * @return array $fieldstoset name => value array of the fields to be set (with extraneous removed)
499 static function validateSettingsInput($params, &$fields, $createMode = TRUE) {
500 $group = CRM_Utils_Array
::value('group', $params);
502 $ignoredParams = array(
520 $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE));
521 $getFieldsParams = array('version' => 3);
522 if (count($settingParams) ==1) {
523 // ie we are only setting one field - we'll pass it into getfields for efficiency
524 list($name) = array_keys($settingParams);
525 $getFieldsParams['name'] = $name;
527 $fields = civicrm_api3('setting','getfields', $getFieldsParams);
528 $invalidParams = (array_diff_key($settingParams, $fields['values']));
529 if (!empty($invalidParams)) {
530 throw new api_Exception(implode(',', $invalidParams) . " not valid settings");
532 if (!empty($settingParams)) {
533 $filteredFields = array_intersect_key($settingParams, $fields['values']);
536 // no filters so we are interested in all for get mode. In create mode this means nothing to set
537 $filteredFields = $createMode ?
array() : $fields['values'];
539 return $filteredFields;
543 * Validate & convert settings input
545 * @value mixed value of the setting to be set
546 * @fieldSpec array Metadata for given field (drawn from the xml)
548 static function validateSetting(&$value, $fieldSpec) {
549 if($fieldSpec['type'] == 'String' && is_array($value)){
550 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,$value) . CRM_Core_DAO
::VALUE_SEPARATOR
;
552 if (empty($fieldSpec['validate_callback'])) {
556 list($class,$fn) = explode('::',$fieldSpec['validate_callback']);
557 if (!$class::$fn($value,$fieldSpec)) {
558 throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}");
564 * Validate & convert settings input - translate True False to 0 or 1
566 * @value mixed value of the setting to be set
567 * @fieldSpec array Metadata for given field (drawn from the xml)
569 static function validateBoolSetting(&$value, $fieldSpec) {
570 if (!CRM_Utils_Rule
::boolean($value)) {
571 throw new api_Exception("Boolean value required for {$fieldSpec['name']}");
583 * Delete some or all of the items in the settings table
585 * @param string $group The group name of the entries to be deleted
586 * @param string $name The name of the setting to be deleted
587 * @param int $componentID The optional component ID (so componenets can share the same name space)
593 static function deleteItem($group, $name = NULL, $componentID = NULL, $contactID = NULL) {
594 $dao = self::dao($group, $name, $componentID, $contactID);
597 // also reset memory cache if any
598 CRM_Utils_System::flushCache();
600 $cacheKey = self::inCache($group, $name, $componentID, $contactID, FALSE);
603 unset(self::$_cache[$cacheKey][$name]);
606 unset(self::$_cache[$cacheKey]);
612 * This provides information about the setting - similar to the fields concept for DAO information.
613 * As the setting is serialized code creating validation setting input needs to know the data type
614 * This also helps move information out of the form layer into the data layer where people can interact with
615 * it via the API or other mechanisms. In order to keep this consistent it is important the form layer
618 * Note that this function should never be called when using the runtime getvalue function. Caching works
619 * around the expectation it will be called during setting administration
621 * Function is intended for configuration rather than runtime access to settings
623 * The following params will filter the result. If none are passed all settings will be returns
625 * @params string $name Name of specific setting e.g customCSSURL
626 * @params integer $componentID id of relevant component.
628 * @param null $componentID
629 * @param array $filters
630 * @param null $domainID
631 * @param null $profile
633 * @return array $result - the following information as appropriate for each setting
637 * - add (CiviCRM version added)
643 static function getSettingSpecification(
649 $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile;
650 foreach ($filters as $filterField => $filterString) {
651 $cacheString .= "_{$filterField}_{$filterString}";
654 // the caching into 'All' seems to be a duplicate of caching to
655 // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook
656 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Specs', $cacheString, $componentID);
657 if ($settingsMetadata === NULL) {
658 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Spec', 'All', $componentID);
659 if (empty($settingsMetadata)) {
660 global $civicrm_root;
661 $metaDataFolders = array($civicrm_root. '/settings');
662 CRM_Utils_Hook
::alterSettingsFolders($metaDataFolders);
663 $settingsMetadata = self
::loadSettingsMetaDataFolders($metaDataFolders);
664 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All', $componentID);
669 CRM_Utils_Hook
::alterSettingsMetaData($settingsMetadata, $domainID, $profile);
670 self
::_filterSettingsSpecification($filters, $settingsMetadata);
673 // this is a bit 'heavy' if you are using hooks but this function
674 // is expected to only be called during setting administration
675 // it should not be called by 'getvalue' or 'getitem
676 CRM_Core_BAO_Cache
::setItem(
678 'CiviCRM setting Specs',
683 return $settingsMetadata;
688 * Load the settings files defined in a series of folders
689 * @param array $metaDataFolders list of folder paths
692 public static function loadSettingsMetaDataFolders($metaDataFolders) {
693 $settingsMetadata = array();
694 $loadedFolders = array();
695 foreach ($metaDataFolders as $metaDataFolder) {
696 $realFolder = realpath($metaDataFolder);
697 if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) {
698 $loadedFolders[$realFolder] = TRUE;
699 $settingsMetadata = $settingsMetadata + self
::loadSettingsMetaData($metaDataFolder);
702 return $settingsMetadata;
706 * Load up settings metadata from files
708 static function loadSettingsMetadata($metaDataFolder) {
709 $settingMetaData = array();
710 $settingsFiles = CRM_Utils_File
::findFiles($metaDataFolder, '*.setting.php');
711 foreach ($settingsFiles as $file) {
712 $settings = include $file;
713 $settingMetaData = array_merge($settingMetaData, $settings);
715 CRM_Core_BAO_Cache
::setItem($settingMetaData,'CiviCRM setting Spec', 'All');
716 return $settingMetaData;
720 * Filter the settings metadata according to filters passed in. This is a convenience filter
721 * and allows selective reverting / filling of settings
723 * @param array $filters Filters to match against data
724 * @param array $settingSpec metadata to filter
726 static function _filterSettingsSpecification($filters, &$settingSpec) {
727 if (empty($filters)) {
730 else if (array_keys($filters) == array('name')) {
731 $settingSpec = array($filters['name'] => CRM_Utils_Array
::value($filters['name'], $settingSpec, ''));
735 foreach ($settingSpec as $field => $fieldValues) {
736 if (array_intersect_assoc($fieldValues, $filters) != $filters) {
737 unset($settingSpec[$field]);
745 * Look for any missing settings and convert them from config or load default as appropriate
746 * This should be run from GenCode & also from upgrades to add any new defaults.
748 * Multisites have often been overlooked in upgrade scripts so can be expected to be missing
749 * a number of settings
751 static function updateSettingsFromMetaData() {
754 'domain_id' => 'all',
755 'filters' => array('prefetch' => 0),
757 $existing = civicrm_api('setting', 'get', $apiParams);
759 if (!empty($existing['values'])) {
760 $allSettings = civicrm_api('setting', 'getfields', array('version' => 3));
761 foreach ($existing['values'] as $domainID => $domainSettings) {
762 CRM_Core_BAO_Domain
::setDomain($domainID);
763 $missing = array_diff_key($allSettings['values'], $domainSettings);
764 foreach ($missing as $name => $settings) {
765 self
::convertConfigToSetting($name, $domainID);
767 CRM_Core_BAO_Domain
::resetDomain();
773 * move an item from being in the config array to being stored as a setting
774 * remove from config - as appropriate based on metadata
776 * Note that where the key name is being changed the 'legacy_key' will give us the old name
778 static function convertConfigToSetting($name, $domainID = NULL) {
779 // we have to force this here in case more than one domain is in play.
780 // whenever there is a possibility of more than one domain we must force it
781 $config = CRM_Core_Config
::singleton();
782 if (empty($domainID)) {
783 $domainID= CRM_Core_Config
::domainID();
785 $domain = new CRM_Core_DAO_Domain();
786 $domain->id
= $domainID;
788 if ($domain->config_backend
) {
789 $values = unserialize($domain->config_backend
);
793 $spec = self
::getSettingSpecification(NULL, array('name' => $name), $domainID);
794 $configKey = CRM_Utils_Array
::value('config_key', $spec[$name], CRM_Utils_Array
::value('legacy_key', $spec[$name], $name));
795 //if the key is set to config_only we don't need to do anything
796 if(empty($spec[$name]['config_only'])){
797 if (!empty($values[$configKey])) {
798 civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID));
801 civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID));
804 if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) {
805 unset($values[$configKey]);
806 $domain->config_backend
= serialize($values);
808 unset($config->$configKey);
813 static function valueOptions($group,
818 $returnField = 'name',
819 $returnNameANDLabels = FALSE,
822 $optionValue = self
::getItem($group, $name);
824 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, $returnField);
826 //enabled name => label require for new contact edit form, CRM-4605
827 if ($returnNameANDLabels) {
828 $names = $labels = $nameAndLabels = array();
829 if ($returnField == 'name') {
830 $names = $groupValues;
831 $labels = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'label');
834 $labels = $groupValues;
835 $names = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'name');
839 $returnValues = array();
840 foreach ($groupValues as $gn => $gv) {
841 $returnValues[$gv] = 0;
844 if ($optionValue && !empty($groupValues)) {
845 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
846 substr($optionValue, 1, -1)
849 if (!empty($dbValues)) {
850 foreach ($groupValues as $key => $val) {
851 if (in_array($key, $dbValues)) {
852 $returnValues[$val] = 1;
853 if ($returnNameANDLabels) {
854 $nameAndLabels[$names[$key]] = $labels[$key];
860 return ($returnNameANDLabels) ?
$nameAndLabels : $returnValues;
863 static function setValueOption($group,
873 elseif (is_array($value)) {
874 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, FALSE, NULL, $keyField);
877 foreach ($groupValues as $key => $val) {
878 if (!empty($value[$val])) {
883 if (!empty($cbValues)) {
884 $optionValue = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
885 array_keys($cbValues)
886 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
893 $optionValue = $value;
896 self
::setItem($optionValue, $group, $name);
899 static function fixAndStoreDirAndURL(&$params, $domainID = NULL) {
900 if (self
::isUpgradeFromPreFourOneAlpha1()) {
904 if (empty($domainID)) {
905 $domainID = CRM_Core_Config
::domainID();
908 SELECT name, group_name
911 AND ( group_name = %2
915 1 => array($domainID, 'Integer'),
916 2 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
917 3 => array(self
::URL_PREFERENCES_NAME
, 'String'),
920 $dirParams = array();
921 $urlParams = array();
923 $dao = CRM_Core_DAO
::executeQuery($sql,
929 // trap exceptions as error
933 if (is_a($dao, 'DB_Error')) {
934 if (CRM_Core_Config
::isUpgradeMode()) {
935 // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue
939 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
944 while ($dao->fetch()) {
945 if (!isset($params[$dao->name
])) {
948 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
949 $dirParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
952 $urlParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
954 unset($params[$dao->name
]);
957 if (!empty($dirParams)) {
958 self
::storeDirectoryOrURLPreferences($dirParams,
959 self
::DIRECTORY_PREFERENCES_NAME
963 if (!empty($urlParams)) {
964 self
::storeDirectoryOrURLPreferences($urlParams,
965 self
::URL_PREFERENCES_NAME
970 static function storeDirectoryOrURLPreferences(&$params, $group) {
971 foreach ($params as $name => $value) {
972 // always try to store relative directory or url from CMS root
973 $value = ($group == self
::DIRECTORY_PREFERENCES_NAME
) ? CRM_Utils_File
::relativeDirectory($value) : CRM_Utils_System
::relativeURL($value);
975 self
::setItem($value, $group, $name);
979 static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
980 if (CRM_Core_Config
::isUpgradeMode()) {
981 $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') ?
TRUE : FALSE;
982 // hack to set the resource base url so that js/ css etc is loaded correctly
984 $params['userFrameworkResourceURL'] = CRM_Utils_File
::addTrailingSlash(CIVICRM_UF_BASEURL
, '/') . str_replace('administrator', '', CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name'));
986 if (self
::isUpgradeFromPreFourOneAlpha1()) {
992 $config = CRM_Core_Config
::singleton();
996 SELECT name, group_name, value
998 WHERE ( group_name = %1
1002 $sqlParams = array(1 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
1003 2 => array(self
::URL_PREFERENCES_NAME
, 'String'),
1004 3 => array(CRM_Core_Config
::domainID(), 'Integer'),
1007 $dao = CRM_Core_DAO
::executeQuery($sql,
1013 // trap exceptions as error
1017 if (is_a($dao, 'DB_Error')) {
1018 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
1022 while ($dao->fetch()) {
1023 $value = self
::getOverride($dao->group_name
, $dao->name
, NULL);
1024 if ($value === NULL && $dao->value
) {
1025 $value = unserialize($dao->value
);
1026 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
1027 $value = CRM_Utils_File
::absoluteDirectory($value);
1030 // CRM-7622: we need to remove the language part
1031 $value = CRM_Utils_System
::absoluteURL($value, TRUE);
1034 // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available
1035 if (!isset($value) && !empty($params[$dao->name
])) {
1036 $value = $params[$dao->name
];
1038 $params[$dao->name
] = $value;
1041 $config->{$dao->name
} = $value;
1047 * Determine what, if any, overrides have been provided
1054 * @return mixed, NULL or an overriden value
1056 protected static function getOverride($group, $name, $default) {
1057 global $civicrm_setting;
1058 if ($group && $name && isset($civicrm_setting[$group][$name])) {
1059 return $civicrm_setting[$group][$name];
1067 * civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade
1071 static function isUpgradeFromPreFourOneAlpha1() {
1072 if (CRM_Core_Config
::isUpgradeMode()) {
1073 $currentVer = CRM_Core_BAO_Domain
::version();
1074 if (version_compare($currentVer, '4.1.alpha1') < 0) {