<?php

include_once('database.php');

class Form
{
  var $Definition = array();
  var $Values = array();
  var $OnSubmit = '';

  function ShowEditForm()
  {
    $Output = '<center><form enctype="multipart/form-data" action="'.$this->OnSubmit.'" method="post"><div align="center">';
    $Table = $this->ShowEditBlock();
    $Output .= $this->Definition['Title'].Table($Table).$this->ShowHiddenBlock().'<input type="submit" value="'.$this->Definition['SubmitButtonText'].'"></div></form>';
    return($Output);
  }

  function ShowEditBlock($Context = '')
  {
    global $Database;

    $Table = array(
      'Header' => array('Položka', 'Hodnota'),
      'Rows' => array(),
    );
    if($Context != '') $Context = $Context.'-';
    foreach($this->Definition['Items'] as $Index => $Item)
    {
      if($Item['Type'] != TypeHiddenId)
      {
        if(!array_key_exists($Index, $this->Values) and isset($Item['Value'])) $this->Values[$Index] = $Item['Value'];
        $Edit = ExecuteTypeEvent($Item['Type'], 'OnEdit', $Item);
        array_push($Table['Rows'], array($Item['Caption'], $Edit));
      }
    }
    return($Table);
  }

  function ShowHiddenBlock($Context = '')
  {
    global $Database;

    $Output = '';
    if($Context != '') $Context = $Context.'-';
    foreach($this->Definition['Items'] as $Item)
    {
      if($Item['Type'] == TypeHiddenId)
      {
        if(!array_key_exists($Item['Name'], $this->Values) and isset($Item['Value'])) $this->Values[$Item['Name']] = $Item['Value'];
        $Edit = ExecuteTypeEvent($Item['Type'], 'OnEdit', $Item);
        $Output .= $Edit;
      }
    }
    return($Output);
  }

  function ShowReadOnlyForm()
  {
    $Output = '<center><div align="center">';
    $Table = $this->ShowReadOnlyBlock();
    $Output .= $this->Definition['Title'].Table($Table).'</div></form>';
    return($Output);
  }

  function ShowReadOnlyBlock($Context = '')
  {
    global $Database;

    $Table = array(
      'Header' => array('Položka', 'Hodnota'),
      'Rows' => array(),
    );
    if($Context != '') $Context = $Context.'-';
    foreach($this->Definition['Items'] as $Item)
    {
      if(!array_key_exists($Item['Name'], $this->Values) and isset($Item['Value'])) $this->Values[$Item['Name']] = $Item['Value'];
      $Edit = ExecuteTypeEvent($Item['Type'], 'OnView', $Item);
      array_push($Table['Rows'], array($Item['Caption'], $Edit));
    }
    return($Table);
  }

  function LoadValuesFromDatabase($Id)
  {
    global $Database;

    $DbResult = $Database->query('SELECT * FROM '.$this->Definition['Table'].' WHERE Id='.$Id);
    $DbRow = $DbResult->fetch_assoc();
    foreach($this->Definition['Items'] as $Item)
    {
      $this->Values[$Item['Name']] = $DbRow[$Item['Name']];
      switch($Item['Type'])
      {
        case 'Password':
          if($Item['Type'] == 'Password') $this->Values[$Item['Name']] = '';  // Dont show password
          break;
        case 'Time':
          $this->Values[$Index] == MysqlDateTimeToTime($this->Values[$Item['Name']]);
          break;
      }
    }
  }

  function SaveValuesToDatabase($Id)
  {
    global $Database;

    foreach($this->Definition['Items'] as $Item)
    {
      switch($Item['Type'])
      {
        case 'Password':
          if($this->Values[$Item['Name']] == '') unset($this->Values[$Item['Name']]); // Do not modify empty passwords
          else $this->Values[$Item['Name']] = sha1($this->Values[$Item['Name']]);
          break;
        case 'Time':
          $this->Values[$Item['Name']] = TimeToMysqlDateTime($this->Values[$Item['Name']]);
          break;
      }
    }
    $this->Values['Id'] = $Id;
    $DbResult = $Database->replace($this->Definition['Table'], $this->Values);
    //echo($Database->LastQuery);
  }

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

  function LoadValuesFromFormBlock($Context = '')
  {
    $Values = array();
    foreach($this->Definition['Items'] as $Index => $Item)
    {
      $Values[$Item['Name']] = ExecuteTypeEvent($Item['Type'], 'OnLoad', $Item);
    }
    //print_r($this->Values);
    return($Values);
  }
}

function MakeLink($Target, $Title)
{
  return('<a href="'.$Target.'">'.$Title.'</a>'); 
}

function Table($Table)
{
  $Result = '<table class="WideTable">';
  $Result .= '<tr>';
  if(array_key_exists('Header', $Table))
  {
    foreach($Table['Header'] as $Item)
      $Result .= '<th>'.$Item.'</th>';
    $Result .= '</tr>';
  }
  foreach($Table['Rows'] as $Row)
  {
    $Result .= '<tr>';
    foreach($Row as $Index => $Item)
    {
      if($Index == 0) $Class = ' class="Header"'; else $Class = '';
      $Result .= '<td'.$Class.'>'.$Item.'</td>';
    }
    $Result .= '</tr>';
  }
  $Result .= '</table>';
  return($Result);
}

function ShowEditTable($ClassName, $Values)
{
}

?>