示例#1
0
/**
 * Validate integer fields being passed into API.
 *
 * It currently converts the incoming value 'user_contact_id' into the id of the currently logged in user.
 *
 * @param array $params
 *   Params from civicrm_api.
 * @param string $fieldName
 *   Uniquename of field being checked.
 * @param array $fieldInfo
 *   Array of fields from getfields function.
 * @param string $entity
 *
 * @throws API_Exception
 */
function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $entity)
{
    list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
    if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
        return;
    }
    if (!empty($fieldValue)) {
        // if value = 'user_contact_id' (or similar), replace value with contact id
        if (!is_numeric($fieldValue) && is_scalar($fieldValue)) {
            $realContactId = _civicrm_api3_resolve_contactID($fieldValue);
            if ('unknown-user' === $realContactId) {
                throw new API_Exception("\"{$fieldName}\" \"{$fieldValue}\" cannot be resolved to a contact ID", 2002, array('error_field' => $fieldName, "type" => "integer"));
            } elseif (is_numeric($realContactId)) {
                $fieldValue = $realContactId;
            }
        }
        if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
            _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo);
        }
        // After swapping options, ensure we have an integer(s)
        foreach ((array) $fieldValue as $value) {
            if ($value && !is_numeric($value) && $value !== 'null' && !is_array($value)) {
                throw new API_Exception("{$fieldName} is not a valid integer", 2001, array('error_field' => $fieldName, "type" => "integer"));
            }
        }
        // Check our field length
        if (is_string($fieldValue) && !empty($fieldInfo['maxlength']) && strlen($fieldValue) > $fieldInfo['maxlength']) {
            throw new API_Exception($fieldValue . " is " . strlen($fieldValue) . " characters  - longer than {$fieldName} length" . $fieldInfo['maxlength'] . ' characters', 2100, array('field' => $fieldName, "max_length" => $fieldInfo['maxlength']));
        }
    }
    if (!empty($op)) {
        $params[$fieldName][$op] = $fieldValue;
    } else {
        $params[$fieldName] = $fieldValue;
    }
}
示例#2
0
文件: utils.php 项目: hguru/224Civi
/**
 * Validate integer fields being passed into API.
 * It currently converts the incoming value 'user_contact_id' into the id of the currenty logged in user
 *
 * @param array $params params from civicrm_api
 * @param string $fieldName uniquename of field being checked
 * @param array $fieldInfo array of fields from getfields function
 * @param string $entity
 * @throws API_Exception
 */
function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $entity)
{
    //if fieldname exists in params
    if (CRM_Utils_Array::value($fieldName, $params)) {
        // if value = 'user_contact_id' (or similar), replace value with contact id
        if (!is_numeric($params[$fieldName]) && is_scalar($params[$fieldName])) {
            $realContactId = _civicrm_api3_resolve_contactID($params[$fieldName]);
            if ('unknown-user' === $realContactId) {
                throw new API_Exception("\"{$fieldName}\" \"{$params[$fieldName]}\" cannot be resolved to a contact ID", 2002, array('error_field' => $fieldName, "type" => "integer"));
            } elseif (is_numeric($realContactId)) {
                $params[$fieldName] = $realContactId;
            }
        }
        if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
            _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, $fieldInfo);
        }
        // After swapping options, ensure we have an integer(s)
        foreach ((array) $params[$fieldName] as $value) {
            if ($value && !is_numeric($value) && $value !== 'null' && !is_array($value)) {
                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']) {
            throw new API_Exception($params[$fieldName] . " is " . strlen($params[$fieldName]) . " characters  - longer than {$fieldName} length" . $fieldInfo['maxlength'] . ' characters', 2100, array('field' => $fieldName, "max_length" => $fieldInfo['maxlength']));
        }
    }
}