* @version $Id: api.php 30486 2010-11-02 16:12:09Z shot $
*/
-/*
+/**
* @param string $entity
* type of entities to deal with
* @param string $action
$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
'api/v' . $apiRequest['version'] . '/' . $camelName . '/' . $actionCamelName . '.php',
);
foreach ($stdFiles as $stdFile) {
- require_once 'CRM/Utils/File.php';
if (CRM_Utils_File::isIncludable($stdFile)) {
require_once $stdFile;
if (function_exists($stdFunction)) {
'api/v' . $apiRequest['version'] . '/Generic/' . $actionCamelName . '.php',
);
foreach ($genericFiles as $genericFile) {
- require_once 'CRM/Utils/File.php';
if (CRM_Utils_File::isIncludable($genericFile)) {
require_once $genericFile;
if (function_exists($genericFunction)) {
$cache[$cachekey] = array('function' => FALSE, 'is_generic' => FALSE);
return $cache[$cachekey];
}
+/**
+ * Version 3 wrapper for civicrm_api. Throws exception
+ * @param string $entity type of entities to deal with
+ * @param string $action create, get, delete or some special action name.
+ * @param array $params array to be passed to function
+ *
+ * @return array
+ *
+ */
+function civicrm_api3($entity, $action, $params){
+ $params['version'] = 3;
+ $result = civicrm_api($entity, $action, $params);
+ if($result['is_error']){
+ throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result);
+ }
+}
/**
* Load/require all files related to an entity.
$camelName = _civicrm_api_get_camel_name($entity, $version);
// Check for master entity file; to match _civicrm_api_resolve(), only load the first one
- require_once 'CRM/Utils/File.php';
$stdFile = 'api/v' . $version . '/' . $camelName . '.php';
if (CRM_Utils_File::isIncludable($stdFile)) {
require_once $stdFile;
}
function _civicrm_api_get_camel_name($entity, $version = NULL) {
- static $_map = NULL;
-
if (empty($version)) {
$version = civicrm_get_api_version();
}
- if (isset($_map[$version][strtolower($entity)])) {
- return $_map[$version][strtolower($entity)];
- }
-
$fragments = explode('_', $entity);
foreach ($fragments as & $fragment) {
$fragment = ucfirst($fragment);
return implode('', $fragments);
}
-/*
+/**
* Call any nested api calls
*/
function _civicrm_api_call_nested_api(&$params, &$result, $action, $entity, $version) {
}
}
-/*
+/**
* Swap out any $values vars - ie. the value after $value is swapped for the parent $result
* 'activity_type_id' => '$value.testfield',
'tag_id' => '$value.api.tag.create.id',
}
}
-/*
+/**
* Convert possibly camel name to underscore separated entity name
*
* @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)) {
}
return $entity;
}
-/*
+
+/**
* Having a DAO object find the entity name
* @param object $bao DAO being passed in
*/
function _civicrm_api_get_entity_name_from_dao($bao){
$daoName = str_replace("BAO", "DAO", get_class($bao));
- $dao = array();
- require ('CRM/Core/DAO/.listAll.php');
- $daos = array_flip($dao);
- return _civicrm_api_get_entity_name_from_camel($daos[$daoName]);
-
+ 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;
+ }
+ }
+}