mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
a50d4fbc10
commit
41ec792cb4
@ -266,9 +266,12 @@ class Text extends StringField {
|
||||
* @param boolean $string Supplied string ("keywords")
|
||||
* @param boolean $striphtml Strip HTML?
|
||||
* @param boolean $highlight Add a highlight <span> element around search query?
|
||||
* @param String prefix text
|
||||
* @param String suffix
|
||||
*
|
||||
* @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)
|
||||
|
||||
// Remove HTML tags so we don't have to deal with matching tags
|
||||
@ -290,12 +293,18 @@ class Text extends StringField {
|
||||
|
||||
if($highlight) {
|
||||
// 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);
|
||||
if($stringPieces) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -123,6 +123,4 @@ class TextTest extends SapphireTest {
|
||||
$textObj->ContextSummary(50, $testKeywords2)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
Loading…
Reference in New Issue
Block a user