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 DEVELOPER_PREFERENCES_NAME
= 'Developer Preferences',
53 DIRECTORY_PREFERENCES_NAME
= 'Directory Preferences',
54 EVENT_PREFERENCES_NAME
= 'Event Preferences',
55 MAILING_PREFERENCES_NAME
= 'Mailing Preferences',
56 CONTRIBUTE_PREFERENCES_NAME
= 'Contribute Preferences',
57 MEMBER_PREFERENCES_NAME
= 'Member Preferences',
58 MULTISITE_PREFERENCES_NAME
= 'Multi Site Preferences',
59 PERSONAL_PREFERENCES_NAME
= 'Personal Preferences',
60 SYSTEM_PREFERENCES_NAME
= 'CiviCRM Preferences',
61 URL_PREFERENCES_NAME
= 'URL Preferences',
62 LOCALIZATION_PREFERENCES_NAME
= 'Localization Preferences',
63 SEARCH_PREFERENCES_NAME
= 'Search Preferences';
64 static $_cache = NULL;
67 * Checks whether an item is present in the in-memory cache table
69 * @param string $group (required) The group name of the item
70 * @param string $name (required) The name of the setting
71 * @param int $componentID The optional component ID (so components can share the same name space)
72 * @param int $contactID If set, this is a contactID specific setting, else its a global setting
73 * @param bool|int $load if true, load from local cache (typically memcache)
75 * @param int $domainID
78 * @return boolean true if item is already in cache
82 static function inCache(
91 if (!isset(self
::$_cache)) {
92 self
::$_cache = array();
95 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
99 ($force ||
!isset(self
::$_cache[$cacheKey]))
102 // check in civi cache if present (typically memcache)
103 $globalCache = CRM_Utils_Cache
::singleton();
104 $result = $globalCache->get($cacheKey);
107 self
::$_cache[$cacheKey] = $result;
111 return isset(self
::$_cache[$cacheKey]) ?
$cacheKey : NULL;
114 * Allow key o be cleared
115 * @param string $cacheKey
117 static function flushCache($cacheKey){
118 unset(self
::$_cache[$cacheKey]);
119 $globalCache = CRM_Utils_Cache
::singleton();
120 $globalCache->delete($cacheKey);
126 * @param int $componentID
127 * @param int $contactID
128 * @param int $domainID
132 static function setCache($values,
138 if (!isset(self
::$_cache)) {
139 self
::$_cache = array();
142 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
144 self
::$_cache[$cacheKey] = $values;
146 $globalCache = CRM_Utils_Cache
::singleton();
147 $result = $globalCache->set($cacheKey, $values);
155 * @param int $componentID
156 * @param int $contactID
157 * @param int $domainID
159 * @return CRM_Core_DAO_Domain|CRM_Core_DAO_Setting
161 static function dao($group,
167 if (self
::isUpgradeFromPreFourOneAlpha1()) {
168 // civicrm_setting table is not going to be present. For now we'll just
169 // return a dummy object
170 $dao = new CRM_Core_DAO_Domain();
171 $dao->id
= -1; // so ->find() doesn't fetch any data later on
174 $dao = new CRM_Core_DAO_Setting();
176 $dao->group_name
= $group;
178 $dao->component_id
= $componentID;
179 if (empty($domainID)) {
180 $dao->domain_id
= CRM_Core_Config
::domainID();
183 $dao->domain_id
= $domainID;
187 $dao->contact_id
= $contactID;
198 * Retrieve the value of a setting from the DB table
200 * @param string $group (required) The group name of the item
201 * @param string $name (required) The name under which this item is stored
202 * @param int $componentID The optional component ID (so componenets can share the same name space)
203 * @param string $defaultValue The default value to return for this setting if not present in DB
204 * @param int $contactID If set, this is a contactID specific setting, else its a global setting
206 * @param int $domainID
208 * @return mixed The data if present in the setting table, else null
212 static function getItem(
216 $defaultValue = NULL,
221 if (NULL !== ($override = self
::getOverride($group, $name, NULL))) {
225 if (empty($domainID)) {
226 $domainID = CRM_Core_Config
::domainID();
228 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, TRUE, $domainID);
230 $dao = self
::dao($group, NULL, $componentID, $contactID, $domainID);
234 while ($dao->fetch()) {
235 if (NULL !== ($override = self
::getOverride($group, $dao->name
, NULL))) {
236 $values[$dao->name
] = $override;
238 elseif ($dao->value
) {
239 $values[$dao->name
] = unserialize($dao->value
);
242 $values[$dao->name
] = NULL;
247 $cacheKey = self
::setCache($values, $group, $componentID, $contactID, $domainID);
249 return $name ? CRM_Utils_Array
::value($name, self
::$_cache[$cacheKey], $defaultValue) : self
::$_cache[$cacheKey];
253 * Store multiple items in the setting table
255 * @param array $params (required) An api formatted array of keys and values
256 * @param null $domains
257 * @param $settingsToReturn
259 * @domains array an array of domains to get settings for. Default is the current domain
264 static function getItems(&$params, $domains = NULL, $settingsToReturn) {
265 $originalDomain = CRM_Core_Config
::domainID();
266 if (empty($domains)) {
267 $domains[] = $originalDomain;
269 if (!empty($settingsToReturn) && !is_array($settingsToReturn)) {
270 $settingsToReturn = array($settingsToReturn);
272 $reloadConfig = FALSE;
274 $fields = $result = array();
275 $fieldsToGet = self
::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE);
276 foreach ($domains as $domainID) {
277 if($domainID != CRM_Core_Config
::domainID()){
278 $reloadConfig = TRUE;
279 CRM_Core_BAO_Domain
::setDomain($domainID);
281 $config = CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
282 $result[$domainID] = array();
283 foreach ($fieldsToGet as $name => $value) {
284 if(!empty($fields['values'][$name]['prefetch'])){
285 if(isset($params['filters']) && isset($params['filters']['prefetch'])
286 && $params['filters']['prefetch'] == 0){
287 // we are filtering out the prefetches from the return array
291 $configKey = CRM_Utils_Array
::value('config_key', $fields['values'][$name], $name);
292 if(isset($config->$configKey)){
293 $setting = $config->$configKey;
298 CRM_Core_BAO_Setting
::getItem(
299 $fields['values'][$name]['group_name'],
301 CRM_Utils_Array
::value('component_id', $params),
303 CRM_Utils_Array
::value('contact_id', $params),
307 if (!is_null($setting)) {
308 // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones
309 // e.g for revert of fill actions
310 $result[$domainID][$name] = $setting;
313 CRM_Core_BAO_Domain
::resetDomain();
319 * Store an item in the setting table
321 * _setItem() is the common logic shared by setItem() and setItems().
323 * @param object $value (required) The value that will be serialized and stored
324 * @param string $group (required) The group name of the item
325 * @param string $name (required) The name of the setting
326 * @param int $componentID The optional component ID (so componenets can share the same name space)
327 * @param int $contactID
328 * @param int $createdID An optional ID to assign the creator to. If not set, retrieved from session
330 * @param int $domainID
336 static function setItem(
346 $fieldsToSet = self
::validateSettingsInput(array($name => $value), $fields);
347 //We haven't traditionally validated inputs to setItem, so this breaks things.
348 //foreach ($fieldsToSet as $settingField => &$settingValue) {
349 // self::validateSetting($settingValue, $fields['values'][$settingField]);
352 return self
::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID);
356 * Store an item in a setting table
358 * _setItem() is the common logic shared by setItem() and setItems().
360 * @param array $metadata metadata describing this field
363 * @param string $name
364 * @param int $componentID
365 * @param int $contactID
366 * @param int $createdID
367 * @param int $domainID
369 static function _setItem(
379 if (empty($domainID)) {
380 $domainID = CRM_Core_Config
::domainID();
383 $dao = self
::dao($group, $name, $componentID, $contactID, $domainID);
386 if (isset($metadata['on_change'])) {
387 foreach ($metadata['on_change'] as $callback) {
388 call_user_func($callback, unserialize($dao->value
), $value, $metadata);
392 if (CRM_Utils_System
::isNull($value)) {
393 $dao->value
= 'null';
396 $dao->value
= serialize($value);
399 $dao->created_date
= date('Ymdhis');
402 $dao->created_id
= $createdID;
405 $session = CRM_Core_Session
::singleton();
406 $createdID = $session->get('userID');
409 // ensure that this is a valid contact id (for session inconsistency rules)
410 $cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
416 $dao->created_id
= $session->get('userID');
424 // also save in cache if needed
425 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, FALSE, $domainID);
427 self
::$_cache[$cacheKey][$name] = $value;
432 * Store multiple items in the setting table. Note that this will also store config keys
433 * the storage is determined by the metdata and is affected by
434 * 'name' setting's name
435 * 'prefetch' = store in config
436 * 'config_only' = don't store in settings
437 * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict
438 * 'legacy_key' = rename from config or setting with this name
440 * _setItem() is the common logic shared by setItem() and setItems().
442 * @param array $params (required) An api formatted array of keys and values
443 * @param null $domains
445 * @throws api_Exception
446 * @domains array an array of domains to get settings for. Default is the current domain
451 static function setItems(&$params, $domains = NULL) {
452 $originalDomain = CRM_Core_Config
::domainID();
453 if (empty($domains)) {
454 $domains[] = $originalDomain;
456 $reloadConfig = FALSE;
457 $fields = $config_keys = array();
458 $fieldsToSet = self
::validateSettingsInput($params, $fields);
460 foreach ($fieldsToSet as $settingField => &$settingValue) {
461 self
::validateSetting($settingValue, $fields['values'][$settingField]);
464 foreach ($domains as $domainID) {
465 if($domainID != CRM_Core_Config
::domainID()){
466 $reloadConfig = TRUE;
467 CRM_Core_BAO_Domain
::setDomain($domainID);
469 $result[$domainID] = array();
470 foreach ($fieldsToSet as $name => $value) {
471 if(empty($fields['values'][$name]['config_only'])){
472 CRM_Core_BAO_Setting
::_setItem(
473 $fields['values'][$name],
475 $fields['values'][$name]['group_name'],
477 CRM_Utils_Array
::value('component_id', $params),
478 CRM_Utils_Array
::value('contact_id', $params),
479 CRM_Utils_Array
::value('created_id', $params),
483 if(!empty($fields['values'][$name]['prefetch'])){
484 if(!empty($fields['values'][$name]['config_key'])){
485 $name = $fields['values'][$name]['config_key'];
487 $config_keys[$name] = $value;
489 $result[$domainID][$name] = $value;
492 CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
495 if(!empty($config_keys)){
496 CRM_Core_BAO_ConfigSetting
::create($config_keys);
499 CRM_Core_BAO_Domain
::resetDomain();
507 * Gets metadata about the settings fields (from getfields) based on the fields being passed in
509 * This function filters on the fields like 'version' & 'debug' that are not settings
511 * @param array $params Parameters as passed into API
512 * @param array $fields empty array to be populated with fields metadata
513 * @param bool $createMode
515 * @throws api_Exception
516 * @return array $fieldstoset name => value array of the fields to be set (with extraneous removed)
518 static function validateSettingsInput($params, &$fields, $createMode = TRUE) {
519 $group = CRM_Utils_Array
::value('group', $params);
521 $ignoredParams = array(
539 $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE));
540 $getFieldsParams = array('version' => 3);
541 if (count($settingParams) ==1) {
542 // ie we are only setting one field - we'll pass it into getfields for efficiency
543 list($name) = array_keys($settingParams);
544 $getFieldsParams['name'] = $name;
546 $fields = civicrm_api3('setting','getfields', $getFieldsParams);
547 $invalidParams = (array_diff_key($settingParams, $fields['values']));
548 if (!empty($invalidParams)) {
549 throw new api_Exception(implode(',', $invalidParams) . " not valid settings");
551 if (!empty($settingParams)) {
552 $filteredFields = array_intersect_key($settingParams, $fields['values']);
555 // no filters so we are interested in all for get mode. In create mode this means nothing to set
556 $filteredFields = $createMode ?
array() : $fields['values'];
558 return $filteredFields;
562 * Validate & convert settings input
564 * @value mixed value of the setting to be set
565 * @fieldSpec array Metadata for given field (drawn from the xml)
567 static function validateSetting(&$value, $fieldSpec) {
568 if($fieldSpec['type'] == 'String' && is_array($value)){
569 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,$value) . CRM_Core_DAO
::VALUE_SEPARATOR
;
571 if (empty($fieldSpec['validate_callback'])) {
575 list($class,$fn) = explode('::',$fieldSpec['validate_callback']);
576 if (!$class::$fn($value,$fieldSpec)) {
577 throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}");
583 * Validate & convert settings input - translate True False to 0 or 1
585 * @value mixed value of the setting to be set
586 * @fieldSpec array Metadata for given field (drawn from the xml)
588 static function validateBoolSetting(&$value, $fieldSpec) {
589 if (!CRM_Utils_Rule
::boolean($value)) {
590 throw new api_Exception("Boolean value required for {$fieldSpec['name']}");
602 * This provides information about the setting - similar to the fields concept for DAO information.
603 * As the setting is serialized code creating validation setting input needs to know the data type
604 * This also helps move information out of the form layer into the data layer where people can interact with
605 * it via the API or other mechanisms. In order to keep this consistent it is important the form layer
608 * Note that this function should never be called when using the runtime getvalue function. Caching works
609 * around the expectation it will be called during setting administration
611 * Function is intended for configuration rather than runtime access to settings
613 * The following params will filter the result. If none are passed all settings will be returns
615 * @param int $componentID id of relevant component
616 * @param array $filters
617 * @param int $domainID
618 * @param null $profile
620 * @return array $result - the following information as appropriate for each setting
624 * - add (CiviCRM version added)
630 static function getSettingSpecification(
636 $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile;
637 foreach ($filters as $filterField => $filterString) {
638 $cacheString .= "_{$filterField}_{$filterString}";
641 // the caching into 'All' seems to be a duplicate of caching to
642 // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook
643 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Specs', $cacheString, $componentID);
644 if ($settingsMetadata === NULL) {
645 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Spec', 'All', $componentID);
646 if (empty($settingsMetadata)) {
647 global $civicrm_root;
648 $metaDataFolders = array($civicrm_root. '/settings');
649 CRM_Utils_Hook
::alterSettingsFolders($metaDataFolders);
650 $settingsMetadata = self
::loadSettingsMetaDataFolders($metaDataFolders);
651 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All', $componentID);
656 CRM_Utils_Hook
::alterSettingsMetaData($settingsMetadata, $domainID, $profile);
657 self
::_filterSettingsSpecification($filters, $settingsMetadata);
660 // this is a bit 'heavy' if you are using hooks but this function
661 // is expected to only be called during setting administration
662 // it should not be called by 'getvalue' or 'getitem
663 CRM_Core_BAO_Cache
::setItem(
665 'CiviCRM setting Specs',
670 return $settingsMetadata;
675 * Load the settings files defined in a series of folders
676 * @param array $metaDataFolders list of folder paths
679 public static function loadSettingsMetaDataFolders($metaDataFolders) {
680 $settingsMetadata = array();
681 $loadedFolders = array();
682 foreach ($metaDataFolders as $metaDataFolder) {
683 $realFolder = realpath($metaDataFolder);
684 if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) {
685 $loadedFolders[$realFolder] = TRUE;
686 $settingsMetadata = $settingsMetadata + self
::loadSettingsMetaData($metaDataFolder);
689 return $settingsMetadata;
693 * Load up settings metadata from files
695 static function loadSettingsMetadata($metaDataFolder) {
696 $settingMetaData = array();
697 $settingsFiles = CRM_Utils_File
::findFiles($metaDataFolder, '*.setting.php');
698 foreach ($settingsFiles as $file) {
699 $settings = include $file;
700 $settingMetaData = array_merge($settingMetaData, $settings);
702 CRM_Core_BAO_Cache
::setItem($settingMetaData,'CiviCRM setting Spec', 'All');
703 return $settingMetaData;
707 * Filter the settings metadata according to filters passed in. This is a convenience filter
708 * and allows selective reverting / filling of settings
710 * @param array $filters Filters to match against data
711 * @param array $settingSpec metadata to filter
713 static function _filterSettingsSpecification($filters, &$settingSpec) {
714 if (empty($filters)) {
717 else if (array_keys($filters) == array('name')) {
718 $settingSpec = array($filters['name'] => CRM_Utils_Array
::value($filters['name'], $settingSpec, ''));
722 foreach ($settingSpec as $field => $fieldValues) {
723 if (array_intersect_assoc($fieldValues, $filters) != $filters) {
724 unset($settingSpec[$field]);
732 * Look for any missing settings and convert them from config or load default as appropriate
733 * This should be run from GenCode & also from upgrades to add any new defaults.
735 * Multisites have often been overlooked in upgrade scripts so can be expected to be missing
736 * a number of settings
738 static function updateSettingsFromMetaData() {
741 'domain_id' => 'all',
742 'filters' => array('prefetch' => 0),
744 $existing = civicrm_api('setting', 'get', $apiParams);
746 if (!empty($existing['values'])) {
747 $allSettings = civicrm_api('setting', 'getfields', array('version' => 3));
748 foreach ($existing['values'] as $domainID => $domainSettings) {
749 CRM_Core_BAO_Domain
::setDomain($domainID);
750 $missing = array_diff_key($allSettings['values'], $domainSettings);
751 foreach ($missing as $name => $settings) {
752 self
::convertConfigToSetting($name, $domainID);
754 CRM_Core_BAO_Domain
::resetDomain();
760 * Move an item from being in the config array to being stored as a setting
761 * remove from config - as appropriate based on metadata
763 * Note that where the key name is being changed the 'legacy_key' will give us the old name
765 static function convertConfigToSetting($name, $domainID = NULL) {
766 // we have to force this here in case more than one domain is in play.
767 // whenever there is a possibility of more than one domain we must force it
768 $config = CRM_Core_Config
::singleton();
769 if (empty($domainID)) {
770 $domainID= CRM_Core_Config
::domainID();
772 $domain = new CRM_Core_DAO_Domain();
773 $domain->id
= $domainID;
775 if ($domain->config_backend
) {
776 $values = unserialize($domain->config_backend
);
780 $spec = self
::getSettingSpecification(NULL, array('name' => $name), $domainID);
781 $configKey = CRM_Utils_Array
::value('config_key', $spec[$name], CRM_Utils_Array
::value('legacy_key', $spec[$name], $name));
782 //if the key is set to config_only we don't need to do anything
783 if(empty($spec[$name]['config_only'])){
784 if (!empty($values[$configKey])) {
785 civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID));
788 civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID));
791 if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) {
792 unset($values[$configKey]);
793 $domain->config_backend
= serialize($values);
795 unset($config->$configKey);
802 * @param string $name
803 * @param bool $system
805 * @param bool $localize
806 * @param string $returnField
807 * @param bool $returnNameANDLabels
808 * @param null $condition
812 static function valueOptions($group,
817 $returnField = 'name',
818 $returnNameANDLabels = FALSE,
821 $optionValue = self
::getItem($group, $name);
823 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, $returnField);
825 //enabled name => label require for new contact edit form, CRM-4605
826 if ($returnNameANDLabels) {
827 $names = $labels = $nameAndLabels = array();
828 if ($returnField == 'name') {
829 $names = $groupValues;
830 $labels = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'label');
833 $labels = $groupValues;
834 $names = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'name');
838 $returnValues = array();
839 foreach ($groupValues as $gn => $gv) {
840 $returnValues[$gv] = 0;
843 if ($optionValue && !empty($groupValues)) {
844 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
845 substr($optionValue, 1, -1)
848 if (!empty($dbValues)) {
849 foreach ($groupValues as $key => $val) {
850 if (in_array($key, $dbValues)) {
851 $returnValues[$val] = 1;
852 if ($returnNameANDLabels) {
853 $nameAndLabels[$names[$key]] = $labels[$key];
859 return ($returnNameANDLabels) ?
$nameAndLabels : $returnValues;
864 * @param string $name
866 * @param bool $system
868 * @param string $keyField
870 static function setValueOption($group,
880 elseif (is_array($value)) {
881 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, FALSE, NULL, $keyField);
884 foreach ($groupValues as $key => $val) {
885 if (!empty($value[$val])) {
890 if (!empty($cbValues)) {
891 $optionValue = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
892 array_keys($cbValues)
893 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
900 $optionValue = $value;
903 self
::setItem($optionValue, $group, $name);
907 * @param array $params
908 * @param int $domainID
910 static function fixAndStoreDirAndURL(&$params, $domainID = NULL) {
911 if (self
::isUpgradeFromPreFourOneAlpha1()) {
915 if (empty($domainID)) {
916 $domainID = CRM_Core_Config
::domainID();
919 SELECT name, group_name
922 AND ( group_name = %2
926 1 => array($domainID, 'Integer'),
927 2 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
928 3 => array(self
::URL_PREFERENCES_NAME
, 'String'),
931 $dirParams = array();
932 $urlParams = array();
934 $dao = CRM_Core_DAO
::executeQuery($sql,
940 // trap exceptions as error
944 if (is_a($dao, 'DB_Error')) {
945 if (CRM_Core_Config
::isUpgradeMode()) {
946 // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue
950 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
955 while ($dao->fetch()) {
956 if (!isset($params[$dao->name
])) {
959 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
960 $dirParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
963 $urlParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
965 unset($params[$dao->name
]);
968 if (!empty($dirParams)) {
969 self
::storeDirectoryOrURLPreferences($dirParams,
970 self
::DIRECTORY_PREFERENCES_NAME
974 if (!empty($urlParams)) {
975 self
::storeDirectoryOrURLPreferences($urlParams,
976 self
::URL_PREFERENCES_NAME
982 * @param array $params
985 static function storeDirectoryOrURLPreferences(&$params, $group) {
986 foreach ($params as $name => $value) {
987 // always try to store relative directory or url from CMS root
988 $value = ($group == self
::DIRECTORY_PREFERENCES_NAME
) ? CRM_Utils_File
::relativeDirectory($value) : CRM_Utils_System
::relativeURL($value);
990 self
::setItem($value, $group, $name);
995 * @param array $params
996 * @param bool $setInConfig
998 static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
999 if (CRM_Core_Config
::isUpgradeMode()) {
1000 $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') ?
TRUE : FALSE;
1001 // hack to set the resource base url so that js/ css etc is loaded correctly
1003 $params['userFrameworkResourceURL'] = CRM_Utils_File
::addTrailingSlash(CIVICRM_UF_BASEURL
, '/') . str_replace('administrator', '', CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name'));
1005 if (self
::isUpgradeFromPreFourOneAlpha1()) {
1011 $config = CRM_Core_Config
::singleton();
1015 SELECT name, group_name, value
1016 FROM civicrm_setting
1017 WHERE ( group_name = %1
1018 OR group_name = %2 )
1021 $sqlParams = array(1 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
1022 2 => array(self
::URL_PREFERENCES_NAME
, 'String'),
1023 3 => array(CRM_Core_Config
::domainID(), 'Integer'),
1026 $dao = CRM_Core_DAO
::executeQuery($sql,
1032 // trap exceptions as error
1036 if (is_a($dao, 'DB_Error')) {
1037 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
1041 while ($dao->fetch()) {
1042 $value = self
::getOverride($dao->group_name
, $dao->name
, NULL);
1043 if ($value === NULL && $dao->value
) {
1044 $value = unserialize($dao->value
);
1045 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
1046 $value = CRM_Utils_File
::absoluteDirectory($value);
1049 // CRM-7622: we need to remove the language part
1050 $value = CRM_Utils_System
::absoluteURL($value, TRUE);
1053 // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available
1054 if (!isset($value) && !empty($params[$dao->name
])) {
1055 $value = $params[$dao->name
];
1057 $params[$dao->name
] = $value;
1060 $config->{$dao->name
} = $value;
1066 * Determine what, if any, overrides have been provided
1070 * @param string $name
1073 * @return mixed, NULL or an overriden value
1075 protected static function getOverride($group, $name, $default) {
1076 global $civicrm_setting;
1077 if ($group && $name && isset($civicrm_setting[$group][$name])) {
1078 return $civicrm_setting[$group][$name];
1086 * Civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade
1090 static function isUpgradeFromPreFourOneAlpha1() {
1091 if (CRM_Core_Config
::isUpgradeMode()) {
1092 $currentVer = CRM_Core_BAO_Domain
::version();
1093 if (version_compare($currentVer, '4.1.alpha1') < 0) {