<?php

class ModuleDownload extends Module
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'Download';
    $this->Version = '1.0';
    $this->Creator = 'Maron';
    $this->License = 'GNU/GPL';
    $this->Description = 'Show list of selected export and link to static files';
    $this->Dependencies = array();
  }

  function DoStart(): void
  {
    $this->System->RegisterPage(['download'], 'PageDownload');
    Core::Cast($this->System)->RegisterMenuItem(array(
      'Title' => T('Download'),
      'Hint' => T('List of files for download'),
      'Link' => $this->System->Link('/download/'),
      'Permission' => LICENCE_ANONYMOUS,
      'Icon' => '',
    ), 1);
  }
}

class PageDownload extends Page
{
  function ShowFiles()
  {
    $fileslink = $this->System->Link('/files');
    $Output = '<h3>'.T('Accessories for client').'</h3>'.
  '<strong><a href="'.$fileslink.'/WowLua/wowpatcher.exe">wowpatcher</a></strong> - '.T('Tool to command line, which search and edit file wow.exe for support editing files Glue Strings.lua and GlobalStrings.lua.').' '.
  T('Designed for one-time creation of new modified version of wow.exe. Help can be called with the - help option. The ability to modify future versions not guaranteed.').' '.
  '<br /><br/>'.
  '<strong>WowLua</strong> - '.T('Specific modified version of WoW.exe for free servers with support for translation Lua files').'<br/>'.
  'enGB a enUS: '.
  ' <a href="'.$fileslink.'/WowLua/WowLua_3.3.5a.exe">3.3.5a</a>'.
  ' <a href="'.$fileslink.'/WowLua/WowLua_3.3.3a.exe">3.3.3a</a>'.
  ' <a href="'.$fileslink.'/WowLua/WowLua_3.3.2.exe">3.3.2</a>'.
  ' <a href="'.$fileslink.'/WowLua/WowLua_3.3.0a.exe">3.3.0a</a>'.
  ' <a href="'.$fileslink.'/WowLua/WowLua_2.4.3.exe">2.4.3</a>'.
  ' <a href="'.$fileslink.'/WowLua/WowLua_1.12.1.exe">1.12.1</a>'.
  '<br/><br/>'.
  '<strong>WoWMe</strong> - '.T('Edited files wow.exe with support of translation dbc files').':<br/>'.
  '<a href="'.$fileslink.'/WoWMe/WoWMe_4.3.4.zip">4.3.4</a> '.
  '<a href="'.$fileslink.'/WoWMe/WoWMe_3.2.0.rar">3.2.0</a> '.
  '<a href="'.$fileslink.'/WoWMe/WoWMe_3.1.3.rar">3.1.3</a> '.
  '<a href="'.$fileslink.'/WoWMe/WoWMe_3.0.3.zip">3.0.3</a> '.
  '<a href="'.$fileslink.'/WoWMe/WoWMe_2.4.3.zip">2.4.3</a> '.
  '<br/><br/>'.
  '<strong>CzechWoW</strong> - '.T('Czech for client with edited client files (old)').'<br/>';

  $Output .= '<a href="'.$fileslink.'/CzechWoW/Instalace_CzechWoW_3.3.5a_2010-09-09.exe">Instalace CzechWoW 3.3.5a 2010-09-09.exe</a><br/>'.
  '<a href="'.$fileslink.'/CzechWoW/Instalace_CzechWoW_3.3.3a_2010-04-23.exe">Instalace CzechWoW 3.3.3a 2010-04-23.exe</a><br/>'.
  '<a href="'.$fileslink.'/CzechWoW/Instalace_CzechWoW_3.3.2_2010-04-12.exe">Instalace CzechWoW 3.3.2 2010-04-12.exe</a><br/>'.
  '<br/>'.
  '<strong>'.T('Other add-ons').':</strong><br/>'.
  '<a href="'.$fileslink.'/ceske_fonty_do_wow.zip">'.T('Czech fonts for game').'</a> - '.T('original fonts from game with added czech symbols.').'<br />'.
  '<a href="http://jenicko.savana.cz/wow/forum/viewtopic.php?id=218&amp;action=all">'.T('Czech shouts from W3').'</a> - '.T('sound files taken from Warcraft 3 and used in wow.').'<br />
  <br />'.
  '<h3>'.T('Add-ons for server').':</h3>'.

  '<a href="http://mangos.cjb.net/forums/index.php?showforum=15">'.T('Czech to Minimanager').'</a><br />
  <a href="http://mangos.cjb.net/forums/index.php?showtopic=359">'.T('Czech for AoWoW').'</a> <a href="'.$fileslink.'/AoWoW.zip">'.T('Czech for AoWoW').' (zip)</a> - '.T('Web viewer database for emulator mangos like wowhead.com').'<br />
  <br />';

  $Output .= '<h3>'.T('Tools for help').':</h3>'.
  '<a href="http://zezula.net/download/mpqediten32.zip">Ladik\'s MPQ Editor</a> <a href="http://zezula.net/">'.T('Author page').'</a><br />'.
  '<a href="'.$fileslink.'/mpq.exe">mpq.exe</a> '.T('command line program for adding files to MPQ archives, created by Maron').'<br />'.
  '<a href="'.$fileslink.'/wowsig.exe">WoWsig.exe</a> addon signature generator by wad (2005)<br />'.
  '<a href="'.$fileslink.'/DBCtoCSV.exe">DBCtoCSV</a> - '.T('tool for transform DBC files to CSV').'<br />'.
  '<a href="'.$fileslink.'/ClientDBExtractor.exe">ClientDBExtractor.exe</a> - '.T('tool for exporting dbc files from game client').'<br />';
  return $Output;
  }

