Třída TForm generuje HTML kód formuláře. Chtěl jsem si ušetřit práci se stálým psaním HTML kódu formulářů a tak jsem si napsal tuhle třídu, která pomocí zařazování jednotlivých komponent generuje výsledný formulář aniž bych musel napsat jediný HTML tag. Veškerý vzhled komponent je definován jednotlivými třídami kaskádových stylů, jejich umístění je pozicováno souřadnicově (viz. GridBagLayout z prostředí Javy). Třídu TForm, včetně příkladu použití si můžete stáhnout zde. On-line ukázka formuláře vytvořeného pomocí třídy TFrom je tady.
TForm se může skládat z těchto komponent:
<?php
require_once "ttext.class.php";
require_once "ttexterror.class.php";
require_once "tcheckbox.class.php";
require_once "tedit.class.php";
require_once "tbutton.class.php";
require_once "timagebutton.class.php";
require_once "tcombo.class.php";
require_once "ttextarea.class.php";
class TForm {
protected $form_header;
protected $a_components=array();
protected $a_bottom=array();
protected $TText_title;
protected $bottom_style;
protected $grid_columns=0;
protected $grid_rows=0;
protected $style = "TForm";
public function __construct($call_script_name="",$form_name="",$class_name="TForm",$method="POST"){
global $PHP_SELF;
if ($call_script_name=="") $call_script_name = $PHP_SELF;
$this->form_header = "<FORM action="$call_script_name" name="$form_name" method="$method" enctype="multipart/form-data">";
}
public function SetTitle(TText $o_title){
$this->TText_title = $o_title;
}
public function SetBottomStyle($class_name){
$this->bottom_style = trim($class_name);
}
public function SetStyle($class_name){
$this->style = trim($class_name);
}
public function Add($component,$row,$column){
$row = (int) $row;
$column = (int) $column;
if ($row <= 0) throw new exception("Index of rows must be bigger then 0.");
if ($column <= 0) throw new exception("Index of columns must be bigger then 0.");
$this->a_components[$row][$column] = $component;
}
public function AddBottom($component){
$this->a_bottom[] = $component;
}
public function SetDataGrid($rows,$columns){
$rows = (int) $rows;
$columns = (int) $columns;
if ($rows <= 0) throw new exception("Count rows must be bigger then 0.");
if ($columns <= 0) throw new exception("Count columns must be bigger then 0.");
$this->grid_rows = $rows;
$this->grid_columns = $columns;
for($row=1;$row<=$rows;$row++){
for($column=1;$column<=$columns;$column++){
$this->a_components[$row][$column] = "0";
}
}
}
public function Render(){
$code .="n$this->form_header n";
$code .= "<TABLE align="center" cellspacing="0" class="$this->style">n";
if ($this->TText_title){
$code .= "<TR><TD colspan="$this->grid_columns">".$this->TText_title->Render()."</TD></TR>n";
$code .= "<TR><TD colspan="$this->grid_columns"> </TD></TR>n";
}
//Render Data Grid in array 'a_components'
for($row=1;$row<=$this->grid_rows;$row++){
$code .= "<TR>";
for($column=1;$column<=$this->grid_columns;$column++){
if ($this->a_components[$row][$column]!=0){
$code .= "<TD colspan="".$this->a_components[$row][$column]->GetColspan()."">";
$code .= $this->a_components[$row][$column]->Render();
$column = $column+($this->a_components[$row][$column]->GetColspan()-1);
}else{
$code .= "<TD> ";
}
$code .= "</TD>";
}
$code .= "</TR>n";
}
//Render Bottom part
if (Count($this->a_bottom)>0){
$code .= "<TR><TD colspan="$this->grid_columns"> </TD></TR>n";
$code .= "<TR><TD colspan="$this->grid_columns" class="$this->bottom_style">";
foreach($this->a_bottom as $object){
$code .= $object->Render()." ";
}
$code .= "</TD></TR>n";
}
$code .= "<TR><TD colspan="$this->grid_columns"> </TD></TR>n";
$code .= "</TABLE>n";
$code .= "</FORM>n";
return $code;
}
}
?>