/** * Sort an array and maintain index association, use Collate from the * PECL "intl" package, if available, for UTF-8 sorting (ex: list of countries). * On Debian/Ubuntu: apt-get install php5-intl * * @param array $array array of values * * @return array Sorted array * @static */ static function asort($array = array()) { $lcMessages = CRM_Utils_System::getUFLocale(); if ($lcMessages && $lcMessages != 'en_US' && class_exists('Collator')) { $collator = new Collator($lcMessages . '.utf8'); $collator->asort($array); } else { asort($array); } return $array; }
/** * Function to retrieve the settings values from db * * @return array $defaults * @static */ static function retrieve(&$defaults) { require_once "CRM/Core/DAO/Domain.php"; $domain = new CRM_Core_DAO_Domain(); $domain->selectAdd(); if (CRM_Utils_Array::value('q', $_GET) == 'civicrm/upgrade') { $domain->selectAdd('config_backend'); } else { $domain->selectAdd('config_backend, locales'); } $domain->id = CRM_Core_Config::domainID(); $domain->find(true); if ($domain->config_backend) { $defaults = unserialize($domain->config_backend); // calculate month var $defaults['dateformatMonthVar'] = strstr($defaults['dateformatQfDate'], '%m') ? 'm' : (strstr($defaults['dateformatQfDate'], '%b') ? 'M' : (strstr($defaults['dateformatQfDate'], '%B') ? 'F' : null)); //calculate month var for Date Time $defaults['datetimeformatMonthVar'] = strstr($defaults['dateformatQfDatetime'], '%m') ? 'm' : (strstr($defaults['dateformatQfDatetime'], '%b') ? 'M' : (strstr($defaults['dateformatQfDatetime'], '%B') ? 'F' : null)); //calculate hour var for Date Time $defaults['datetimeformatHourVar'] = strstr($defaults['dateformatQfDatetime'], '%I') ? 'h' : (strstr($defaults['dateformatQfDatetime'], '%l') ? 'g' : null); // set proper monetary formatting, falling back to en_US and C (CRM-2782) setlocale(LC_MONETARY, $defaults['lcMonetary'] . '.utf8', $defaults['lcMonetary'], 'en_US.utf8', 'en_US', 'C'); $skipVars = array('dsn', 'templateCompileDir', 'userFrameworkDSN', 'userFrameworkBaseURL', 'userFrameworkClass', 'userHookClass', 'userPermissionClass', 'userFrameworkURLVar', 'qfKey', 'gettextResourceDir', 'cleanURL'); foreach ($skipVars as $skip) { if (array_key_exists($skip, $defaults)) { unset($defaults[$skip]); } } // since language field won't be present before upgrade. if (CRM_Utils_Array::value('q', $_GET) == 'civicrm/upgrade') { return; } // are we in a multi-language setup? $multiLang = $domain->locales ? true : false; // set the current language $lcMessages = null; $session =& CRM_Core_Session::singleton(); // on multi-lang sites based on request and civicrm_uf_match if ($multiLang) { require_once 'CRM/Utils/Request.php'; $lcMessagesRequest = CRM_Utils_Request::retrieve('lcMessages', 'String', $this); $languageLimit = array(); if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) { $languageLimit = $defaults['languageLimit']; } if (in_array($lcMessagesRequest, array_keys($languageLimit))) { $lcMessages = $lcMessagesRequest; } else { $lcMessagesRequest = null; } if (!$lcMessagesRequest) { $lcMessagesSession = $session->get('lcMessages'); if (in_array($lcMessagesSession, array_keys($languageLimit))) { $lcMessages = $lcMessagesSession; } else { $lcMessagesSession = null; } } if ($lcMessagesRequest) { require_once 'CRM/Core/DAO/UFMatch.php'; $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(true)) { $ufm->language = $lcMessages; $ufm->save(); } $session->set('lcMessages', $lcMessages); } if (!$lcMessages and $session->get('userID')) { require_once 'CRM/Core/DAO/UFMatch.php'; $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(true) && in_array($ufm->language, array_keys($languageLimit))) { $lcMessages = $ufm->language; } $session->set('lcMessages', $lcMessages); } } // if unset, try to inherit the language from the hosting CMS if ($lcMessages === null) { require_once 'CRM/Utils/System.php'; $lcMessages = CRM_Utils_System::getUFLocale(); require_once 'CRM/Core/BAO/CustomOption.php'; if ($domain->locales and !in_array($lcMessages, explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $domain->locales))) { $lcMessages = null; } } if ($lcMessages) { // update config lcMessages - CRM-5027 fixed. $defaults['lcMessages'] = $lcMessages; } else { // if a single-lang site or the above didn't yield a result, use default $lcMessages = $defaults['lcMessages']; } // set suffix for table names - use views if more than one language global $dbLocale; $dbLocale = $multiLang ? "_{$lcMessages}" : ''; // FIXME: an ugly hack to fix CRM-4041 global $tsLocale; $tsLocale = $lcMessages; } }
/** * Evaluate locale preferences and activate a chosen locale by * updating session+global variables. * * @param \Civi\Core\SettingsBag $settings * @param string $activatedLocales * Imploded list of locales which are supported in the DB. */ public static function applyLocale($settings, $activatedLocales) { // are we in a multi-language setup? $multiLang = $activatedLocales ? TRUE : FALSE; // set the current language $chosenLocale = NULL; $session = CRM_Core_Session::singleton(); // on multi-lang sites based on request and civicrm_uf_match if ($multiLang) { $languageLimit = array(); if (is_array($settings->get('languageLimit'))) { $languageLimit = $settings->get('languageLimit'); } $requestLocale = CRM_Utils_Request::retrieve('lcMessages', 'String'); if (in_array($requestLocale, array_keys($languageLimit))) { $chosenLocale = $requestLocale; //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache. // Ed: This doesn't sound good. CRM_Core_BAO_Cache::deleteGroup('navigation'); } else { $requestLocale = NULL; } if (!$requestLocale) { $sessionLocale = $session->get('lcMessages'); if (in_array($sessionLocale, array_keys($languageLimit))) { $chosenLocale = $sessionLocale; } else { $sessionLocale = NULL; } } if ($requestLocale) { $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(TRUE)) { $ufm->language = $chosenLocale; $ufm->save(); } $session->set('lcMessages', $chosenLocale); } if (!$chosenLocale and $session->get('userID')) { $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(TRUE) && in_array($ufm->language, array_keys($languageLimit))) { $chosenLocale = $ufm->language; } $session->set('lcMessages', $chosenLocale); } } global $dbLocale; // try to inherit the language from the hosting CMS if ($settings->get('inheritLocale')) { // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set $dbLocale = $multiLang ? "_" . $settings->get('lcMessages') : ''; $chosenLocale = CRM_Utils_System::getUFLocale(); if ($activatedLocales and !in_array($chosenLocale, explode(CRM_Core_DAO::VALUE_SEPARATOR, $activatedLocales))) { $chosenLocale = NULL; } } if (empty($chosenLocale)) { //CRM-11993 - if a single-lang site, use default $chosenLocale = $settings->get('lcMessages'); } // set suffix for table names - use views if more than one language $dbLocale = $multiLang ? "_{$chosenLocale}" : ''; // FIXME: an ugly hack to fix CRM-4041 global $tsLocale; $tsLocale = $chosenLocale; // FIXME: as bad aplace as any to fix CRM-5428 // (to be moved to a sane location along with the above) if (function_exists('mb_internal_encoding')) { mb_internal_encoding('UTF-8'); } }
/** * Retrieve the settings values from db. * * @param $defaults * * @return array */ public static function retrieve(&$defaults) { $domain = new CRM_Core_DAO_Domain(); //we are initializing config, really can't use, CRM-7863 $urlVar = 'q'; if (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') { $urlVar = 'task'; } if (CRM_Core_Config::isUpgradeMode()) { $domain->selectAdd('config_backend'); } elseif (CRM_Utils_Array::value($urlVar, $_GET) == 'admin/modules/list/confirm') { $domain->selectAdd('config_backend', 'locales'); } else { $domain->selectAdd('config_backend, locales, locale_custom_strings'); } $domain->id = CRM_Core_Config::domainID(); $domain->find(TRUE); if ($domain->config_backend) { $defaults = unserialize($domain->config_backend); if ($defaults === FALSE || !is_array($defaults)) { $defaults = array(); return FALSE; } $skipVars = self::skipVars(); foreach ($skipVars as $skip) { if (array_key_exists($skip, $defaults)) { unset($defaults[$skip]); } } // check if there are any locale strings if ($domain->locale_custom_strings) { $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings); } else { $defaults['localeCustomStrings'] = NULL; } // are we in a multi-language setup? $multiLang = $domain->locales ? TRUE : FALSE; // set the current language $lcMessages = NULL; $session = CRM_Core_Session::singleton(); // on multi-lang sites based on request and civicrm_uf_match if ($multiLang) { $lcMessagesRequest = CRM_Utils_Request::retrieve('lcMessages', 'String', $this); $languageLimit = array(); if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) { $languageLimit = $defaults['languageLimit']; } if (in_array($lcMessagesRequest, array_keys($languageLimit))) { $lcMessages = $lcMessagesRequest; //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache. CRM_Core_BAO_Cache::deleteGroup('navigation'); } else { $lcMessagesRequest = NULL; } if (!$lcMessagesRequest) { $lcMessagesSession = $session->get('lcMessages'); if (in_array($lcMessagesSession, array_keys($languageLimit))) { $lcMessages = $lcMessagesSession; } else { $lcMessagesSession = NULL; } } if ($lcMessagesRequest) { $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(TRUE)) { $ufm->language = $lcMessages; $ufm->save(); } $session->set('lcMessages', $lcMessages); } if (!$lcMessages and $session->get('userID')) { $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(TRUE) && in_array($ufm->language, array_keys($languageLimit))) { $lcMessages = $ufm->language; } $session->set('lcMessages', $lcMessages); } } global $dbLocale; // try to inherit the language from the hosting CMS if (!empty($defaults['inheritLocale'])) { // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set $dbLocale = $multiLang ? "_{$defaults['lcMessages']}" : ''; $lcMessages = CRM_Utils_System::getUFLocale(); if ($domain->locales and !in_array($lcMessages, explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales))) { $lcMessages = NULL; } } if (empty($lcMessages)) { //CRM-11993 - if a single-lang site, use default $lcMessages = CRM_Utils_Array::value('lcMessages', $defaults); } // set suffix for table names - use views if more than one language $dbLocale = $multiLang ? "_{$lcMessages}" : ''; // FIXME: an ugly hack to fix CRM-4041 global $tsLocale; $tsLocale = $lcMessages; // FIXME: as bad aplace as any to fix CRM-5428 // (to be moved to a sane location along with the above) if (function_exists('mb_internal_encoding')) { mb_internal_encoding('UTF-8'); } } // dont add if its empty if (!empty($defaults)) { // retrieve directory and url preferences also CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($defaults); // Pickup enabled-components from settings table if found. $enableComponents = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array()); if (!empty($enableComponents)) { $defaults['enableComponents'] = $enableComponents; $components = CRM_Core_Component::getComponents(); $enabledComponentIDs = array(); foreach ($defaults['enableComponents'] as $name) { $enabledComponentIDs[] = $components[$name]->componentID; } $defaults['enableComponentIDs'] = $enabledComponentIDs; } } }
/** * Function to retrieve the settings values from db * * @return array $defaults * @static */ static function retrieve(&$defaults) { require_once "CRM/Core/DAO/Domain.php"; $domain = new CRM_Core_DAO_Domain(); $domain->selectAdd(); if (CRM_Utils_Array::value('q', $_GET) == 'civicrm/upgrade') { $domain->selectAdd('config_backend'); } else { $domain->selectAdd('config_backend, locales, locale_custom_strings'); } $domain->id = CRM_Core_Config::domainID(); $domain->find(true); if ($domain->config_backend) { $defaults = unserialize($domain->config_backend); if ($defaults === false || !is_array($defaults)) { $defaults = array(); return; } $skipVars = array('dsn', 'templateCompileDir', 'userFrameworkDSN', 'userFrameworkBaseURL', 'userFrameworkClass', 'userHookClass', 'userPermissionClass', 'userFrameworkURLVar', 'newBaseURL', 'newBaseDir', 'newSiteName', 'configAndLogDir', 'qfKey', 'gettextResourceDir', 'cleanURL', 'locale_custom_strings', 'localeCustomStrings'); foreach ($skipVars as $skip) { if (array_key_exists($skip, $defaults)) { unset($defaults[$skip]); } } // since language field won't be present before upgrade. if (CRM_Utils_Array::value('q', $_GET) == 'civicrm/upgrade') { return; } // check if there are any locale strings if ($domain->locale_custom_strings) { $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings); } else { $defaults['localeCustomStrings'] = null; } // are we in a multi-language setup? $multiLang = $domain->locales ? true : false; // set the current language $lcMessages = null; $session = CRM_Core_Session::singleton(); // for logging purposes, pass the userID to the db if ($session->get('userID')) { CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1', array(1 => array($session->get('userID'), 'Integer'))); } // on multi-lang sites based on request and civicrm_uf_match if ($multiLang) { require_once 'CRM/Utils/Request.php'; $lcMessagesRequest = CRM_Utils_Request::retrieve('lcMessages', 'String', $this); $languageLimit = array(); if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) { $languageLimit = $defaults['languageLimit']; } if (in_array($lcMessagesRequest, array_keys($languageLimit))) { $lcMessages = $lcMessagesRequest; } else { $lcMessagesRequest = null; } if (!$lcMessagesRequest) { $lcMessagesSession = $session->get('lcMessages'); if (in_array($lcMessagesSession, array_keys($languageLimit))) { $lcMessages = $lcMessagesSession; } else { $lcMessagesSession = null; } } if ($lcMessagesRequest) { require_once 'CRM/Core/DAO/UFMatch.php'; $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(true)) { $ufm->language = $lcMessages; $ufm->save(); } $session->set('lcMessages', $lcMessages); } if (!$lcMessages and $session->get('userID')) { require_once 'CRM/Core/DAO/UFMatch.php'; $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(true) && in_array($ufm->language, array_keys($languageLimit))) { $lcMessages = $ufm->language; } $session->set('lcMessages', $lcMessages); } } global $dbLocale; // if unset and the install is so configured, try to inherit the language from the hosting CMS if ($lcMessages === null and CRM_Utils_Array::value('inheritLocale', $defaults)) { // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set $dbLocale = $multiLang ? "_{$defaults['lcMessages']}" : ''; require_once 'CRM/Utils/System.php'; $lcMessages = CRM_Utils_System::getUFLocale(); require_once 'CRM/Core/BAO/CustomOption.php'; if ($domain->locales and !in_array($lcMessages, explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $domain->locales))) { $lcMessages = null; } } if ($lcMessages) { // update config lcMessages - CRM-5027 fixed. $defaults['lcMessages'] = $lcMessages; } else { // if a single-lang site or the above didn't yield a result, use default $lcMessages = $defaults['lcMessages']; } // set suffix for table names - use views if more than one language $dbLocale = $multiLang ? "_{$lcMessages}" : ''; // FIXME: an ugly hack to fix CRM-4041 global $tsLocale; $tsLocale = $lcMessages; // FIXME: as bad aplace as any to fix CRM-5428 // (to be moved to a sane location along with the above) if (function_exists('mb_internal_encoding')) { mb_internal_encoding('UTF-8'); } } // dont add if its empty if (!empty($defaults)) { // retrieve directory and url preferences also require_once 'CRM/Core/BAO/Preferences.php'; CRM_Core_BAO_Preferences::retrieveDirectoryAndURLPreferences($defaults); } }