aboutsummaryrefslogtreecommitdiff
path: root/mod/search/start.php
diff options
context:
space:
mode:
Diffstat (limited to 'mod/search/start.php')
-rw-r--r--mod/search/start.php43
1 files changed, 29 insertions, 14 deletions
diff --git a/mod/search/start.php b/mod/search/start.php
index 4f57a64c5..8a112a3a3 100644
--- a/mod/search/start.php
+++ b/mod/search/start.php
@@ -14,7 +14,7 @@ function search_init() {
require_once 'search_hooks.php';
// page handler for search actions and results
- register_page_handler('search','search_page_handler');
+ elgg_register_page_handler('search', 'search_page_handler');
// register some default search hooks
elgg_register_plugin_hook_handler('search', 'object', 'search_objects_hook');
@@ -46,16 +46,17 @@ function search_init() {
}
// add in CSS for search elements
- elgg_extend_view('css/screen', 'search/css');
+ elgg_extend_view('css/elgg', 'search/css');
// extend view for elgg topbar search box
- elgg_extend_view('header/extend', 'search/search_box');
+ elgg_extend_view('page/elements/header', 'search/header');
}
/**
* Page handler for search
*
- * @param array $page Page elements from pain page handler
+ * @param array $page Page elements from core page handler
+ * @return bool
*/
function search_page_handler($page) {
@@ -68,26 +69,34 @@ function search_page_handler($page) {
//set_input('search_type', 'tags');
}
- include_once('index.php');
+ $base_dir = elgg_get_plugins_path() . 'search/pages/search';
+
+ include_once("$base_dir/index.php");
+ return true;
}
/**
* 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
* @param int $min_match_context = 30
* @param int $max_length = 300
+ * @param bool $tag_match Search is for tags. Don't ignore words.
* @return string
*/
-function search_get_highlighted_relevant_substrings($haystack, $query, $min_match_context = 30, $max_length = 300) {
+function search_get_highlighted_relevant_substrings($haystack, $query, $min_match_context = 30, $max_length = 300, $tag_match = false) {
$haystack = strip_tags($haystack);
$haystack_length = elgg_strlen($haystack);
$haystack_lc = elgg_strtolower($haystack);
- $words = search_remove_ignored_words($query, 'array');
+ if (!$tag_match) {
+ $words = search_remove_ignored_words($query, 'array');
+ } else {
+ $words = array();
+ }
// if haystack < $max_length return the entire haystack w/formatting immediately
if ($haystack_length <= $max_length) {
@@ -103,6 +112,7 @@ function search_get_highlighted_relevant_substrings($haystack, $query, $min_matc
$word = elgg_strtolower($word);
$count = elgg_substr_count($haystack_lc, $word);
$word_len = elgg_strlen($word);
+ $haystack_len = elgg_strlen($haystack_lc);
// find the start positions for the words
if ($count > 1) {
@@ -113,6 +123,10 @@ function search_get_highlighted_relevant_substrings($haystack, $query, $min_matc
$stop = $pos + $word_len + $min_match_context;
$lengths[] = $stop - $start;
$offset += $pos + $word_len;
+
+ if ($offset >= $haystack_len) {
+ break;
+ }
}
} else {
$pos = elgg_strpos($haystack_lc, $word);
@@ -130,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();
@@ -249,8 +263,8 @@ function search_highlight_words($words, $string) {
$replace_html = array(
'strong' => rand(10000, 99999),
'class' => rand(10000, 99999),
- 'searchMatch' => rand(10000, 99999),
- 'searchMatchColor' => rand(10000, 99999)
+ 'search-highlight' => rand(10000, 99999),
+ 'search-highlight-color' => rand(10000, 99999)
);
foreach ($words as $word) {
@@ -262,16 +276,17 @@ function search_highlight_words($words, $string) {
$search = "/($word)/i";
+ // @todo
// must replace with placeholders in case one of the search terms is
// in the html string.
// later, will replace the placeholders with the actual html.
// Yeah this is hacky. I'm tired.
$strong = $replace_html['strong'];
$class = $replace_html['class'];
- $searchMatch = $replace_html['searchMatch'];
- $searchMatchColor = $replace_html['searchMatchColor'];
+ $highlight = $replace_html['search-highlight'];
+ $color = $replace_html['search-highlight-color'];
- $replace = "<$strong $class=\"$searchMatch $searchMatchColor{$i}\">$1</$strong>";
+ $replace = "<$strong $class=\"$highlight $color{$i}\">$1</$strong>";
$string = preg_replace($search, $replace, $string);
$i++;
}