<?php

define('PAGE_NOT_FOUND', 'Stránka nenalezena');

class BaseView extends View
{
  var $TimeStart;
  var $FormatHTML = false;
  var $ShowRuntimeInfo = false;
  var $ShowNavigation = false;
  var $ClearPage = false;
  var $BasicHTML = false;
  var $ParentClass = '';
  var $ShortTitle;
  var $FullTitle;
  var $Encoding;
  var $Style;

  function __construct($System)
  {
    parent::__construct($System);

    $this->FormatHTML = false;
    $this->ShowRuntimeInfo = false;
    $this->Encoding = 'utf-8';
    $this->Style = 'default';

    // TODO: Move to external code
    if(isset($this->System->Config['Web']['FormatHTML']))
      $this->FormatHTML = $this->System->Config['Web']['FormatHTML'];
    if(isset($this->System->Config['Web']['ShowRuntimeInfo']))
      $this->ShowRuntimeInfo = $this->System->Config['Web']['ShowRuntimeInfo'];
    if(isset($this->System->Config['Web']['Charset']))
      $this->Encoding = $this->System->Config['Web']['Charset'];
    if(isset($this->System->Config['Web']['Style']))
      $this->Style = $this->System->Config['Web']['Style'];
  }

  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 ShowNavigation($Page)
  {
    if(array_key_exists('REQUEST_URI', $_SERVER))
      $ScriptName = $_SERVER['REQUEST_URI'];
      else $ScriptName = '';
    while(strpos($ScriptName, '//') !== false)
      $ScriptName = str_replace('//', '/', $ScriptName);
    if(strpos($ScriptName, '?') !== false)
      $ScriptName = substr($ScriptName, 0, strrpos($ScriptName, '?'));
    $ScriptName = substr($ScriptName, strlen($this->System->Link('')));
    if(substr($ScriptName, -1, 1) == '/') $ScriptName = substr($ScriptName, 0, -1);

    $Output = '';
    while($Page)
    {
      $Output = ' &gt; <a href="'.$this->System->Link($ScriptName).'/">'.$Page->ShortTitle.'</a>'.$Output;

      if(class_exists($Page->ParentClass))
      {
        $PageClass = $Page->ParentClass;
        $Page = new $PageClass($this->System);
        $ScriptName = substr($ScriptName, 0, strrpos($ScriptName, '/'));
      } else $Page = null;
    }
    $Output = substr($Output, 6);
    return($Output);
  }

  function ShowHeader($Page)
  {
    $Title = $Page->FullTitle;
    $Path = $Page->ShortTitle;

    $Navigation = $this->ShowNavigation($Page);

    $BodyParam = '';
    if(isset($Page->Load)) $BodyParam .= ' onload="'.$Page->Load.'"';
    if(isset($Page->Unload)) $BodyParam .= ' onunload="'.$Page->Unload.'"';
    $Output = '<?xml version="1.0" encoding="'.$this->Encoding.'"?>'."\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">'.
    '<head>'.
    '<link rel="stylesheet" href="'.$this->System->Link('/style/').$this->Style.'/style.css" type="text/css" media="all" />';
    $Output .= '<link rel="alternate" title="Taneční seznamka" href="'.
      $this->System->Link('/seznamka/rss/').'" type="application/rss+xml" />';
    $Output .= '<meta http-equiv="content-type" content="application/xhtml+xml; charset='.$this->Encoding.'" />'.
    '<meta name="viewport" content="width=device-width, initial-scale=1">'.
    '<script type="text/javascript" src="'.$this->System->Link('/style/').$this->Style.'/jquery.js"></script>'.
    '<title>'.$Path.' - '.$this->System->Config['Web']['Title'].'</title>';

    // Show page headers
    $Bar = '';
    foreach($this->System->PageHeaders as $Item)
      $Output .= call_user_func($Item);

    $Output .= '</head><body'.$BodyParam.'>';
    $Output .= $this->System->ShowMenu();
    if($this->BasicHTML == false)
    {
      //$Output .= '<div class="MainTitle">'.$Title.'</div>';
      if ($this->ShowNavigation)
        $Output .= '<div class="MainTitle"><span class="MenuItem"><strong>Navigace :: </strong> '.$Navigation.'</span><div class="MenuItem2">';
      $Bar = '';
      foreach($this->System->Bars['Top'] as $BarItem)
        $Bar .= call_user_func($BarItem);
      if(trim($Bar) != '') $Output .= $Bar;
        else $Output .= '&nbsp;';
      $Output .= '</div></div>';
    }
    return($Output);
  }

  function ShowFooter()
  {
    global $ScriptTimeStart, $Revision, $ReleaseTime;

    $Time = round(GetMicrotime() - $ScriptTimeStart, 2);
    $Output = '';
    if($this->BasicHTML == false)
    {            
      $Output .= '<div class="Footer">'.
      '<i>';
      $Output .= ' Kontakt: <a href="mailto:'.$this->System->Config['Web']['AdminEmail'].'">'.$this->System->Config['Web']['AdminEmail'].'</a> ';
      $Output .= ' <a href="https://app.zdechov.net/tanec/">Zdrojový kód</a> ';
      //$Output .= ' Verze: '.$Revision.' ('.HumanDate($ReleaseTime).') ';
      if($this->ShowRuntimeInfo == true) 
      {
        $Output .= ' Doba generování: '.$Time.' s / '.ini_get('max_execution_time').' s '.
        ' Použitá paměť: '.HumanSize(memory_get_peak_usage(FALSE)).' / '.ini_get('memory_limit').'B ';
      }
      $Output .= '</i></div>';
    }
    $Output .= '</body></html>';
    return($Output);
  }

  function GetOutput($Page)
  {
    $Page->OnSystemMessage = array($this->System->BaseView, 'SystemMessage');

    $Output = $Page->Show();
    if($Page->ClearPage == false)
    {
      $Output = $this->ShowHeader($Page).$Output.$this->ShowFooter();
      if($this->FormatHTML == true) $Output = $this->FormatOutput($Output);
    }
    return($Output);
  }

  function NewPage($ClassName)
  {
    $Page = new $ClassName();
    $Page->System = $this->System;
    $Page->Database = $this->Database;
    $Page->FormatHTML = $this->FormatHTML;
    return($Page);
  }

  // XML formating function
  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);
  }
}
