<?
header('Content-Type: text/html; charset=utf-8');

// ini_set('display_errors',1);
// error_reporting(E_ALL);

$regexp = '/[,.;\s ]+/';
$country= $_GET[country];
$city= $_GET[city];

$first= preg_split ($regexp, $_GET[country]);
$second= preg_split ($regexp, $_GET[city]);

if ($first[1]== false){
	$first[0] = preg_replace ("/[^a-zа-я]/ui","/",$first[0]);
	unset($first[1]);
}
if ($second[1]== false){
	$second[0] = preg_replace ("/[^a-zа-я]/ui","/",$second[0]);
	unset($second[1]);
}

mysql_set_charset('utf8');
$link = mysql_connect(localhost, 'sletatru_fedort', '*hrkq6J3');
mysql_select_db ('sletatru_fedort', $link);

if ($first[0] == "" ){
	$linkcountry[0] = '0';
}else{
foreach ($first as $count){
	$str_sql_query = "SELECT id FROM dbCountries WHERE name COLLATE UTF8_GENERAL_CI LIKE '$count' OR alias COLLATE UTF8_GENERAL_CI LIKE '$count' OR alias1 COLLATE UTF8_GENERAL_CI LIKE '$count%' OR alias2 COLLATE UTF8_GENERAL_CI LIKE '$count'";
	$countrysSql[] = mysql_query($str_sql_query, $link);
}
foreach ($countrysSql as $countryTable){ 
while ($line = mysql_fetch_array($countryTable, MYSQL_NUM)) { 
foreach ($line as $a){
$linkcountry[] =$a;
}
} 
}
}

if ($second[0] == "" ){
	$linkcity[0] = '832';
}else{
foreach ($second as $cit){
	$str_sql_query = "SELECT id FROM dptCities WHERE name COLLATE UTF8_GENERAL_CI LIKE '$cit' OR synonymus COLLATE UTF8_GENERAL_CI LIKE '$cit' OR synonymus1 COLLATE UTF8_GENERAL_CI LIKE '$cit' OR synonymus2 COLLATE UTF8_GENERAL_CI LIKE '$cit' ";
	$citiesSql[] = mysql_query($str_sql_query, $link);
}
foreach ($citiesSql as $cityTable){
while ($sline = mysql_fetch_array($cityTable, MYSQL_NUM)) {
foreach ($sline as $s=>$a){
$linkcity[] =$a;
}
} 
}
}

if ($linkcountry == NULL){
	$linkcountry[0]=0;
}
if ($linkcity == NULL){
	$linkcity[0]=832;
}
//Тут запиливаем взрослых и детей


preg_match_all('/\d{1,3}/', $_GET[kids], $preg); 
if (!empty($preg[0][1])){
	$kids = 2;
}else{
$kids = $preg[0][0];
}
if ($kids > 3){
	$kids = 0;
}

preg_match_all('/\d{1,3}/', $_GET[adults], $preg); 
if (!empty($preg[0][1])){
	$adults = 2;
}else{
$adults = $preg[0][0];
}
if ($adults == 0){
	$adults = 2;
}else if ($adults > 4){
	$adults = 1;
}

//Тут запилим количество ночей
preg_match_all('/\d{1,3}/', $_GET[nights], $preg); 
$nightsmin = $preg[0][0];
$nightsmax = $preg[0][1];

if ($nightsmin==0){
	$nightsmin = 14 ;
}else if ($nightsmax==0){
	$nightsmax = $nightsmin ;
}else if ($nightsmax < $nightsmin){
	$y = $nightsmax;
	$nightsmax = $nightsmin;
	$nightsmin = $y;
}	
if (($_GET[from] == NULL)&&($_GET[to] != NULL)){
	$dateF = date_create($_GET[to]);
	$dateT = date_create($_GET[to]);
	date_modify($dateF, '-7 day');
	$dateto = date_format($dateT, 'd-m-Y');
	$datefrom = date_format($dateF, 'd-m-Y');
	echo $datefrom, '<br>';
	echo $dateto, '<br>';
}
	if (($_GET[from] != NULL)&&($_GET[to] == NULL)){
	$dateF = date_create($_GET[from]);
	$dateT = date_create($_GET[from]);
	$dateT = date_modify($dateT, '+7 day');
	$dateto = date_format($dateT, 'd-m-Y');
	$datefrom = date_format($dateF, 'd-m-Y');	
} 
if (($_GET[from] != NULL)&&($_GET[to] != NULL)){
	$dateF = date_create($_GET[from]);
	$dateT = date_create($_GET[to]);
	$dateto = date_format($dateT, 'd-m-Y');
	$datefrom = date_format($dateF, 'd-m-Y');
}

//Звезды и еда
$s = preg_replace ("/\[|\]/ui","",$_GET[stars]);
$m = preg_replace ("/\[|\]/ui","",$_GET[meal]);
$st = explode(', ', $s);
$me = explode(', ', $m);


$foodAndStars = array(
    '1*' => '401', //отныне одна звезда - это две звезды!
    '2*' => '401',
    '3*' => '402',
	'4*' => '403',
	'5*' => '404',
	'Apts' => '405',
	'Villas' => '406',
	'HV1' => '410',
	'HV2' => '411',
	'Любая' => '',
	'Любое' => '',
    'UAI' => '116',
    'AI' => '115',
    'FB' => '112',
	'FB+' => '121',
	'HB' => '113',
	'HB+' => '122',
	'BB' => '114',
	'RO' => '117',
);
foreach ($st as $ars){
$star[]= $foodAndStars[$ars];
}
$stars = implode(',' ,$star);

foreach ($me as $al){
$mea[]= $foodAndStars[$al];
}
$meal = implode(',' ,$mea);

if (in_array('Любая', $st)) {
	$stars = '';
}

if (in_array('Любое', $me)) {
	$meal = '';
}

$numbers = count($linkcity) + count($linkcountry);
if ($numbers == 2){
	header("Location: https://s...content-available-to-author-only...t.ru/?callback=search&country={$linkcountry[0]}&city={$linkcity[0]}&stars={$stars}&meal={$meal}&adults={$adults}&kids={$kids}&datefrom={$datefrom}&dateto={$dateto}&nightsmin={$nightsmin}&nightsmax={$nightsmax}");
}else{
foreach ($linkcity as $n=>$id){
	foreach ($linkcountry as $k=>$did){
		echo "<div style=\"padding:10; font-size:20\";>$second[$n] - $first[$k]<a href=https://s...content-available-to-author-only...t.ru/?callback=search&country={$did}&city={$id}&stars={$stars}&meal={$meal}&adults={$adults}&kids={$kids}&datefrom={$datefrom}&dateto={$dateto}&nightsmin={$nightsmin}&nightsmax={$nightsmax} target=_blank style=\"padding:10; font-size:20\";>Link</a></div>";
	}
}
}
?>