*/ class Mage_CatalogSearch_Model_Resource_Query_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract { /** * Store for filter * * @var int */ protected $_storeId; /** * Init model for collection * */ protected function _construct() { $this->_init('catalogsearch/query'); } /** * Set Store ID for filter * * @param mixed $store * @return Mage_CatalogSearch_Model_Resource_Query_Collection */ public function setStoreId($store) { if ($store instanceof Mage_Core_Model_Store) { $store = $store->getId(); } $this->_storeId = $store; return $this; } /** * Retrieve Store ID Filter * * @return int|null */ public function getStoreId() { return $this->_storeId; } /** * Set search query text to filter * * @param string $query * @return Mage_CatalogSearch_Model_Resource_Query_Collection */ public function setQueryFilter($query) { $ifSynonymFor = $this->getConnection() ->getIfNullSql('synonym_for', 'query_text'); $this->getSelect()->reset(Zend_Db_Select::FROM)->distinct(true) ->from( array('main_table' => $this->getTable('catalogsearch/search_query')), array('query' => $ifSynonymFor, 'num_results') ) ->where('num_results > 0 AND display_in_terms = 1 AND query_text LIKE ?', Mage::getResourceHelper('core')->addLikeEscape($query, array('position' => 'start'))) ->order('popularity ' . Varien_Db_Select::SQL_DESC); if ($this->getStoreId()) { $this->getSelect() ->where('store_id = ?', (int)$this->getStoreId()); } return $this; } /** * Set Popular Search Query Filter * * @param int|array $storeIds * @return Mage_CatalogSearch_Model_Resource_Query_Collection */ public function setPopularQueryFilter($storeIds = null) { $ifSynonymFor = new Zend_Db_Expr($this->getConnection() ->getCheckSql("synonym_for IS NOT NULL AND synonym_for != ''", 'synonym_for', 'query_text')); $this->getSelect() ->reset(Zend_Db_Select::FROM) ->reset(Zend_Db_Select::COLUMNS) ->distinct(true) ->from( array('main_table' => $this->getTable('catalogsearch/search_query')), array('name' => $ifSynonymFor, 'num_results', 'popularity') ); if ($storeIds) { $this->addStoreFilter($storeIds); $this->getSelect()->where('num_results > 0'); } elseif (null === $storeIds) { $this->addStoreFilter(Mage::app()->getStore()->getId()); $this->getSelect()->where('num_results > 0'); } $this->getSelect()->order(array('popularity desc','name')); return $this; } /** * Set Recent Queries Order * * @return Mage_CatalogSearch_Model_Resource_Query_Collection */ public function setRecentQueryFilter() { $this->setOrder('updated_at', 'desc'); return $this; } /** * Filter collection by specified store ids * * @param array|int $storeIds * @return Mage_CatalogSearch_Model_Resource_Query_Collection */ public function addStoreFilter($storeIds) { if (!is_array($storeIds)) { $storeIds = array($storeIds); } $this->getSelect()->where('main_table.store_id IN (?)', $storeIds); return $this; } }