<?php

// --- klasa formularza ---
class HtmlForm {
   // odpowiedzialna za generowanie szybkich i brzydkich formularzy
   var $actionTarget; // cieka do otrzymywania strony
   var $inputForms; // tablica HtmlFormInput
   var $hiddenVariables; // przypisanie name/val

   // Konstruktor
   function __construct($action_target) {
      $this->actionTarget = $action_target;
      $this->inputForms = array();
      $this->hiddenVariables = array();
   }
   // metody publiczne
   function toString () {
      $return_string = "";
      $return_string .= 
         "<FORM METHOD=\"POST\" ".
         "ACTION=\"$this->actionTarget\">\n";
      $return_string .= $this->inputFormsString();
      $return_string .= $this->hiddenVariablesString();
      $return_string .= "<BR>\n";
      $return_string .= $this->submitButtonString();
      $return_string .= "</FORM>";
      return($return_string);
   }
   // dodawanie elementw do formularza
   function addInputForm ($input_form) {
      if (!isSet($input_form) || 
  !is_object($input_form) || 
  !is_subclass_of($input_form, 'htmlforminput')) {
         die("Argument dla HtmlForm::addInputForm ".
  "musi by egzemplarzem HtmlFormInput.".
  " nadanego argumentu klasy " .
         get_class($input_form));
      }
      else {
         array_push($this->inputForms, $input_form);
      }
   }
   function addInputButton ($input_button) {
      if (!isSet($input_button) || 
  !isObject($input_button) || 
  !is_a($input_button, 'HtmlInputButton')) {
         die("Argument dla HtmlForm::addInputButton ".
  "musi by egzemplarzem HtmlInputButton");
      }
      else {
         array_push($this->inputButtons, $input_button);
      }
   }
   function addHiddenVariable ($name, $value) {
      if (!isSet($value)) {
         die("HtmlForm::addHiddenVariable wymaga ".
  "dwch argumentw (nazwy i wartoci)");
      }
      else {
         $this->hiddenVariables[$name] = $value;
      }
   }
   function inputFormsString () {
      $return_string = "";
      $form_array = $this->inputForms;
      foreach ($form_array as $input_form) {
         $return_string .= "<B>$input_form->heading</B>";
         if ($this->headingElementBreak()) {
            $return_string .= "<BR>";
         }
         $return_string .= $input_form->toString();
         $return_string .= "<BR>\n";
      }
      return($return_string);
   }
   function hiddenVariablesString () {
      $return_string = "";
      while ($hidden_var = each($this->hiddenVariables)) {
         $var_name = $hidden_var['key'];
         $var_value = $hidden_var['value'];
         $return_string .= 
            "<INPUT TYPE=HIDDEN " .
            "NAME=$var_name ".
            "VALUE=$var_value >";
         $return_string .= "\n";
      }
      return($return_string);
   }
   function headingElementBreak () {
      // nadpisanie w celu zablokowania przerw po nagwku
      // lub aby utworzy bardziej skomplikowany ukad
      return(TRUE);
   }
   function submitButtonString () {
      $return_string = "<INPUT TYPE=Submit " .
         " VALUE=Wylij >\n";
      return($return_string);
   }
}
// --- klasy elementw formularza --
abstract class HtmlFormInput {
   var $name; // nazwa zmiennej do wysania formularza
   var $heading; // widoczna etykieta formularza
   function __construct () {
      die("Klasa HtmlFormInput przeznaczona tylko " .
  "aby by podklas");
   }
   function toString () {
      die("Podklasa HtmlFormInput zagubia " .
  "definicj toString()");
   }
}
class HtmlFormSelect extends HtmlFormInput
{
   var $_valueArray = array();
   var $_selectedValue;

   function __construct ($name, $heading, $value_array, 
  $selected_value=NULL) {
      if (!isSet($value_array)) {
         die("HtmlFormSelect potrzebuje minimum dwch " .
  "argumentw: nazwy i wartoci tablicy");
      }
      elseif (!is_array($value_array)) {
         die("Trzeci argument dla HtmlFormSelect()" .
  "powinien by tablic, w ktrej klucze s wartociami ".
  "wysanymi, a wartoci s wywietlanymi.");
      }
      else {
         // rzeczywista inicjalizacja
         $this->name = $name;
         $this->heading = $heading;
         $this->_valueArray = $value_array;
         $this->_selected_value = $selected_value;
      }
   }
   function toString () {
      $return_string = "";
      $return_string .= "<SELECT NAME=\"$this->name\">";
      while ($var_entry = each($this->_valueArray)) {
         $submit_value = $var_entry['key'];
         $display_value = $var_entry['value'];
         if ($submit_value == $this->_selected_value) {
           $return_string .= "<OPTION VALUE=${submit_value} SELECTED >";
         }
         else {
            $return_string .= "<OPTION VALUE=${submit_value}>";
         }
         $return_string .= $display_value;
      }
      $return_string .= "</SELECT>";
      return($return_string);
   }
}
class HtmlFormText extends HtmlFormInput
{
   var $initial_value;

   function __construct ($name, $heading, $initial_value="")
   {
      // inicjalizacja zmiennych
      if (!isSet($name) || !isSet($heading)) {
         die("Konstruktor HtmlFormText wymaga " .
  "co najmniej dwch argumentw (nazwy, nagwka)");
      }
      $this->name = $name; // nazwa zdefiniowana u rodzica
      $this->heading = $heading; // zdefiniowane u rodzica
      $this->initial_value = $initial_value;
   }
   function toString () {
      $return_string = "";
      $return_string .= "<INPUT TYPE=TEXT ";
      $return_string .= "NAME=\"$this->name\" ";
      $return_string .= "VALUE=\"$this->initial_value\" ";
      $return_string .= " >";
      return($return_string);
   }
}
class HtmlFormTextArea extends HtmlFormInput {
   var $initial_value;
   var $rows;
   var $cols;
   var $wrapType;

   function __construct ($name, 
  $heading,
// argumenty opcjonalne:
$initial_value="", 
  $rows=1, $cols=60, 
$wrapType="VIRTUAL")
   {
      // inicjalizacja zmiennych
      if (!isSet($name)) {
         die("Konstruktor HtmlFormTextArea wymaga " .
  "co najmniej dwch argumentw (nazwy, nagwka)");
      }
      $this->name = $name; // nazwa zdefiniowana u rodzica
      $this->heading = $heading; // nazwa zdefiniowana u rodzica
      $this->initial_value = $initial_value;
      $this->rows = $rows;
      $this->cols = $cols;
      $this->wrapType = $wrapType;
   }
   function toString ()
   {
      $return_string = "";
      $return_string .= "<TEXTAREA ";
      $return_string .= "NAME=\"$this->name\" ";
      $return_string .= "ROWS=$this->rows ";
      $return_string .= "COLS=$this->cols ";
      $return_string .= "WRAP=$this->wrapType ";
      $return_string .= $this->additionalAttributes();
      $return_string .= ">";
      $return_string .= $this->initial_value;
      $return_string .= "</TEXTAREA>";
      return($return_string);
   }
   function additionalAttributes () {
      // NADPISZ TO aby zwrci acuch z 
      // atrybutami TextArea innymi ni
      // NAME, ROWS, COLS i WRAP
      return("");
   }
}
?>
