<?php
/*-------1---------2---------3---------4---------5---------6---------7---------8---*/
// +----------------------------------------------------------------------+
// | PHP version 4, 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The YHA (Yono Hendro Arie) Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | hide@address.com so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Symbio Form Maker Class |
// | Generate a HTML form |
// | This class is only abstract class, if you want to use it |
// | extend this class |
// +----------------------------------------------------------------------+
// | Authors: Arie Nugraha <hide@address.com> |
// +----------------------------------------------------------------------+
//
// $Id$
# global variables
$form_name = '';
$form_method = 'POST';
$form_action = $_SERVER['PHP_SELF'];
class GUI_form_maker
{
# properties
var $elements = array(); // an array of form element to extract
var $hidden_elements = array(); // an array of hidden input fields
var $form_name = ''; // the name of form
var $form_method = ''; // method in which data sent
var $form_action = ''; // url of proccessing script
# class constructor
# set the form attribute
/*
@return void
*/
function GUI_form_maker()
{
global $form_name, $form_method, $form_action;
$this->form_name = $form_name;
$this->form_method = $form_method;
$this->form_action = $form_action;
}
# method to adding Text, Textarea, Password and File input form
/*
@return void
@param string $elmnt_type - type of text field ('text', 'textarea', 'password' or 'file')
@param string $elmnt_name - element name
@param string $elmnt_label - label of the element
@param string $elmnt_value - element default value
@param string $elmnt_attr - element additional attribute
*/
function addTextField($elmnt_type, $elmnt_name, $elmnt_label, $elmnt_value = '', $elmnt_attr = '')
{
if ($elmnt_type == 'textarea') {
$_buffer = "<textarea name='$elmnt_name' $elmnt_attr>\n";
$_buffer .= "$elmnt_value\n";
$_buffer .= "</textarea>\n";
} else {
$_buffer = "<input type='$elmnt_type' name='$elmnt_name' ";
$_buffer .= "value='$elmnt_value' $elmnt_attr>\n";
}
$this->elements[] = array('label' => $elmnt_label, 'element' => $_buffer);
}
# method to add drop down list element
/*
@return void
@param string $elmnt_name - element name
@param string $elmnt_label - label of the element
@param array $option_array - an array of options
the array must look like example below
$option[1] = array('option value 1', 'option text 1');
$option[2] = array('option value 2', 'option text 2');
$option[n] = array('option value n', 'option text n');
@param string $default_selected - the default selected option value that match with
one of value contained in $option_array
*/
function addSelectList($elmnt_name, $elmnt_label, $option_array, $default_selected = '', $elmnt_attr = '')
{
// check for $option_array param
if (!is_array($option_array)) {
$error = 'The third argument must be an array';
return $error;
} else {
foreach ($option_array as $option) {
// if the $option is not an array
if (!is_array($option)) {
$error = 'Value in options array must be an array to';
return $error;
}
}
}
$_buffer = "<select name='$elmnt_name' $elmnt_attr>\n";
foreach ($option_array as $option) {
if ($option[0] == $default_selected) {
$_buffer .= "<option value='".$option[0]."' selected>";
$_buffer .= $option[1]."</option>\n";
} else {
$_buffer .= "<option value='".$option[0]."'>".$option[1]."</option>\n";
}
}
$_buffer .= "</select>\n";
$this->elements[] = array('label' => $elmnt_label, 'element' => $_buffer);
}
# method to add checkbox element
/*
@return void
@param string $elmnt_name - element name
@param string $elmnt_label - label of the element
@param array $cbox_array - an array of options
the array must look like example below
$cbox_array[1] = array('checkbox value 1', 'checkbox text 1');
$cbox_array[2] = array('checkbox value 2', 'checkbox text 2');
$cbox_array[n] = array('checkbox value n', 'checkbox text n');
*/
function addCheckBox($elmnt_name, $elmnt_label, $cbox_array, $default_checked = '')
{
// check for $cbox_array param
if (!is_array($cbox_array)) {
$error = 'The third argument must be an array';
return $error;
} else {
foreach ($cbox_array as $cbox) {
// if the $cbox is not an array
if (!is_array($cbox)) {
$error = 'Value in checkbox array must be an array to';
return $error;
}
}
}
$elmnt_num = count($cbox_array);
$row_column = 5;
$_buffer = '';
if ($elmnt_num <= $row_column) {
foreach ($cbox_array as $cbox) {
if ($cbox[0] == $default_checked) {
$_buffer .= "<input type='checkbox' name='$elmnt_name'"
." value='".$cbox[0]."' checked>"
.' '.$cbox[1]."<br>\n";
} else {
$_buffer .= "<input type='checkbox' name='$elmnt_name'"
." value='".$cbox[0]."'>"
.' '.$cbox[1]."<br>\n";
}
}
} else {
$column_array = array_chunk($cbox_array, $row_column);
$_buffer = "<table>\n";
$_buffer .= "<tr>\n";
foreach ($column_array as $cbox_array2) {
$_buffer .= "<td valign='top'>\n";
foreach ($cbox_array2 as $cbox) {
if ($cbox[0] == $default_checked) {
$_buffer .= "<input type='checkbox' name='".$elmnt_name."[]'"
." value='".$cbox[0]."' checked>"
.' '.$cbox[1]."<br>\n";
} else {
$_buffer .= "<input type='checkbox' name='".$elmnt_name."[]'"
." value='".$cbox[0]."'>"
.' '.$cbox[1]."<br>\n";
}
}
$_buffer .= "</td>\n";
}
$_buffer .= "</tr>\n";
$_buffer .= "</table>\n";
}
$this->elements[] = array('label' => $elmnt_label, 'element' => $_buffer);
}
# method to add radio element
/*
@return void
@param string $elmnt_name - element name
@param string $elmnt_label - label of the element
@param array $radio_array - an array of options
the array must look like example below
$radio_array[1] = array('radio value 1', 'radio text 1');
$radio_array[2] = array('radio value 2', 'radio text 2');
$radio_array[n] = array('radio value n', 'radio text n');
*/
function addRadio($elmnt_name, $elmnt_label, $radio_array, $default_checked = '')
{
// check for $radio_array param
if (!is_array($radio_array)) {
$error = 'The third argument must be an array';
return $error;
} else {
foreach ($radio_array as $radio) {
// if the $radio is not an array
if (!is_array($radio)) {
$error = 'Value in radio array must be an array to';
return $error;
}
}
}
$_buffer = '';
foreach ($radio_array as $radio) {
if ($radio[0] == $default_checked) {
$_buffer .= "<input type='radio' name='$elmnt_name'"
." value='".$radio[0]."' checked>"
.' '.$radio[1]."<br>\n";
} else {
$_buffer .= "<input type='radio' name='$elmnt_name'"
." value='".$radio[0]."'>"
.' '.$radio[1]."<br>\n";
}
}
$this->elements[] = array('label' => $elmnt_label, 'element' => $_buffer);
}
# method to add hidden element
/*
@return void
@param string $elmnt_name - the name of element
@param string $elmnt_value - the value of hidden element
*/
function addHidden($elmnt_name, $elmnt_value)
{
$this->hidden_elements[] = "<input type='hidden' "
."name='$elmnt_name' value='$elmnt_value'>\n";
}
}
# End of GUI_form_maker class declaration
?>