ENHANCEMENT Add a <span class="highlight"> around all keywords (space delimited) and not just the entire search phrase

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@69370 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2008-12-17 06:05:24 +00:00 committed by Sam Minnee
parent 42520dff8f
commit ae2cb53fb1

View File

@ -250,9 +250,6 @@ class Text extends DBField {
* Perform context searching to give some context to searches, optionally
* highlighting the search term.
*
* @todo Would be useful for this to highlight each individual search keyword
* instead of the entire search query.
*
* @param int $characters Number of characters in the summary
* @param boolean $string Supplied string ("keywords")
* @param boolean $striphtml Strip HTML?
@ -260,11 +257,8 @@ class Text extends DBField {
* @return string
*/
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'];
}
if(!$string) $string = $_REQUEST['Search']; // Use the default "Search" request variable (from SearchForm)
// Remove HTML tags so we don't have to deal with matching tags
$text = $striphtml ? $this->NoHTML() : $this->value;
@ -281,13 +275,20 @@ class Text extends DBField {
$summary = substr($text, $position, $characters);
$stringPieces = explode(' ', $string);
if($highlight) {
// Add a span around all occurences of the search term
$summary = str_ireplace($string, "<span class=\"highlight\">$string</span>", $summary);
// Add a span around all key words from the search term as well
if($stringPieces) {
foreach($stringPieces as $stringPiece) {
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</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);
}