<?php

include('form_classes.php');

class Form
{
  var $Definition = array();
  var $Values = array();
  var $OnSubmit = '';
  
  function __construct($ClassName)
  {  
    global $FormClasses;

    if(array_key_exists($ClassName, $FormClasses))
    {
      $this->Definition = &$FormClasses[$ClassName];
      foreach($this->Definition['Items'] as $Index => $Item)
      {
        $this->Values[$Index] = '';
      }    
    } else 
    {
      $this->Definition = array('Title' => 'Neznámý formulář', 'Table' => '', 'Items' => array());
    }
  }

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

  function ShowTableBlock($Context = '')
  {
    global $Database, $FormTypes;

    $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 = 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>'.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 = '')
  {
    global $Database, $FormTypes;

    $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 = 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>'.Table($Table, 'BasicTable').
    '</fieldset>';
    foreach($this->Definition['Items'] as $Index => $Item)
      if($Item['Type'] == 'Hidden') $Output .= ExecuteTypeEvent($Item['Type'], 'OnEdit', array('Name' => $Index, 'Value' => $this->Values[$Index], 'Type' => $Item['Type']));
    return($Output); 
  }

  function LoadValuesFromDatabase($Id)
  {
    global $Database;

    $DbResult = $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] = '';
    }
    return($DbResult->num_rows > 0);
  }

  function SaveValuesToDatabase($Id)
  {
    global $Database;

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

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

  function LoadValuesFromFormBlock($Context = '')
  {
    global $FormTypes;

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

?>
