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 components 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);
125 * @param null $componentID
126 * @param null $contactID
127 * @param null $domainID
131 static function setCache($values,
137 if (!isset(self
::$_cache)) {
138 self
::$_cache = array();
141 $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}";
143 self
::$_cache[$cacheKey] = $values;
145 $globalCache = CRM_Utils_Cache
::singleton();
146 $result = $globalCache->set($cacheKey, $values);
154 * @param null $componentID
155 * @param null $contactID
156 * @param null $domainID
158 * @return CRM_Core_DAO_Domain|CRM_Core_DAO_Setting
160 static function dao($group,
166 if (self
::isUpgradeFromPreFourOneAlpha1()) {
167 // civicrm_setting table is not going to be present. For now we'll just
168 // return a dummy object
169 $dao = new CRM_Core_DAO_Domain();
170 $dao->id
= -1; // so ->find() doesn't fetch any data later on
173 $dao = new CRM_Core_DAO_Setting();
175 $dao->group_name
= $group;
177 $dao->component_id
= $componentID;
178 if (empty($domainID)) {
179 $dao->domain_id
= CRM_Core_Config
::domainID();
182 $dao->domain_id
= $domainID;
186 $dao->contact_id
= $contactID;
197 * Retrieve the value of a setting from the DB table
199 * @param string $group (required) The group name of the item
200 * @param string $name (required) The name under which this item is stored
201 * @param int $componentID The optional component ID (so componenets can share the same name space)
202 * @param string $defaultValue The default value to return for this setting if not present in DB
203 * @param int $contactID If set, this is a contactID specific setting, else its a global setting
205 * @param null $domainID
207 * @return object The data if present in the setting table, else null
211 static function getItem(
215 $defaultValue = NULL,
220 if (NULL !== ($override = self
::getOverride($group, $name, NULL))) {
224 if (empty($domainID)) {
225 $domainID = CRM_Core_Config
::domainID();
227 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, TRUE, $domainID);
229 $dao = self
::dao($group, NULL, $componentID, $contactID, $domainID);
233 while ($dao->fetch()) {
234 if (NULL !== ($override = self
::getOverride($group, $dao->name
, NULL))) {
235 $values[$dao->name
] = $override;
237 elseif ($dao->value
) {
238 $values[$dao->name
] = unserialize($dao->value
);
241 $values[$dao->name
] = NULL;
246 $cacheKey = self
::setCache($values, $group, $componentID, $contactID, $domainID);
248 return $name ? CRM_Utils_Array
::value($name, self
::$_cache[$cacheKey], $defaultValue) : self
::$_cache[$cacheKey];
252 * Store multiple items in the setting table
254 * @param array $params (required) An api formatted array of keys and values
255 * @param null $domains
256 * @param $settingsToReturn
258 * @domains array an array of domains to get settings for. Default is the current domain
263 static function getItems(&$params, $domains = NULL, $settingsToReturn) {
264 $originalDomain = CRM_Core_Config
::domainID();
265 if (empty($domains)) {
266 $domains[] = $originalDomain;
268 if (!empty($settingsToReturn) && !is_array($settingsToReturn)) {
269 $settingsToReturn = array($settingsToReturn);
271 $reloadConfig = FALSE;
273 $fields = $result = array();
274 $fieldsToGet = self
::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE);
275 foreach ($domains as $domainID) {
276 if($domainID != CRM_Core_Config
::domainID()){
277 $reloadConfig = TRUE;
278 CRM_Core_BAO_Domain
::setDomain($domainID);
280 $config = CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
281 $result[$domainID] = array();
282 foreach ($fieldsToGet as $name => $value) {
283 if(!empty($fields['values'][$name]['prefetch'])){
284 if(isset($params['filters']) && isset($params['filters']['prefetch'])
285 && $params['filters']['prefetch'] == 0){
286 // we are filtering out the prefetches from the return array
290 $configKey = CRM_Utils_Array
::value('config_key', $fields['values'][$name], $name);
291 if(isset($config->$configKey)){
292 $setting = $config->$configKey;
297 CRM_Core_BAO_Setting
::getItem(
298 $fields['values'][$name]['group_name'],
300 CRM_Utils_Array
::value('component_id', $params),
302 CRM_Utils_Array
::value('contact_id', $params),
306 if (!is_null($setting)) {
307 // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones
308 // e.g for revert of fill actions
309 $result[$domainID][$name] = $setting;
312 CRM_Core_BAO_Domain
::resetDomain();
318 * Store an item in the setting table
320 * _setItem() is the common logic shared by setItem() and setItems().
322 * @param object $value (required) The value that will be serialized and stored
323 * @param string $group (required) The group name of the item
324 * @param string $name (required) The name of the setting
325 * @param int $componentID The optional component ID (so componenets can share the same name space)
326 * @param null $contactID
327 * @param int $createdID An optional ID to assign the creator to. If not set, retrieved from session
329 * @param null $domainID
335 static function setItem(
345 $fieldsToSet = self
::validateSettingsInput(array($name => $value), $fields);
346 //We haven't traditionally validated inputs to setItem, so this breaks things.
347 //foreach ($fieldsToSet as $settingField => &$settingValue) {
348 // self::validateSetting($settingValue, $fields['values'][$settingField]);
351 return self
::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID);
355 * Store an item in a setting table
357 * _setItem() is the common logic shared by setItem() and setItems().
359 * @param array $metadata metadata describing this field
363 * @param null $componentID
364 * @param null $contactID
365 * @param null $createdID
366 * @param null $domainID
368 static function _setItem(
378 if (empty($domainID)) {
379 $domainID = CRM_Core_Config
::domainID();
382 $dao = self
::dao($group, $name, $componentID, $contactID, $domainID);
385 if (isset($metadata['on_change'])) {
386 foreach ($metadata['on_change'] as $callback) {
387 call_user_func($callback, unserialize($dao->value
), $value, $metadata);
391 if (CRM_Utils_System
::isNull($value)) {
392 $dao->value
= 'null';
395 $dao->value
= serialize($value);
398 $dao->created_date
= date('Ymdhis');
401 $dao->created_id
= $createdID;
404 $session = CRM_Core_Session
::singleton();
405 $createdID = $session->get('userID');
408 // ensure that this is a valid contact id (for session inconsistency rules)
409 $cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
415 $dao->created_id
= $session->get('userID');
423 // also save in cache if needed
424 $cacheKey = self
::inCache($group, $name, $componentID, $contactID, FALSE, $domainID);
426 self
::$_cache[$cacheKey][$name] = $value;
431 * Store multiple items in the setting table. Note that this will also store config keys
432 * the storage is determined by the metdata and is affected by
433 * 'name' setting's name
434 * 'prefetch' = store in config
435 * 'config_only' = don't store in settings
436 * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict
437 * 'legacy_key' = rename from config or setting with this name
439 * _setItem() is the common logic shared by setItem() and setItems().
441 * @param array $params (required) An api formatted array of keys and values
442 * @param null $domains
444 * @throws api_Exception
445 * @domains array an array of domains to get settings for. Default is the current domain
450 static function setItems(&$params, $domains = NULL) {
451 $originalDomain = CRM_Core_Config
::domainID();
452 if (empty($domains)) {
453 $domains[] = $originalDomain;
455 $reloadConfig = FALSE;
456 $fields = $config_keys = array();
457 $fieldsToSet = self
::validateSettingsInput($params, $fields);
459 foreach ($fieldsToSet as $settingField => &$settingValue) {
460 self
::validateSetting($settingValue, $fields['values'][$settingField]);
463 foreach ($domains as $domainID) {
464 if($domainID != CRM_Core_Config
::domainID()){
465 $reloadConfig = TRUE;
466 CRM_Core_BAO_Domain
::setDomain($domainID);
468 $result[$domainID] = array();
469 foreach ($fieldsToSet as $name => $value) {
470 if(empty($fields['values'][$name]['config_only'])){
471 CRM_Core_BAO_Setting
::_setItem(
472 $fields['values'][$name],
474 $fields['values'][$name]['group_name'],
476 CRM_Utils_Array
::value('component_id', $params),
477 CRM_Utils_Array
::value('contact_id', $params),
478 CRM_Utils_Array
::value('created_id', $params),
482 if(!empty($fields['values'][$name]['prefetch'])){
483 if(!empty($fields['values'][$name]['config_key'])){
484 $name = $fields['values'][$name]['config_key'];
486 $config_keys[$name] = $value;
488 $result[$domainID][$name] = $value;
491 CRM_Core_Config
::singleton($reloadConfig, $reloadConfig);
494 if(!empty($config_keys)){
495 CRM_Core_BAO_ConfigSetting
::create($config_keys);
498 CRM_Core_BAO_Domain
::resetDomain();
506 * gets metadata about the settings fields (from getfields) based on the fields being passed in
508 * This function filters on the fields like 'version' & 'debug' that are not settings
510 * @param array $params Parameters as passed into API
511 * @param array $fields empty array to be populated with fields metadata
512 * @param bool $createMode
514 * @throws api_Exception
515 * @return array $fieldstoset name => value array of the fields to be set (with extraneous removed)
517 static function validateSettingsInput($params, &$fields, $createMode = TRUE) {
518 $group = CRM_Utils_Array
::value('group', $params);
520 $ignoredParams = array(
538 $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE));
539 $getFieldsParams = array('version' => 3);
540 if (count($settingParams) ==1) {
541 // ie we are only setting one field - we'll pass it into getfields for efficiency
542 list($name) = array_keys($settingParams);
543 $getFieldsParams['name'] = $name;
545 $fields = civicrm_api3('setting','getfields', $getFieldsParams);
546 $invalidParams = (array_diff_key($settingParams, $fields['values']));
547 if (!empty($invalidParams)) {
548 throw new api_Exception(implode(',', $invalidParams) . " not valid settings");
550 if (!empty($settingParams)) {
551 $filteredFields = array_intersect_key($settingParams, $fields['values']);
554 // no filters so we are interested in all for get mode. In create mode this means nothing to set
555 $filteredFields = $createMode ?
array() : $fields['values'];
557 return $filteredFields;
561 * Validate & convert settings input
563 * @value mixed value of the setting to be set
564 * @fieldSpec array Metadata for given field (drawn from the xml)
566 static function validateSetting(&$value, $fieldSpec) {
567 if($fieldSpec['type'] == 'String' && is_array($value)){
568 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,$value) . CRM_Core_DAO
::VALUE_SEPARATOR
;
570 if (empty($fieldSpec['validate_callback'])) {
574 list($class,$fn) = explode('::',$fieldSpec['validate_callback']);
575 if (!$class::$fn($value,$fieldSpec)) {
576 throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}");
582 * Validate & convert settings input - translate True False to 0 or 1
584 * @value mixed value of the setting to be set
585 * @fieldSpec array Metadata for given field (drawn from the xml)
587 static function validateBoolSetting(&$value, $fieldSpec) {
588 if (!CRM_Utils_Rule
::boolean($value)) {
589 throw new api_Exception("Boolean value required for {$fieldSpec['name']}");
601 * Delete some or all of the items in the settings table
603 * @param string $group The group name of the entries to be deleted
604 * @param string $name The name of the setting to be deleted
605 * @param int $componentID The optional component ID (so componenets can share the same name space)
611 static function deleteItem($group, $name = NULL, $componentID = NULL, $contactID = NULL) {
612 $dao = self::dao($group, $name, $componentID, $contactID);
615 // also reset memory cache if any
616 CRM_Utils_System::flushCache();
618 $cacheKey = self::inCache($group, $name, $componentID, $contactID, FALSE);
621 unset(self::$_cache[$cacheKey][$name]);
624 unset(self::$_cache[$cacheKey]);
630 * This provides information about the setting - similar to the fields concept for DAO information.
631 * As the setting is serialized code creating validation setting input needs to know the data type
632 * This also helps move information out of the form layer into the data layer where people can interact with
633 * it via the API or other mechanisms. In order to keep this consistent it is important the form layer
636 * Note that this function should never be called when using the runtime getvalue function. Caching works
637 * around the expectation it will be called during setting administration
639 * Function is intended for configuration rather than runtime access to settings
641 * The following params will filter the result. If none are passed all settings will be returns
643 * @params string $name Name of specific setting e.g customCSSURL
644 * @params integer $componentID id of relevant component.
646 * @param null $componentID
647 * @param array $filters
648 * @param null $domainID
649 * @param null $profile
651 * @return array $result - the following information as appropriate for each setting
655 * - add (CiviCRM version added)
661 static function getSettingSpecification(
667 $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile;
668 foreach ($filters as $filterField => $filterString) {
669 $cacheString .= "_{$filterField}_{$filterString}";
672 // the caching into 'All' seems to be a duplicate of caching to
673 // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook
674 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Specs', $cacheString, $componentID);
675 if ($settingsMetadata === NULL) {
676 $settingsMetadata = CRM_Core_BAO_Cache
::getItem('CiviCRM setting Spec', 'All', $componentID);
677 if (empty($settingsMetadata)) {
678 global $civicrm_root;
679 $metaDataFolders = array($civicrm_root. '/settings');
680 CRM_Utils_Hook
::alterSettingsFolders($metaDataFolders);
681 $settingsMetadata = self
::loadSettingsMetaDataFolders($metaDataFolders);
682 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All', $componentID);
687 CRM_Utils_Hook
::alterSettingsMetaData($settingsMetadata, $domainID, $profile);
688 self
::_filterSettingsSpecification($filters, $settingsMetadata);
691 // this is a bit 'heavy' if you are using hooks but this function
692 // is expected to only be called during setting administration
693 // it should not be called by 'getvalue' or 'getitem
694 CRM_Core_BAO_Cache
::setItem(
696 'CiviCRM setting Specs',
701 return $settingsMetadata;
706 * Load the settings files defined in a series of folders
707 * @param array $metaDataFolders list of folder paths
710 public static function loadSettingsMetaDataFolders($metaDataFolders) {
711 $settingsMetadata = array();
712 $loadedFolders = array();
713 foreach ($metaDataFolders as $metaDataFolder) {
714 $realFolder = realpath($metaDataFolder);
715 if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) {
716 $loadedFolders[$realFolder] = TRUE;
717 $settingsMetadata = $settingsMetadata + self
::loadSettingsMetaData($metaDataFolder);
720 return $settingsMetadata;
724 * Load up settings metadata from files
726 static function loadSettingsMetadata($metaDataFolder) {
727 $settingMetaData = array();
728 $settingsFiles = CRM_Utils_File
::findFiles($metaDataFolder, '*.setting.php');
729 foreach ($settingsFiles as $file) {
730 $settings = include $file;
731 $settingMetaData = array_merge($settingMetaData, $settings);
733 CRM_Core_BAO_Cache
::setItem($settingMetaData,'CiviCRM setting Spec', 'All');
734 return $settingMetaData;
738 * Filter the settings metadata according to filters passed in. This is a convenience filter
739 * and allows selective reverting / filling of settings
741 * @param array $filters Filters to match against data
742 * @param array $settingSpec metadata to filter
744 static function _filterSettingsSpecification($filters, &$settingSpec) {
745 if (empty($filters)) {
748 else if (array_keys($filters) == array('name')) {
749 $settingSpec = array($filters['name'] => CRM_Utils_Array
::value($filters['name'], $settingSpec, ''));
753 foreach ($settingSpec as $field => $fieldValues) {
754 if (array_intersect_assoc($fieldValues, $filters) != $filters) {
755 unset($settingSpec[$field]);
763 * Look for any missing settings and convert them from config or load default as appropriate
764 * This should be run from GenCode & also from upgrades to add any new defaults.
766 * Multisites have often been overlooked in upgrade scripts so can be expected to be missing
767 * a number of settings
769 static function updateSettingsFromMetaData() {
772 'domain_id' => 'all',
773 'filters' => array('prefetch' => 0),
775 $existing = civicrm_api('setting', 'get', $apiParams);
777 if (!empty($existing['values'])) {
778 $allSettings = civicrm_api('setting', 'getfields', array('version' => 3));
779 foreach ($existing['values'] as $domainID => $domainSettings) {
780 CRM_Core_BAO_Domain
::setDomain($domainID);
781 $missing = array_diff_key($allSettings['values'], $domainSettings);
782 foreach ($missing as $name => $settings) {
783 self
::convertConfigToSetting($name, $domainID);
785 CRM_Core_BAO_Domain
::resetDomain();
791 * move an item from being in the config array to being stored as a setting
792 * remove from config - as appropriate based on metadata
794 * Note that where the key name is being changed the 'legacy_key' will give us the old name
796 static function convertConfigToSetting($name, $domainID = NULL) {
797 // we have to force this here in case more than one domain is in play.
798 // whenever there is a possibility of more than one domain we must force it
799 $config = CRM_Core_Config
::singleton();
800 if (empty($domainID)) {
801 $domainID= CRM_Core_Config
::domainID();
803 $domain = new CRM_Core_DAO_Domain();
804 $domain->id
= $domainID;
806 if ($domain->config_backend
) {
807 $values = unserialize($domain->config_backend
);
811 $spec = self
::getSettingSpecification(NULL, array('name' => $name), $domainID);
812 $configKey = CRM_Utils_Array
::value('config_key', $spec[$name], CRM_Utils_Array
::value('legacy_key', $spec[$name], $name));
813 //if the key is set to config_only we don't need to do anything
814 if(empty($spec[$name]['config_only'])){
815 if (!empty($values[$configKey])) {
816 civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID));
819 civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID));
822 if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) {
823 unset($values[$configKey]);
824 $domain->config_backend
= serialize($values);
826 unset($config->$configKey);
834 * @param bool $system
835 * @param null $userID
836 * @param bool $localize
837 * @param string $returnField
838 * @param bool $returnNameANDLabels
839 * @param null $condition
843 static function valueOptions($group,
848 $returnField = 'name',
849 $returnNameANDLabels = FALSE,
852 $optionValue = self
::getItem($group, $name);
854 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, $returnField);
856 //enabled name => label require for new contact edit form, CRM-4605
857 if ($returnNameANDLabels) {
858 $names = $labels = $nameAndLabels = array();
859 if ($returnField == 'name') {
860 $names = $groupValues;
861 $labels = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'label');
864 $labels = $groupValues;
865 $names = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, $localize, $condition, 'name');
869 $returnValues = array();
870 foreach ($groupValues as $gn => $gv) {
871 $returnValues[$gv] = 0;
874 if ($optionValue && !empty($groupValues)) {
875 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
876 substr($optionValue, 1, -1)
879 if (!empty($dbValues)) {
880 foreach ($groupValues as $key => $val) {
881 if (in_array($key, $dbValues)) {
882 $returnValues[$val] = 1;
883 if ($returnNameANDLabels) {
884 $nameAndLabels[$names[$key]] = $labels[$key];
890 return ($returnNameANDLabels) ?
$nameAndLabels : $returnValues;
897 * @param bool $system
898 * @param null $userID
899 * @param string $keyField
901 static function setValueOption($group,
911 elseif (is_array($value)) {
912 $groupValues = CRM_Core_OptionGroup
::values($name, FALSE, FALSE, FALSE, NULL, $keyField);
915 foreach ($groupValues as $key => $val) {
916 if (!empty($value[$val])) {
921 if (!empty($cbValues)) {
922 $optionValue = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
923 array_keys($cbValues)
924 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
931 $optionValue = $value;
934 self
::setItem($optionValue, $group, $name);
939 * @param null $domainID
941 static function fixAndStoreDirAndURL(&$params, $domainID = NULL) {
942 if (self
::isUpgradeFromPreFourOneAlpha1()) {
946 if (empty($domainID)) {
947 $domainID = CRM_Core_Config
::domainID();
950 SELECT name, group_name
953 AND ( group_name = %2
957 1 => array($domainID, 'Integer'),
958 2 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
959 3 => array(self
::URL_PREFERENCES_NAME
, 'String'),
962 $dirParams = array();
963 $urlParams = array();
965 $dao = CRM_Core_DAO
::executeQuery($sql,
971 // trap exceptions as error
975 if (is_a($dao, 'DB_Error')) {
976 if (CRM_Core_Config
::isUpgradeMode()) {
977 // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue
981 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
986 while ($dao->fetch()) {
987 if (!isset($params[$dao->name
])) {
990 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
991 $dirParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
994 $urlParams[$dao->name
] = CRM_Utils_Array
::value($dao->name
, $params, '');
996 unset($params[$dao->name
]);
999 if (!empty($dirParams)) {
1000 self
::storeDirectoryOrURLPreferences($dirParams,
1001 self
::DIRECTORY_PREFERENCES_NAME
1005 if (!empty($urlParams)) {
1006 self
::storeDirectoryOrURLPreferences($urlParams,
1007 self
::URL_PREFERENCES_NAME
1016 static function storeDirectoryOrURLPreferences(&$params, $group) {
1017 foreach ($params as $name => $value) {
1018 // always try to store relative directory or url from CMS root
1019 $value = ($group == self
::DIRECTORY_PREFERENCES_NAME
) ? CRM_Utils_File
::relativeDirectory($value) : CRM_Utils_System
::relativeURL($value);
1021 self
::setItem($value, $group, $name);
1027 * @param bool $setInConfig
1029 static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
1030 if (CRM_Core_Config
::isUpgradeMode()) {
1031 $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') ?
TRUE : FALSE;
1032 // hack to set the resource base url so that js/ css etc is loaded correctly
1034 $params['userFrameworkResourceURL'] = CRM_Utils_File
::addTrailingSlash(CIVICRM_UF_BASEURL
, '/') . str_replace('administrator', '', CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name'));
1036 if (self
::isUpgradeFromPreFourOneAlpha1()) {
1042 $config = CRM_Core_Config
::singleton();
1046 SELECT name, group_name, value
1047 FROM civicrm_setting
1048 WHERE ( group_name = %1
1049 OR group_name = %2 )
1052 $sqlParams = array(1 => array(self
::DIRECTORY_PREFERENCES_NAME
, 'String'),
1053 2 => array(self
::URL_PREFERENCES_NAME
, 'String'),
1054 3 => array(CRM_Core_Config
::domainID(), 'Integer'),
1057 $dao = CRM_Core_DAO
::executeQuery($sql,
1063 // trap exceptions as error
1067 if (is_a($dao, 'DB_Error')) {
1068 echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n";
1072 while ($dao->fetch()) {
1073 $value = self
::getOverride($dao->group_name
, $dao->name
, NULL);
1074 if ($value === NULL && $dao->value
) {
1075 $value = unserialize($dao->value
);
1076 if ($dao->group_name
== self
::DIRECTORY_PREFERENCES_NAME
) {
1077 $value = CRM_Utils_File
::absoluteDirectory($value);
1080 // CRM-7622: we need to remove the language part
1081 $value = CRM_Utils_System
::absoluteURL($value, TRUE);
1084 // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available
1085 if (!isset($value) && !empty($params[$dao->name
])) {
1086 $value = $params[$dao->name
];
1088 $params[$dao->name
] = $value;
1091 $config->{$dao->name
} = $value;
1097 * Determine what, if any, overrides have been provided
1104 * @return mixed, NULL or an overriden value
1106 protected static function getOverride($group, $name, $default) {
1107 global $civicrm_setting;
1108 if ($group && $name && isset($civicrm_setting[$group][$name])) {
1109 return $civicrm_setting[$group][$name];
1117 * civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade
1121 static function isUpgradeFromPreFourOneAlpha1() {
1122 if (CRM_Core_Config
::isUpgradeMode()) {
1123 $currentVer = CRM_Core_BAO_Domain
::version();
1124 if (version_compare($currentVer, '4.1.alpha1') < 0) {