Merge pull request #785 from yashodha/4.3
[civicrm-core.git] / api / api.php
index dcd430231cb8b900fed60ff1dbc65b5b388ce9a5..84f0a99330271d1c1d65c8400e1695c153354f52 100644 (file)
@@ -41,12 +41,17 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
       $transaction = new CRM_Core_Transaction();
     }
 
+    // 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']);
 
     // we do this before we
     _civicrm_api3_swap_out_aliases($apiRequest);
     if (strtolower($action) != 'getfields') {
-      if (!CRM_Utils_Array::value('id', $params)) {
+      if (!CRM_Utils_Array::value('id', $apiRequest['params'])) {
         $apiRequest['params'] = array_merge(_civicrm_api3_getdefaults($apiRequest), $apiRequest['params']);
       }
       //if 'id' is set then only 'version' will be checked but should still be checked for consistency
@@ -512,6 +517,8 @@ function _civicrm_api_replace_variables($entity, $action, &$params, &$parentResu
  *
  * @param string $entity entity name in various formats e.g. Contribution, contribution, OptionValue, option_value, UFJoin, uf_join
  * @return string $entity entity name in underscore separated format
+ *
+ * FIXME: Why isn't this called first thing in civicrm_api wrapper?
  */
 function _civicrm_api_get_entity_name_from_camel($entity) {
   if ($entity == strtolower($entity)) {
@@ -540,3 +547,50 @@ function _civicrm_api_get_entity_name_from_dao($bao){
 
 }
 
+
+/**
+ * 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;
+    }
+  }
+}