]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixes #2906. Added elgg_sql_reverse_order_by() and wired it up to ege() and ega(...
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sat, 12 Feb 2011 23:08:00 +0000 (23:08 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sat, 12 Feb 2011 23:08:00 +0000 (23:08 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@8184 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/elgglib.php
engine/lib/entities.php

index 12560308ede32846e21ddbf5730cb2503ed0c155..7ebf918b78328545258b275abfe71d78f20647eb 100644 (file)
@@ -1658,6 +1658,30 @@ function css_page_handler($page) {
        echo $return;
 }
 
+/**
+ * Reverses the ordering in an ORDER BY clause.  This is achived by replacing
+ * asc with desc, or appending desc to the end of the clause.
+ *
+ * This is used mostly for elgg_get_entities() and other similar functions.
+ *
+ * @access private
+ * @param string $order_by An order by clause
+ */
+function elgg_sql_reverse_order_by_clause($order_by) {
+       $order_by = strtolower($order_by);
+
+       if (strpos($order_by, ' asc') !== false) {
+               $return = str_replace(' asc', ' desc', $order_by);
+       } elseif (strpos($order_by, ' desc') !== false) {
+               $return = str_replace(' desc', ' asc', $order_by);
+       } else {
+               // no order specified, so default to desc since mysql defaults to asc
+               $return = $order_by . ' desc';
+       }
+
+       return $return;
+}
+
 /**
  * Intercepts the index page when Walled Garden mode is enabled.
  *
index 4a1e496ec84b33a8fb79441f9df77f2c65be12ec..7ab25de88d865b11490d20d1ecce2c0c6f1ef30b 100644 (file)
@@ -708,6 +708,8 @@ function get_entity($guid) {
  *
  *     order_by => NULL (time_created desc)|STR SQL order by clause
  *
+ *  reverse_order_by => BOOL Reverse the default order by clause
+ *
  *     limit => NULL (10)|INT SQL limit clause
  *
  *     offset => NULL (0)|INT SQL offset clause
@@ -755,6 +757,7 @@ function elgg_get_entities(array $options = array()) {
                'created_time_lower'    =>      ELGG_ENTITIES_ANY_VALUE,
                'created_time_upper'    =>      ELGG_ENTITIES_ANY_VALUE,
 
+               'reverse_order_by'              =>      false,
                'order_by'                              =>      'e.time_created desc',
                'group_by'                              =>      ELGG_ENTITIES_ANY_VALUE,
                'limit'                                 =>      10,
@@ -860,6 +863,12 @@ function elgg_get_entities(array $options = array()) {
 
        // Add access controls
        $query .= get_access_sql_suffix('e');
+
+       // reverse order by
+       if ($options['reverse_order_by']) {
+               $options['order_by'] = elgg_sql_reverse_order_by_clause($options['order_by']);
+       }
+
        if (!$options['count']) {
                if ($options['group_by'] = sanitise_string($options['group_by'])) {
                        $query .= " GROUP BY {$options['group_by']}";