Sort and limit options.
authorJohan Vervloet <johanv@johanv.org>
Thu, 5 Mar 2015 15:11:22 +0000 (16:11 +0100)
committereileenmcnaugton <eileen@fuzion.co.nz>
Wed, 12 Aug 2015 23:39:28 +0000 (11:39 +1200)
api/v3/utils.php

index dc2b7f8b504d04ec4975c8a3c3a57e8570a2900a..595031f36f28fbe1b3c4de6d5a1b75a6f049cdde 100644 (file)
@@ -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();