<?php

include_once(dirname(__FILE__).'/../../Base/View.php');

class TaskView extends View
{
  var $ItemListFormClass = array(
    'Title' => 'Seznam úloh',
    'Table' => 'Task',
    'DefaultOrderColumn' => 'TimeCreate',
    'DefaultOrderDirection' => 1,
    'Items' => array(
      'TimeCreate' => array('Type' => 'DateTime', 'Caption' => 'Čas vytvoření', 'Default' => ''),
      'Title' => array('Type' => 'String', 'Caption' => 'Operace', 'Default' => ''),
      'State' => array('Type' => 'TaskState', 'Caption' => 'Stav', 'Default' => ''),
      'Duration' => array('Type' => 'String', 'Caption' => 'Trvání', 'Default' => ''),
    ),
  );
  
  function ShowListOnRow($Row)
  {
    $Row['Duration'] = $this->System->AddPrefixMultipliers($Row['Duration'], '', 4, 'Time');
    return($Row);
  }
  
  function ItemList()
  {
    $Output = '<h4>Fronta úloh</h4>';
    $Table = new Table($this->ItemListFormClass, $this->System);
    $Table->OnRow = array($this, 'ShowListOnRow');
    $Table->Definition['Table'] = '(SELECT Id, (COALESCE(UNIX_TIMESTAMP(TimeEnd), UNIX_TIMESTAMP(NOW())) - UNIX_TIMESTAMP(TimeStart)) AS Duration, TimeCreate, Title, State FROM Task WHERE User='.
      $this->System->Modules['User']->Data['Id'].' ORDER BY Id DESC)';
    $Table->Definition['Items']['Id'] = array('Type' => 'Hidden', 'Caption' => '', 'Default' => '');
    $Table->LoadValuesFromDatabase($this->Database);
    $Table->Definition['Items']['Actions'] = array('Type' => 'String', 'Caption' => '', 'Default' => '');
    foreach($Table->Values as $Index => $Item)
    {
      $Table->Values[$Index]['Actions'] = '<a href="?Module=Task&amp;Action=ErrorLog&amp;Id='.$Item['Id'].'">Výpis chyb</a> <a href="?Module=Task&amp;Action=StandardLog&amp;Id='.$Item['Id'].'">Výpis výstupu</a> ';
      unset($Table->Values[$Index]['Id']);
    }
    $Output .= $Table->Show();
    $Output .= '<br /><div style="text-align: center;"><a href="?Module=Task&amp;Action=ItemList">Obnovit pohled</a></dev>';      
    return($Output);
  } 
  
  function ErrorLog()
  {
    $DbResult = $this->Database->select('Task', 'Title, Error, State', 'Id='.$_GET['Id']);
    $DbRow = $DbResult->fetch_assoc();   
    $Output = '<h4>Chybový výpis úlohy: '.$DbRow['Title'].'</h4>';
    if($DbRow['State'] == 1) $Output .= '<pre>'.file_get_contents('../temp/wowhosting_script.sh.err').'</pre>';
      else $Output .= '<pre>'.$DbRow['Error'].'</pre>';
    return($Output);
  }

  function StandardLog()
  {
    $DbResult = $this->Database->select('Task', 'Title, Output, State', 'Id='.$_GET['Id']);
    $DbRow = $DbResult->fetch_assoc();
    $Output = '<h4>Výpis výstupu úlohy: '.$DbRow['Title'].'</h4>';
    if($DbRow['State'] == 1) $Output .= '<pre>'.file_get_contents('../temp/wowhosting_script.sh.log').'</pre>';
    $Output .= '<pre>'.$DbRow['Output'].'</pre>';
    return($Output);
  }
}
