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
This commit is contained in:
Andrew O'Neil 2007-12-13 21:50:22 +00:00
parent bb5f6593e8
commit 8fc8eec672

View File

@ -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(!$string) {
// If no string is supplied, use the string from a SearchForm // If no string is supplied, use the string from a SearchForm
$string = $_REQUEST['Search']; $string = $_REQUEST['Search'];
} }
// Remove HTML tags so we don't have to deal with matching tags // 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 // Find the search string
$position = (int) stripos($text, $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 // We want to search string to be in the middle of our block to give it some context
$position = max(0, $position - ($characters / 2)); $position = max(0, $position - ($characters / 2));
if($position > 0) { if($position > 0) {
// We don't want to start mid-word // 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); $summary = substr($text, $position, $characters);
return $summary; if($highlight) {
// Add a span around all occurences of the search term
$summary = str_ireplace($string, "<span class=\"highlight\">$string</span>", $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);
} }
/** /**