1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_CatalogSearch_Model_Resource_Query_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_storeId;
43:
44: 45: 46: 47:
48: protected function _construct()
49: {
50: $this->_init('catalogsearch/query');
51: }
52:
53: 54: 55: 56: 57: 58:
59: public function setStoreId($store)
60: {
61: if ($store instanceof Mage_Core_Model_Store) {
62: $store = $store->getId();
63: }
64: $this->_storeId = $store;
65: return $this;
66: }
67:
68: 69: 70: 71: 72:
73: public function getStoreId()
74: {
75: return $this->_storeId;
76: }
77:
78: 79: 80: 81: 82: 83:
84: public function setQueryFilter($query)
85: {
86: $ifSynonymFor = $this->getConnection()
87: ->getIfNullSql('synonym_for', 'query_text');
88: $this->getSelect()->reset(Zend_Db_Select::FROM)->distinct(true)
89: ->from(
90: array('main_table' => $this->getTable('catalogsearch/search_query')),
91: array('query' => $ifSynonymFor, 'num_results')
92: )
93: ->where('num_results > 0 AND display_in_terms = 1 AND query_text LIKE ?',
94: Mage::getResourceHelper('core')->addLikeEscape($query, array('position' => 'start')))
95: ->order('popularity ' . Varien_Db_Select::SQL_DESC);
96: if ($this->getStoreId()) {
97: $this->getSelect()
98: ->where('store_id = ?', (int)$this->getStoreId());
99: }
100: return $this;
101: }
102:
103: 104: 105: 106: 107: 108:
109: public function setPopularQueryFilter($storeIds = null)
110: {
111: $ifSynonymFor = new Zend_Db_Expr($this->getConnection()
112: ->getCheckSql("synonym_for IS NOT NULL AND synonym_for != ''", 'synonym_for', 'query_text'));
113:
114: $this->getSelect()
115: ->reset(Zend_Db_Select::FROM)
116: ->reset(Zend_Db_Select::COLUMNS)
117: ->distinct(true)
118: ->from(
119: array('main_table' => $this->getTable('catalogsearch/search_query')),
120: array('name' => $ifSynonymFor, 'num_results', 'popularity')
121: );
122: if ($storeIds) {
123: $this->addStoreFilter($storeIds);
124: $this->getSelect()->where('num_results > 0');
125: }
126: elseif (null === $storeIds) {
127: $this->addStoreFilter(Mage::app()->getStore()->getId());
128: $this->getSelect()->where('num_results > 0');
129: }
130:
131: $this->getSelect()->order(array('popularity desc','name'));
132:
133: return $this;
134: }
135:
136: 137: 138: 139: 140:
141: public function setRecentQueryFilter()
142: {
143: $this->setOrder('updated_at', 'desc');
144: return $this;
145: }
146:
147: 148: 149: 150: 151: 152:
153: public function addStoreFilter($storeIds)
154: {
155: if (!is_array($storeIds)) {
156: $storeIds = array($storeIds);
157: }
158: $this->getSelect()->where('main_table.store_id IN (?)', $storeIds);
159: return $this;
160: }
161: }
162: