00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 class Mage_CatalogSearch_Block_Autocomplete extends Mage_Core_Block_Abstract
00031 {
00032 protected $_suggestData = null;
00033
00034 protected function _toHtml()
00035 {
00036 $html = '';
00037
00038 if (!$this->_beforeToHtml()) {
00039 return $html;
00040 }
00041
00042 $suggestData = $this->getSuggestData();
00043 if (!($count = count($suggestData))) {
00044 return $html;
00045 }
00046
00047 $count--;
00048
00049 $html = '<ul><li style="display:none"></li>';
00050 foreach ($suggestData as $index => $item) {
00051 if ($index == 0) {
00052 $item['row_class'] .= ' first';
00053 }
00054
00055 if ($index == $count) {
00056 $item['row_class'] .= ' last';
00057 }
00058
00059 $html .= '<li title="'.$this->htmlEscape($item['title']).'" class="'.$item['row_class'].'">'
00060 . '<span class="amount">'.$item['num_of_results'].'</span>'.$this->htmlEscape($item['title']).'</li>';
00061 }
00062
00063 $html.= '</ul>';
00064
00065 return $html;
00066 }
00067
00068 public function getSuggestData()
00069 {
00070 if (!$this->_suggestData) {
00071 $collection = $this->helper('catalogSearch')->getSuggestCollection();
00072 $query = $this->helper('catalogSearch')->getQueryText();
00073 $counter = 0;
00074 $data = array();
00075 foreach ($collection as $item) {
00076 $_data = array(
00077 'title' => $item->getQueryText(),
00078 'row_class' => (++$counter)%2?'odd':'even',
00079 'num_of_results' => $item->getNumResults()
00080 );
00081
00082 if ($item->getQueryText() == $query) {
00083 array_unshift($data, $_data);
00084 }
00085 else {
00086 $data[] = $_data;
00087 }
00088 }
00089 $this->_suggestData = $data;
00090 }
00091 return $this->_suggestData;
00092 }
00093
00094
00095
00096 }