From 8fc8eec672cd8a35964fc347a14bf3ad7c11da40 Mon Sep 17 00:00:00 2001 From: Andrew O'Neil Date: Thu, 13 Dec 2007 21:50:22 +0000 Subject: [PATCH] Merged revisions 46591 via svnmerge from svn://svn.silverstripe.com/silverstripe/modules/sapphire/branches/2.2.0-privacy ........ r46591 | aoneil | 2007-12-11 14:29:01 +1300 (Tue, 11 Dec 2007) | 1 line Highlighting of search results ........ git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@46780 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/fieldtypes/Text.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/model/fieldtypes/Text.php b/core/model/fieldtypes/Text.php index b04181cc2..3d671ac93 100644 --- a/core/model/fieldtypes/Text.php +++ b/core/model/fieldtypes/Text.php @@ -212,14 +212,14 @@ class Text extends DBField { } } - function ContextSummary($characters = 500, $string = false, $html = true) { + function ContextSummary($characters = 500, $string = false, $striphtml = true, $highlight = true) { if(!$string) { // If no string is supplied, use the string from a SearchForm $string = $_REQUEST['Search']; } // Remove HTML tags so we don't have to deal with matching tags - $text = $html ? $this->NoHTML() : $this->value; + $text = $striphtml ? $this->NoHTML() : $this->value; // Find the search string $position = (int) stripos($text, $string); @@ -227,14 +227,22 @@ class Text extends DBField { // We want to search string to be in the middle of our block to give it some context $position = max(0, $position - ($characters / 2)); + if($position > 0) { // We don't want to start mid-word - $position = (int) strrpos($text, ' ', -$position); + $position = max((int) strrpos(substr($text, 0, $position), ' '), (int) strrpos(substr($text, 0, $position), "\n")); } $summary = substr($text, $position, $characters); - return $summary; + if($highlight) { + // Add a span around all occurences of the search term + $summary = str_ireplace($string, "$string", $summary); + } + + // trim it, because if we counted back and found a space then there will be an extra + // space at the front + return trim($summary); } /**