X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=api%2Fv3%2Futils.php;h=14f6daa352ffbea6d20a59df98bc6410040b507a;hb=d75241a408e82529568c540996dc3e35b7207288;hp=f0e714b7e0d8a708d6e7b71d29d1a3705ada201c;hpb=52910807c090423707113d77d4e38ef1d606ad6a;p=civicrm-core.git diff --git a/api/v3/utils.php b/api/v3/utils.php index f0e714b7e0..14f6daa352 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1,9 +1,7 @@ $criteria) { - if (in_array($operator, $acceptedSQLOperators)) { - switch ($operator) { - // unary operators - - case 'IS NULL': - case 'IS NOT NULL': - $dao->whereAdd(sprintf('%s %s', $fieldName, $operator)); - break; - - // ternary operators - - case 'BETWEEN': - case 'NOT BETWEEN': - if (empty($criteria[0]) || empty($criteria[1])) { - throw new exception("invalid criteria for $operator"); - } - $dao->whereAdd(sprintf('%s ' . $operator . ' "%s" AND "%s"', $fieldName, CRM_Core_DAO::escapeString($criteria[0]), CRM_Core_DAO::escapeString($criteria[1]))); - break; - - // n-ary operators - - case 'IN': - case 'NOT IN': - if (empty($criteria)) { - throw new exception("invalid criteria for $operator"); - } - $escapedCriteria = array_map(array('CRM_Core_DAO', 'escapeString'), $criteria); - $dao->whereAdd(sprintf('%s %s ("%s")', $fieldName, $operator, implode('", "', $escapedCriteria))); - break; - - // binary operators - - default: - - $dao->whereAdd(sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO::escapeString($criteria))); - } - } + $where = CRM_Core_DAO::createSqlFilter($fieldName, $params[$field], 'String'); + if(!empty($where)) { + $dao->whereAdd($where); } } else { @@ -535,7 +507,7 @@ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) { } } } - if (!empty($options['return']) && is_array($options['return'])) { + if (!empty($options['return']) && is_array($options['return']) && empty($options['is_count'])) { $dao->selectAdd(); $options['return']['id'] = TRUE;// ensure 'id' is included $allfields = _civicrm_api3_get_unique_name_array($dao); @@ -593,6 +565,7 @@ function _civicrm_api3_apply_filters_to_dao($filterField, $filterValue, &$dao) { * @return array $options options extracted from params */ function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $entity = '', $action = '') { + $is_count = FALSE; $sort = CRM_Utils_Array::value('sort', $params, 0); $sort = CRM_Utils_Array::value('option.sort', $params, $sort); $sort = CRM_Utils_Array::value('option_sort', $params, $sort); @@ -607,6 +580,8 @@ function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $ $limit = CRM_Utils_Array::value('option_limit', $params, $limit); if (is_array(CRM_Utils_Array::value('options', $params))) { + // is count is set by generic getcount not user + $is_count = CRM_Utils_Array::value('is_count', $params['options']); $offset = CRM_Utils_Array::value('offset', $params['options'], $offset); $limit = CRM_Utils_Array::value('limit', $params['options'], $limit); $sort = CRM_Utils_Array::value('sort', $params['options'], $sort); @@ -641,8 +616,10 @@ function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $ 'offset' => $offset, 'sort' => $sort, 'limit' => $limit, + 'is_count' => $is_count, 'return' => !empty($returnProperties) ? $returnProperties : NULL, ); + if (!$queryObject) { return $options; } @@ -678,9 +655,11 @@ function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $ function _civicrm_api3_apply_options_to_dao(&$params, &$dao, $entity) { $options = _civicrm_api3_get_options_from_params($params,FALSE,$entity); - $dao->limit((int)$options['offset'], (int)$options['limit']); - if (!empty($options['sort'])) { - $dao->orderBy($options['sort']); + if(!$options['is_count']) { + $dao->limit((int)$options['offset'], (int)$options['limit']); + if (!empty($options['sort'])) { + $dao->orderBy($options['sort']); + } } } @@ -727,10 +706,16 @@ function _civicrm_api3_get_unique_name_array(&$bao) { */ function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE, $entity = "") { $result = array(); + if(isset($params['options']) && CRM_Utils_Array::value('is_count', $params['options'])) { + return $dao->count(); + } if (empty($dao) || !$dao->find()) { return array(); } + if(isset($dao->count)) { + return $dao->count; + } //if custom fields are required we will endeavour to set them . NB passing $entity in might be a bit clunky / unrequired if (!empty($entity) && CRM_Utils_Array::value('return', $params) && is_array($params['return'])) { foreach ($params['return'] as $return) { @@ -921,7 +906,7 @@ function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $ $bao = new $bao_name(); _civicrm_api3_dao_set_filter($bao, $params, TRUE,$entity); if ($returnAsSuccess) { - return civicrm_api3_create_success(_civicrm_api3_dao_to_array($bao, $params, FALSE, $entity), $params, $entity); + return civicrm_api3_create_success(_civicrm_api3_dao_to_array($bao, $params, FALSE, $entity), $params, $entity); } else { return _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity); @@ -963,7 +948,7 @@ function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) { else { $values = array(); _civicrm_api3_object_to_array($bao, $values[$bao->id]); - return civicrm_api3_create_success($values, $params, NULL, 'create', $bao); + return civicrm_api3_create_success($values, $params, $entity, 'create', $bao); } } @@ -1008,7 +993,7 @@ function _civicrm_api3_basic_delete($bao_name, &$params) { if ($bao !== FALSE) { return civicrm_api3_create_success(TRUE); } - return civicrm_api3_create_error('Could not delete entity id ' . $params['id']); + throw new API_Exception('Could not delete entity id ' . $params['id']); } elseif (method_exists($bao_name, 'delete')) { $dao = new $bao_name(); @@ -1020,11 +1005,11 @@ function _civicrm_api3_basic_delete($bao_name, &$params) { } } else { - return civicrm_api3_create_error('Could not delete entity id ' . $params['id']); + throw new API_Exception('Could not delete entity id ' . $params['id']); } } - return civicrm_api3_create_error('no delete method found'); + throw new API_Exception('no delete method found'); } /** @@ -1108,7 +1093,7 @@ function _civicrm_api3_validate_fields($entity, $action, &$params, $errorMode = break; case CRM_Utils_Type::T_MONEY: - if (!CRM_Utils_Rule::money($params[$fieldName])) { + if (!CRM_Utils_Rule::money($params[$fieldName]) && !empty($params[$fieldName])) { throw new Exception($fieldName . " is not a valid amount: " . $params[$fieldName]); } } @@ -1350,6 +1335,7 @@ function _civicrm_api_get_custom_fields($entity, &$params) { foreach ($customfields as $key => $value) { // Regular fields have a 'name' property $value['name'] = 'custom_' . $key; + $value['type'] = _getStandardTypeFromCustomDataType($value['data_type']); $customfields['custom_' . $key] = $value; if (in_array('custom_' . $key, $getoptions)) { $customfields['custom_' . $key]['options'] = CRM_Core_BAO_CustomOption::valuesByID($key); @@ -1358,17 +1344,35 @@ function _civicrm_api_get_custom_fields($entity, &$params) { } return $customfields; } - +/** + * Translate the custom field data_type attribute into a std 'type' + */ +function _getStandardTypeFromCustomDataType($dataType) { + $mapping = array( + 'String' => CRM_Utils_Type::T_STRING, + 'Int' => CRM_Utils_Type::T_INT, + 'Money' => CRM_Utils_Type::T_MONEY, + 'Memo' => CRM_Utils_Type::T_LONGTEXT, + 'Float' => CRM_Utils_Type::T_FLOAT, + 'Date' => CRM_Utils_Type::T_DATE, + 'Boolean' => CRM_Utils_Type::T_BOOLEAN, + 'StateProvince' => CRM_Utils_Type::T_INT, + 'File' => CRM_Utils_Type::T_STRING, + 'Link' => CRM_Utils_Type::T_STRING, + 'ContactReference' => CRM_Utils_Type::T_INT, + 'Country' => CRM_Utils_Type::T_INT, + ); + return $mapping[$dataType]; +} /** * Return array of defaults for the given API (function is a wrapper on getfields) */ function _civicrm_api3_getdefaults($apiRequest) { $defaults = array(); - $result = civicrm_api($apiRequest['entity'], + $result = civicrm_api3($apiRequest['entity'], 'getfields', array( - 'version' => 3, 'action' => $apiRequest['action'], ) ); @@ -1416,10 +1420,9 @@ function _civicrm_api3_swap_out_aliases(&$apiRequest) { } return; } - $result = civicrm_api($apiRequest['entity'], + $result = civicrm_api3($apiRequest['entity'], 'getfields', array( - 'version' => 3, 'action' => $apiRequest['action'], ) ); @@ -1538,7 +1541,7 @@ function _civicrm_api3_resolve_contactID($contactIdExpr) { function _civicrm_api3_validate_html(&$params, &$fieldName, &$fieldInfo) { if ($value = CRM_Utils_Array::value($fieldName, $params)) { if (!CRM_Utils_Rule::xssString($value)) { - throw new API_Exception('Illegal characters in input (potential scripting attack)',array("field"=>$fieldName,"error_code"=>"xss")); + throw new API_Exception('Illegal characters in input (potential scripting attack)', array("field"=>$fieldName,"error_code"=>"xss")); } } } @@ -1569,7 +1572,7 @@ function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $enti throw new Exception("Currency not a valid code: $value"); } } - if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { + if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options']) || !empty($fieldInfo['enumValues'])) { _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, $fieldInfo); } // Check our field length