Created
October 13, 2014 09:56
-
-
Save Realetive/fafc9d6912c2b1ca42e4 to your computer and use it in GitHub Desktop.
simple test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Realetive | |
* Date: 13.10.14 | |
* Time: 9:49 | |
*/ | |
$group_city = [ | |
'spb' => [ | |
'status' => 1 | |
, 'name' => 'Санкт-Петербург' | |
, 'parent' => 1083 | |
] | |
, 'msk' => [ | |
'status' => 1 | |
, 'name' => 'Москва' | |
, 'parent' => 5122 | |
] | |
, 'sam' => [ | |
'status' => 0 | |
, 'name' => 'Самара' | |
, 'parent' => 1 | |
] | |
, 'arm' => [ | |
'status' => 0 | |
, 'name' => 'Армавир' | |
, 'parent' => 1 | |
] | |
, 'prm' => [ | |
'status' => 0 | |
, 'name' => 'Пермь' | |
, 'parent' => 1 | |
] | |
]; | |
$defparam = 'city'; | |
$defkey = 'spb'; | |
$expires = time() + 2678400; | |
$path = '/shop/'; | |
/** | |
* @param $AVarray | |
* @param $AVkey | |
* @param $AVreturn == 'name'||'key'||'parent' | |
* @return string | |
*/ | |
function AValidator (&$AVarray, $AVkey = 'spb', $AVreturn = 'name') { | |
switch ($AVreturn) { | |
case 'key': | |
$returnKey = 'Ключ не найден'; | |
foreach ($AVarray as $key => $value) { | |
if ($value['status'] != 0 && $value['name'] == $AVkey) { | |
$returnKey = $key; | |
break; | |
} | |
} | |
return $returnKey; | |
break; | |
case 'parent': | |
if (!empty($AVarray[$AVkey]['parent'])) { | |
return $AVarray[$AVkey]['parent']; | |
} else { | |
$returnParent = 'Родитель не найден'; | |
foreach ($AVarray as $key => $value) { | |
if ($value['status'] != 0 && $value['name'] == $AVkey) { | |
$returnParent = $value['parent']; | |
break; | |
} | |
} | |
return $returnParent; | |
} | |
break; | |
default: | |
if (!empty($AVarray[$AVkey][$AVreturn])) { | |
return $AVarray[$AVkey][$AVreturn]; | |
} | |
break; | |
} | |
} | |
if (isset($_GET[$defparam])) { | |
// Есть гет | |
$param = $_GET[$defparam]; | |
// Проверка гета на валидность | |
if (!empty(AValidator($group_city, $param, 'key'))) { | |
// Гет прошел проверку | |
// Устанавливаем новые куки … | |
setcookie($defparam, AValidator($group_city, $param, 'key'), $expires, $path); | |
// … и плейсхолдеры | |
$modx->toPlaceholders( | |
[ | |
'key' => AValidator($group_city, $param, 'key') | |
, 'name' => $param | |
, 'parent' => AValidator($group_city, $param, 'parent') | |
] | |
, 'affiliator' | |
); | |
} | |
} elseif (!isset($_GET[$defparam]) || empty(AValidator($group_city, $_GET[$defparam], 'key'))) { | |
if (isset($_COOKIE[$defparam])) { | |
$param = $_COOKIE[$defparam]; | |
// Есть куки | |
if (!empty(AValidator($group_city, $param, 'name'))) { | |
// Куки прошли проверку | |
$modx->toPlaceholders( | |
[ | |
'key' => $param | |
, 'name' => AValidator($group_city, $param, 'name') | |
, 'parent' => AValidator($group_city, $param, 'parent') | |
] | |
, 'affiliator' | |
); | |
} else { | |
// Куки не прошли проверку | |
// Устанавливаем по дефолту значение кук … | |
setcookie($defparam, $defkey, $expires, $path); | |
// … и плейсхолдеров | |
$modx->toPlaceholders( | |
[ | |
'key' => $defkey | |
, 'name' => AValidator($group_city, $defkey, 'name') | |
, 'parent' => AValidator($group_city, $defkey, 'parent') | |
] | |
, 'affiliator' | |
); | |
} | |
} else { | |
// Нет кук | |
// Устанавливаем по дефолту значение кук … | |
setcookie($defparam, $defkey, $expires, $path); | |
// … и плейсхолдеров | |
$modx->toPlaceholders( | |
[ | |
'key' => $defkey | |
, 'name' => AValidator($group_city, $defkey, 'name') | |
, 'parent' => AValidator($group_city, $defkey, 'parent') | |
] | |
, 'affiliator' | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment