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
@ -279,23 +282,29 @@ class Text extends StringField {
// 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 = max((int) strrpos(substr($text, 0, $position), ' '), (int) strrpos(substr($text, 0, $position), "\n")); $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);
$stringPieces = explode(' ', $string); $stringPieces = explode(' ', $string);
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

@ -122,7 +122,5 @@ class TextTest extends SapphireTest {
'This is <span class="highlight">some</span> <span class="highlight">test</span> text. <span class="highlight">test</span> <span class="highlight">test</span> what if you have', 'This is <span class="highlight">some</span> <span class="highlight">test</span> text. <span class="highlight">test</span> <span class="highlight">test</span> what if you have',
$textObj->ContextSummary(50, $testKeywords2) $textObj->ContextSummary(50, $testKeywords2)
); );
} }
}
}
?>