X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=api%2Fv3%2Futils.php;h=a9f0762464574c4725bc39a2ed55b0e7378a0e49;hb=7fbb4198d03adaf0b536941641b438103998efa3;hp=c680e00b8e4e208ef25674c7de450a92234201df;hpb=46b6363ca84aff553c548c77d5a0ea903b5b0a08;p=civicrm-core.git diff --git a/api/v3/utils.php b/api/v3/utils.php index c680e00b8e..a9f0762464 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1,6 +1,4 @@ selectAdd(); $options['return']['id'] = TRUE;// ensure 'id' is included $allfields = _civicrm_api3_get_unique_name_array($dao); @@ -593,6 +596,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 +611,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 +647,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 +686,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 +737,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 +937,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); @@ -1008,7 +1024,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 +1036,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'); } /** @@ -1470,14 +1486,15 @@ function _civicrm_api3_swap_out_aliases(&$apiRequest) { */ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $entity) { //if fieldname exists in params - if (CRM_Utils_Array::value($fieldname, $params)) { + if (CRM_Utils_Array::value($fieldName, $params)) { // if value = 'user_contact_id' (or similar), replace value with contact id - if (!is_integer($params[$fieldname])) { - $realContactId = _civicrm_api3_resolve_contactID($params[$fieldname]); - if (!is_numeric($realContactId)) { - throw new API_Exception("\"$fieldname\" cannot be resolved to a contact ID", 2002, array('error_field' => $fieldname,"type"=>"integer")); + 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; } - $params[$fieldname] = $realContactId; } if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, $fieldInfo); @@ -1506,7 +1523,7 @@ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $ent * Determine a contact ID using a string expression * * @param string $contactIdExpr e.g. "user_contact_id" or "@user:username" - * @return int|NULL + * @return int|NULL|'unknown-user' */ function _civicrm_api3_resolve_contactID($contactIdExpr) { //if value = 'user_contact_id' replace value with logged in user id @@ -1521,22 +1538,23 @@ function _civicrm_api3_resolve_contactID($contactIdExpr) { $ufID = $config->userSystem->getUfId($matches[1]); if (!$ufID) { - return NULL; + return 'unknown-user'; } $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID); - if (!$ufID) { - return NULL; + if (!$contactID) { + return 'unknown-user'; } return $contactID; } + return NULL; } 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")); } } } @@ -1567,7 +1585,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