<?php
/*
* --------------------------------------------------------------------------------------------------------------------------
* Program : HTML Form Field Generator, PHP Class Library
* Files : htmlform.inc.php, htmlform_base.inc.php, htmlform_exc.inc.php
* Author : Lasantha Samarakoon
* Date released : Monday, September 21, 2009
* Email : hide@address.com
* Licence : http://www.gnu.org/licenses/gpl.txt
* --------------------------------------------------------------------------------------------------------------------------
*
* This program is a freeware, which falls under GNU Genral Public Licence.
* ---------------------------------------------------------------------------------------------------------------------------
* You can modify this program, without any permission from the author. But be kind enough to send the updated version to the
* author through the above mentioned Email address.
* ---------------------------------------------------------------------------------------------------------------------------
* Documentation:
* Please refer the test.php file for hints on the usage of this class library.
* ---------------------------------------------------------------------------------------------------------------------------
* ************************************* PROUD TO BE A SRI LANKAN...!!! ******************************************************
*/
require_once 'htmlform_exc.inc.php'; // exception classes
require_once 'htmlform_base.inc.php'; // base classes
// this class is used to generate html form element
class HTMLForm implements IGeneratable {
private $_name = null;
private $_id = null;
private $_enctype = null;
private $_action = null;
private $_method = null;
protected $_class = null;
private $_elems = array();
// constructor
function __construct($name = null, $id = null, $action = null, $method = null, $enctype = null, $class = null) {
$this->_name = $name;
$this->_id = $id;
$this->_action = $action;
$this->_method = $method;
$this->_enctype = $enctype;
$this->_class = $class;
}
//getters
function get_name() {return $this->_name;}
function get_id() {return $this->_id;}
function get_enctype() {return $this->_enctype;}
function get_action() {return $this->_action;}
function get_method() {return $this->_method;}
function get_class() {return $this->_class;}
//setters
function set_name($val) {$this->_name = $val;}
function set_id($val) {$this->_id = $val;}
function set_enctype($val) {$this->_enctype = $val;}
function set_action($val) {$this->_action = $val;}
function set_method($val) {$this->_method = $val;}
function set_class($val) {$this->_class = $val;}
function generate() {
// create form
$r = "<form";
if($this->_name != null) $r .= " name=\"{$this->_name}\"";
if($this->_id != null) $r .= " id=\"{$this->_id}\"";
if($this->_enctype != null) $r .= " enctype=\"{$this->_enctype}\"";
if($this->_action != null) $r .= " action=\"{$this->_action}\"";
if($this->_method != null) $r .= " method=\"{$this->_method}\"";
if($this->_class != null) $r .= " class=\"{$this->_class}\"";
$r .= ">\n";
// add components
foreach($this->_elems as $e) {
$r .= $e->generate();
}
// close form
$r .= "</form>\n";
// return html code
return $r;
}
// this function is used to add form elements to the form
function addElement($elem, $elname) {
// check all required parameters are not null
if($elem != null && $elname != null)
$this->_elems[$elname] = $elem;
else
throw new NullElementException();
}
// this function is used to remove elements from the form
function removeElement($elname) {
// temporary array to save filtered components
$new_elems = array();
// traverse through the component collection
foreach($this->_elems as $k=>$v)
if(strtolower($k) != strtolower($elname))
$new_elems[$k] = $v; // save filtered components to the new temporary array
// set the reproduced array as the component collection
$this->_elems = $new_elems;
}
}
// create html text input command
class TextField extends TextFieldBase implements IGeneratable {
function generate() {
return parent::generate_field('text');
}
}
// create html hidden input command
class HiddenField extends TextFieldBase implements IGeneratable {
function generate() {
return parent::generate_field('hidden');
}
}
// create html password input command
class PasswordField extends TextFieldBase implements IGeneratable {
function generate() {
return parent::generate_field('password');
}
}
// create html checkbox command
class CheckBox extends CheckboxBase implements IGeneratable {
function generate() {
return parent::generate_field('checkbox');
}
}
// create html radio button command
class Radio extends CheckboxBase implements IGeneratable {
function generate() {
return parent::generate_field('radio');
}
}
// create html textarea element
class Textarea extends FormField implements IGeneratable {
private $_value = null;
// constructor
function __construct($name = null, $id = null, $value = null, $class = null, $disabled = false) {
// invoke super class constructor
parent::__construct($name, $id, $class, $disabled);
$this->set_value($value);
}
//getters
function get_value() {return $this->_value;}
//setters
function set_value($val) {$this->_value = $val;}
function generate() {
$r = "<textarea";
// check for available attributes
if($this->_name != null) $r .= " name=\"{$this->_name}\"";
if($this->_id != null) $r .= " id=\"{$this->_id}\"";
if($this->_class != null) $r .= " class=\"{$this->_class}\"";
if($this->_disabled == true) $r .= " disabled=\"disabled\"";
$r .= ">$value</textarea>\n";
//return html coding
return $r;
}
}
// create html submit button command
class SubmitButton extends ButtonBase implements IGeneratable {
function generate() {
return parent::generate_field('submit');
}
}
// create html reset button command
class ResetButton extends ButtonBase implements IGeneratable {
function generate() {
return parent::generate_field('reset');
}
}
// create html button command
class GeneralButton extends ButtonBase implements IGeneratable {
function generate() {
return parent::generate_field('button');
}
}
// create html label element
class Label implements IGeneratable {
private $_value = null;
private $_id = null;
private $_rel = null;
private $_class = null;
// constructor
function __construct($value = null, $rel = null, $id = null, $class = null) {
$this->_id = $id;
$this->_value = $value;
$this->_rel = $rel;
$this->_class = $class;
}
//getters
function get_id() {return $this->_id;}
function get_value() {return $this->_value;}
function get_rel() {return $this->_rel;}
function get_class() {return $this->_class;}
//setters
function set_id($val) {$this->_id = $val;}
function set_value($val) {$this->_value = $val;}
function set_rel($val) {$this->_rel = $val;}
function set_class($val) {$this->_class = $val;}
function generate() {
$r = "<label";
// check for available attributes
if($this->_id != null) $r .= " id=\"{$this->_id}\"";
if($this->_rel != null) $r .= " rel=\"{$this->_rel}\"";
if($this->_class != null) $r .= " class=\"{$this->_class}\"";
$r .= ">{$this->_value}</label>";
// return html coding
return $r;
}
}
// create html select box command
class SelectBox extends FormField implements IGeneratable {
private $_options = array();
// constructor
function __construct($name = null, $id = null, $class = null, $disabled = false) {
// invoke super class constructor
parent::__construct($name, $id, $class, $disabled);
}
// this function is used to add options to the select box
function addOption($key, $text, $value = null, $selected = false) {
$sbo = new SBOption($text, $value, $selected);
$this->_options[$key] = $sbo;
}
// this function is used to remove options from the select box
function removeOption($optname) {
// temporary array to save filtered options
$new_options = array();
// traverse through the option collection
foreach($this->_options as $k=>$v)
if(strtolower($k) != strtolower($optname))
$new_options[$k] = $v;
// set the reproduced array as the option collection
$this->_options = $new_options;
}
function generate() {
$r = "<select";
// check for available attributes
if($this->_name != null) $r .= " name=\"{$this->_name}\"";
if($this->_id != null) $r .= " id=\"{$this->_id}\"";
if($this->_class != null) $r .= " class=\"{$this->_class}\"";
if($this->_disabled) $r .= " disabled=\"disabled\"";
$r .= ">\n";
// add options
foreach($this->_options as $o)
$r .= $o->generate();
$r .= "</select>\n";
// return html coding
return $r;
}
}
// this class is used to define select box option
class SBOption implements IGeneratable {
private $_value = null;
private $_selected = false;
private $_text = null;
// constructor
function __construct($text = null, $value = null, $selected = false) {
$this->_value = $value;
$this->_text = $text;
$this->set_selected($selected);
}
//getters
function get_value() {return $this->_value;}
function get_text() {return $this->_text;}
function get_selected() {return $this->_selected;}
//setters
function set_value($val) {$this->_value = $val;}
function set_text($val) {$this->_text = $val;}
function set_selected($val) {
// check whether the parameter is boolean, else throw an exception
if(is_bool($val))
$this->_selected = $val;
else
throw new InvalidParameterException("boolean (true or false)");
}
function generate() {
$r = "<option";
// check for available attributes
if($this->_value != null) $r .= " value=\"{$this->_value}\"";
if($this->_selected) $r .= " selected=\"selected\"";
$r .= ">{$this->_text}</option>\n";
// return html coding
return $r;
}
}
?>