PageRenderTime 23ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/CatalogSearch/Helper/Data.php

https://bitbucket.org/acidel/buykoala
PHP | 363 lines | 164 code | 33 blank | 166 comment | 21 complexity | 3a509a771e80d935c4720d227409f46c MD5 | raw file
  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) 2011 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. * Catalog search helper
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_CatalogSearch_Helper_Data extends Mage_Core_Helper_Abstract
  32. {
  33. const QUERY_VAR_NAME = 'q';
  34. const MAX_QUERY_LEN = 200;
  35. /**
  36. * Query object
  37. *
  38. * @var Mage_CatalogSearch_Model_Query
  39. */
  40. protected $_query;
  41. /**
  42. * Query string
  43. *
  44. * @var string
  45. */
  46. protected $_queryText;
  47. /**
  48. * Note messages
  49. *
  50. * @var array
  51. */
  52. protected $_messages = array();
  53. /**
  54. * Is a maximum length cut
  55. *
  56. * @var bool
  57. */
  58. protected $_isMaxLength = false;
  59. /**
  60. * Search engine model
  61. *
  62. * @var Mage_CatalogSearch_Model_Resource_Fulltext_Engine
  63. */
  64. protected $_engine;
  65. /**
  66. * Retrieve search query parameter name
  67. *
  68. * @return string
  69. */
  70. public function getQueryParamName()
  71. {
  72. return self::QUERY_VAR_NAME;
  73. }
  74. /**
  75. * Retrieve query model object
  76. *
  77. * @return Mage_CatalogSearch_Model_Query
  78. */
  79. public function getQuery()
  80. {
  81. if (!$this->_query) {
  82. $this->_query = Mage::getModel('catalogsearch/query')
  83. ->loadByQuery($this->getQueryText());
  84. if (!$this->_query->getId()) {
  85. $this->_query->setQueryText($this->getQueryText());
  86. }
  87. }
  88. return $this->_query;
  89. }
  90. /**
  91. * Is a minimum query length
  92. *
  93. * @return bool
  94. */
  95. public function isMinQueryLength()
  96. {
  97. if (Mage::helper('core/string')->strlen($this->getQueryText()) < $this->getMinQueryLength()) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * Retrieve search query text
  104. *
  105. * @return string
  106. */
  107. public function getQueryText()
  108. {
  109. if (is_null($this->_queryText)) {
  110. $this->_queryText = $this->_getRequest()->getParam($this->getQueryParamName());
  111. if ($this->_queryText === null) {
  112. $this->_queryText = '';
  113. } else {
  114. if (is_array($this->_queryText)) {
  115. $this->_queryText = null;
  116. }
  117. $this->_queryText = trim($this->_queryText);
  118. $this->_queryText = Mage::helper('core/string')->cleanString($this->_queryText);
  119. if (Mage::helper('core/string')->strlen($this->_queryText) > $this->getMaxQueryLength()) {
  120. $this->_queryText = Mage::helper('core/string')->substr(
  121. $this->_queryText,
  122. 0,
  123. $this->getMaxQueryLength()
  124. );
  125. $this->_isMaxLength = true;
  126. }
  127. }
  128. }
  129. return $this->_queryText;
  130. }
  131. /**
  132. * Retrieve HTML escaped search query
  133. *
  134. * @return string
  135. */
  136. public function getEscapedQueryText()
  137. {
  138. return $this->htmlEscape($this->getQueryText());
  139. }
  140. /**
  141. * Retrieve suggest collection for query
  142. *
  143. * @return Mage_CatalogSearch_Model_Resource_Query_Collection
  144. */
  145. public function getSuggestCollection()
  146. {
  147. return $this->getQuery()->getSuggestCollection();
  148. }
  149. /**
  150. * Retrieve result page url and set "secure" param to avoid confirm
  151. * message when we submit form from secure page to unsecure
  152. *
  153. * @param string $query
  154. * @return string
  155. */
  156. public function getResultUrl($query = null)
  157. {
  158. return $this->_getUrl('catalogsearch/result', array(
  159. '_query' => array(self::QUERY_VAR_NAME => $query),
  160. '_secure' => Mage::app()->getFrontController()->getRequest()->isSecure()
  161. ));
  162. }
  163. /**
  164. * Retrieve suggest url
  165. *
  166. * @return string
  167. */
  168. public function getSuggestUrl()
  169. {
  170. return $this->_getUrl('catalogsearch/ajax/suggest', array(
  171. '_secure' => Mage::app()->getFrontController()->getRequest()->isSecure()
  172. ));
  173. }
  174. /**
  175. * Retrieve search term url
  176. *
  177. * @return string
  178. */
  179. public function getSearchTermUrl()
  180. {
  181. return $this->_getUrl('catalogsearch/term/popular');
  182. }
  183. /**
  184. * Retrieve advanced search URL
  185. *
  186. * @return string
  187. */
  188. public function getAdvancedSearchUrl()
  189. {
  190. return $this->_getUrl('catalogsearch/advanced');
  191. }
  192. /**
  193. * Retrieve minimum query length
  194. *
  195. * @param mixed $store
  196. * @return int
  197. */
  198. public function getMinQueryLength($store = null)
  199. {
  200. return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MIN_QUERY_LENGTH, $store);
  201. }
  202. /**
  203. * Retrieve maximum query length
  204. *
  205. * @param mixed $store
  206. * @return int
  207. */
  208. public function getMaxQueryLength($store = null)
  209. {
  210. return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MAX_QUERY_LENGTH, $store);
  211. }
  212. /**
  213. * Retrieve maximum query words count for like search
  214. *
  215. * @param mixed $store
  216. * @return int
  217. */
  218. public function getMaxQueryWords($store = null)
  219. {
  220. return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MAX_QUERY_WORDS, $store);
  221. }
  222. /**
  223. * Add Note message
  224. *
  225. * @param string $message
  226. * @return Mage_CatalogSearch_Helper_Data
  227. */
  228. public function addNoteMessage($message)
  229. {
  230. $this->_messages[] = $message;
  231. return $this;
  232. }
  233. /**
  234. * Set Note messages
  235. *
  236. * @param array $messages
  237. * @return Mage_CatalogSearch_Helper_Data
  238. */
  239. public function setNoteMessages(array $messages)
  240. {
  241. $this->_messages = $messages;
  242. return $this;
  243. }
  244. /**
  245. * Retrieve Current Note messages
  246. *
  247. * @return array
  248. */
  249. public function getNoteMessages()
  250. {
  251. return $this->_messages;
  252. }
  253. /**
  254. * Check query of a warnings
  255. *
  256. * @param mixed $store
  257. * @return Mage_CatalogSearch_Helper_Data
  258. */
  259. public function checkNotes($store = null)
  260. {
  261. if ($this->_isMaxLength) {
  262. $this->addNoteMessage($this->__('Maximum Search query length is %s. Your query was cut.', $this->getMaxQueryLength()));
  263. }
  264. $stringHelper = Mage::helper('core/string');
  265. /* @var $stringHelper Mage_Core_Helper_String */
  266. $searchType = Mage::getStoreConfig(Mage_CatalogSearch_Model_Fulltext::XML_PATH_CATALOG_SEARCH_TYPE);
  267. if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE ||
  268. $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE) {
  269. $wordsFull = $stringHelper->splitWords($this->getQueryText(), true);
  270. $wordsLike = $stringHelper->splitWords($this->getQueryText(), true, $this->getMaxQueryWords());
  271. if (count($wordsFull) > count($wordsLike)) {
  272. $wordsCut = array_diff($wordsFull, $wordsLike);
  273. $wordsCut = array_map(array($this, 'htmlEscape'), $wordsCut);
  274. $this->addNoteMessage(
  275. $this->__('Maximum words count is %1$s. In your search query was cut next part: %2$s.',
  276. $this->getMaxQueryWords(),
  277. join(' ', $wordsCut)
  278. )
  279. );
  280. }
  281. }
  282. }
  283. /**
  284. * Join index array to string by separator
  285. * Support 2 level array gluing
  286. *
  287. * @param array $index
  288. * @param string $separator
  289. * @return string
  290. */
  291. public function prepareIndexdata($index, $separator = ' ')
  292. {
  293. $_index = array();
  294. foreach ($index as $key => $value) {
  295. if (!is_array($value)) {
  296. $_index[] = $value;
  297. }
  298. else {
  299. $_index = array_merge($_index, $value);
  300. }
  301. }
  302. return join($separator, $_index);
  303. }
  304. /**
  305. * Get current search engine resource model
  306. *
  307. * @return object
  308. */
  309. public function getEngine()
  310. {
  311. if (!$this->_engine) {
  312. $engine = Mage::getStoreConfig('catalog/search/engine');
  313. /**
  314. * This needed if there already was saved in configuration some none-default engine
  315. * and module of that engine was disabled after that.
  316. * Problem is in this engine in database configuration still set.
  317. */
  318. if ($engine && Mage::getConfig()->getResourceModelClassName($engine)) {
  319. $model = Mage::getResourceSingleton($engine);
  320. if ($model && $model->test()) {
  321. $this->_engine = $model;
  322. }
  323. }
  324. if (!$this->_engine) {
  325. $this->_engine = Mage::getResourceSingleton('catalogsearch/fulltext_engine');
  326. }
  327. }
  328. return $this->_engine;
  329. }
  330. }