aboutsummaryrefslogtreecommitdiff
path: root/mod/search
diff options
context:
space:
mode:
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-01-14 15:40:22 +0000
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-01-14 15:40:22 +0000
commitde0963eafb5143fdf44dbac17fa115deacc46453 (patch)
tree78eb372f48570c9925aec8dcf145fa9a892d3f2f /mod/search
parent9c0a913909e0b560ce5e2723098e233601e86903 (diff)
downloadelgg-de0963eafb5143fdf44dbac17fa115deacc46453.tar.gz
elgg-de0963eafb5143fdf44dbac17fa115deacc46453.tar.bz2
Fixed an incorrect var name that caused a bug when hooking against a type but not a subtype.
Added first pass at results sorting. git-svn-id: http://code.elgg.org/elgg/trunk@3808 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'mod/search')
-rw-r--r--mod/search/index.php30
-rw-r--r--mod/search/search_hooks.php3
-rw-r--r--mod/search/start.php51
3 files changed, 77 insertions, 7 deletions
diff --git a/mod/search/index.php b/mod/search/index.php
index e92eee312..bbdad89e6 100644
--- a/mod/search/index.php
+++ b/mod/search/index.php
@@ -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().
diff --git a/mod/search/search_hooks.php b/mod/search/search_hooks.php
index e56056701..b66a17467 100644
--- a/mod/search/search_hooks.php
+++ b/mod/search/search_hooks.php
@@ -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;
diff --git a/mod/search/start.php b/mod/search/start.php
index 8e84a4144..4b3268f6e 100644
--- a/mod/search/start.php
+++ b/mod/search/start.php
@@ -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