Skip to content

Instantly share code, notes, and snippets.

@asyndrige
Last active September 18, 2015 09:45
Show Gist options
  • Save asyndrige/de60dda52ee75dd4e473 to your computer and use it in GitHub Desktop.
Save asyndrige/de60dda52ee75dd4e473 to your computer and use it in GitHub Desktop.
/*CategoryController*/
public static function searchCorrection($key)
{
$keyboardLayout = array('`' => 'ё', 'q' => 'й', 'w' => 'ц', 'e' => 'у', 'r' => 'к', 't' => 'е', 'y' => 'н', 'u' => 'г', 'i' => 'ш', 'o' => 'щ', 'p' => 'з', '[' => 'х', ']' => 'ъ', 'a' => 'ф', 's' => 'ы', 'd' => 'в', 'f' => 'а', 'g' => 'п', 'h' => 'р', 'j' => 'о', 'k' => 'л', 'l' => 'д', ';' => 'ж', "'" => 'э', 'z' => 'я', 'x' => 'ч', 'c' => 'с', 'v' => 'м', 'b' => 'и', 'n' => 'т', 'm' => 'ь', ',' => 'б', '.' => 'ю', '/' => '.', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '0' => '0');
$replace = array('0'=>'7', '1'=>'5', '2'=>'1', '3'=>'8', '4'=>'0', '5'=>'2', '6'=>'9', '7'=>'3', '8'=>'4', '9'=>'6');
$decodedArticleKeyChars = array();
$articleKeyChars = str_replace('а', '', str_replace('f', '', $key));
if(is_numeric($articleKeyChars))
{
$articleKeyCharsAr = str_split($articleKeyChars);
foreach($articleKeyCharsAr as $char)
{
array_push($decodedArticleKeyChars, array_search($char, $replace));
}
}
self::$articleKey = implode($decodedArticleKeyChars) ? implode($decodedArticleKeyChars) : $key;
if(preg_match("/^[\w\d\s.,-]*$/", $key))
{
$correctKeyChars = array();
$keyChars = str_split($key);
foreach($keyChars as $char) {
array_push($correctKeyChars, $keyboardLayout[$char]);
}
self::$inverseKey = implode($correctKeyChars) ? implode($correctKeyChars) : $key;
}
else
{
$correctKeyChars = array();
$keyChars = preg_split('//u', $key, -1, PREG_SPLIT_NO_EMPTY);
foreach($keyChars as $char) {
array_push($correctKeyChars, array_search($char, $keyboardLayout));
}
self::$inverseKey = implode($correctKeyChars) ? implode($correctKeyChars) : $key;
}
}
/*AjaxController*/
public function actionAutocomplete($key)
{
CategoryController::searchCorrection($key);
$autocomplete = Yii::app()->db->createCommand()->select('sp.url as value, spt.name as label')
->from('StoreProduct sp')
->join('StoreProductTranslate spt', 'sp.id=spt.object_id')
->where('(sp.is_active=1 and spt.language_id=1 and spt.name like :key) or
(sp.is_active=1 and spt.language_id=1 and spt.name like :inverseKey) or
(sp.is_active=1 and spt.language_id=1 and sp.external_id like :articleKey)',
array(':key'=>'%'.$key.'%', ':inverseKey'=>'%'.CategoryController::$inverseKey.'%', ':articleKey'=>'%'.CategoryController::$articleKey.'%'))
->limit(8)
->order('sp.added_to_cart_count desc')
->queryAll();
header('Content-Type: application/json');
die(json_encode($autocomplete));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment