00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_CatalogSearch_Helper_Data extends Mage_Core_Helper_Abstract
00033 {
00034 const QUERY_VAR_NAME = 'q';
00035 const MAX_QUERY_LEN = 200;
00036
00037
00038
00039
00040
00041
00042 protected $_query;
00043
00044
00045
00046
00047
00048
00049 protected $_queryText;
00050
00051
00052
00053
00054
00055
00056 protected $_messages = array();
00057
00058
00059
00060
00061
00062
00063 protected $_isMaxLength = false;
00064
00065
00066
00067
00068
00069
00070 public function getQueryParamName()
00071 {
00072 return self::QUERY_VAR_NAME;
00073 }
00074
00075
00076
00077
00078
00079
00080 public function getQuery()
00081 {
00082 if (!$this->_query) {
00083 $this->_query = Mage::getModel('catalogsearch/query')
00084 ->loadByQuery($this->getQueryText())
00085 ->setQueryText($this->getQueryText());
00086 }
00087 return $this->_query;
00088 }
00089
00090
00091
00092
00093
00094
00095 public function isMinQueryLength()
00096 {
00097 if (Mage::helper('core/string')->strlen($this->getQueryText()) < $this->getMinQueryLength()) {
00098 return true;
00099 }
00100 return false;
00101 }
00102
00103
00104
00105
00106
00107
00108 public function getQueryText()
00109 {
00110 if (is_null($this->_queryText)) {
00111 $this->_queryText = $this->_getRequest()->getParam($this->getQueryParamName());
00112 if ($this->_queryText === null) {
00113 $this->_queryText = '';
00114 } else {
00115 if (is_array($this->_queryText)) {
00116 $this->_queryText = null;
00117 }
00118 $this->_queryText = trim($this->_queryText);
00119 if (Mage::helper('core/string')->strlen($this->_queryText) > $this->getMaxQueryLength()) {
00120 $this->_queryText = Mage::helper('core/string')->substr(
00121 $this->_queryText,
00122 0,
00123 $this->getMaxQueryLength()
00124 );
00125 $this->_isMaxLength = true;
00126 }
00127 }
00128 }
00129 return $this->_queryText;
00130 }
00131
00132
00133
00134
00135
00136
00137 public function getEscapedQueryText()
00138 {
00139 return $this->htmlEscape($this->getQueryText());
00140 }
00141
00142
00143
00144
00145
00146
00147 public function getSuggestCollection()
00148 {
00149 return $this->getQuery()->getSuggestCollection();
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 public function getResultUrl($query = null)
00159 {
00160 return $this->_getUrl('catalogsearch/result', array('_query' => array(
00161 self::QUERY_VAR_NAME => $query
00162 )));
00163 }
00164
00165
00166
00167
00168
00169
00170 public function getSuggestUrl()
00171 {
00172 return $this->_getUrl('catalogsearch/ajax/suggest');
00173 }
00174
00175
00176
00177
00178
00179
00180 public function getSearchTermUrl()
00181 {
00182 return $this->_getUrl('catalogsearch/term/popular');
00183 }
00184
00185
00186
00187
00188
00189
00190 public function getAdvancedSearchUrl()
00191 {
00192 return $this->_getUrl('catalogsearch/advanced');
00193 }
00194
00195
00196
00197
00198
00199
00200
00201 public function getMinQueryLength($store = null)
00202 {
00203 return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MIN_QUERY_LENGTH, $store);
00204 }
00205
00206
00207
00208
00209
00210
00211
00212 public function getMaxQueryLength($store = null)
00213 {
00214 return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MAX_QUERY_LENGTH, $store);
00215 }
00216
00217
00218
00219
00220
00221
00222
00223 public function getMaxQueryWords($store = null)
00224 {
00225 return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MAX_QUERY_WORDS, $store);
00226 }
00227
00228
00229
00230
00231
00232
00233
00234 public function addNoteMessage($message)
00235 {
00236 $this->_messages[] = $message;
00237 return $this;
00238 }
00239
00240
00241
00242
00243
00244
00245
00246 public function setNoteMessages(array $messages)
00247 {
00248 $this->_messages = $messages;
00249 return $this;
00250 }
00251
00252
00253
00254
00255
00256
00257 public function getNoteMessages()
00258 {
00259 return $this->_messages;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268 public function checkNotes($store = null)
00269 {
00270 if ($this->_isMaxLength) {
00271 $this->addNoteMessage($this->__('Maximum Search query length is %s. Your query was cut.', $this->getMaxQueryLength()));
00272 }
00273
00274 $stringHelper = Mage::helper('core/string');
00275
00276
00277 $searchType = Mage::getStoreConfig(Mage_CatalogSearch_Model_Fulltext::XML_PATH_CATALOG_SEARCH_TYPE);
00278 if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE ||
00279 $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE) {
00280
00281 $wordsFull = $stringHelper->splitWords($this->getQueryText(), true);
00282 $wordsLike = $stringHelper->splitWords($this->getQueryText(), true, $this->getMaxQueryWords());
00283
00284 if (count($wordsFull) > count($wordsLike)) {
00285 $wordsCut = array_diff($wordsFull, $wordsLike);
00286
00287 $this->addNoteMessage(
00288 $this->__('Maximum words count is %1$s. In your search query was cut next part: %2$s.',
00289 $this->getMaxQueryWords(),
00290 join(' ', $wordsCut)
00291 )
00292 );
00293 }
00294 }
00295 }
00296 }