Esempio n. 1
0
/**
 * Validate fields being passed into API.
 *
 * This function relies on the getFields function working accurately
 * for the given API. If error mode is set to TRUE then it will also check
 * foreign keys
 *
 * As of writing only date was implemented.
 *
 * @param string $entity
 * @param string $action
 * @param array $params
 *   -.
 * @param array $fields
 *   Response from getfields all variables are the same as per civicrm_api.
 * @param bool $errorMode
 *   ErrorMode do intensive post fail checks?.
 *
 * @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:
                //field is of type integer
                _civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
                break;
            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
                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:
                //blob
                _civicrm_api3_validate_html($params, $fieldName, $fieldInfo);
                break;
            case CRM_Utils_Type::T_STRING:
                _civicrm_api3_validate_string($params, $fieldName, $fieldInfo, $entity);
                break;
            case CRM_Utils_Type::T_MONEY:
                list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
                if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
                    break;
                }
                foreach ((array) $fieldValue as $fieldvalue) {
                    if (!CRM_Utils_Rule::money($fieldvalue) && !empty($fieldvalue)) {
                        throw new Exception($fieldName . " is  not a valid amount: " . $params[$fieldName]);
                    }
                }
                break;
        }
        // intensive checks - usually only called after DB level fail
        if (!empty($errorMode) && strtolower($action) == 'create') {
            if (!empty($fieldInfo['FKClassName'])) {
                if (!empty($fieldValue)) {
                    _civicrm_api3_validate_constraint($params, $fieldName, $fieldInfo);
                } elseif (!empty($fieldInfo['required'])) {
                    throw new Exception("DB Constraint Violation - possibly {$fieldName} should possibly be marked as mandatory for this API. If so, please raise a bug report");
                }
            }
            if (!empty($fieldInfo['api.unique'])) {
                $params['entity'] = $entity;
                _civicrm_api3_validate_unique_key($params, $fieldName);
            }
        }
    }
}
Esempio n. 2
0
/**
 * Validate foreign key values of fields being passed into API.
 *
 * This function relies on the getFields function working accurately
 * for the given API.
 *
 * @param string $entity
 * @param string $action
 * @param array $params
 *
 * @param array $fields
 *   Response from getfields all variables are the same as per civicrm_api.
 *
 * @throws Exception
 */
function _civicrm_api3_validate_foreign_keys($entity, $action, &$params, $fields)
{
    // intensive checks - usually only called after DB level fail
    foreach ($fields as $fieldName => $fieldInfo) {
        if (!empty($fieldInfo['FKClassName'])) {
            if (!empty($params[$fieldName])) {
                _civicrm_api3_validate_constraint($params[$fieldName], $fieldName, $fieldInfo);
            } elseif (!empty($fieldInfo['required'])) {
                throw new Exception("DB Constraint Violation - possibly {$fieldName} should possibly be marked as mandatory for this API. If so, please raise a bug report.");
            }
        }
        if (!empty($fieldInfo['api.unique'])) {
            $params['entity'] = $entity;
            _civicrm_api3_validate_unique_key($params, $fieldName);
        }
    }
}