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 null $domains
268 * @param $settingsToReturn
270 * @domains array an array of domains to get settings for. Default is the current domain
273 public static function getItems(&$params, $domains = NULL, $settingsToReturn) {
274 $originalDomain = CRM_Core_Config
::domainID();
275 if (empty($domains)) {
276 $domains[] = $originalDomain;
278 if (!empty($settingsToReturn) && !is_array($settingsToReturn)) {
279 $settingsToReturn = array($settingsToReturn);
281 $reloadConfig = FALSE;
283 $fields = $result = array();
284 $fieldsToGet = self
::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE);
285 foreach ($domains as $domainID) {
286 if ($domainID != CRM_Core_Config
::domainID()) {
287 $reloadConfig = TRUE;
288 CRM_Core_BAO_Domain
::setDomain($domainID);
290 $config = CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
291 $result[$domainID] = array();
292 foreach ($fieldsToGet as $name => $value) {
293 if (!empty($fields['values'][$name]['prefetch'])) {
294 if (isset($params['filters']) && isset($params['filters']['prefetch'])
295 && $params['filters']['prefetch'] == 0
297 // we are filtering out the prefetches from the return array
301 $configKey = CRM_Utils_Array
::value('config_key', $fields['values'][$name], $name);
302 if (isset($config->$configKey)) {
303 $setting = $config->$configKey;
308 CRM_Core_BAO_Setting
::getItem(
309 $fields['values'][$name]['group_name'],
311 CRM_Utils_Array
::value('component_id', $params),
313 CRM_Utils_Array
::value('contact_id', $params),
317 if (!is_null($setting)) {
318 // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones
319 // e.g for revert of fill actions
320 $result[$domainID][$name] = $setting;
323 CRM_Core_BAO_Domain
::resetDomain();
329 * Store an item in the setting table
331 * _setItem() is the common logic shared by setItem() and setItems().
333 * @param object $value
334 * (required) The value that will be serialized and stored.
335 * @param string $group
336 * (required) The group name of the item.
337 * @param string $name
338 * (required) The name of the setting.
339 * @param int $componentID
340 * The optional component ID (so componenets can share the same name space).
341 * @param int $contactID
342 * @param int $createdID
343 * An optional ID to assign the creator to. If not set, retrieved from session.
345 * @param int $domainID
349 public static function setItem(
359 $fieldsToSet = self
::validateSettingsInput(array($name => $value), $fields);
360 //We haven't traditionally validated inputs to setItem, so this breaks things.
361 //foreach ($fieldsToSet as $settingField => &$settingValue) {
362 // self::validateSetting($settingValue, $fields['values'][$settingField]);
365 return self
::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID);
369 * Store an item in a setting table
371 * _setItem() is the common logic shared by setItem() and setItems().
373 * @param array $metadata
374 * Metadata describing this field.
377 * @param string $name
378 * @param int $componentID
379 * @param int $contactID
380 * @param int $createdID
381 * @param int $domainID
383 public static function _setItem(
393 if (empty($domainID)) {
394 $domainID = CRM_Core_Config
::domainID();
397 $dao = self
::dao($group, $name, $componentID, $contactID, $domainID);
400 if (isset($metadata['on_change'])) {
401 foreach ($metadata['on_change'] as $callback) {
402 call_user_func($callback, unserialize($dao->value
), $value, $metadata);
406 if (CRM_Utils_System
::isNull($value)) {
407 $dao->value
= 'null';
410 $dao->value
= serialize($value);
413 $dao->created_date
= date('Ymdhis');
416 $dao->created_id
= $createdID;
419 $session = CRM_Core_Session
::singleton();
420 $createdID = $session->get('userID');
423 // ensure that this is a valid contact id (for session inconsistency rules)
424 $cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
430 $dao->created_id
= $session->get('userID');
438 // also save in cache if needed
439 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, FALSE, $domainID);
441 self
::$_cache[$cacheKey][$name] = $value;
446 * Store multiple items in the setting table. Note that this will also store config keys
447 * the storage is determined by the metdata and is affected by
448 * 'name' setting's name
449 * 'prefetch' = store in config
450 * 'config_only' = don't store in settings
451 * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict
452 * 'legacy_key' = rename from config or setting with this name
454 * _setItem() is the common logic shared by setItem() and setItems().
456 * @param array $params
457 * (required) An api formatted array of keys and values.
458 * @param null $domains
460 * @throws api_Exception
461 * @domains array an array of domains to get settings for. Default is the current domain
464 public static function setItems(&$params, $domains = NULL) {
465 $originalDomain = CRM_Core_Config
::domainID();
466 if (empty($domains)) {
467 $domains[] = $originalDomain;
469 $reloadConfig = FALSE;
470 $fields = $config_keys = array();
471 $fieldsToSet = self
::validateSettingsInput($params, $fields);
473 foreach ($fieldsToSet as $settingField => &$settingValue) {
474 self
::validateSetting($settingValue, $fields['values'][$settingField]);
477 foreach ($domains as $domainID) {
478 if ($domainID != CRM_Core_Config
::domainID()) {
479 $reloadConfig = TRUE;
480 CRM_Core_BAO_Domain
::setDomain($domainID);
482 $result[$domainID] = array();
483 foreach ($fieldsToSet as $name => $value) {
484 if (empty($fields['values'][$name]['config_only'])) {
485 CRM_Core_BAO_Setting
::_setItem(
486 $fields['values'][$name],
488 $fields['values'][$name]['group_name'],
490 CRM_Utils_Array
::value('component_id', $params),
491 CRM_Utils_Array
::value('contact_id', $params),
492 CRM_Utils_Array
::value('created_id', $params),
496 if (!empty($fields['values'][$name]['prefetch'])) {
497 if (!empty($fields['values'][$name]['config_key'])) {
498 $name = $fields['values'][$name]['config_key'];
500 $config_keys[$name] = $value;
502 $result[$domainID][$name] = $value;
505 CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
508 if (!empty($config_keys)) {
509 CRM_Core_BAO_ConfigSetting
::create($config_keys);
512 CRM_Core_BAO_Domain
::resetDomain();
520 * Gets metadata about the settings fields (from getfields) based on the fields being passed in
522 * This function filters on the fields like 'version' & 'debug' that are not settings
524 * @param array $params
525 * Parameters as passed into API.
526 * @param array $fields
527 * Empty array to be populated with fields metadata.
528 * @param bool $createMode
530 * @throws api_Exception
532 * name => value array of the fields to be set (with extraneous removed)
534 public static function validateSettingsInput($params, &$fields, $createMode = TRUE) {
535 $group = CRM_Utils_Array
::value('group', $params);
537 $ignoredParams = array(
556 $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE));
557 $getFieldsParams = array('version' => 3);
558 if (count($settingParams) == 1) {
559 // ie we are only setting one field - we'll pass it into getfields for efficiency
560 list($name) = array_keys($settingParams);
561 $getFieldsParams['name'] = $name;
563 $fields = civicrm_api3('setting', 'getfields', $getFieldsParams);
564 $invalidParams = (array_diff_key($settingParams, $fields['values']));
565 if (!empty($invalidParams)) {
566 throw new api_Exception(implode(',', array_keys($invalidParams)) . " not valid settings");
568 if (!empty($settingParams)) {
569 $filteredFields = array_intersect_key($settingParams, $fields['values']);
572 // no filters so we are interested in all for get mode. In create mode this means nothing to set
573 $filteredFields = $createMode ?
array() : $fields['values'];
575 return $filteredFields;
579 * Validate & convert settings input
581 * @value mixed value of the setting to be set
582 * @fieldSpec array Metadata for given field (drawn from the xml)
584 public static function validateSetting(&$value, $fieldSpec) {
585 if ($fieldSpec['type'] == 'String' && is_array($value)) {
586 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value) . CRM_Core_DAO
::VALUE_SEPARATOR
;
588 if (empty($fieldSpec['validate_callback'])) {
592 list($class, $fn) = explode('::', $fieldSpec['validate_callback']);
593 if (!$class::$fn($value, $fieldSpec)) {
594 throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}");
600 * Validate & convert settings input - translate True False to 0 or 1
602 * @value mixed value of the setting to be set
603 * @fieldSpec array Metadata for given field (drawn from the xml)
605 public static function validateBoolSetting(&$value, $fieldSpec) {
606 if (!CRM_Utils_Rule
::boolean($value)) {
607 throw new api_Exception("Boolean value required for {$fieldSpec['name']}");
619 * This provides information about the setting - similar to the fields concept for DAO information.
620 * As the setting is serialized code creating validation setting input needs to know the data type
621 * This also helps move information out of the form layer into the data layer where people can interact with
622 * it via the API or other mechanisms. In order to keep this consistent it is important the form layer
625 * Note that this function should never be called when using the runtime getvalue function. Caching works
626 * around the expectation it will be called during setting administration
628 * Function is intended for configuration rather than runtime access to settings
630 * The following params will filter the result. If none are passed all settings will be returns
632 * @param int $componentID
633 * Id of relevant component.
634 * @param array $filters
635 * @param int $domainID
636 * @param null $profile
639 * the following information as appropriate for each setting
643 * - add (CiviCRM version added)
649 public static function getSettingSpecification(
655 $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile;
656 foreach ($filters as $filterField => $filterString) {
657 $cacheString .= "_{$filterField}_{$filterString}";
660 // the caching into 'All' seems to be a duplicate of caching to
661 // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook
662 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Specs', $cacheString, $componentID);
663 if ($settingsMetadata === NULL) {
664 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Spec', 'All', $componentID);
665 if (empty($settingsMetadata)) {
666 global $civicrm_root;
667 $metaDataFolders = array($civicrm_root . '/settings');
668 CRM_Utils_Hook
::alterSettingsFolders($metaDataFolders);
669 $settingsMetadata = self
::loadSettingsMetaDataFolders($metaDataFolders);
670 CRM_Core_BAO_Cache
::setItem($settingsMetadata, 'CiviCRM setting Spec', 'All', $componentID);
675 CRM_Utils_Hook
::alterSettingsMetaData($settingsMetadata, $domainID, $profile);
676 self
::_filterSettingsSpecification($filters, $settingsMetadata);
679 // this is a bit 'heavy' if you are using hooks but this function
680 // is expected to only be called during setting administration
681 // it should not be called by 'getvalue' or 'getitem
682 CRM_Core_BAO_Cache
::setItem(
684 'CiviCRM setting Specs',
689 return $settingsMetadata;
694 * Load the settings files defined in a series of folders
695 * @param array $metaDataFolders
696 * List of folder paths.
699 public static function loadSettingsMetaDataFolders($metaDataFolders) {
700 $settingsMetadata = array();
701 $loadedFolders = array();
702 foreach ($metaDataFolders as $metaDataFolder) {
703 $realFolder = realpath($metaDataFolder);
704 if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) {
705 $loadedFolders[$realFolder] = TRUE;
706 $settingsMetadata = $settingsMetadata + self
::loadSettingsMetaData($metaDataFolder);
709 return $settingsMetadata;
713 * Load up settings metadata from files
715 public static function loadSettingsMetadata($metaDataFolder) {
716 $settingMetaData = array();
717 $settingsFiles = CRM_Utils_File
::findFiles($metaDataFolder, '*.setting.php');
718 foreach ($settingsFiles as $file) {
719 $settings = include $file;
720 $settingMetaData = array_merge($settingMetaData, $settings);
722 CRM_Core_BAO_Cache
::setItem($settingMetaData, 'CiviCRM setting Spec', 'All');
723 return $settingMetaData;
727 * Filter the settings metadata according to filters passed in. This is a convenience filter
728 * and allows selective reverting / filling of settings
730 * @param array $filters
731 * Filters to match against data.
732 * @param array $settingSpec
733 * Metadata to filter.
735 public static function _filterSettingsSpecification($filters, &$settingSpec) {
736 if (empty($filters)) {
739 elseif (array_keys($filters) == array('name')) {
740 $settingSpec = array($filters['name'] => CRM_Utils_Array
::value($filters['name'], $settingSpec, ''));
744 foreach ($settingSpec as $field => $fieldValues) {
745 if (array_intersect_assoc($fieldValues, $filters) != $filters) {
746 unset($settingSpec[$field]);
754 * Look for any missing settings and convert them from config or load default as appropriate
755 * This should be run from GenCode & also from upgrades to add any new defaults.
757 * Multisites have often been overlooked in upgrade scripts so can be expected to be missing
758 * a number of settings
760 public static function updateSettingsFromMetaData() {
763 'domain_id' => 'all',
764 'filters' => array('prefetch' => 0),
766 $existing = civicrm_api('setting', 'get', $apiParams);
768 if (!empty($existing['values'])) {
769 $allSettings = civicrm_api('setting', 'getfields', array('version' => 3));
770 foreach ($existing['values'] as $domainID => $domainSettings) {
771 CRM_Core_BAO_Domain
::setDomain($domainID);
772 $missing = array_diff_key($allSettings['values'], $domainSettings);
773 foreach ($missing as $name => $settings) {
774 self
::convertConfigToSetting($name, $domainID);
776 CRM_Core_BAO_Domain
::resetDomain();
782 * Move an item from being in the config array to being stored as a setting
783 * remove from config - as appropriate based on metadata
785 * Note that where the key name is being changed the 'legacy_key' will give us the old name
787 public static function convertConfigToSetting($name, $domainID = NULL) {
788 // we have to force this here in case more than one domain is in play.
789 // whenever there is a possibility of more than one domain we must force it
790 $config = CRM_Core_Config
::singleton();
791 if (empty($domainID)) {
792 $domainID = CRM_Core_Config
::domainID();
794 $domain = new CRM_Core_DAO_Domain();
795 $domain->id
= $domainID;
797 if ($domain->config_backend
) {
798 $values = unserialize($domain->config_backend
);
803 $spec = self
::getSettingSpecification(NULL, array('name' => $name), $domainID);
804 $configKey = CRM_Utils_Array
::value('config_key', $spec[$name], CRM_Utils_Array
::value('legacy_key', $spec[$name], $name));
805 //if the key is set to config_only we don't need to do anything
806 if (empty($spec[$name]['config_only'])) {
807 if (!empty($values[$configKey])) {
808 civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID));
811 civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID));
814 if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) {
815 unset($values[$configKey]);
816 $domain->config_backend
= serialize($values);
818 unset($config->$configKey);
825 * @param string $name
826 * @param bool $system
828 * @param bool $localize
829 * @param string $returnField
830 * @param bool $returnNameANDLabels
831 * @param null $condition
835 public static function valueOptions(
841 $returnField = 'name',
842 $returnNameANDLabels = FALSE,
845 $optionValue = self
::getItem($group, $name);
847 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, $returnField);
849 //enabled name => label require for new contact edit form, CRM-4605
850 if ($returnNameANDLabels) {
851 $names = $labels = $nameAndLabels = array();
852 if ($returnField == 'name') {
853 $names = $groupValues;
854 $labels = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'label');
857 $labels = $groupValues;
858 $names = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'name');
862 $returnValues = array();
863 foreach ($groupValues as $gn => $gv) {
864 $returnValues[$gv] = 0;
867 if ($optionValue && !empty($groupValues)) {
868 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
869 substr($optionValue, 1, -1)
872 if (!empty($dbValues)) {
873 foreach ($groupValues as $key => $val) {
874 if (in_array($key, $dbValues)) {
875 $returnValues[$val] = 1;
876 if ($returnNameANDLabels) {
877 $nameAndLabels[$names[$key]] = $labels[$key];
883 return ($returnNameANDLabels) ?
$nameAndLabels : $returnValues;
888 * @param string $name
890 * @param bool $system
892 * @param string $keyField
894 public static function setValueOption(
905 elseif (is_array($value)) {
906 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, FALSE, NULL, $keyField);
909 foreach ($groupValues as $key => $val) {
910 if (!empty($value[$val])) {
915 if (!empty($cbValues)) {
916 $optionValue = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
917 array_keys($cbValues)
918 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
925 $optionValue = $value;
928 self
::setItem($optionValue, $group, $name);
932 * @param array $params
933 * @param int $domainID
935 public static function fixAndStoreDirAndURL(&$params, $domainID = NULL) {
936 if (self
::isUpgradeFromPreFourOneAlpha1()) {
940 if (empty($domainID)) {
941 $domainID = CRM_Core_Config
::domainID();
944 SELECT name, group_name
947 AND ( group_name = %2
951 1 => array($domainID, 'Integer'),
952 2 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
953 3 => array(self
::URL_PREFERENCES_NAME
, 'String'),
956 $dirParams = array();
957 $urlParams = array();
959 $dao = CRM_Core_DAO
::executeQuery($sql,
965 // trap exceptions as error
969 if (is_a($dao, 'DB_Error')) {
970 if (CRM_Core_Config
::isUpgradeMode()) {
971 // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue
975 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
980 while ($dao->fetch()) {
981 if (!isset($params[$dao->name
])) {
984 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
985 $dirParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
988 $urlParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
990 unset($params[$dao->name
]);
993 if (!empty($dirParams)) {
994 self
::storeDirectoryOrURLPreferences($dirParams,
995 self
::DIRECTORY_PREFERENCES_NAME
999 if (!empty($urlParams)) {
1000 self
::storeDirectoryOrURLPreferences($urlParams,
1001 self
::URL_PREFERENCES_NAME
1007 * @param array $params
1010 public static function storeDirectoryOrURLPreferences(&$params, $group) {
1011 foreach ($params as $name => $value) {
1012 // always try to store relative directory or url from CMS root
1013 $value = ($group == self
::DIRECTORY_PREFERENCES_NAME
) ? CRM_Utils_File
::relativeDirectory($value) : CRM_Utils_System
::relativeURL($value);
1015 self
::setItem($value, $group, $name);
1020 * @param array $params
1021 * @param bool $setInConfig
1023 public static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
1024 if (CRM_Core_Config
::isUpgradeMode()) {
1025 $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') ?
TRUE : FALSE;
1026 // hack to set the resource base url so that js/ css etc is loaded correctly
1028 $params['userFrameworkResourceURL'] = CRM_Utils_File
::addTrailingSlash(CIVICRM_UF_BASEURL
, '/') . str_replace('administrator', '', CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name'));
1030 if (self
::isUpgradeFromPreFourOneAlpha1()) {
1036 $config = CRM_Core_Config
::singleton();
1040 SELECT name, group_name, value
1041 FROM civicrm_setting
1042 WHERE ( group_name = %1
1043 OR group_name = %2 )
1047 1 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
1048 2 => array(self
::URL_PREFERENCES_NAME
, 'String'),
1049 3 => array(CRM_Core_Config
::domainID(), 'Integer'),
1052 $dao = CRM_Core_DAO
::executeQuery($sql,
1058 // trap exceptions as error
1062 if (is_a($dao, 'DB_Error')) {
1063 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
1067 while ($dao->fetch()) {
1068 $value = self
::getOverride($dao->group_name
, $dao->name
, NULL);
1069 if ($value === NULL && $dao->value
) {
1070 $value = unserialize($dao->value
);
1071 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
1072 $value = CRM_Utils_File
::absoluteDirectory($value);
1075 // CRM-7622: we need to remove the language part
1076 $value = CRM_Utils_System
::absoluteURL($value, TRUE);
1079 // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available
1080 if (!isset($value) && !empty($params[$dao->name
])) {
1081 $value = $params[$dao->name
];
1083 $params[$dao->name
] = $value;
1086 $config->{$dao->name
} = $value;
1092 * Determine what, if any, overrides have been provided
1096 * @param string $name
1099 * @return mixed, NULL or an overriden value
1101 protected static function getOverride($group, $name, $default) {
1102 global $civicrm_setting;
1103 if ($group && $name && isset($civicrm_setting[$group][$name])) {
1104 return $civicrm_setting[$group][$name];
1112 * Civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade
1116 public static function isUpgradeFromPreFourOneAlpha1() {
1117 if (CRM_Core_Config
::isUpgradeMode()) {
1118 $currentVer = CRM_Core_BAO_Domain
::version();
1119 if (version_compare($currentVer, '4.1.alpha1') < 0) {