<?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';
interface IGeneratable {
// this function is used to generate html coding.
function generate();
}
// this is the base class for all form fields except checkbox and radio
abstract class FormField {
protected $_name = null;
protected $_id = null;
protected $_class = null;
protected $_disabled = false;
// constructor
function __construct($name = null, $id = null, $class = null, $disabled = false) {
$this->_name = $name;
$this->_id = $id;
$this->_class = $class;
$this->set_disabled($disabled);
}
//getters
function get_name() {return $this->_name;}
function get_id() {return $this->_id;}
function get_class() {return $this->_class;}
function get_disabled() {return $this->_disabled;}
//setters
function set_name($val) {$this->_name = $val;}
function set_id($val) {$this->_id = $val;}
function set_class($val) {$this->_class = $val;}
function set_disabled($val) {
// check whether the parameter is boolean, else throw an exception
if(is_bool($val))
$this->_disabled = $val;
else
throw new InvalidParameterException("boolean (true or false)");
}
}
// this is the base class for text, password and hidden fields
abstract class TextFieldBase extends FormField {
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_field($type) {
$r = "<input type=\"$type\"";
// 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->_value != null) $r .= " value=\"{$this->_value}\"";
if($this->_disabled == true) $r .= " disabled=\"disabled\"";
$r .= " />\n";
// return html code
return $r;
}
}
// this is the base class for checkbox and radio
abstract class CheckboxBase extends FormField {
private $_checked = false;
//constructor
function __construct($name = null, $id = null, $checked = false, $class = null, $disabled = false) {
// invoke super class constructor
parent::__construct($name, $id, $class, $disabled);
$this->set_checked($checked);
}
//getters
function get_checked() {return $this->_checked;}
//setters
function set_checked($val) {
// check whether the parameter is boolean, else throw an exception
if(is_bool($val))
$this->_checked = $val;
else
throw new InvalidParameterException("boolean (true or false)");
}
function generate_field($type) {
$r = "<input type=\"$type\"";
// 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->_checked == true) $r .= " checked=\"checked\"";
if($this->_disabled == true) $r .= " disabled=\"disabled\"";
$r .= " />\n";
// return htm code
return $r;
}
}
// this is the base class for all buttons
abstract class ButtonBase extends FormField {
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_field($type) {
$r = "<button type=\"$type\"";
// 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 .= ">{$this->_value}</button>\n";
// return html code
return $r;
}
}
?>