<?
if(!$_SESSION['ID_Glob']) exit();

function _svod_reg ($a) 
{
global $m;
	$r = mysql_query("SELECT * FROM region WHERE parent_id = '".$a."' ORDER BY num, region");
	$nr=mysql_num_rows($r);
	if($nr)
		{
		while ($rr = mysql_fetch_array($r))
			{
			$m .= '$arr_regions['.$rr['parent_id'].']['.$rr['id_region'].']=\''.$rr['region'].'\';';
			$m .= "\n";
			$m .= '$arr_regions_url['.$rr['parent_id'].']['.$rr['id_region'].']=\''.$rr['url'].'\';';
			$m .= "\n";
			_svod_reg($rr['id_region']);
			}
		}
return $m;
} 


if($parent_glob)
{
$m='';
ob_start();

echo "<?\n";
echo _svod_reg($parent_glob);

	$r = mysql_query("SELECT * FROM region WHERE rayon='1' AND parent_glob = '".$parent_glob."'");
	$nr=mysql_num_rows($r);
	if($nr)
		{
		while ($rr = mysql_fetch_array($r))
			{
			$arrq[]=$rr['id_region'];
			}
		$f=implode(",",$arrq);
		echo '$arr_rayon=array('.$f.');';
		echo "\n";
		}


echo "?>";

$buf = ob_get_contents();
ob_end_clean();

file_put_contents("../cashe_region/_svod_region_".$parent_glob.".php", $buf);
/*
$file1 = fopen ("../cashe_region/_svod_region_".$parent_glob.".php", "w");
flock($file1, LOCK_EX);
fputs($file1, $buf);
fclose($file1);
*/
ob_start();
echo "<?\n";
	$r = mysql_query("SELECT COUNT(id_region) as cou, parent_id FROM region WHERE rayon = '1' GROUP BY parent_id");
	$nr=mysql_num_rows($r);
	if($nr)
		{
		while ($rr = mysql_fetch_array($r))
			{
			echo '$arr_rayon_yes[]='.$rr['parent_id'].';';
			echo "\n";
			}
		}
echo "?>";

$buf = ob_get_contents();
ob_end_clean();

file_put_contents("../cashe_n/_svod_rayon.php", $buf);
/*
$file1 = fopen ("../cashe_n/_svod_rayon.php", "w");
flock($file1, LOCK_EX);
fputs($file1, $buf);
fclose($file1);
*/
}
else
{
ob_start();

echo "<?\n";
	$r = mysql_query("SELECT * FROM region WHERE parent_id = '0' ORDER BY num, region");
	$nr=mysql_num_rows($r);
	if($nr)
		{
		while ($rr = mysql_fetch_array($r))
			{
			echo '$arr_regions['.$rr['parent_id'].']['.$rr['id_region'].']=\''.$rr['region'].'\';';
			echo "\n";
			echo '$arr_regions_url['.$rr['parent_id'].']['.$rr['id_region'].']=\''.$rr['url'].'\';';
			echo "\n";
			}
		}
echo "?>";

$buf = ob_get_contents();
ob_end_clean();

file_put_contents("../cashe_region/_svod_region_0.php", $buf);
/*
$file1 = fopen ("../cashe_region/_svod_region_0.php", "w");
flock($file1, LOCK_EX);
fputs($file1, $buf);
fclose($file1);
*/
}
?>
