From 6fa8a394bc4e54f16935e0f67cc53801f085d0e6 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 7 Jun 2013 13:58:16 -0700 Subject: [PATCH] Fix API option matching and validation CRM-12464 ---------------------------------------- * CRM-12464: Add PseudoConstants to Schema Metadata http://issues.civicrm.org/jira/browse/CRM-12464 --- api/v3/utils.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/api/v3/utils.php b/api/v3/utils.php index d7cfd2c592..fefb043a4a 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1448,11 +1448,16 @@ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $ent $session = &CRM_Core_Session::singleton(); $params[$fieldName] = $session->get('userID'); } - if (!empty($fieldInfo['pseudoconstant'])) { - _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, 'integer'); + if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { + _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, $fieldInfo); } - // once we have done any swaps check our field length + // After swapping options, ensure we have an integer + if (!is_numeric($params[$fieldName]) || (int) $params[$fieldName] != $params[$fieldName]) { + throw new API_Exception("$fieldname is not a valid integer", 2001, array('error_field' => $fieldname,"type" => "integer")); + } + + // Check our field length if(is_string($params[$fieldName]) && CRM_Utils_Array::value('maxlength',$fieldInfo) && strlen($params[$fieldName]) > $fieldInfo['maxlength'] @@ -1498,8 +1503,8 @@ function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $enti throw new Exception("Currency not a valid code: $value"); } } - if (!empty($fieldInfo['pseudoconstant'])) { - _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, 'string'); + if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { + _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, $fieldInfo); } // Check our field length elseif (is_string($value) && !empty($fieldInfo['maxlength']) && strlen($value) > $fieldInfo['maxlength']) { @@ -1515,12 +1520,15 @@ function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $enti * * @param $params: api parameters * @param $entity: api entity name - * @param $fieldName: entity field name - * @param $type: field type from metadata + * @param $fieldName: field name used in api call (not necessarily the canonical name) + * @param $fieldInfo: getfields meta-data */ -function _civicrm_api3_api_match_pseudoconstant(&$params, $entity, $fieldName, $type) { - $options = civicrm_api($entity, 'getoptions', array('version' => 3, 'field' => $fieldName)); - $options = CRM_Utils_Array::value('values', $options, array()); +function _civicrm_api3_api_match_pseudoconstant(&$params, $entity, $fieldName, $fieldInfo) { + $options = CRM_Utils_Array::value('options', $fieldInfo); + if (!$options) { + $options = civicrm_api($entity, 'getoptions', array('version' => 3, 'field' => $fieldInfo['name'])); + $options = CRM_Utils_Array::value('values', $options, array()); + } // If passed a value-seperated string, explode to an array, then re-implode after matching values $implode = FALSE; @@ -1550,7 +1558,7 @@ function _civicrm_api3_api_match_pseudoconstant(&$params, $entity, $fieldName, $ * * @param $value: field value * @param $options: array of options for this field - * @param $fieldName: entity field name + * @param $fieldName: field name used in api call (not necessarily the canonical name) */ function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldName) { // If option is a key, no need to translate -- 2.25.1