3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
70 * (required) The group name of the item.
72 * (required) The name of the setting.
73 * @param int $componentID
74 * The optional component ID (so components can share the same name space).
75 * @param int $contactID
76 * If set, this is a contactID specific setting, else its a global setting.
77 * @param bool|int $load if true, load from local cache (typically memcache)
79 * @param int $domainID
83 * true if item is already in cache
85 public static function inCache(
94 if (!isset(self
::$_cache)) {
95 self
::$_cache = array();
98 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
102 ($force ||
!isset(self
::$_cache[$cacheKey]))
105 // check in civi cache if present (typically memcache)
106 $globalCache = CRM_Utils_Cache
::singleton();
107 $result = $globalCache->get($cacheKey);
110 self
::$_cache[$cacheKey] = $result;
114 return isset(self
::$_cache[$cacheKey]) ?
$cacheKey : NULL;
118 * Allow key o be cleared.
119 * @param string $cacheKey
121 public static function flushCache($cacheKey) {
122 unset(self
::$_cache[$cacheKey]);
123 $globalCache = CRM_Utils_Cache
::singleton();
124 $globalCache->delete($cacheKey);
130 * @param int $componentID
131 * @param int $contactID
132 * @param int $domainID
136 public static function setCache(
143 if (!isset(self
::$_cache)) {
144 self
::$_cache = array();
147 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
149 self
::$_cache[$cacheKey] = $values;
151 $globalCache = CRM_Utils_Cache
::singleton();
152 $result = $globalCache->set($cacheKey, $values);
160 * @param int $componentID
161 * @param int $contactID
162 * @param int $domainID
164 * @return CRM_Core_DAO_Domain|CRM_Core_DAO_Setting
166 public static function dao(
173 if (self
::isUpgradeFromPreFourOneAlpha1()) {
174 // civicrm_setting table is not going to be present. For now we'll just
175 // return a dummy object
176 $dao = new CRM_Core_DAO_Domain();
177 $dao->id
= -1; // so ->find() doesn't fetch any data later on
180 $dao = new CRM_Core_DAO_Setting();
182 $dao->group_name
= $group;
184 $dao->component_id
= $componentID;
185 if (empty($domainID)) {
186 $dao->domain_id
= CRM_Core_Config
::domainID();
189 $dao->domain_id
= $domainID;
193 $dao->contact_id
= $contactID;
204 * Retrieve the value of a setting from the DB table.
206 * @param string $group
207 * (required) The group name of the item.
208 * @param string $name
209 * (required) The name under which this item is stored.
210 * @param int $componentID
211 * The optional component ID (so componenets can share the same name space).
212 * @param string $defaultValue
213 * The default value to return for this setting if not present in DB.
214 * @param int $contactID
215 * If set, this is a contactID specific setting, else its a global setting.
217 * @param int $domainID
220 * The data if present in the setting table, else null
222 public static function getItem(
226 $defaultValue = NULL,
231 if (NULL !== ($override = self
::getOverride($group, $name, NULL))) {
235 if (empty($domainID)) {
236 $domainID = CRM_Core_Config
::domainID();
238 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, TRUE, $domainID);
240 $dao = self
::dao($group, NULL, $componentID, $contactID, $domainID);
244 while ($dao->fetch()) {
245 if (NULL !== ($override = self
::getOverride($group, $dao->name
, NULL))) {
246 $values[$dao->name
] = $override;
248 elseif ($dao->value
) {
249 $values[$dao->name
] = unserialize($dao->value
);
252 $values[$dao->name
] = NULL;
257 $cacheKey = self
::setCache($values, $group, $componentID, $contactID, $domainID);
259 return $name ? CRM_Utils_Array
::value($name, self
::$_cache[$cacheKey], $defaultValue) : self
::$_cache[$cacheKey];
263 * Store multiple items in the setting table.
265 * @param array $params
266 * (required) An api formatted array of keys and values.
267 * @param array $domains Array of domains to get settings for. Default is the current domain
268 * @param $settingsToReturn
272 public static function getItems(&$params, $domains = NULL, $settingsToReturn) {
273 $originalDomain = CRM_Core_Config
::domainID();
274 if (empty($domains)) {
275 $domains[] = $originalDomain;
277 if (!empty($settingsToReturn) && !is_array($settingsToReturn)) {
278 $settingsToReturn = array($settingsToReturn);
280 $reloadConfig = FALSE;
282 $fields = $result = array();
283 $fieldsToGet = self
::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE);
284 foreach ($domains as $domainID) {
285 if ($domainID != CRM_Core_Config
::domainID()) {
286 $reloadConfig = TRUE;
287 CRM_Core_BAO_Domain
::setDomain($domainID);
289 $config = CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
290 $result[$domainID] = array();
291 foreach ($fieldsToGet as $name => $value) {
292 if (!empty($fields['values'][$name]['prefetch'])) {
293 if (isset($params['filters']) && isset($params['filters']['prefetch'])
294 && $params['filters']['prefetch'] == 0
296 // we are filtering out the prefetches from the return array
300 $configKey = CRM_Utils_Array
::value('config_key', $fields['values'][$name], $name);
301 if (isset($config->$configKey)) {
302 $setting = $config->$configKey;
306 $setting = CRM_Core_BAO_Setting
::getItem(
307 $fields['values'][$name]['group_name'],
309 CRM_Utils_Array
::value('component_id', $params),
311 CRM_Utils_Array
::value('contact_id', $params),
315 if (!is_null($setting)) {
316 // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones
317 // e.g for revert of fill actions
318 $result[$domainID][$name] = $setting;
321 CRM_Core_BAO_Domain
::resetDomain();
327 * Store an item in the setting table.
329 * _setItem() is the common logic shared by setItem() and setItems().
331 * @param object $value
332 * (required) The value that will be serialized and stored.
333 * @param string $group
334 * (required) The group name of the item.
335 * @param string $name
336 * (required) The name of the setting.
337 * @param int $componentID
338 * The optional component ID (so componenets can share the same name space).
339 * @param int $contactID
340 * @param int $createdID
341 * An optional ID to assign the creator to. If not set, retrieved from session.
343 * @param int $domainID
347 public static function setItem(
357 $fieldsToSet = self
::validateSettingsInput(array($name => $value), $fields);
358 //We haven't traditionally validated inputs to setItem, so this breaks things.
359 //foreach ($fieldsToSet as $settingField => &$settingValue) {
360 // self::validateSetting($settingValue, $fields['values'][$settingField]);
363 return self
::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID);
367 * Store an item in a setting table.
369 * _setItem() is the common logic shared by setItem() and setItems().
371 * @param array $metadata
372 * Metadata describing this field.
375 * @param string $name
376 * @param int $componentID
377 * @param int $contactID
378 * @param int $createdID
379 * @param int $domainID
381 public static function _setItem(
391 if (empty($domainID)) {
392 $domainID = CRM_Core_Config
::domainID();
395 $dao = self
::dao($group, $name, $componentID, $contactID, $domainID);
398 if (isset($metadata['on_change'])) {
399 foreach ($metadata['on_change'] as $callback) {
400 call_user_func($callback, unserialize($dao->value
), $value, $metadata);
404 if (CRM_Utils_System
::isNull($value)) {
405 $dao->value
= 'null';
408 $dao->value
= serialize($value);
411 $dao->created_date
= date('Ymdhis');
414 $dao->created_id
= $createdID;
417 $session = CRM_Core_Session
::singleton();
418 $createdID = $session->get('userID');
421 // ensure that this is a valid contact id (for session inconsistency rules)
422 $cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
428 $dao->created_id
= $session->get('userID');
436 // also save in cache if needed
437 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, FALSE, $domainID);
439 self
::$_cache[$cacheKey][$name] = $value;
444 * Store multiple items in the setting table. Note that this will also store config keys
445 * the storage is determined by the metdata and is affected by
446 * 'name' setting's name
447 * 'prefetch' = store in config
448 * 'config_only' = don't store in settings
449 * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict
450 * 'legacy_key' = rename from config or setting with this name
452 * _setItem() is the common logic shared by setItem() and setItems().
454 * @param array $params
455 * (required) An api formatted array of keys and values.
456 * @param null $domains
458 * @throws api_Exception
459 * @domains array an array of domains to get settings for. Default is the current domain
462 public static function setItems(&$params, $domains = NULL) {
463 $originalDomain = CRM_Core_Config
::domainID();
464 if (empty($domains)) {
465 $domains[] = $originalDomain;
467 $reloadConfig = FALSE;
468 $fields = $config_keys = array();
469 $fieldsToSet = self
::validateSettingsInput($params, $fields);
471 foreach ($fieldsToSet as $settingField => &$settingValue) {
472 self
::validateSetting($settingValue, $fields['values'][$settingField]);
475 foreach ($domains as $domainID) {
476 if ($domainID != CRM_Core_Config
::domainID()) {
477 $reloadConfig = TRUE;
478 CRM_Core_BAO_Domain
::setDomain($domainID);
480 $result[$domainID] = array();
481 foreach ($fieldsToSet as $name => $value) {
482 if (empty($fields['values'][$name]['config_only'])) {
483 CRM_Core_BAO_Setting
::_setItem(
484 $fields['values'][$name],
486 $fields['values'][$name]['group_name'],
488 CRM_Utils_Array
::value('component_id', $params),
489 CRM_Utils_Array
::value('contact_id', $params),
490 CRM_Utils_Array
::value('created_id', $params),
494 if (!empty($fields['values'][$name]['prefetch'])) {
495 if (!empty($fields['values'][$name]['config_key'])) {
496 $name = $fields['values'][$name]['config_key'];
498 $config_keys[$name] = $value;
500 $result[$domainID][$name] = $value;
503 CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
506 if (!empty($config_keys)) {
507 CRM_Core_BAO_ConfigSetting
::create($config_keys);
510 CRM_Core_BAO_Domain
::resetDomain();
518 * Gets metadata about the settings fields (from getfields) based on the fields being passed in
520 * This function filters on the fields like 'version' & 'debug' that are not settings
522 * @param array $params
523 * Parameters as passed into API.
524 * @param array $fields
525 * Empty array to be populated with fields metadata.
526 * @param bool $createMode
528 * @throws api_Exception
530 * name => value array of the fields to be set (with extraneous removed)
532 public static function validateSettingsInput($params, &$fields, $createMode = TRUE) {
533 $group = CRM_Utils_Array
::value('group', $params);
535 $ignoredParams = array(
554 $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE));
555 $getFieldsParams = array('version' => 3);
556 if (count($settingParams) == 1) {
557 // ie we are only setting one field - we'll pass it into getfields for efficiency
558 list($name) = array_keys($settingParams);
559 $getFieldsParams['name'] = $name;
561 $fields = civicrm_api3('setting', 'getfields', $getFieldsParams);
562 $invalidParams = (array_diff_key($settingParams, $fields['values']));
563 if (!empty($invalidParams)) {
564 throw new api_Exception(implode(',', array_keys($invalidParams)) . " not valid settings");
566 if (!empty($settingParams)) {
567 $filteredFields = array_intersect_key($settingParams, $fields['values']);
570 // no filters so we are interested in all for get mode. In create mode this means nothing to set
571 $filteredFields = $createMode ?
array() : $fields['values'];
573 return $filteredFields;
577 * Validate & convert settings input
579 * @value mixed value of the setting to be set
580 * @fieldSpec array Metadata for given field (drawn from the xml)
582 public static function validateSetting(&$value, $fieldSpec) {
583 if ($fieldSpec['type'] == 'String' && is_array($value)) {
584 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value) . CRM_Core_DAO
::VALUE_SEPARATOR
;
586 if (empty($fieldSpec['validate_callback'])) {
590 list($class, $fn) = explode('::', $fieldSpec['validate_callback']);
591 if (!$class::$fn($value, $fieldSpec)) {
592 throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}");
598 * Validate & convert settings input - translate True False to 0 or 1
600 * @value mixed value of the setting to be set
601 * @fieldSpec array Metadata for given field (drawn from the xml)
603 public static function validateBoolSetting(&$value, $fieldSpec) {
604 if (!CRM_Utils_Rule
::boolean($value)) {
605 throw new api_Exception("Boolean value required for {$fieldSpec['name']}");
617 * This provides information about the setting - similar to the fields concept for DAO information.
618 * As the setting is serialized code creating validation setting input needs to know the data type
619 * This also helps move information out of the form layer into the data layer where people can interact with
620 * it via the API or other mechanisms. In order to keep this consistent it is important the form layer
623 * Note that this function should never be called when using the runtime getvalue function. Caching works
624 * around the expectation it will be called during setting administration
626 * Function is intended for configuration rather than runtime access to settings
628 * The following params will filter the result. If none are passed all settings will be returns
630 * @param int $componentID
631 * Id of relevant component.
632 * @param array $filters
633 * @param int $domainID
634 * @param null $profile
637 * the following information as appropriate for each setting
641 * - add (CiviCRM version added)
647 public static function getSettingSpecification(
653 $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile;
654 foreach ($filters as $filterField => $filterString) {
655 $cacheString .= "_{$filterField}_{$filterString}";
658 // the caching into 'All' seems to be a duplicate of caching to
659 // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook
660 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Specs', $cacheString, $componentID);
661 if ($settingsMetadata === NULL) {
662 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Spec', 'All', $componentID);
663 if (empty($settingsMetadata)) {
664 global $civicrm_root;
665 $metaDataFolders = array($civicrm_root . '/settings');
666 CRM_Utils_Hook
::alterSettingsFolders($metaDataFolders);
667 $settingsMetadata = self
::loadSettingsMetaDataFolders($metaDataFolders);
668 CRM_Core_BAO_Cache
::setItem($settingsMetadata, 'CiviCRM setting Spec', 'All', $componentID);
673 CRM_Utils_Hook
::alterSettingsMetaData($settingsMetadata, $domainID, $profile);
674 self
::_filterSettingsSpecification($filters, $settingsMetadata);
677 // this is a bit 'heavy' if you are using hooks but this function
678 // is expected to only be called during setting administration
679 // it should not be called by 'getvalue' or 'getitem
680 CRM_Core_BAO_Cache
::setItem(
682 'CiviCRM setting Specs',
687 return $settingsMetadata;
692 * Load the settings files defined in a series of folders.
693 * @param array $metaDataFolders
694 * List of folder paths.
697 public static function loadSettingsMetaDataFolders($metaDataFolders) {
698 $settingsMetadata = array();
699 $loadedFolders = array();
700 foreach ($metaDataFolders as $metaDataFolder) {
701 $realFolder = realpath($metaDataFolder);
702 if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) {
703 $loadedFolders[$realFolder] = TRUE;
704 $settingsMetadata = $settingsMetadata + self
::loadSettingsMetaData($metaDataFolder);
707 return $settingsMetadata;
711 * Load up settings metadata from files.
713 public static function loadSettingsMetadata($metaDataFolder) {
714 $settingMetaData = array();
715 $settingsFiles = CRM_Utils_File
::findFiles($metaDataFolder, '*.setting.php');
716 foreach ($settingsFiles as $file) {
717 $settings = include $file;
718 $settingMetaData = array_merge($settingMetaData, $settings);
720 CRM_Core_BAO_Cache
::setItem($settingMetaData, 'CiviCRM setting Spec', 'All');
721 return $settingMetaData;
725 * Filter the settings metadata according to filters passed in. This is a convenience filter
726 * and allows selective reverting / filling of settings
728 * @param array $filters
729 * Filters to match against data.
730 * @param array $settingSpec
731 * Metadata to filter.
733 public static function _filterSettingsSpecification($filters, &$settingSpec) {
734 if (empty($filters)) {
737 elseif (array_keys($filters) == array('name')) {
738 $settingSpec = array($filters['name'] => CRM_Utils_Array
::value($filters['name'], $settingSpec, ''));
742 foreach ($settingSpec as $field => $fieldValues) {
743 if (array_intersect_assoc($fieldValues, $filters) != $filters) {
744 unset($settingSpec[$field]);
752 * Look for any missing settings and convert them from config or load default as appropriate.
753 * This should be run from GenCode & also from upgrades to add any new defaults.
755 * Multisites have often been overlooked in upgrade scripts so can be expected to be missing
756 * a number of settings
758 public static function updateSettingsFromMetaData() {
761 'domain_id' => 'all',
762 'filters' => array('prefetch' => 0),
764 $existing = civicrm_api('setting', 'get', $apiParams);
766 if (!empty($existing['values'])) {
767 $allSettings = civicrm_api('setting', 'getfields', array('version' => 3));
768 foreach ($existing['values'] as $domainID => $domainSettings) {
769 CRM_Core_BAO_Domain
::setDomain($domainID);
770 $missing = array_diff_key($allSettings['values'], $domainSettings);
771 foreach ($missing as $name => $settings) {
772 self
::convertConfigToSetting($name, $domainID);
774 CRM_Core_BAO_Domain
::resetDomain();
780 * Move an item from being in the config array to being stored as a setting
781 * remove from config - as appropriate based on metadata
783 * Note that where the key name is being changed the 'legacy_key' will give us the old name
785 public static function convertConfigToSetting($name, $domainID = NULL) {
786 // we have to force this here in case more than one domain is in play.
787 // whenever there is a possibility of more than one domain we must force it
788 $config = CRM_Core_Config
::singleton();
789 if (empty($domainID)) {
790 $domainID = CRM_Core_Config
::domainID();
792 $domain = new CRM_Core_DAO_Domain();
793 $domain->id
= $domainID;
795 if ($domain->config_backend
) {
796 $values = unserialize($domain->config_backend
);
801 $spec = self
::getSettingSpecification(NULL, array('name' => $name), $domainID);
802 $configKey = CRM_Utils_Array
::value('config_key', $spec[$name], CRM_Utils_Array
::value('legacy_key', $spec[$name], $name));
803 //if the key is set to config_only we don't need to do anything
804 if (empty($spec[$name]['config_only'])) {
805 if (!empty($values[$configKey])) {
806 civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID));
809 civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID));
812 if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) {
813 unset($values[$configKey]);
814 $domain->config_backend
= serialize($values);
816 unset($config->$configKey);
823 * @param string $name
824 * @param bool $system
826 * @param bool $localize
827 * @param string $returnField
828 * @param bool $returnNameANDLabels
829 * @param null $condition
833 public static function valueOptions(
839 $returnField = 'name',
840 $returnNameANDLabels = FALSE,
843 $optionValue = self
::getItem($group, $name);
845 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, $returnField);
847 //enabled name => label require for new contact edit form, CRM-4605
848 if ($returnNameANDLabels) {
849 $names = $labels = $nameAndLabels = array();
850 if ($returnField == 'name') {
851 $names = $groupValues;
852 $labels = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'label');
855 $labels = $groupValues;
856 $names = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'name');
860 $returnValues = array();
861 foreach ($groupValues as $gn => $gv) {
862 $returnValues[$gv] = 0;
865 if ($optionValue && !empty($groupValues)) {
866 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
867 substr($optionValue, 1, -1)
870 if (!empty($dbValues)) {
871 foreach ($groupValues as $key => $val) {
872 if (in_array($key, $dbValues)) {
873 $returnValues[$val] = 1;
874 if ($returnNameANDLabels) {
875 $nameAndLabels[$names[$key]] = $labels[$key];
881 return ($returnNameANDLabels) ?
$nameAndLabels : $returnValues;
886 * @param string $name
888 * @param bool $system
890 * @param string $keyField
892 public static function setValueOption(
903 elseif (is_array($value)) {
904 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, FALSE, NULL, $keyField);
907 foreach ($groupValues as $key => $val) {
908 if (!empty($value[$val])) {
913 if (!empty($cbValues)) {
914 $optionValue = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
915 array_keys($cbValues)
916 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
923 $optionValue = $value;
926 self
::setItem($optionValue, $group, $name);
930 * @param array $params
931 * @param int $domainID
933 public static function fixAndStoreDirAndURL(&$params, $domainID = NULL) {
934 if (self
::isUpgradeFromPreFourOneAlpha1()) {
938 if (empty($domainID)) {
939 $domainID = CRM_Core_Config
::domainID();
942 SELECT name, group_name
945 AND ( group_name = %2
949 1 => array($domainID, 'Integer'),
950 2 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
951 3 => array(self
::URL_PREFERENCES_NAME
, 'String'),
954 $dirParams = array();
955 $urlParams = array();
957 $dao = CRM_Core_DAO
::executeQuery($sql,
963 // trap exceptions as error
967 if (is_a($dao, 'DB_Error')) {
968 if (CRM_Core_Config
::isUpgradeMode()) {
969 // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue
973 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
978 while ($dao->fetch()) {
979 if (!isset($params[$dao->name
])) {
982 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
983 $dirParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
986 $urlParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
988 unset($params[$dao->name
]);
991 if (!empty($dirParams)) {
992 self
::storeDirectoryOrURLPreferences($dirParams,
993 self
::DIRECTORY_PREFERENCES_NAME
997 if (!empty($urlParams)) {
998 self
::storeDirectoryOrURLPreferences($urlParams,
999 self
::URL_PREFERENCES_NAME
1005 * @param array $params
1008 public static function storeDirectoryOrURLPreferences(&$params, $group) {
1009 foreach ($params as $name => $value) {
1010 // always try to store relative directory or url from CMS root
1011 $value = ($group == self
::DIRECTORY_PREFERENCES_NAME
) ? CRM_Utils_File
::relativeDirectory($value) : CRM_Utils_System
::relativeURL($value);
1013 self
::setItem($value, $group, $name);
1018 * @param array $params
1019 * @param bool $setInConfig
1021 public static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
1022 if (CRM_Core_Config
::isUpgradeMode()) {
1023 $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') ?
TRUE : FALSE;
1024 // hack to set the resource base url so that js/ css etc is loaded correctly
1026 $params['userFrameworkResourceURL'] = CRM_Utils_File
::addTrailingSlash(CIVICRM_UF_BASEURL
, '/') . str_replace('administrator', '', CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name'));
1028 if (self
::isUpgradeFromPreFourOneAlpha1()) {
1034 $config = CRM_Core_Config
::singleton();
1038 SELECT name, group_name, value
1039 FROM civicrm_setting
1040 WHERE ( group_name = %1
1041 OR group_name = %2 )
1045 1 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
1046 2 => array(self
::URL_PREFERENCES_NAME
, 'String'),
1047 3 => array(CRM_Core_Config
::domainID(), 'Integer'),
1050 $dao = CRM_Core_DAO
::executeQuery($sql,
1056 // trap exceptions as error
1060 if (is_a($dao, 'DB_Error')) {
1061 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
1065 while ($dao->fetch()) {
1066 $value = self
::getOverride($dao->group_name
, $dao->name
, NULL);
1067 if ($value === NULL && $dao->value
) {
1068 $value = unserialize($dao->value
);
1069 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
1070 $value = CRM_Utils_File
::absoluteDirectory($value);
1073 // CRM-7622: we need to remove the language part
1074 $value = CRM_Utils_System
::absoluteURL($value, TRUE);
1077 // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available
1078 if (!isset($value) && !empty($params[$dao->name
])) {
1079 $value = $params[$dao->name
];
1081 $params[$dao->name
] = $value;
1084 $config->{$dao->name
} = $value;
1090 * Determine what, if any, overrides have been provided
1094 * @param string $name
1097 * @return mixed, NULL or an overriden value
1099 protected static function getOverride($group, $name, $default) {
1100 global $civicrm_setting;
1101 if ($group && $name && isset($civicrm_setting[$group][$name])) {
1102 return $civicrm_setting[$group][$name];
1110 * Civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade
1114 public static function isUpgradeFromPreFourOneAlpha1() {
1115 if (CRM_Core_Config
::isUpgradeMode()) {
1116 $currentVer = CRM_Core_BAO_Domain
::version();
1117 if (version_compare($currentVer, '4.1.alpha1') < 0) {