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: class Mage_CatalogSearch_Helper_Data extends Mage_Core_Helper_Abstract
33: {
34: const QUERY_VAR_NAME = 'q';
35: const MAX_QUERY_LEN = 200;
36:
37: 38: 39: 40: 41:
42: protected $_query;
43:
44: 45: 46: 47: 48:
49: protected $_queryText;
50:
51: 52: 53: 54: 55:
56: protected $_messages = array();
57:
58: 59: 60: 61: 62:
63: protected $_isMaxLength = false;
64:
65: 66: 67: 68: 69:
70: protected $_engine;
71:
72: 73: 74: 75: 76:
77: public function getQueryParamName()
78: {
79: return self::QUERY_VAR_NAME;
80: }
81:
82: 83: 84: 85: 86:
87: public function getQuery()
88: {
89: if (!$this->_query) {
90: $this->_query = Mage::getModel('catalogsearch/query')
91: ->loadByQuery($this->getQueryText());
92: if (!$this->_query->getId()) {
93: $this->_query->setQueryText($this->getQueryText());
94: }
95: }
96: return $this->_query;
97: }
98:
99: 100: 101: 102: 103:
104: public function isMinQueryLength()
105: {
106: $minQueryLength = $this->getMinQueryLength();
107: $thisQueryLength = Mage::helper('core/string')->strlen($this->getQueryText());
108: return !$thisQueryLength || $minQueryLength !== '' && $thisQueryLength < $minQueryLength;
109: }
110:
111: 112: 113: 114: 115:
116: public function getQueryText()
117: {
118: if (!isset($this->_queryText)) {
119: $this->_queryText = $this->_getRequest()->getParam($this->getQueryParamName());
120: if ($this->_queryText === null) {
121: $this->_queryText = '';
122: } else {
123:
124: $stringHelper = Mage::helper('core/string');
125: $this->_queryText = is_array($this->_queryText) ? ''
126: : $stringHelper->cleanString(trim($this->_queryText));
127:
128: $maxQueryLength = $this->getMaxQueryLength();
129: if ($maxQueryLength !== '' && $stringHelper->strlen($this->_queryText) > $maxQueryLength) {
130: $this->_queryText = $stringHelper->substr($this->_queryText, 0, $maxQueryLength);
131: $this->_isMaxLength = true;
132: }
133: }
134: }
135: return $this->_queryText;
136: }
137:
138: 139: 140: 141: 142:
143: public function getEscapedQueryText()
144: {
145: return $this->escapeHtml($this->getQueryText());
146: }
147:
148: 149: 150: 151: 152:
153: public function getSuggestCollection()
154: {
155: return $this->getQuery()->getSuggestCollection();
156: }
157:
158: 159: 160: 161: 162: 163: 164:
165: public function getResultUrl($query = null)
166: {
167: return $this->_getUrl('catalogsearch/result', array(
168: '_query' => array(self::QUERY_VAR_NAME => $query),
169: '_secure' => Mage::app()->getFrontController()->getRequest()->isSecure()
170: ));
171: }
172:
173: 174: 175: 176: 177:
178: public function getSuggestUrl()
179: {
180: return $this->_getUrl('catalogsearch/ajax/suggest', array(
181: '_secure' => Mage::app()->getFrontController()->getRequest()->isSecure()
182: ));
183: }
184:
185: 186: 187: 188: 189:
190: public function getSearchTermUrl()
191: {
192: return $this->_getUrl('catalogsearch/term/popular');
193: }
194:
195: 196: 197: 198: 199:
200: public function getAdvancedSearchUrl()
201: {
202: return $this->_getUrl('catalogsearch/advanced');
203: }
204:
205: 206: 207: 208: 209: 210:
211: public function getMinQueryLength($store = null)
212: {
213: return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MIN_QUERY_LENGTH, $store);
214: }
215:
216: 217: 218: 219: 220: 221:
222: public function getMaxQueryLength($store = null)
223: {
224: return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MAX_QUERY_LENGTH, $store);
225: }
226:
227: 228: 229: 230: 231: 232:
233: public function getMaxQueryWords($store = null)
234: {
235: return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MAX_QUERY_WORDS, $store);
236: }
237:
238: 239: 240: 241: 242: 243:
244: public function addNoteMessage($message)
245: {
246: $this->_messages[] = $message;
247: return $this;
248: }
249:
250: 251: 252: 253: 254: 255:
256: public function setNoteMessages(array $messages)
257: {
258: $this->_messages = $messages;
259: return $this;
260: }
261:
262: 263: 264: 265: 266:
267: public function getNoteMessages()
268: {
269: return $this->_messages;
270: }
271:
272: 273: 274: 275: 276: 277:
278: public function checkNotes($store = null)
279: {
280: if ($this->_isMaxLength) {
281: $this->addNoteMessage($this->__('Maximum Search query length is %s. Your query was cut.', $this->getMaxQueryLength()));
282: }
283:
284:
285: $stringHelper = Mage::helper('core/string');
286:
287: $searchType = Mage::getStoreConfig(Mage_CatalogSearch_Model_Fulltext::XML_PATH_CATALOG_SEARCH_TYPE);
288: if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE
289: || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE
290: ) {
291: $wordsFull = $stringHelper->splitWords($this->getQueryText(), true);
292: $wordsLike = $stringHelper->splitWords($this->getQueryText(), true, $this->getMaxQueryWords());
293: if (count($wordsFull) > count($wordsLike)) {
294: $wordsCut = array_map(array($this, 'escapeHtml'), array_diff($wordsFull, $wordsLike));
295: $this->addNoteMessage(
296: $this->__('Maximum words count is %1$s. In your search query was cut next part: %2$s.', $this->getMaxQueryWords(), join(' ', $wordsCut))
297: );
298: }
299: }
300: }
301:
302: 303: 304: 305: 306: 307: 308: 309:
310: public function prepareIndexdata($index, $separator = ' ')
311: {
312: $_index = array();
313: foreach ($index as $value) {
314: if (!is_array($value)) {
315: $_index[] = $value;
316: }
317: else {
318: $_index = array_merge($_index, $value);
319: }
320: }
321: return join($separator, $_index);
322: }
323:
324: 325: 326: 327: 328:
329: public function getEngine()
330: {
331: if (!$this->_engine) {
332: $engine = Mage::getStoreConfig('catalog/search/engine');
333:
334: 335: 336: 337: 338:
339: if ($engine && Mage::getConfig()->getResourceModelClassName($engine)) {
340: $model = Mage::getResourceSingleton($engine);
341: if ($model && $model->test()) {
342: $this->_engine = $model;
343: }
344: }
345: if (!$this->_engine) {
346: $this->_engine = Mage::getResourceSingleton('catalogsearch/fulltext_engine');
347: }
348: }
349:
350: return $this->_engine;
351: }
352: }
353: