From 30eddf57c8de433e8ea02b9e552c8e1744a505a7 Mon Sep 17 00:00:00 2001 From: wei <> Date: Sun, 7 May 2006 03:34:25 +0000 Subject: Add search to quickstart demo. --- demos/quickstart/protected/pages/Search.php | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 demos/quickstart/protected/pages/Search.php (limited to 'demos/quickstart/protected/pages/Search.php') diff --git a/demos/quickstart/protected/pages/Search.php b/demos/quickstart/protected/pages/Search.php new file mode 100644 index 00000000..cadca368 --- /dev/null +++ b/demos/quickstart/protected/pages/Search.php @@ -0,0 +1,53 @@ +IsPostBack && strlen($text = $this->search->getText()) > 0) + { + $search = $this->getApplication()->getModule("search"); + $this->results->setDataSource($search->find($text)); + $this->results->dataBind(); + } + } + + public function highlightSearch($text) + { + $words = str_word_count($text, 1); + $keys = str_word_count(strtolower($this->search->getText()),1); + $where = 0; + $t = count($words); + for($i = 0; $i<$t; $i++) + { + if($this->containsKeys($words[$i], $keys)) + { + $words[$i] = ''.$words[$i].''; + $where = $i; + break; + } + } + + $min = $where - 15 < 0 ? 0 : $where - 15; + $max = $where + 15 > $t ? $t : $where + 15; + $subtext = array_splice($words, $min, $max-$min); + $prefix = $min == 0 ? '' : '...'; + $suffix = $max == $t ? '' : '...'; + return $prefix.implode(' ', $subtext).$suffix; + } + + protected function containsKeys($word, $keys) + { + foreach($keys as $key) + { + if(is_int(strpos($word, $key))) + return true; + } + return false; + } +} + +?> \ No newline at end of file -- cgit v1.2.3