Without this patch contribution.getcount api does a full query with the full set of select fields and
the nasty joins those entail and only after doing that does it filter for count.
This patch re-uses the code in use for contact api query for contribution getcount
// we add distinct to get the right count for components
// for the more complex result set, we use GROUP BY the same id
// CRM-9630
- $select = "SELECT count( DISTINCT {$this->_distinctComponentClause} )";
+ $select = "SELECT count( DISTINCT {$this->_distinctComponentClause} ) as rowCount";
}
else {
$select = 'SELECT count(DISTINCT contact_a.id) as rowCount';
* Return count obnly.
* @param bool $skipPermissions
* Should permissions be ignored or should the logged in user's permissions be applied.
- *
+ * @param int $mode
+ * This basically correlates to the component.
*
* @return array
*/
$row_count = 25,
$smartGroupCache = TRUE,
$count = FALSE,
- $skipPermissions = TRUE
+ $skipPermissions = TRUE,
+ $mode = 1
) {
$query = new CRM_Contact_BAO_Query(
$params, $returnProperties,
- NULL, TRUE, FALSE, 1,
+ NULL, TRUE, FALSE, $mode,
$skipPermissions,
TRUE, $smartGroupCache
);
}
if ($row_count > 0 && $offset >= 0) {
$offset = CRM_Utils_Type::escape($offset, 'Int');
- $rowCount = CRM_Utils_Type::escape($row_count, 'Int');
+ $row_count = CRM_Utils_Type::escape($row_count, 'Int');
$sql .= " LIMIT $offset, $row_count ";
}
$dao = CRM_Core_DAO::executeQuery($sql);
+ // @todo derive this from the component class rather than hard-code two options.
+ $entityIDField = ($mode == CRM_Contact_BAO_Query::MODE_CONTRIBUTE) ? 'contribution_id' : 'contact_id';
+
$values = array();
while ($dao->fetch()) {
if ($count) {
if (!empty($convertedVals)) {
$val = array_replace_recursive($val, $convertedVals);
}
- $values[$dao->contact_id] = $val;
+ $values[$dao->$entityIDField] = $val;
}
$dao->free();
return array($values, $options);
* @param CRM_Contact_BAO_Query $query
*/
public static function initializeAnySoftCreditClause(&$query) {
- if (self::isSoftCreditOptionEnabled($query->_params)) {
- if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
+
+ if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
+ if (self::isSoftCreditOptionEnabled($query->_params)) {
unset($query->_distinctComponentClause);
$query->_rowCountClause = " count(civicrm_contribution.id)";
$query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
}
+ else {
+ $query->_distinctComponentClause = ' civicrm_contribution.id';
+ }
}
}
// format soft credit for backward compatibility
_civicrm_api3_format_soft_credit($contribution[$dao->contribution_id]);
}
- return civicrm_api3_create_success($contribution, $params, 'Contribution', 'get', $dao);
+ return civicrm_api3_create_success($contribution, $params, 'Contribution', 'get');
+}
+
+/**
+ * Get number of contacts matching the supplied criteria.
+ *
+ * @param array $params
+ *
+ * @return int
+ */
+function civicrm_api3_contribution_getcount($params) {
+ $count = _civicrm_api3_get_using_query_object('Contribution', $params, array(), TRUE, CRM_Contact_BAO_Query::MODE_CONTRIBUTE);
+ return (int) $count;
}
/**
* Array of options (so we can modify the filter).
* @param bool $getCount
* Are we just after the count.
+ * @param int $mode
+ * This basically correlates to the component.
*
* @return array
*/
-function _civicrm_api3_get_using_query_object($entity, $params, $additional_options = array(), $getCount = NULL) {
+function _civicrm_api3_get_using_query_object($entity, $params, $additional_options = array(), $getCount = NULL, $mode = 1, $defaultReturnProperties = NULL) {
$lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
// Convert id to e.g. contact_id
if (empty($params[$lowercase_entity . '_id']) && isset($params['id'])) {
CRM_Utils_Array::value('return', $additional_options, array())
);
if (empty($returnProperties)) {
- $returnProperties = NULL;
+ $returnProperties = $defaultReturnProperties;
}
if (!empty($params['check_permissions'])) {
// we will filter query object against getfields
$skipPermissions = !empty($params['check_permissions']) ? 0 : 1;
- list($entities, $options) = CRM_Contact_BAO_Query::apiQuery(
+ list($entities) = CRM_Contact_BAO_Query::apiQuery(
$newParams,
$returnProperties,
NULL,
$limit,
$smartGroupCache,
$getCount,
- $skipPermissions
+ $skipPermissions,
+ $mode
);
if ($getCount) {
// only return the count of contacts