<?php

$ScriptStartTime = GetMicrotime();

class Page extends Module
{
  var $TimeStart;
  var $PathTree = array('Informační systém',
    'index.php' => '',
  );

  function __construct()
  {
    $this->TimeStart = GetMicrotime();   // Get script start time
  }

  function SystemMessage($Title, $Text)
  {
    return('<table align="center"><tr><td><div class="SystemMessage"><h3>'.$Title.'</h3><div>'.$Text.'</div></div></td></tr></table>');
    //ShowFooter();
    //die();
  }

  function ShowHeader($Title, $Path, $BodyParam = '')
  {
    $ScriptName = $_SERVER['SCRIPT_NAME'];
    while(strpos($ScriptName, '//') !== false)
      $ScriptName = str_replace('//', '/', $ScriptName);
    $PathTreePath = '/';
    $PathTreeItem = $this->PathTree;
    $Navigation = '<a href="'.$this->System->Config['Web']['RootFolder'].$PathTreePath.'">'.$PathTreeItem[0].'</a> &gt; ';
    $ScriptName = substr($ScriptName, strlen($this->System->Config['Web']['RootFolder']));
    $ScriptNameParts = explode('/', $ScriptName);
    array_shift($ScriptNameParts);
    foreach($ScriptNameParts as $ScriptNamePart)
    {
      //echo($ScriptNamePart.'<br>');
      if(array_key_exists($ScriptNamePart, $PathTreeItem))
      {
        if(is_array($PathTreeItem[$ScriptNamePart])) 
        {
          $PathTreeItem = $PathTreeItem[$ScriptNamePart];
          $PathTreePath .= $ScriptNamePart.'/';
          if($PathTreeItem[0] != '')
            $Navigation .= '<a href="'.$this->System->Config['Web']['RootFolder'].$PathTreePath.'">'.$PathTreeItem[0].'</a> &gt; ';
        } else 
        {
          if($PathTreeItem[$ScriptNamePart] != '')
            $Navigation .= '<a href="'.$this->System->Config['Web']['RootFolder'].$PathTreePath.$ScriptNamePart.'">'.$PathTreeItem[$ScriptNamePart].'</a> &gt; ';
        }
      }
    }
    $Navigation = substr($Navigation, 0, -6);

    $Output = '<?xml version="1.0" encoding="'.$this->System->Config['Web']['Charset'].'"?>'."\n".
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.
    '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">'.
	'<meta http-equiv="content-type" content="application/xhtml+xml; charset='.$this->System->Config['Web']['Charset'].'" />'.
    '<head><link rel="stylesheet" href="'.$this->System->Config['Web']['RootFolder'].'/style.css" type="text/css" media="all" />'.
    '<script type="text/javascript" src="'.$this->System->Config['Web']['RootFolder'].'/global.js"></script>'.
    '<title>'.$this->System->Config['Web']['Title'].' - '.$Path.'</title>
    </head><body'.$BodyParam.'>';
    return($Output);
  }

  function ShowFooter()
  {
	global $ScriptStartTime;
	
    $ScriptGenerateDuration = round(GetMicrotime() - $ScriptStartTime, 2);
    $Output = '<div class="Footer">| Správa webu: '.$this->System->Config['Web']['Admin'].' | E-mail: '.$this->System->Config['Web']['AdminEmail'].' |';
    if($this->System->Config['Web']['ShowRuntimeInfo'] == true) $Output .= ' Doba generování: '.$ScriptGenerateDuration.' s / '.ini_get('max_execution_time').' s | Použitá paměť: '.HumanSize(memory_get_peak_usage(FALSE)).' / '.ini_get('memory_limit').'B |';
    $Output .= '</div></body></html>';
    return($Output);
  }

  function GetOutput()
  {
    $Output = $this->Show();
    $Output = $this->ShowHeader($this->FullTitle, $this->ShortTitle).$Output;
    $Output .= $this->ShowFooter();
    //$Output = $this->FormatOutput($Output);
    echo($Output);
  }

  // Funkce formatovani vystupu
  function FormatOutput($s)
  {
    $out = '';
    $nn = 0;
    $n = 0;
    while($s != '')
    {
      $start = strpos($s, '<');
      $end = strpos($s, '>');
      if($start != 0)
      {
        $end = $start - 1;
        $start = 0;
      }
      $line = trim(substr($s, $start, $end + 1));
      if(strlen($line) > 0)
      if($line[0] == '<')
      {
        if($s[$start + 1] == '/')
        {
          $n = $n - 2;
          $nn = $n;
        } else
        {
          if(strpos($line, ' ')) $cmd = substr($line, 1, strpos($line, ' ') - 1);
          else $cmd = substr($line, 1, strlen($line) - 2);
          //echo('['.$cmd.']');
          if(strpos($s, '</'.$cmd.'>')) $n = $n + 2;
        }
      }// else $line = '['.$line.']';
      //if($line != '') echo(htmlspecialchars(str_repeat(' ',$nn).$line."\n"));
      if($line != '') $out .= (str_repeat(' ', $nn).$line."\n");
      $s = substr($s, $end + 1, strlen($s));
      $nn = $n;
    }
    return($out);
  }
}

?>
