diff options
Diffstat (limited to 'mod/search')
-rw-r--r-- | mod/search/search_hooks.php | 59 | ||||
-rw-r--r-- | mod/search/start.php | 6 | ||||
-rw-r--r-- | mod/search/views/default/search/no_results.php | 2 |
3 files changed, 48 insertions, 19 deletions
diff --git a/mod/search/search_hooks.php b/mod/search/search_hooks.php index 2143a0d24..62058abf1 100644 --- a/mod/search/search_hooks.php +++ b/mod/search/search_hooks.php @@ -122,24 +122,35 @@ function search_users_hook($hook, $type, $value, $params) { $query = sanitise_string($params['query']); - $join = "JOIN {$db_prefix}users_entity ue ON e.guid = ue.guid"; - $params['joins'] = array($join); - -// $where = "(ue.guid = e.guid -// AND (ue.username LIKE '%$query%' -// OR ue.name LIKE '%$query%' -// ) -// )"; - + $params['joins'] = array( + "JOIN {$db_prefix}users_entity ue ON e.guid = ue.guid", + "JOIN {$db_prefix}metadata md on e.guid = md.entity_guid", + "JOIN {$db_prefix}metastrings msv ON n_table.value_id = msv.id" + ); + + // username and display name $fields = array('username', 'name'); $where = search_get_where_sql('ue', $fields, $params, FALSE); + + // profile fields + $profile_fields = array_keys(elgg_get_config('profile_fields')); - $params['wheres'] = array($where); + // get the where clauses for the md names + // can't use egef_metadata() because the n_table join comes too late. + $clauses = elgg_entities_get_metastrings_options('metadata', array( + 'metadata_names' => $profile_fields, + )); + + $params['joins'] = array_merge($clauses['joins'], $params['joins']); + // no fulltext index, can't disable fulltext search in this function. + // $md_where .= " AND " . search_get_where_sql('msv', array('string'), $params, FALSE); + $md_where = "(({$clauses['wheres'][0]}) AND msv.string LIKE '%$query%')"; + + $params['wheres'] = array("(($where) OR ($md_where))"); // override subtype -- All users should be returned regardless of subtype. $params['subtype'] = ELGG_ENTITIES_ANY_VALUE; - - $params['count'] = TRUE; + $params['count'] = true; $count = elgg_get_entities($params); // no need to continue if nothing here. @@ -152,11 +163,27 @@ function search_users_hook($hook, $type, $value, $params) { // add the volatile data for why these entities have been returned. foreach ($entities as $entity) { - $username = search_get_highlighted_relevant_substrings($entity->username, $query); - $entity->setVolatileData('search_matched_title', $username); + + $title = search_get_highlighted_relevant_substrings($entity->name, $query); - $name = search_get_highlighted_relevant_substrings($entity->name, $query); - $entity->setVolatileData('search_matched_description', $name); + // include the username if it matches but the display name doesn't. + if (false !== strpos($entity->username, $query)) { + $username = search_get_highlighted_relevant_substrings($entity->username, $query); + $title .= " ($username)"; + } + + $entity->setVolatileData('search_matched_title', $title); + + $matched = ''; + foreach ($profile_fields as $md) { + $text = $entity->$md; + if (stristr($text, $query)) { + $matched .= elgg_echo("profile:{$md}") . ': ' + . search_get_highlighted_relevant_substrings($text, $query); + } + } + + $entity->setVolatileData('search_matched_description', $matched); } return array( diff --git a/mod/search/start.php b/mod/search/start.php index d2d7ed3c2..8a112a3a3 100644 --- a/mod/search/start.php +++ b/mod/search/start.php @@ -77,7 +77,7 @@ function search_page_handler($page) { /** * Return a string with highlighted matched queries and relevant context - * Determins context based upon occurance and distance of words with each other. + * Determines context based upon occurance and distance of words with each other. * * @param string $haystack * @param string $query @@ -94,6 +94,8 @@ function search_get_highlighted_relevant_substrings($haystack, $query, $min_matc if (!$tag_match) { $words = search_remove_ignored_words($query, 'array'); + } else { + $words = array(); } // if haystack < $max_length return the entire haystack w/formatting immediately @@ -142,7 +144,7 @@ function search_get_highlighted_relevant_substrings($haystack, $query, $min_matc $total_length = array_sum($offsets); $add_length = 0; - if ($total_length < $max_length) { + if ($total_length < $max_length && $offsets) { $add_length = floor((($max_length - $total_length) / count($offsets)) / 2); $starts = array(); diff --git a/mod/search/views/default/search/no_results.php b/mod/search/views/default/search/no_results.php index 74b5b2cfa..0e9a5e295 100644 --- a/mod/search/views/default/search/no_results.php +++ b/mod/search/views/default/search/no_results.php @@ -3,4 +3,4 @@ * No results from search */ -echo autop(elgg_echo('search:no_results')); +echo elgg_autop(elgg_echo('search:no_results')); |