Index: branches/RC/core/units/categories/categories_tag_processor.php =================================================================== diff -u -N -r10538 -r10908 --- branches/RC/core/units/categories/categories_tag_processor.php (.../categories_tag_processor.php) (revision 10538) +++ branches/RC/core/units/categories/categories_tag_processor.php (.../categories_tag_processor.php) (revision 10908) @@ -769,6 +769,86 @@ return $this->Application->HREF($t, '', $params); } + + + /** + * Returns spelling suggestions against search keyword + * + * @param Array $params + * @return string + */ + function SpellingSuggestions($params) + { + $keywords = unhtmlentities( trim($this->Application->GetVar('keywords')) ); + if (!$keywords) { + return ; + } + + // 1. try to get already cached suggestion + $suggestion = $this->Application->getCache('search.suggestion', $keywords); + if ($suggestion !== false) { + return $suggestion; + } + + $table_name = $this->Application->getUnitOption('spelling-dictionary', 'TableName'); + + // 2. search suggestion in database + $sql = 'SELECT SuggestedCorrection + FROM ' . $table_name . ' + WHERE MisspelledWord = ' . $this->Conn->qstr($keywords); + $suggestion = $this->Conn->GetOne($sql); + if ($suggestion !== false) { + $this->Application->setCache('search.suggestion', $keywords, $suggestion); + return $suggestion; + } + + // 3. suggestion not found in database, ask webservice + $app_id = $this->Application->ConfigValue('YahooApplicationId'); + $url = 'http://search.yahooapis.com/WebSearchService/V1/spellingSuggestion?appid=' . $app_id . '&query='; + + $curl_helper =& $this->Application->recallObject('CurlHelper'); + /* @var $curl_helper kCurlHelper */ + + $xml_data = $curl_helper->Send($url . urlencode($keywords)); + + $xml_helper =& $this->Application->recallObject('kXMLHelper'); + /* @var $xml_helper kXMLHelper */ + + $root_node =& $xml_helper->Parse($xml_data); + + $result = $root_node->FindChild('RESULT'); + /* @var $result kXMLNode */ + + if (is_object($result)) { + // webservice responded -> save in local database + $fields_hash = Array ( + 'MisspelledWord' => $keywords, + 'SuggestedCorrection' => $result->Data, + ); + + $this->Conn->doInsert($fields_hash, $table_name); + $this->Application->setCache('search.suggestion', $keywords, $result->Data); + + return $result->Data; + } + + return ''; + } + + /** + * Shows link for searching by suggested word + * + * @param Array $params + * @return string + */ + function SuggestionLink($params) + { + $params['keywords'] = $this->SpellingSuggestions($params); + + return $this->Application->ProcessParsedTag('m', 'Link', $params); + } + + } ?> \ No newline at end of file