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 (from r106200)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112526 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
5fe548f7b5
commit
e36247c1ed
@ -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
|
||||
@ -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
|
||||
$position = max(0, $position - ($characters / 2));
|
||||
|
||||
|
||||
if($position > 0) {
|
||||
// We don't want to start mid-word
|
||||
$position = max((int) strrpos(substr($text, 0, $position), ' '), (int) strrpos(substr($text, 0, $position), "\n"));
|
||||
}
|
||||
|
||||
|
||||
$summary = substr($text, $position, $characters);
|
||||
$stringPieces = explode(' ', $string);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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',
|
||||
$textObj->ContextSummary(50, $testKeywords2)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user