  function ShowDownload()
  {
    $Output = '<h3>'.T('Download files').'</h3><br />';

    $Output .= T('Additional files: modified wow.exe, fonts to game, translated interface aowow and more can be found on page').
      ' <a href="'.$this->System->Link('/download/?Files').'">'.T('Files').'</a><br />';
    $Output .= T('If none of these files is suitable for you, for example, to the Czech without the translated interface. You can generate your own section in section').' <a href="'.$this->System->Link('/export').'">'.T('Export').'</a><br /><br />';
    $Output .= T('The following files are generated every day, if needed. That means if is added new translation to database. Therefore, if you translate a translation or correct the mistake, tomorrow you can download the new patched version. Or you can rebuild and download for a few minutes the repaired file.').'<br />';
    $Output .= T('If you no longer want translation in game, you can uninstall it by file Uninstall.exe at the directory of game.').'<br />';
    $Output .= '<br /><br />';

    $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Export` WHERE `Featured`=1');
    $DbRow = $DbResult->fetch_row();
    $PageList = GetPageList($DbRow[0]);

    $Output .= $PageList['Output'];
    $TableColumns = array(
      array('Name' => 'Version', 'Title' => T('Download')),
      array('Name' => 'Title', 'Title' => T('Name2'),),
      array('Name' => 'TimeCreate', 'Title' => T('Creation date')),
      array('Name' => 'Description', 'Title' => T('Description')),
    );
    $Order = GetOrderTableHeader($TableColumns, 'Id', 1);
    $Output .= '<table class="BaseTable">'.
      $Order['Output'];
    $DbRows = $this->Database->query('SELECT `ClientVersion`.`Version` AS `Version`, '.
      '`Export`.`Id`, `Export`.`ClientVersion`, `Export`.`OutputType`, `Export`.`Title`, '.
      '`Export`.`Description` FROM `Export` '.
      'JOIN `ClientVersion` as `ClientVersion` ON `ClientVersion`.`Id`=`Export`.`ClientVersion` '.
      'WHERE `Featured`=1 '.$Order['SQL'].$PageList['SQLLimit']);
    while ($DbExport = $DbRows->fetch_assoc())
    {
        $ExportId = $DbExport['Id'];

        if ($DbExport['OutputType'] == 10)
          $filename = Core::Cast($this->System)->Config['Web']['TempFolder'].'Export/'.$ExportId.'/'.'Instalace_CzechWoW_'.$DbExport['Version'].'.exe';
        if ($DbExport['OutputType'] == 7)
          $filename = Core::Cast($this->System)->Config['Web']['TempFolder'].'Export/'.$ExportId.'/'.'CzWoW_Addon-'.$DbExport['Version'].'.zip';

        if ($DbExport['OutputType'] == 10)
          $Output .= '<tr><td><a href="'.$this->System->Link('/'.$filename).
            '">CzechWoW_'.$DbExport['Version'].'.exe</a><br /> <a href="'.
            $this->System->Link('/export/?Action=View&amp;ExportId='.$ExportId).'">Export '.$ExportId.'</a></td>';
        else
        if ($DbExport['OutputType'] == 7)
          $Output .= '<tr><td><a href="'.$this->System->Link('/'.$filename).
            '">CzWoW_Addon-'.$DbExport['Version'].'.zip</a><br /> <a href="'.
            $this->System->Link('/export/?Action=View&amp;ExportId='.$ExportId).'">Export '.$ExportId.'</a></td>';
        else
          $Output .= '<tr><td><a href="'.$this->System->Link('/export/?Action=View&amp;ExportId='.
            $ExportId.'&amp;Tab=7').'">'.T('Make export').'</a><br /> <a href="'.
            $this->System->Link('/export/?Action=View&amp;ExportId='.$ExportId).'">Export '.$ExportId.'</a></td>';

        $Output .= '<td>'.$DbExport['Title'].'</td>'; //.'<td>'.$DbExport['ClientVersion'].'</td>';

        if ((($DbExport['OutputType'] == 10) or ($DbExport['OutputType'] == 7)) and (file_exists($filename)))
          $Output .= '<td>'.date('d.m.y H:i',filemtime($filename)).'</td>';
        else
          $Output .= '<td>&nbsp;</td>';

          $Output .=
          '<td>'.str_replace("\n", '<br />',$DbExport['Description']).'</td>'.
          '</tr>';
    }
    $Output .= '</table>'.
      $PageList['Output'];

    return $Output;
  }

  function Show(): string
  {
    $this->Title = T('Download');
    $Output = '';
    if (isset($_GET['Files'])) $Output .= $this->ShowFiles();
    else $Output .= $this->ShowDownload();

    return $Output;
  }
}
