From f4b04d2ccd4ad3818f9a4c6ff02813bf20a0ce88 Mon Sep 17 00:00:00 2001 From: Johan Vervloet Date: Thu, 5 Mar 2015 16:11:22 +0100 Subject: [PATCH] Sort and limit options. --- api/v3/utils.php | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/api/v3/utils.php b/api/v3/utils.php index dc2b7f8b50..595031f36f 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -471,7 +471,6 @@ function _civicrm_api3_store_values(&$fields, &$params, &$values) { * @return array */ function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { - // TODO: count() query $dao = new $dao_name(); $entity = _civicrm_api_get_entity_name_from_dao($dao); $custom_fields = _civicrm_api3_custom_fields_for_entity($entity); @@ -561,6 +560,7 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { $select = "SELECT 1"; $from = "FROM " . $dao->tableName() . " a"; $where = "WHERE 1=1"; + $misc = ""; $query_params = array(); foreach ($select_fields as $column => $alias) { @@ -584,9 +584,33 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { } }; - // TODO: limit, sort - $query = "$select $from $where"; + // order by + if (!empty($options['sort'])) { + $sort_fiels=array(); + foreach (explode(',', $options['sort']) as $sort_option) { + $words = preg_split("/[\s]+/", $sort_option); + if (count($words) > 0 && in_array($words[0], array_values($select_fields))) { + $tmp = $words[0]; + if (strtoupper($words[1]) == 'DESC') { + $tmp .= " DESC"; + } + $sort_fields[] = $tmp; + } + } + if (count($sort_fields) > 0) { + $misc .= " ORDER BY ".implode(",", $sort_fields); + } + } + + // limit + if (!empty($options['limit'])) { + ++$param_nr; + $misc .= " LIMIT %$param_nr"; + $query_params[$param_nr] = array($options['limit'], 'Integer'); + } + + $query = "$select $from $where $misc"; $result_entities = array(); -- 2.25.1