initialize(); } return self::$instance; } /** * Tworzy zapytanie do bazy danych na podsawie frazy, zwraca obiekt z kryteriami * * @param string $what * @return object */ public function search($what = '') { $this->context = sfContext::getInstance(); $this->searchConfig = stConfig::getInstance($this->context, 'stSearchBackend'); $this->culture = $this->context->getUser()->getCulture(); $this->currency = $this->context->getUser()->getAttribute('currency'); $c = new Criteria(); $this->showWithoutPrice($c); $c->add(ProductPeer::IS_GIFT, 0); return $c; } /** * Ustawia ktore produkty maja byc wyswietlane * * @param $object $c */ protected function showWithoutPrice($c = null) { $config = stConfig::getInstance(sfContext::getInstance(), array( 'show_without_price' => stConfig::BOOL),'stProduct' ); $config->load(); $c->add(ProductPeer::ACTIVE,1); if ($config->get('show_without_price')) { $c->add(ProductPeer::PRICE,0,Criteria::GREATER_THAN); } stEventDispatcher::getInstance()->notify(new sfEvent($this, 'stProductActions.postAddProductCriteria', array('criteria' => $c))); } /** * Usuwa zbedne tagi * * @param string $word * @return string */ public static function removeTags($word) { if (is_string($word)) { $tmp = mb_strtolower($word,'UTF-8'); $tmp = strip_tags($tmp); return html_entity_decode($tmp,ENT_NOQUOTES,'UTF-8'); } return ''; } /** * Dodaje parametr do wyszukiwania * * @param string $name * @param mixed $value */ public function addParam($name = '', $value = null) { if (mb_strlen($name) && !empty($value)) { if (!is_array($value)) $this->params[$name] = $value; else $this->params[$name] = $this->cleanArray($value); } } public function cleanArray($array) { if (is_array($array)) { foreach ($array as $key=>$value) { $array[$key] = $this->cleanArray($value); if (is_string($array[$key]) && strlen(trim($array[$key]))==0) unset($array[$key]); } } elseif (is_string($array)) { return $array; } return $array; } public function setParams($params = array(), $prefix='st_search') { if (is_array($params)) foreach ($params as $key=>$param) { $this->addParam("{$prefix}[{$key}]",$param); } } /** * Pobiera parameter * * @param string $name * @return mixed */ public function getParam($name = '') { if (isset($this->params[$name])) { return $this->params[$name]; } } /** * Pobiera wszystkie paramtery * * @return array */ public function getAllParams() { return $this->params; } /** * Tworzy linki w postaic param1=y¶m2=x * * @return string */ public function getPagerParams($as_array = false) { if ($as_array) return $this->params; return (http_build_query($this->params,'','&')); } public function getRequestParameter($param, $default) { return sfContext::getInstance()->getRequest()->getParameter($param, $default); } function addStats($searched_word = '', $results = -1) { // sprawdz czy slowo juz bylo wyszukiwane $c = new Criteria(); $c->add(SearchedWordsPeer::WORD,mb_strtolower($searched_word,'utf-8')); $searched = SearchedWordsPeer::doSelectOne($c); // jezeli nie to stworz nowe if (!$searched) { $searched=new SearchedWords(); $searched->setWord($searched_word); } // ustaw i zapisz zmiany $searched->setSearched(($searched->getSearched()+1)); $searched->setResults($results); $searched->save(); } /** * Removes local symbol and special char for fulltext search * @param string $str * @return srting */ public static function removeLocalSymbols($str) { mb_internal_encoding('UTF-8'); mb_regex_encoding("UTF-8"); $search = array("/\+/","/'/", '/\./','/-/', '/ä/', '/ö/', '/ü/', '/Ä/', '/Ö/', '/Ü/', '/ß/', '/ą/', '/Ą/', '/ć/', '/Ć/', '/ę/', '/Ę/', '/ł/', '/Ł/' ,'/ń/', '/Ń/', '/ó/', '/Ó/', '/ś/', '/Ś/', '/ź/', '/Ź/', '/ż/', '/Ż/', '/Š/','/Ž/','/š/','/ž/','/Ÿ/','/Ŕ/','/Á/','/Â/','/Ă/','/Ä/','/Ĺ/','/Ç/','/Č/','/É/','/Ę/','/Ë/','/Ě/','/Í/','/Î/','/Ď/','/Ń/', '/Ň/','/Ó/','/Ô/','/Ő/','/Ö/','/Ř/','/Ů/','/Ú/','/Ű/','/Ü/','/Ý/','/ŕ/','/á/','/â/','/ă/','/ä/','/ĺ/','/ç/','/č/','/é/','/ę/', '/ë/','/ě/','/í/','/î/','/ď/','/ń/','/ň/','/ó/','/ô/','/ő/','/ö/','/ř/','/ů/','/ú/','/ű/','/ü/','/ý/','/˙/', '/Ţ/','/ţ/','/Đ/','/đ/','/ß/','/Œ/','/œ/','/Ć/','/ć/','/ľ/'); $replace = array('_', '_', '_','_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'A', 'c', 'C', 'e', 'E', 'l', 'L', 'n', 'N', 'o', 'O', 's', 'S', 'z', 'Z', 'z', 'Z', 'S','Z','s','z','Y','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I','N', 'O','O','O','O','O','O','U','U','U','U','Y','a','a','a','a','a','a','c','e','e','e', 'e','i','i','i','i','n','o','o','o','o','o','o','u','u','u','u','y','y', 'TH','th','DH','dh','ss','OE','oe','AE','ae','u'); $str = preg_replace($search, $replace, $str); return mb_convert_case($str, MB_CASE_LOWER, "UTF-8"); } /** * Build string with no special chars, html tags * @param string $data * @return string */ public static function rebuildSearchIndex($data) { $data = stSearch::removeTags($data); $items = split("[\(\)\n\r\t ]+",stSearch::removeLocalSymbols($data).' '.$data); // add '_' to words shorten than ft_min_word_len but longer than stSearch::getMinLen() foreach ($items as $key=>$value) { $value = trim($value,"'\"!:;.,_"); if (strlen($value)) $items[$key] = str_pad($value, stSearch::getMinLen(), '_', STR_PAD_RIGHT); } $tmp = ' '.implode(' ', $items).' '; return str_replace(self::$stopwords, self::$nonstopwords, $tmp); } public static function productGetCode($product) { return $product->getCode(); } /** * Retrurns ft_min_word_len for mysql * @return integer */ public static function getMinLen() { // if ft_min_word_len is not set get from database if (stSearch::$min_len==null) { // default value is 3 stSearch::$min_len = 3; // get ft_min_word_len from database $con = Propel::getConnection(); $sql = "SHOW GLOBAL VARIABLES LIKE 'ft_min_word_len'"; $stmt = $con->PrepareStatement($sql); $rs = $stmt->executeQuery(); if ($rs->next()) { stSearch::$min_len = $rs->getInt('Value'); } } return stSearch::$min_len; } /** * Gets product code for search * @param object $product * @return string */ public static function productGetName($product) { return $product->getName(); } /** * Return product keywords as a string * @param object $product * @return string */ public static function productGetKeywords($product) { $c = new Criteria(); $c->add(ProductHasPositioningPeer::PRODUCT_ID, $product->getId()); $positioning = ProductHasPositioningPeer::doSelectOne($c); if (is_object($positioning)) { $positioning->setCulture($product->getCulture()); return $positioning->getKeywords(); } return ''; } /** * Returns producer of the product as a string * @param object $product * @return string */ public static function productGetProducer($product) { if (is_object($product->getProducer())) return $product->getProducer()->getName(); return ''; } public static function productGetLongDesc($product) { return strip_tags($product->getDescription()); } public static function productGetShortDesc($product) { return strip_tags($product->getShortDescription()); } public static function useFriendlyLink($friendly = true) { ini_set('arg_separator.output','&'); sfConfig::set('sf_url_format', $friendly?'PATH':'GET'); } } ?>