<?
if(!$_SESSION['ID_Glob']) exit();
if($id_catalog)
	{
	ob_start();
	echo "<?\n";

	$result = mysql_query("SELECT * FROM catalog WHERE id_catalog = '".$id_catalog."'");
	while($r_result=mysql_fetch_array($result)) 
		{
		echo '$arr_catalog[\'id_catalog\']='.$r_result['id_catalog'].';';
		echo "\n";
		echo '$arr_catalog[\'parent_id\']='.$r_result['parent_id'].';';
		echo "\n";
		echo '$arr_catalog[\'catalog\']=\''.$r_result['catalog'].'\';';
		echo "\n";
		echo '$arr_catalog[\'path\']=\''.$r_result['path'].'\';';
		echo "\n";
		echo '$arr_catalog[\'url\']=\''.$r_result['url'].'\';';
		echo "\n";
		echo '$tree_cat=array('.str_replace(" ", ",", trim($r_result['path'])).",".$r_result['id_catalog'].');';
		echo "\n";

		if($r_result['rayon_obyaz'])
			{
			echo '$arr_catalog[\'rayon_obyaz\']=1;';
			echo "\n";
			}
			
		if($r_result['map'])
			{
			echo '$arr_catalog[\'map\']=1;';
			echo "\n";
			}
			
		if($r_result['tab'])
			{
			echo '$arr_catalog[\'tab\']=1;';
			echo "\n";
			}
			
		if($r_result['stolb'])
			{
			echo '$arr_catalog[\'stolb\']='.$r_result['stolb'].';';
			echo "\n";
			}
			
		if($r_result['h1'])
			{
			echo '$arr_catalog[\'h1\']=\''.$r_result['h1'].'\';';
			echo "\n";
			}
			
		if($r_result['title'])
			{
			echo '$arr_catalog[\'title\']=\''.$r_result['title'].'\';';
			echo "\n";
			}
			
		if($r_result['description'])
			{
			echo '$arr_catalog[\'description\']=\''.$r_result['description'].'\';';
			echo "\n";
			}
			
		if($r_result['keywords'])
			{
			echo '$arr_catalog[\'keywords\']=\''.$r_result['keywords'].'\';';
			echo "\n";
			}
		} 

 
	$list_f = mysql_list_fields ($database, 'field');
	$n = mysql_num_fields($list_f);

	for($i=0; $i<$n; $i++)
		{
		$a[$i][0] = mysql_field_name($list_f, $i);
		$a[$i][1] = mysql_field_type($list_f, $i);
		}

	$result = mysql_query("SELECT * FROM field WHERE id_catalog = '".$id_catalog."' ORDER BY field_num");
	while($r_result=mysql_fetch_array($result)) 
		{
		foreach($a as $aa)
			{
			if($aa[1]=='int' && $r_result[$aa[0]])
				{
				echo '$arr_field['.$r_result['id_field'].'][\''.$aa[0].'\']='.intval($r_result[''.$aa[0].'']).';';
				echo "\n";
				}
			elseif($aa[1]=='blob' && $r_result[$aa[0]])
				{
				echo '$arr_field['.$r_result['id_field'].'][\''.$aa[0].'\']=\''.$r_result[''.$aa[0].''].'\';';
				echo "\n";
				}
			}
	
		if($r_result['field_spisok']) 
			{
			$arr_sps[$r_result['id_field']][0]=$r_result['field_spisok'];
			if($r_result['field_field_two'])
			$arr_sps[$r_result['id_field']][1]=$r_result['field_spisok'];
			}
			
		if($r_result['field_zag']) 
		$arr_zag=1;
		} 

	if($arr_zag)
		{
		echo '$arr_zagg=1;';
		echo "\n";
		}

	if($arr_sps)
		{
		foreach($arr_sps as $index=>$arr_sp)
			{
			echo '$arr_sp['.$index.'][0]='.$arr_sp[0].';';
			echo "\n";
			if($arr_sp[1])
				{
				echo '$arr_sp['.$index.'][1]='.$arr_sp[1].';';
				echo "\n";
				}
			}
		}
	echo "?>";

	$buf = ob_get_contents();
	ob_end_clean();

	file_put_contents("../cashe_catalog/_svod_catalog_".$id_catalog.".php", $buf);
/*
	$file1 = fopen ("../cashe_catalog/_svod_catalog_".$id_catalog.".php", "w");
	flock($file1, LOCK_EX);
	fputs($file1, $buf);
	fclose($file1);
*/
	}

ob_start();
echo "<?\n";

function get_tree_map_all($parent_id) {
      global $out;

	  $query = "SELECT * FROM catalog WHERE id_catalog > 0 AND parent_id = '".$parent_id."' ORDER BY num, id_catalog";
      $result = mysql_query($query);	
	  
      while ($rr = mysql_fetch_array($result)) 
	  	{
		echo'$arr_razdel['.$rr['parent_id'].']['.$rr['id_catalog'].']=\''.$rr['catalog'].'\';';
		echo "\n";
		echo '$arr_razdel_url['.$rr['parent_id'].']['.$rr['id_catalog'].']=\''.$rr['url'].'\';';
		echo "\n";
		get_tree_map_all($rr['id_catalog']);
      	}
      return $out;
} // end function

get_tree_map_all(0);
echo "?>";

$buf = ob_get_contents();
ob_end_clean();

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