2013-03-11 13:12:48 +01:00
|
|
|
<?php
|
|
|
|
App::uses('AppHelper', 'View/Helper');
|
|
|
|
|
|
|
|
// This helper accepts a string to be processed and an single or an array of strings that get turned into regex search patterns.
|
|
|
|
// It will then run each of those through the string with preg_replaces, highlighting all the matched terms in red
|
|
|
|
// Used for things such as searches in the logs to highlight found terms
|
|
|
|
|
|
|
|
class HighlightHelper extends AppHelper {
|
2013-05-31 17:38:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Important: data needs to be sanitized using the h() function before entering this function
|
|
|
|
* @param unknown_type $keywordArray
|
|
|
|
*/
|
|
|
|
public function build_replace_pairs($keywordArray) {
|
|
|
|
// build the $replacePairs variable used to highlight the keywords
|
|
|
|
$replacementArray = array();
|
|
|
|
if (!is_array($keywordArray)) {
|
|
|
|
$keywordArray = array($keywordArray);
|
|
|
|
}
|
2013-06-07 12:03:28 +02:00
|
|
|
foreach ($keywordArray as $k => &$keywordArrayElement) {
|
2013-05-31 17:38:46 +02:00
|
|
|
$keywordArrayElement = trim($keywordArrayElement);
|
2013-06-07 12:03:28 +02:00
|
|
|
if ("" == $keywordArrayElement) {
|
|
|
|
unset($keywordArray[$k]);
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-31 17:38:46 +02:00
|
|
|
$replacementArray[] = '<span style="color:red">'.$keywordArrayElement.'</span>';
|
|
|
|
}
|
|
|
|
if (!empty($replacementArray))
|
|
|
|
return array_combine($keywordArray, $replacementArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function highlighter($str, $replacePairs) {
|
|
|
|
if (is_array($replacePairs)) {
|
|
|
|
return strtr($str, $replacePairs);
|
2013-03-11 13:12:48 +01:00
|
|
|
} else {
|
|
|
|
return $str;
|
|
|
|
}
|
2013-05-31 17:38:46 +02:00
|
|
|
|
2013-03-11 13:12:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|