<?php

include_once(dirname(__FILE__).'/HTML.php');

class Form extends HTML
{
  var $Definition = array();
  var $Values = array();
  var $OnSubmit = '';
  
  function __construct($System, $FormClass)
  {
    $this->System = $System;
    $this->Definition = $FormClass;
    foreach($this->Definition['Items'] as $Index => $Item)
    {
      $this->Values[$Index] = '';
    }    
  }

  function ShowTable()
  {
    $Output = $this->ShowTableBlock();
    return($Output);
  }

  function ShowTableBlock($Context = '')
  {
    $Table = array(
      'Header' => array('Položka', 'Hodnota'),
      'Rows' => array(),
    );
    if($Context != '') $Context = $Context.'-';
    foreach($this->Definition['Items'] as $Index => $Item)
    {
      if($Item['Type'] != 'Hidden')
      {
        if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default']; 
        $Edit = $this->System->Type->ExecuteTypeEvent($Item['Type'], 'OnView',
          array('Name' => $Index, 'Value' => $this->Values[$Index], 'Type' => $Item['Type']));
        array_push($Table['Rows'], array($Item['Caption'], $Edit));
      }
    }
    $Output = '<div style="text-align: center">'.$this->Definition['Title'].'</div>'.$this->Table($Table, 'WideTable');
    return($Output); 
  }

  function ShowEditForm()
  {
    if(!array_key_exists('SubmitText', $this->Definition)) $this->Definition['SubmitText'] = 'Uložit';
    $Output = '<form class="Form" action="'.$this->OnSubmit.'" method="post">'.$this->ShowEditBlock().'<div><input type="submit" value="'.$this->Definition['SubmitText'].'" /></div></form>';
    return($Output);
  }

  function ShowEditBlock($Context = '')
  {
    $Table = array(
      //'Header' => array('Položka', 'Hodnota'),
      'Rows' => array(),
    );
    if($Context != '') $Context = $Context.'-';
    foreach($this->Definition['Items'] as $Index => $Item)
    {
      if($Item['Type'] != 'Hidden') 
      {
        if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default']; 
        $Edit = $this->System->Type->ExecuteTypeEvent($Item['Type'], 'OnEdit', array('Name' => $Index, 'Value' => $this->Values[$Index], 'Type' => $Item['Type']));
        array_push($Table['Rows'], array($Item['Caption'].':', $Edit));
      }
    }
    $Output = '<fieldset><legend>'.$this->Definition['Title'].'</legend>'.$this->Table($Table, 'BasicTable').
    '</fieldset>';
    foreach($this->Definition['Items'] as $Index => $Item)
      if($Item['Type'] == 'Hidden') $Output .= $this->System->Type->ExecuteTypeEvent($Item['Type'], 'OnEdit', array('Name' => $Index, 'Value' => $this->Values[$Index], 'Type' => $Item['Type']));
    return($Output); 
  }

  function LoadValuesFromDatabase($Id)
  {
    $DbResult = $this->System->Database->query('SELECT T.* FROM '.$this->Definition['Table'].' AS T WHERE T.Id='.$Id);
    if($DbResult->num_rows > 0) $DbRow = $DbResult->fetch_assoc();
    foreach($this->Definition['Items'] as $Index => $Item)
    {
      if($Item['Type'] != 'Hidden')
        if($DbResult->num_rows > 0) $this->Values[$Index] = $DbRow[$Index];
          else $this->Values[$Index] = '';
    }
    $this->Definition['Items']['Id'] = array('Type' => 'Hidden', 'Caption' => 'Id', 'Default' => 0);
    $this->Values['Id'] = $Id;
    return($DbResult->num_rows > 0);
  }

  function SaveValuesToDatabase($Id)
  {
    if($Id == 0)
    {
      $this->Values['Id'] = $Id;
      $DbResult = $this->System->Database->replace($this->Definition['Table'], $this->Values);
    } else
    $DbResult = $this->System->Database->update($this->Definition['Table'], 'Id='.$Id, $this->Values);
  }

  function LoadValuesFromForm()
  {
    $this->Values = $this->LoadValuesFromFormBlock();
  }

  function LoadValuesFromFormBlock($Context = '')
  {
    if($Context != '') $Context = $Context.'-';
    $Values = array();
    foreach($this->Definition['Items'] as $Index => $Item)
    {
      if(array_key_exists($Context.$Index, $_POST))
      $Values[$Index] = $this->System->Type->ExecuteTypeEvent($Item['Type'], 'OnLoad',
        array('Name' => $Index, 'Type' => $Item['Type']));
    }
    return($Values);
  }
}

?>
