From bace5cd9bbd7fe769b72990335865b6c7428993a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 21 Mar 2014 17:06:56 -0700 Subject: [PATCH] CRM-14370 - API Kernel - Extract I18nSubscriber --- Civi/API/Kernel.php | 5 -- Civi/API/Subscriber/I18nSubscriber.php | 94 ++++++++++++++++++++++++++ Civi/Core/Container.php | 1 + api/api.php | 46 ------------- 4 files changed, 95 insertions(+), 51 deletions(-) create mode 100644 Civi/API/Subscriber/I18nSubscriber.php diff --git a/Civi/API/Kernel.php b/Civi/API/Kernel.php index a974c50415..7a596240ba 100644 --- a/Civi/API/Kernel.php +++ b/Civi/API/Kernel.php @@ -100,11 +100,6 @@ class Kernel { $apiRequest = $this->dispatcher->dispatch(Events::PREPARE, new PrepareEvent(NULL, $apiRequest))->getApiRequest(); - // support multi-lingual requests - if ($language = \CRM_Utils_Array::value('option.language', $params)) { - _civicrm_api_set_locale($language); - } - _civicrm_api3_api_check_permission($apiRequest['entity'], $apiRequest['action'], $apiRequest['params']); $fields = _civicrm_api3_api_getfields($apiRequest); // we do this before we diff --git a/Civi/API/Subscriber/I18nSubscriber.php b/Civi/API/Subscriber/I18nSubscriber.php new file mode 100644 index 0000000000..f920c44206 --- /dev/null +++ b/Civi/API/Subscriber/I18nSubscriber.php @@ -0,0 +1,94 @@ + array('onApiPrepare', Events::W_MIDDLE) + ); + } + + public function onApiPrepare(\Civi\API\Event\Event $event) { + $apiRequest = $event->getApiRequest(); + + // support multi-lingual requests + if ($language = \CRM_Utils_Array::value('option.language', $apiRequest['params'])) { + $this->setLocale($language); + } + } + + /** + * Sets the tsLocale and dbLocale for multi-lingual sites. + * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() + * to avoid regressions from refactoring. + */ + public function setLocale($lcMessagesRequest) { + // We must validate whether the locale is valid, otherwise setting a bad + // dbLocale could probably lead to sql-injection. + $domain = new \CRM_Core_DAO_Domain(); + $domain->id = \CRM_Core_Config::domainID(); + $domain->find(TRUE); + + if ($domain->config_backend) { + $defaults = unserialize($domain->config_backend); + + // are we in a multi-language setup? + $multiLang = $domain->locales ? TRUE : FALSE; + $lcMessages = NULL; + + // on multi-lang sites based on request and civicrm_uf_match + if ($multiLang) { + $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 { + throw new \API_Exception(ts('Language not enabled: %1', array(1 => $lcMessagesRequest))); + } + } + + global $dbLocale; + + // set suffix for table names - use views if more than one language + if ($lcMessages) { + $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : ''; + + // FIXME: an ugly hack to fix CRM-4041 + global $tsLocale; + $tsLocale = $lcMessages; + } + } + } +} diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index e849038456..e6fbc55bfb 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -87,6 +87,7 @@ class Container { */ public function createApiKernel($dispatcher) { $dispatcher->addSubscriber(new \Civi\API\Subscriber\TransactionSubscriber()); + $dispatcher->addSubscriber(new \Civi\API\Subscriber\I18nSubscriber()); $dispatcher->addSubscriber(new \Civi\API\Subscriber\XDebugSubscriber()); $dispatcher->addListener(\Civi\API\Events::AUTHORIZE, function($event) { // dummy placeholder diff --git a/api/api.php b/api/api.php index d0aa4653e9..2548d072f5 100644 --- a/api/api.php +++ b/api/api.php @@ -455,49 +455,3 @@ function _civicrm_api_get_entity_name_from_dao($bao){ return _civicrm_api_get_entity_name_from_camel(CRM_Core_DAO_AllCoreTables::getBriefName($daoName)); } -/** - * Sets the tsLocale and dbLocale for multi-lingual sites. - * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() - * to avoid regressions from refactoring. - */ -function _civicrm_api_set_locale($lcMessagesRequest) { - // We must validate whether the locale is valid, otherwise setting a bad - // dbLocale could probably lead to sql-injection. - $domain = new CRM_Core_DAO_Domain(); - $domain->id = CRM_Core_Config::domainID(); - $domain->find(TRUE); - - if ($domain->config_backend) { - $defaults = unserialize($domain->config_backend); - - // are we in a multi-language setup? - $multiLang = $domain->locales ? TRUE : FALSE; - $lcMessages = NULL; - - // on multi-lang sites based on request and civicrm_uf_match - if ($multiLang) { - $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 { - throw new API_Exception(ts('Language not enabled: %1', array(1 => $lcMessagesRequest))); - } - } - - global $dbLocale; - - // set suffix for table names - use views if more than one language - if ($lcMessages) { - $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : ''; - - // FIXME: an ugly hack to fix CRM-4041 - global $tsLocale; - $tsLocale = $lcMessages; - } - } -} -- 2.25.1