*/
/**
- * File for CiviCRM APIv3 utilitity functions
+ * CiviCRM APIv3 utility functions.
*
* @package CiviCRM_APIv3
- * @subpackage API_utils
- *
- * @copyright CiviCRM LLC (c) 2004-2014
- * @version $Id: utils.php 30879 2010-11-22 15:45:55Z shot $
*/
/**
*/
function civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL, &$dao = NULL, $extraReturnValues = array()) {
$result = array();
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
+ // TODO: This shouldn't be necessary but this fn sometimes gets called with lowercase entity
+ $entity = _civicrm_api_get_camel_name($entity);
$result['is_error'] = 0;
//lets set the ['id'] field if it's not set & we know what the entity is
- if (is_array($values) && !empty($entity) && $action != 'getfields') {
+ if (is_array($values) && $entity && $action != 'getfields') {
foreach ($values as $key => $item) {
- if (empty($item['id']) && !empty($item[$entity . "_id"])) {
- $values[$key]['id'] = $item[$entity . "_id"];
+ if (empty($item['id']) && !empty($item[$lowercase_entity . "_id"])) {
+ $values[$key]['id'] = $item[$lowercase_entity . "_id"];
}
if (!empty($item['financial_type_id'])) {
//4.3 legacy handling
}
if ($deprecated) {
// Metadata-level deprecations or wholesale entity deprecations.
- if ($entity == 'entity' || $action == 'getactions' || is_string($deprecated)) {
+ if ($entity == 'Entity' || $action == 'getactions' || is_string($deprecated)) {
$result['deprecated'] = $deprecated;
}
// Action-specific deprecations
$name = substr($name, 13, $last - 13);
}
- $name = _civicrm_api_get_camel_name($name, 3);
+ $name = _civicrm_api_get_camel_name($name);
if ($name == 'Individual' || $name == 'Household' || $name == 'Organization') {
$name = 'Contact';
* @return array
*/
function _civicrm_api3_get_using_query_object($entity, $params, $additional_options = array(), $getCount = NULL) {
-
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
// Convert id to e.g. contact_id
- if (empty($params[$entity . '_id']) && isset($params['id'])) {
- $params[$entity . '_id'] = $params['id'];
+ if (empty($params[$lowercase_entity . '_id']) && isset($params['id'])) {
+ $params[$lowercase_entity . '_id'] = $params['id'];
}
unset($params['id']);
// we will filter query object against getfields
$fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'get'));
// we need to add this in as earlier in this function 'id' was unset in favour of $entity_id
- $fields['values'][$entity . '_id'] = array();
+ $fields['values'][$lowercase_entity . '_id'] = array();
$varsToFilter = array('returnProperties', 'inputParams');
foreach ($varsToFilter as $varToFilter) {
if (!is_array($$varToFilter)) {
$returnProperties = NULL;
}
+ if (substr($sort, 0, 2) == 'id') {
+ $sort = $lowercase_entity . "_" . $sort;
+ }
+
$newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
foreach ($newParams as &$newParam) {
if ($newParam[1] == '=' && is_array($newParam[2])) {
$newParam[2] = $sqlFilter;
}
}
-
}
$skipPermissions = !empty($params['check_permissions']) ? 0 : 1;
* @param CRM_Core_DAO $dao
* @param array $params
* @param bool $unique
- * @param string $entity
*
* @throws API_Exception
* @throws Exception
*/
-function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) {
- $entity = substr($dao->__table, 8);
- if (!empty($params[$entity . "_id"]) && empty($params['id'])) {
+function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE) {
+ $entity = _civicrm_api_get_entity_name_from_dao($dao);
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
+ if (!empty($params[$lowercase_entity . "_id"]) && empty($params['id'])) {
//if entity_id is set then treat it as ID (will be overridden by id if set)
- $params['id'] = $params[$entity . "_id"];
+ $params['id'] = $params[$lowercase_entity . "_id"];
}
$allfields = _civicrm_api3_build_fields_array($dao, $unique);
$fields = array_intersect(array_keys($allfields), array_keys($params));
* options extracted from params
*/
function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $entity = '', $action = '') {
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
$is_count = FALSE;
$sort = CRM_Utils_Array::value('sort', $params, 0);
$sort = CRM_Utils_Array::value('option.sort', $params, $sort);
}
if ($entity && $action == 'get') {
if (!empty($returnProperties['id'])) {
- $returnProperties[$entity . '_id'] = 1;
+ $returnProperties[$lowercase_entity . '_id'] = 1;
unset($returnProperties['id']);
}
switch (trim(strtolower($sort))) {
case 'id':
case 'id desc':
case 'id asc':
- $sort = str_replace('id', $entity . '_id', $sort);
+ $sort = str_replace('id', $lowercase_entity . '_id', $sort);
}
}
$legacyreturnProperties[substr($n, 7)] = $v;
}
elseif ($n == 'id') {
- $inputParams[$entity . '_id'] = $v;
+ $inputParams[$lowercase_entity . '_id'] = $v;
}
elseif (in_array($n, $otherVars)) {
}
$fields = $bao->fields();
if ($unique) {
if (empty($fields['id'])) {
- $entity = _civicrm_api_get_entity_name_from_dao($bao);
- $fields['id'] = $fields[$entity . '_id'];
- unset($fields[$entity . '_id']);
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel(_civicrm_api_get_entity_name_from_dao($bao));
+ $fields['id'] = $fields[$lowercase_entity . '_id'];
+ unset($fields[$lowercase_entity . '_id']);
}
return $fields;
}
*
* @param array $params
* Associative array of property name/value.
- * pairs to insert in new history.
+ * pairs to insert in new history.
* @param string $daoName
* @param bool $return
*
*/
function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $entity = "") {
$bao = new $bao_name();
- _civicrm_api3_dao_set_filter($bao, $params, TRUE, $entity);
+ _civicrm_api3_dao_set_filter($bao, $params, TRUE);
if ($returnAsSuccess) {
return civicrm_api3_create_success(_civicrm_api3_dao_to_array($bao, $params, FALSE, $entity), $params, $entity, 'get');
}
function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) {
_civicrm_api3_format_params_for_create($params, $entity);
$args = array(&$params);
- if (!empty($entity)) {
+ if ($entity) {
$ids = array($entity => CRM_Utils_Array::value('id', $params));
$args[] = &$ids;
}
* @throws Exception
*/
function _civicrm_api3_validate_fields($entity, $action, &$params, $fields, $errorMode = FALSE) {
+ //CRM-15792 handle datetime for custom fields below code handles chain api call
+ $chainApikeys = array_flip(preg_grep("/^api./", array_keys($params)));
+ if (!empty($chainApikeys) && is_array($chainApikeys)) {
+ foreach ($chainApikeys as $key => $value) {
+ if (is_array($params[$key])) {
+ $chainApiParams = array_intersect_key($fields, $params[$key]);
+ $customFields = array_fill_keys(array_keys($params[$key]), $key);
+ }
+ }
+ }
$fields = array_intersect_key($fields, $params);
+ if (!empty($chainApiParams)) {
+ $fields = array_merge($fields, $chainApiParams);
+ }
foreach ($fields as $fieldName => $fieldInfo) {
switch (CRM_Utils_Array::value('type', $fieldInfo)) {
case CRM_Utils_Type::T_INT:
_civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
break;
- case 4:
- case 12:
+ case CRM_Utils_Type::T_DATE:
+ case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
case CRM_Utils_Type::T_TIMESTAMP:
//field is of type date or datetime
- _civicrm_api3_validate_date($params, $fieldName, $fieldInfo);
+ if (!empty($customFields) && array_key_exists($fieldName, $customFields)) {
+ $dateParams = &$params[$customFields[$fieldName]];
+ }
+ else {
+ $dateParams = &$params;
+ }
+ _civicrm_api3_validate_date($dateParams, $fieldName, $fieldInfo);
break;
case 32:
* Replace the old set of entities (matching some given keys) with a new set of
* entities (matching the same keys).
*
- * Note: This will verify that 'values' is present, but it does not directly verify
+ * @note This will verify that 'values' is present, but it does not directly verify
* any other parameters.
*
* @param string $entity
// Regular fields have a 'name' property
$value['name'] = 'custom_' . $key;
$value['title'] = $value['label'];
- $value['type'] = _getStandardTypeFromCustomDataType($value['data_type']);
+ $value['type'] = _getStandardTypeFromCustomDataType($value);
$ret['custom_' . $key] = $value;
}
return $ret;
/**
* Translate the custom field data_type attribute into a std 'type'.
*
- * @param $dataType
+ * @param array $value
*
* @return int
*/
-function _getStandardTypeFromCustomDataType($dataType) {
+function _getStandardTypeFromCustomDataType($value) {
+ $dataType = $value['data_type'];
+ //CRM-15792 - If date custom field contains timeformat change type to DateTime
+ if ($value['data_type'] == 'Date' && isset($value['time_format']) && $value['time_format'] > 0) {
+ $dataType = 'DateTime';
+ }
$mapping = array(
'String' => CRM_Utils_Type::T_STRING,
'Int' => CRM_Utils_Type::T_INT,
'Memo' => CRM_Utils_Type::T_LONGTEXT,
'Float' => CRM_Utils_Type::T_FLOAT,
'Date' => CRM_Utils_Type::T_DATE,
+ 'DateTime' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
'Boolean' => CRM_Utils_Type::T_BOOLEAN,
'StateProvince' => CRM_Utils_Type::T_INT,
'File' => CRM_Utils_Type::T_STRING,
* @return bool|string
* fieldName or FALSE if the field does not exist
*/
-function _civicrm_api3_api_resolve_alias($entity, $fieldName) {
+function _civicrm_api3_api_resolve_alias($entity, $fieldName, $action = 'create') {
if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
return $fieldName;
}
- if ($fieldName == "{$entity}_id") {
+ if ($fieldName == _civicrm_api_get_entity_name_from_camel($entity) . '_id') {
return 'id';
}
$result = civicrm_api($entity, 'getfields', array(
'version' => 3,
- 'action' => 'create',
+ 'action' => $action,
));
$meta = $result['values'];
if (!isset($meta[$fieldName]['name']) && isset($meta[$fieldName . '_id'])) {
return $info['name'];
}
}
+ // Create didn't work, try with get
+ if ($action == 'create') {
+ return _civicrm_api3_api_resolve_alias($entity, $fieldName, 'get');
+ }
return FALSE;
}
*/
function _civicrm_api3_deprecation_check($entity, $result = array()) {
if ($entity) {
- $apiFile = 'api/v3/' . _civicrm_api_get_camel_name($entity) . '.php';
+ $apiFile = "api/v3/$entity.php";
if (CRM_Utils_File::isIncludable($apiFile)) {
require_once $apiFile;
}
- $entity = _civicrm_api_get_entity_name_from_camel($entity);
- $fnName = "_civicrm_api3_{$entity}_deprecation";
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
+ $fnName = "_civicrm_api3_{$lowercase_entity}_deprecation";
if (function_exists($fnName)) {
return $fnName($result);
}