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
00031
00032 class Mage_CatalogSearch_Model_Mysql4_Query_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
00033 {
00034
00035
00036
00037
00038
00039 protected $_storeId;
00040
00041
00042
00043
00044
00045 protected function _construct()
00046 {
00047 $this->_init('catalogsearch/query');
00048 }
00049
00050
00051
00052
00053
00054
00055
00056 public function setStoreId($store)
00057 {
00058 if ($store instanceof Mage_Core_Model_Store) {
00059 $store = $store->getId();
00060 }
00061 $this->_storeId = $store;
00062 return $this;
00063 }
00064
00065
00066
00067
00068
00069
00070 public function getStoreId()
00071 {
00072 return $this->_storeId;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 public function setQueryFilter($query)
00082 {
00083 $this->getSelect()->reset(Zend_Db_Select::FROM)->distinct(true)
00084 ->from(
00085 array('main_table' => $this->getTable('catalogsearch/search_query')),
00086 array('query' => "IF(IFNULL(synonim_for,'')<>'', synonim_for, query_text)", 'num_results')
00087 )
00088 ->where('num_results>0 AND display_in_terms=1 AND query_text LIKE ?', $query.'%')
00089 ->order('popularity desc');
00090 if ($this->getStoreId()) {
00091 $this->getSelect()
00092 ->where('store_id=?', $this->getStoreId());
00093 }
00094 return $this;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103 public function setPopularQueryFilter($storeIds = null)
00104 {
00105 $this->getSelect()->reset(Zend_Db_Select::FROM)->distinct(true)
00106 ->from(
00107 array('main_table'=>$this->getTable('catalogsearch/search_query')),
00108 array('name'=>"if(ifnull(synonim_for,'')<>'', synonim_for, query_text)", 'num_results')
00109 );
00110 if ($storeIds) {
00111 $this->addStoreFilter($storeIds);
00112 $this->getSelect()->where('num_results > 0');
00113 }
00114 elseif (null === $storeIds) {
00115 $this->addStoreFilter(Mage::app()->getStore()->getId());
00116 $this->getSelect()->where('num_results > 0');
00117 }
00118
00119 $this->getSelect()->order(array('popularity desc','name'));
00120
00121 return $this;
00122 }
00123
00124
00125
00126
00127
00128
00129 public function setRecentQueryFilter()
00130 {
00131 $this->setOrder('updated_at', 'desc');
00132 return $this;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 public function addStoreFilter($storeIds)
00142 {
00143 if (!is_array($storeIds)) {
00144 $storeIds = array($storeIds);
00145 }
00146 $this->getSelect()->where('main_table.store_id IN (?)', $storeIds);
00147 return $this;
00148 }
00149 }