<?php

include_once(dirname(__FILE__).'/../../Base/View.php');

class BackupView extends View
{
  var $ItemListFormClass = array(
    'Title' => 'Zálohy',
    'Table' => '(SELECT * FROM `Backup`)',
    'Items' => array(
      'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => ''),
      'Description' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
    ),
  );

  function ShowListOnRow($Row)
  {
    //$Row['Name'] = '<a href="?Action=EmulatorShow&amp;Id='.$Row['Id'].'">'.$Row['Name'].'</a>';
    return($Row);
  }
  
  function ItemList()
  {
    $ServerId = $_GET['Id'];
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Server = new Server($this->System, $_GET['Id']);
      if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Output = '<h4>Dostupné zálohy</h4>';
        $Table = new Table($this->ItemListFormClass, $this->System);
        $Table->OnRow = array($this, 'ShowListOnRow');
        $Table->Definition['Table'] = '(SELECT * FROM `Backup` WHERE `Server` = '.$Server->Id.')';
        $Table->Definition['Items']['Id'] = array('Type' => 'Hidden', 'Caption' => 'Id', 'Default' => '');
        $Table->Definition['Items']['Lock'] = array('Type' => 'Hidden', 'Caption' => 'Zámek', 'Default' => '');
        $Table->LoadValuesFromDatabase($this->Database);
        $Table->Definition['Items']['Actions'] = array('Type' => 'String', 'Caption' => '', 'Default' => '');
        foreach($Table->Values as $Index => $Value)
        {
          $Table->Values[$Index]['Actions'] = '';
          if($Value['Lock'] == 0) $Table->Values[$Index]['Actions'] = '<a href="?Action=BackupDownload&amp;Id='.$Value['Id'].'">Stáhnout</a>';
          if(($Server->Server['Lock'] == 0) and ($Value['Lock'] == 0)) $Table->Values[$Index]['Actions'] .= ' <a href="?Action=BackupRestore&amp;Id='.$Value['Id'].'">Obnovit</a>';
          unset($Table->Values[$Index]['Id']);
          unset($Table->Values[$Index]['Lock']);
        }
        $Output .= $Table->Show();
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR) 
        {
          if($Server->Server['Lock'] == 0) $Output .= '<br /><div style="text-align: center;"><a href="?Action=BackupAdd&amp;Id='.$ServerId.'">Zálohovat</a></dev>';      
        }
      } else $Output = $this->SystemMessage('Zastavení serveru', 'Nemáte oprávnění');
    } else $Output = USER_BAD_ROLE;    
    return($Output);
  }
  
  function Download()
  {
    if(!array_key_exists('Id', $_GET)) $Output = $this->SystemMessage('Stažení souboru zálohy', 'Nebylo zadáno Id zálohy');
    else if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Backup = new Backup($this->System, $_GET['Id']);
      $Server = new Server($this->System, $Backup->Backup['Server']);
      if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        Header('Content-Type: application/x-tar-gz');
        Header('Content-Disposition: attachment; filename="wowhosting-'.$Backup->Id.'.tar.bz2"');
        echo(file_get_contents('../backup/wowhosting-'.$Backup->Id.'.tar.bz2'));
        exit;
      } else $this->SystemMessage('Stažení souboru zálohy', 'Nemáte oprávnění');
    } else $Output = USER_BAD_ROLE;
    return($Output);
  }
  
  function Restore()
  {
    if(!array_key_exists('Id', $_GET)) $Output = $this->SystemMessage('Obnovení ze zálohy', 'Nebylo zadáno Id zálohy');
    else if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Backup = new Backup($this->System, $_GET['Id']);
      $Server = new Server($this->System, $Backup->Backup['Server']);
      if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Output = $this->SystemMessage('Obnovení ze zálohy', $Backup->Restore());
        $Output .= $this->ShowTaskList();
      } else $this->SystemMessage('Obnovení ze zálohy', 'Nemáte oprávnění');
    } else $Output = USER_BAD_ROLE;
    return($Output);
  }
  
  function Add()
  {
    if(!array_key_exists('Id', $_GET)) $Output = $this->SystemMessage('Ladící informace', 'Nebylo zadáno Id serveru');
    else if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Server = new Server($this->System, $_GET['Id']);
      if(($this->System->Modules['User']->User['Id'] == $Server->Server['User']) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Backup = new Backup($this->System, 0);
        $Output = $this->SystemMessage('Ruční zálohování', $Backup->Create($Server->Id));
        $Output .= $this->ShowTaskList();
      } else $this->SystemMessage('Ladící informace', 'Nemáte oprávnění');
    } else $Output = USER_BAD_ROLE;
    return($Output);
  }
}
