<?php

  include('includes/global.php');

  include 'includes/zip.lib.php';

	function WithOutDiacritical($teststring){
		$teststring=StrTr($teststring, "áäčďéěëíňóöřťúůüýžÁÄČĎÉĚËÍŇÓÖŘŠŤÚŮÜÝŽ", "aacdeeeinoorstuuuyzAACDEEEINOORSTUUUYZ");

		return $teststring;
	}

  if (array_key_exists('Type', $_GET)) {
    $Diacritical = @$_GET['Diacritical'];
    if (array_key_exists('CZ', $_GET)) $CZ = $_GET['CZ'];    
    if (array_key_exists('SK', $_GET)) $SK = $_GET['SK'];    
    if (array_key_exists('OtherLanguage', $_GET)) $OtherLanguage = $_GET['OtherLanguage'];
    if (array_key_exists('Use', $_GET)) $Use = $_GET['Use'];
    $Type = $_GET['Type'];
    $Export = $_GET['Export'];
    $AddTitle = $_GET['AddTitle'];
    $NumberVote = $_GET['NumberVote'];
    $Vote = $_GET['Vote'];

      $AddDetails = @$_GET['AddDetails'];
      $AddObjectives = @$_GET['AddObjectives'];
      $AddOfferRewardText = @$_GET['AddOfferRewardText'];
      $AddRequestItemsText = @$_GET['AddRequestItemsText'];
      $AddEndText = @$_GET['AddEndText'];
      $AddObjectiveText1 = @$_GET['AddObjectiveText1'];	
      $AddObjectiveText2 = @$_GET['AddObjectiveText2'];	
      $AddObjectiveText3 = @$_GET['AddObjectiveText3'];	
      $AddObjectiveText4 = @$_GET['AddObjectiveText4'];	

    
    $Buffer = '
/*  File made by maron web aplikacion 
    =================================
    
    WebProject: http://wow.zdechov.net/quests/
    email: maron2@cetrum.cz 
    DateExport: '.date("m.d.y  H:i").'   */
    
    ';
    
    if (isset($Use)) $Buffer .= 'USE `mangos`;
    
    ';

    $sql = "SELECT * FROM quests WHERE Complete = '1' AND CountVote >= '$NumberVote' AND Vote >= '$Vote'";
    $Where = '';
    if (isset($CZ)) $Where .= " Language = '1'";
    if (isset($SK)) {
      if ($Where <> '')
        $Where .= " OR Language = '2'";
      else
        $Where .= "Language = '2'";
    }
    if (isset($OtherLanguage)) {
      if ($Where <> '')
        $Where .= " OR Language = '3'";
      else
        $Where .= "Language = '3'";
    }
    if ($Where <> "")  $sql .= " AND ($Where)";

    $ID = $Database->SQLCommand($sql);
    while ($Line = mysql_fetch_array($ID)) {
      $entry = $Line['entry']; 
      $Title = $Line['Title'];  
      $Details = $Line['Details'];
      $Objectives = $Line['Objectives'];
      $OfferRewardText = $Line['OfferRewardText'];
      $RequestItemsText = $Line['RequestItemsText'];
      $EndText = $Line['EndText'];
      $ObjectiveText1 = $Line['ObjectiveText1'];	
      $ObjectiveText2 = $Line['ObjectiveText2'];	
      $ObjectiveText3 = $Line['ObjectiveText3'];	
      $ObjectiveText4 = $Line['ObjectiveText4'];	
    
    if ($AddTitle == 'Translate')
      $Title = "Title = '$Title'";
    else {
      $IDtitle = $Database->SQLCommand("SELECT Title FROM quests WHERE entry = '$entry' AND Language = '0'");
      $Linetitle = mysql_fetch_array($IDtitle);

      if ($AddTitle == 'EN') {   
        $Title = "Title = '".$Linetitle['Title']."'";
      }
      if ($AddTitle == 'Both') {   
        $Title = "Title = '$Title (".$Linetitle['Title'].")'";
      }
    }
    if (($Details <> '') and ($AddDetails))
      $Details = "Details = '$Details', ";
    else $Details = '';
    if ($Objectives <> '' and $AddObjectives)
      $Objectives = 'Objectives = \''.$Objectives.'\', ';
    else $Objectives= '';
    if ($OfferRewardText <> '' and $AddOfferRewardText)
      $OfferRewardText = 'OfferRewardText = \''.$OfferRewardText.'\', ';
    else $OfferRewardText = '';
    if ($RequestItemsText <> '' and $AddRequestItemsText)
      $RequestItemsText = 'RequestItemsText = \''.$RequestItemsText.'\', ';
    else $RequestItemsText = '';
  //  echo  $entry.$Details;
    // UPDATE  SET Title = '', Title = '$Title', Details = '$Details', Objectives = '$Objectives', OfferRewardText = '$OfferRewardText', RequestItemsText = '$RequestItemsText' WHWRE entry = '$entry'
    $sql = "UPDATE quest_template SET $Details $Objectives $OfferRewardText $RequestItemsText $Title WHERE entry = '$entry';"; 
    if ($Diacritical == False) $sql=WithOutDiacritical($sql);
    
    $Buffer .= $sql." 
    ";

  }  
  
  if ($Export == 'Display') {
    echo '<textarea rows="30" cols="100">';
    echo $Buffer;
    echo '</textarea>';
  }
  
  if ($Export == 'Zip') {
      if (@function_exists('gzcompress')) {
        $save_filename = 'SqlTranslate.zip';
        $SQL_filename = 'SqlTranslate.sql';
          
        $zipfile = new zipfile();
        $zipfile -> addFile($Buffer, $SQL_filename);
        $Buffer = $zipfile -> file();

        $file_handle = @fopen($save_filename, 'w');
        $write_result = @fwrite($file_handle, $Buffer);
        fclose($file_handle);
        header("Location: ".$save_filename);

      }
  }
    
    
} else {
?>
  <form action="export.php" method="get">
    <table border="1">
      <tr><th>Sql</th><th>Jazyk</th><th>Texty</th></tr>
      <tr><td>
        <input type="radio" name="Export" value="Zip" checked="true"> Exportovat do zip<br>
        <input type="radio" name="Export" value="Display"> Export zobrazit<br>
        <br />
        <input type="radio" name="Type" value="Insert"> Insert (zatím nefunguje)<br>
        <input type="radio" name="Type" value="Update" checked="true"> Update <br>
        <input type="checkbox" name="Use" checked="true"> Přidat Use mangos <br> 
        <br /> 
        <b>Filtr:</b><br />
        <input type="text" name="NumberVote" value="4" size="1"> Počet nutných hlasů <br>                                                               
        <input type="text" name="Vote" value="3" size="1"> Známka lepší jak                                                               
      </td><td>
        <input type="checkbox" name="Diacritical" checked="true"> Včetně diakritiky <br>                                                                 
        <input type="checkbox" name="CZ" checked="true"> Český překlad <br>                                                                 
        <input type="checkbox" name="SK" checked="true"> Slovenský překlad <br>                                                                 
        <input type="checkbox" name="OtherLanguage"> Jiné jazyky <br>
      </td><td>
        <b>Title questu:</b><br>
        <input type="radio" name="AddTitle" value="Translate" checked="true"> Přeložený název <br>
        <input type="radio" name="AddTitle" value="EN"> Původní název <br>
        <input type="radio" name="AddTitle" value="Both"> Přeložený název (Anglický název) <br>
    <br>                                                                 
<?php    
  foreach($Texts as $Index => $TextItem)
    echo '<input type="checkbox" name="Add'.$TextItem[0].'" checked="true"> '.$TextItem[0].'<br>';
?>
      </td></tr>
    </table>
    <br><input type="submit" value="Generovat">
  </form>  
<?php
}
  
          
  ShowFooter();      
?>
