﻿<?
include("../conn.php");


$q = $_GET['q'];
$err = $_GET['err'];
$info = $_GET['info'];
$err_text = array("Toto ID nebylo nalezeno v databázi!", "Při vkládání do seznamu došlo k chybě!", "Při exportu došlo k chybě!", "Položku se nepodařilo vymazat ze seznamu!");
$info_text = array("Přidání do seznamu proběhlo úspěšně!", "Export proběhl vpořádku", "Vymazání položky ze seznamu proběhlo úspěšně");

if(empty($q)){
//////////////////////////////////////////////////////////Základní zobrazení




if(!empty($err)):
$err = $err - 1;
echo"<p align=center><font color=red><b>$err_text[$err]</b></font>";
endif;
if(!empty($info)):
$info = $info - 1;
echo"<p align=center><font color=green><b>$info_text[$info]</b></font>";
endif;

echo"<br />
<br />
";

echo"<form method=post action=index.php?q=1&i=gameobject><input type=submit value='Přidat object do seznamu exportovaných'></form>";
echo"<form method=post action=index.php?q=1&i=creature><input type=submit value='Přidat NPC do seznamu exportovaných'></form>";
echo"<br><br>";
echo"<form method=post action=index.php?q=3&i=gameobject><input type=submit value='Zobrazit seznam objectů'></form>";
echo"<form method=post action=index.php?q=3&i=creature><input type=submit value='Zobrazit seznam NPC'></form>";
echo"<br><br>";
echo"<form method=post action=index.php?q=2&i=gameobject><input type=submit value='Vytvořit export objectů'></form>";
echo"<form method=post action=index.php?q=2&i=creature><input type=submit value='Vytvořit export NPC'></form>";

} elseif($q==1){
//////////////////////////////////////////////////////////Přidávání do seznamů exportovaných
$typ = $_GET['i'];
$instal = $_POST['instal'];

if(empty($instal)):
////formulář před přidáním
echo"<form method=post action=index.php?q=1&i=$typ>
ID ($typ): <input type=\"text\" name='guid'><br />
<input type=hidden name=instal value=1>
<input type=submit value='Přidat do seznamu'>
</form>
";
else:
////instalace do seznamu
$guid = $_POST['guid'];

mysql_connect($SQL_Server, $SQL_Uzivatel, $SQL_Heslo);
  mysql_select_db($Databaze_world);
    mysql_query("SET NAMES 'utf8'");
$existuje_guid = mysql_num_rows(mysql_query("SELECT id from $typ where id=$guid"));
	
	if($existuje_guid==0):
	echo"<meta http-equiv=\"refresh\" content=\"0.1;url=index.php?err=1\">";
	else:
	mysql_select_db($Databaze);
	$pridat_do_seznamu = mysql_query("insert into export_list values ('$typ', '$guid')");
	
	if($pridat_do_seznamu):
	echo"<meta http-equiv=\"refresh\" content=\"0.1;url=index.php?info=1\">";
	else:
    echo"<meta http-equiv=\"refresh\" content=\"0.1;url=index.php?err=2\">";
	endif;
	
	endif;
endif;

} elseif($q==2){
//////////////////////////////////////////////////////////Export
$typ = $_GET['i'];
$datum = date("Y-m-d");
////////////////Vytvoření souboru:
$soubor = fopen("$typ.sql", "w");


$hotovo = 0;
$x = 0;

mysql_connect($SQL_Server, $SQL_Uzivatel, $SQL_Heslo);
    mysql_select_db($Databaze);
    mysql_query("SET NAMES 'utf8'");
	 $vyhledat_guid_exportovanych = mysql_query("select * from export_list where typ='$typ'");
	
	while($row = mysql_fetch_array($vyhledat_guid_exportovanych)){
	$guid_exp = $row['guid'];
	
	mysql_select_db($Databaze_world);
    mysql_query("SET NAMES 'utf8'");
	 $vypsat_data_exportovanych = mysql_query("select * from $typ where id=$guid_exp"); 
	 $pocet_exportovanych = mysql_num_rows(mysql_query("select id from $typ where id=$guid_exp"));
	 while($a = mysql_fetch_array($vypsat_data_exportovanych)):
	 
	 if($typ=="gameobject"):
	
	 $prikaz = "INSERT INTO gamobject values('', '".$a['id']."', '".$a['map']."', '".$a['spawnMask']."', '".$a['position_x']."', '".$a['position_y']."', '".$a['position_z']."', '".$a['orientation']."', '".$a['rotation0']."', '".$a['rotation1']."', '".$a['rotation2']."', '".$a['rotation3']."', '".$a['spawntimesecs']."', '".$a['animprogress']."', '".$a['state']."');";
	 $prikaz .= "\n";
	 elseif($typ=="creature"):
	  
	 $prikaz = "INSERT INTO creature values('', '".$a['id']."', '".$a['map']."', '".$a['spawnMask']."', '".$a['modelid']."', , '".$a['equipment_id']."', '".$a['position_x']."', '".$a['position_y']."', '".$a['position_z']."', '".$a['orientation']."', '".$a['spawntimesecs']."', '".$a['spawndist']."', '".$a['currentwaypoint']."', '".$a['curhealth']."', '".$a['curmana']."', '".$a['DeathState']."', '".$a['MovementType']."');";
	 $prikaz .= "\n";
	 endif;
	
	 $zapis = fwrite($soubor,$prikaz);
   
   if($zapis):
$hotovo = $hotovo + 1;
  endif;
  
  $x = $x + 1;
	  endwhile;
	////////////////vložení informací do souboru:
  

 
	}
fclose($soubor);
if($hotovo==$x):
echo"<meta http-equiv=\"refresh\" content=\"0.1;url=index.php?info=2\">";
else:
echo"<meta http-equiv=\"refresh\" content=\"0.1;url=index.php?err=3\">";
endif;

} elseif($q==3){
$typ = $_GET['i'];
$delete = $_POST['delete'];

if(empty($delete)):
if(!empty($err)):
$err = $err - 1;
echo"<p align=center><font color=red><b>$err_text[$err]</b></font>";
endif;
if(!empty($info)):
$info = $info - 1;
echo"<p align=center><font color=green><b>$info_text[$info]</b></font>";
endif;
echo"<p align=center>Seznam $typ ID k exportu<br>
<table align=center><tr><td><b>ID</b><td>&nbsp;";
mysql_connect($SQL_Server, $SQL_Uzivatel, $SQL_Heslo);
    mysql_select_db($Databaze);
    mysql_query("SET NAMES 'utf8'");
	$vyhledat_data_v_seznamu = mysql_query("select * from export_list where typ='$typ'");
	 while($row = mysql_fetch_array($vyhledat_data_v_seznamu)){
	 $id = $row['guid'];
	 echo"<tr><td>$id</td><form method=post action=index.php?q=3&i=$typ><td><input type=hidden name=delete value=1><input type=hidden name=id value='$id'><input type=submit value='Smazat ze seznamu'></form>";
	 
	 }
echo"</table><br>
<br>
<a href=index.php><-- Zpět do menu</a>";
elseif($delete==1):
$id = $_POST['id'];

mysql_connect($SQL_Server, $SQL_Uzivatel, $SQL_Heslo);
    mysql_select_db($Databaze);
    mysql_query("SET NAMES 'utf8'");
	$vyhledat_data_v_seznamu = mysql_query("delete from export_list where guid='$id' LIMIT 1");
	if($vyhledat_data_v_seznamu):
	echo"<meta http-equiv=\"refresh\" content=\"0.1;url=index.php?q=3&i=$typ&info=3\">";
	else:
	echo"<meta http-equiv=\"refresh\" content=\"0.1;url=index.php?q=3&i=$typ&err=4\">";
	endif;
endif;
}





















