]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixed an incorrect var name that caused a bug when hooking against a type but not...
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 14 Jan 2010 15:40:22 +0000 (15:40 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 14 Jan 2010 15:40:22 +0000 (15:40 +0000)
Added first pass at results sorting.

git-svn-id: http://code.elgg.org/elgg/trunk@3808 36083f99-b078-4883-b0ff-0f9b5a30f544

mod/search/index.php
mod/search/search_hooks.php
mod/search/start.php

index e92eee3127c7e00e2c0f9741432d92b4fdc9299a..bbdad89e686f698df92203f00668ac83e1f7be8b 100644 (file)
@@ -22,13 +22,33 @@ $offset = ($search_type == 'all') ? 0 : get_input('offset', 0);
 $entity_type = get_input('entity_type', NULL);
 $entity_subtype = get_input('entity_subtype', NULL);
 $owner_guid = get_input('owner_guid', NULL);
-$friends = (int)get_input('friends', 0);
+$friends = get_input('friends', 0);
+$sort = get_input('sort');
+switch ($sort) {
+       case 'relevance':
+       case 'created':
+       case 'updated':
+       case 'action_on':
+       case 'alpha':
+               break;
+
+       default:
+               $sort = 'relevance';
+               break;
+}
+
+$order = get_input('sort', 'desc');
+if ($order != 'asc' && $order != 'desc') {
+       $order = 'desc';
+}
 
 // set up search params
 $params = array(
        'query' => $query,
        'offset' => $offset,
        'limit' => $limit,
+       'sort' => $sort,
+       'order' => $order,
        'search_type' => $search_type,
        'type' => $entity_type,
        'subtype' => $entity_subtype,
@@ -147,13 +167,13 @@ if ($search_type == 'all' || $search_type == 'entities') {
                                $current_params['subtype'] = $subtype;
                                $current_params['type'] = $type;
 
-                               $entities = trigger_plugin_hook('search', "$type:$subtype", $current_params, NULL);
-                               if ($entities === FALSE) {
+                               $results = trigger_plugin_hook('search', "$type:$subtype", $current_params, NULL);
+                               if ($results === FALSE) {
                                        // someone is saying not to display these types in searches.
                                        continue;
-                               } elseif (is_array($entities) && !count($entities)) {
+                               } elseif (is_array($results) && !count($results)) {
                                        // no results, but results searched in hook.
-                               } elseif (!$entities) {
+                               } elseif (!$results) {
                                        // no results and not hooked.  use default type search.
                                        // don't change the params here, since it's really a different subtype.
                                        // Will be passed to elgg_get_entities().
index e5605670185cddaa9cfec1665d3ceb866f8737c0..b66a1746718cda87efbe9182aafb4aa9ad6cb73e 100644 (file)
@@ -28,8 +28,7 @@ function search_objects_hook($hook, $type, $value, $params) {
 
        $params['wheres'] = array($where);
 
-       //@todo allow sorting by recent time
-       $params['order_by'] = NULL;
+       $params['order_by'] = search_get_order_by_sql('oe', $params['sort'], $params['order']);
 
        $entities = elgg_get_entities($params);
        $params['count'] = TRUE;
index 8e84a4144affa6cad89e0a4f7edbd46be46acd26..4b3268f6ed8f762b80dc7088b36ec26fd268244c 100644 (file)
@@ -424,6 +424,57 @@ function search_get_where_sql($table, $fields, $params, $use_fulltext = TRUE) {
        return $where;
 }
 
+
+/**
+ * Returns ORDER BY sql for insertion into elgg_get_entities().
+ *
+ * @param str $entities_table Prefix for entities table.
+ * @param str $type_table Prefix for the type table.
+ * @param str $sort ORDER BY part
+ * @param str $order ASC or DESC
+ * @return str
+ */
+function search_get_order_by_sql($entities_table, $type_table, $sort, $order) {
+
+       $on = NULL;
+
+       switch ($sort) {
+               default:
+               case 'relevance':
+                       // default is relevance descending.
+                       // acending relevancy is silly and complicated.
+                       $on = '';
+                       break;
+               case 'created':
+                       $on = "$entities_table.time_created";
+                       break;
+               case 'updated':
+                       $on = "$entities_table.time_updated";
+                       break;
+               case 'action_on':
+                       // @todo not supported yet in core
+                       $on = '';
+                       break;
+               case 'alpha':
+                       // @todo not support yet because both title
+                       // and name columns are used for this depending
+                       // on the entity, which we don't always know.  >:O
+                       break;
+       }
+
+       $order = strtolower($order);
+       if ($order != 'asc' && $order != 'desc') {
+               $order = 'DESC';
+       }
+
+       if ($on) {
+               $order_by = "ORDER BY $table.$column $dir";
+       } else {
+               $order_by = '';
+       }
+
+       return $ob;
+}
 /** Register init system event **/
 
 register_elgg_event_handler('init','system','search_init');
\ No newline at end of file