ENHANCEMENT: added prefix and suffix support to ContextSummary

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@106200 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Will Rossiter 2010-06-04 03:23:19 +00:00 committed by Sam Minnee
parent a50d4fbc10
commit 41ec792cb4
2 changed files with 17 additions and 10 deletions

View File

@ -266,9 +266,12 @@ class Text extends StringField {
* @param boolean $string Supplied string ("keywords") * @param boolean $string Supplied string ("keywords")
* @param boolean $striphtml Strip HTML? * @param boolean $striphtml Strip HTML?
* @param boolean $highlight Add a highlight <span> element around search query? * @param boolean $highlight Add a highlight <span> element around search query?
* @param String prefix text
* @param String suffix
*
* @return string * @return string
*/ */
function ContextSummary($characters = 500, $string = false, $striphtml = true, $highlight = true) { function ContextSummary($characters = 500, $string = false, $striphtml = true, $highlight = true, $prefix = "... ", $suffix = "...") {
if(!$string) $string = $_REQUEST['Search']; // Use the default "Search" request variable (from SearchForm) 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 // Remove HTML tags so we don't have to deal with matching tags
@ -290,12 +293,18 @@ class Text extends StringField {
if($highlight) { if($highlight) {
// Add a span around all key words from the search term as well // Add a span around all key words from the search term as well
if($stringPieces) foreach($stringPieces as $stringPiece) { if($stringPieces) {
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary); foreach($stringPieces as $stringPiece) {
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);
}
} }
} }
$summary = trim($summary);
return trim($summary); if($position > 0) $summary = $prefix . $summary;
if(strlen($this->value) > ($characters + $position)) $summary = $summary . $suffix;
return $summary;
} }
/** /**

View File

@ -123,6 +123,4 @@ class TextTest extends SapphireTest {
$textObj->ContextSummary(50, $testKeywords2) $textObj->ContextSummary(50, $testKeywords2)
); );
} }
} }
?>