1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_CatalogSearch
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Catalog search query model
29: *
30: * @method Mage_CatalogSearch_Model_Resource_Query _getResource()
31: * @method Mage_CatalogSearch_Model_Resource_Query getResource()
32: * @method string getQueryText()
33: * @method Mage_CatalogSearch_Model_Query setQueryText(string $value)
34: * @method int getNumResults()
35: * @method Mage_CatalogSearch_Model_Query setNumResults(int $value)
36: * @method int getPopularity()
37: * @method Mage_CatalogSearch_Model_Query setPopularity(int $value)
38: * @method string getRedirect()
39: * @method Mage_CatalogSearch_Model_Query setRedirect(string $value)
40: * @method string getSynonymFor()
41: * @method Mage_CatalogSearch_Model_Query setSynonymFor(string $value)
42: * @method int getDisplayInTerms()
43: * @method Mage_CatalogSearch_Model_Query setDisplayInTerms(int $value)
44: * @method int getIsActive()
45: * @method Mage_CatalogSearch_Model_Query setIsActive(int $value)
46: * @method int getIsProcessed()
47: * @method Mage_CatalogSearch_Model_Query setIsProcessed(int $value)
48: * @method string getUpdatedAt()
49: * @method Mage_CatalogSearch_Model_Query setUpdatedAt(string $value)
50: *
51: * @category Mage
52: * @package Mage_CatalogSearch
53: * @author Magento Core Team <core@magentocommerce.com>
54: */
55: class Mage_CatalogSearch_Model_Query extends Mage_Core_Model_Abstract
56: {
57: /**
58: * Event prefix
59: *
60: * @var string
61: */
62: protected $_eventPrefix = 'catalogsearch_query';
63:
64: /**
65: * Event object key name
66: *
67: * @var string
68: */
69: protected $_eventObject = 'catalogsearch_query';
70:
71: const CACHE_TAG = 'SEARCH_QUERY';
72: const XML_PATH_MIN_QUERY_LENGTH = 'catalog/search/min_query_length';
73: const XML_PATH_MAX_QUERY_LENGTH = 'catalog/search/max_query_length';
74: const XML_PATH_MAX_QUERY_WORDS = 'catalog/search/max_query_words';
75:
76: /**
77: * Init resource model
78: *
79: */
80: protected function _construct()
81: {
82: $this->_init('catalogsearch/query');
83: }
84:
85: /**
86: * Retrieve search collection
87: *
88: * @return Mage_CatalogSearch_Model_Resource_Search_Collection
89: */
90: public function getSearchCollection()
91: {
92: return Mage::getResourceModel('catalogsearch/search_collection');
93: }
94:
95: /**
96: * Retrieve collection of search results
97: *
98: * @return Mage_Eav_Model_Entity_Collection_Abstract
99: */
100: public function getResultCollection()
101: {
102: $collection = $this->getData('result_collection');
103: if (is_null($collection)) {
104: $collection = $this->getSearchCollection();
105:
106: $text = $this->getSynonymFor();
107: if (!$text) {
108: $text = $this->getQueryText();
109: }
110:
111: $collection->addSearchFilter($text)
112: ->addStoreFilter()
113: ->addMinimalPrice()
114: ->addTaxPercents();
115: $this->setData('result_collection', $collection);
116: }
117: return $collection;
118: }
119:
120: /**
121: * Retrieve collection of suggest queries
122: *
123: * @return Mage_CatalogSearch_Model_Resource_Query_Collection
124: */
125: public function getSuggestCollection()
126: {
127: $collection = $this->getData('suggest_collection');
128: if (is_null($collection)) {
129: $collection = Mage::getResourceModel('catalogsearch/query_collection')
130: ->setStoreId($this->getStoreId())
131: ->setQueryFilter($this->getQueryText());
132: $this->setData('suggest_collection', $collection);
133: }
134: return $collection;
135: }
136:
137: /**
138: * Load Query object by query string
139: *
140: * @param string $text
141: * @return Mage_CatalogSearch_Model_Query
142: */
143: public function loadByQuery($text)
144: {
145: $this->_getResource()->loadByQuery($this, $text);
146: $this->_afterLoad();
147: $this->setOrigData();
148: return $this;
149: }
150:
151: /**
152: * Load Query object only by query text (skip 'synonym For')
153: *
154: * @param string $text
155: * @return Mage_CatalogSearch_Model_Query
156: */
157: public function loadByQueryText($text)
158: {
159: $this->_getResource()->loadByQueryText($this, $text);
160: $this->_afterLoad();
161: $this->setOrigData();
162: return $this;
163: }
164:
165: /**
166: * Set Store Id
167: *
168: * @param int $storeId
169: * @return Mage_CatalogSearch_Model_Query
170: */
171: public function setStoreId($storeId)
172: {
173: $this->setData('store_id', $storeId);
174: }
175:
176: /**
177: * Retrieve store Id
178: *
179: * @return int
180: */
181: public function getStoreId()
182: {
183: if (!$storeId = $this->getData('store_id')) {
184: $storeId = Mage::app()->getStore()->getId();
185: }
186: return $storeId;
187: }
188:
189: /**
190: * Prepare save query for result
191: *
192: * @return Mage_CatalogSearch_Model_Query
193: */
194: public function prepare()
195: {
196: if (!$this->getId()) {
197: $this->setIsActive(0);
198: $this->setIsProcessed(0);
199: $this->save();
200: $this->setIsActive(1);
201: }
202:
203: return $this;
204: }
205:
206: /**
207: * Retrieve minimum query length
208: *
209: * @deprecated after 1.3.2.3 use getMinQueryLength() instead
210: * @return int
211: */
212: public function getMinQueryLenght()
213: {
214: return Mage::getStoreConfig(self::XML_PATH_MIN_QUERY_LENGTH, $this->getStoreId());
215: }
216:
217: /**
218: * Retrieve minimum query length
219: *
220: * @return int
221: */
222: public function getMinQueryLength(){
223: return $this->getMinQueryLenght();
224: }
225:
226: /**
227: * Retrieve maximum query length
228: *
229: * @deprecated after 1.3.2.3 use getMaxQueryLength() instead
230: * @return int
231: */
232: public function getMaxQueryLenght()
233: {
234: return 0;
235: }
236:
237: /**
238: * Retrieve maximum query length
239: *
240: * @return int
241: */
242: public function getMaxQueryLength()
243: {
244: return Mage::getStoreConfig(self::XML_PATH_MAX_QUERY_LENGTH, $this->getStoreId());
245: }
246:
247: /**
248: * Retrieve maximum query words for like search
249: *
250: * @return int
251: */
252: public function getMaxQueryWords()
253: {
254: return Mage::getStoreConfig(self::XML_PATH_MAX_QUERY_WORDS, $this->getStoreId());
255: }
256: }
257: