<?php
/*
------------------------------------------------------------
Form generation class
Version: 1.0
------------------------------------------------------------
Author: Ingus Rukis
e-mail: hide@address.com
url: http://3a3-interactive.net
------------------------------------------------------------
Created: 05-04-2003
Last changes: 30-04-2003
------------------------------------------------------------
Short description:
Class for generating forms. Mostly generating forms by
hand is a real pain, you've got to build one form for
adding information, another one for editing, and then
those countless radiogroups, text fields, checkboxes
and other input controls.
There's a solution - this class :)
------------------------------------------------------------
Note on web standards:
Generated forms will be XHTML 1.0 compatible
(at least, I hope so .. )
------------------------------------------------------------
*/
class MyForm
{
// array for CSS class variables only these are used:
// table, tr, th, td, option, textarea, select, radio, checkbox,
// input, form
var $CSS_format = array();
// html|plaintext version of the form
var $tr = false;
// array for all of the fields on the form
var $fields = array();
// variable for form request method
var $form_method;
// variable for form action filename
var $form_action;
// Function - constructor
// ----------------------------------------------
// action - Filename, where to post the form
// method - POST|GET
// html - True|False - HTML|Plain-text result
function MyForm($action,$method,$html)
{
$this->setMethod($method);
$this->setAction($action);
if($html)
{
$this->tr = True;
}
}
// Function to set method property of the form
// ----------------------------------------------
// method - form method variable
function setMethod($method)
{
$this->form_method = $method;
}
// Function to set action property of the form
// ----------------------------------------------
// action - form action variable
function setAction($action)
{
$this->form_action = $action;
}
// Function to add CSS properties
// ----------------------------------------------
// tag - tag name to add CSS class name
// talue - CSS class name
function setCSS($tag,$value)
{
$this->CSS_format[$tag] = $value;
}
// Function to format HTML|Plain-text result
// ----------------------------------------------
// field_html - text to format
// label - label of the form element
// ----------------------------------------------
// NO DIRECT CALLING OF THIS FUNCTION!
function html_format_field($field_html,$label)
{
// HTML
if ($this->tr)
{
return '<tr class="'.$this->CSS_format['tr'].'"><td class="'.$this->CSS_format['td'].'"><p align="right">'.$label.': </p></td><td class="'.$this->CSS_format['td'].'"><p align="left">'.$field_html.'</p></td></tr>';
}
else
// Plain-text
{
return $label.': '.$field_html;
}
}
// Function to add DropDown boxes and listboxes to form
// ----------------------------------------------
// name - field name
// label - field label
// elements - elements of the field, array of associative arrays, structure :
// array('id'=>'blah','text'=>'dah')
// sel_id - selected element value
// size - by setting this to a bigger number than 1, you get listbox
// listbox_ms - allow|disallow select multiple values
function addDropDown($name, $elements, $label, $sel_id = 0, $size = 1, $listbox_ms=false)
{
$drop_down = '<select class="'.$this->CSS_format['select'].'" name="'.$name;
// multi-select listbox
if ($listbox_ms)
{
$drop_down .= '[]" multiple="multiple';
}
$drop_down .= '" size="'.$size.'">';
// adding elements
foreach($elements as $el)
{
$drop_down .= '<option class="'.$CSS_format['option'].'" value="'.$el['id'].'"';
// check if this element has to be selected or not
if ($sel_id == $el['id'])
{
$drop_down .= ' selected="selected "';
}
$drop_down .= '>'.$el['text'].'</option>';
}
$drop_down .= '</select>';
$this->fields[sizeof($this->fields)] = $this->html_format_field($drop_down,$label);
}
// Function to add hidden fields to form
// ----------------------------------------------
// name - name
// value - value
function addHidden($name, $value)
{
$this->fields[sizeof($this->fields)] = '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
}
// Function to add textarea field to form
// ----------------------------------------------
// name - name
// value - value
// label - label of the field
// rows - number of rows for the field
// cols - number of columns for the field
function addTextarea($name, $value, $label,$rows = 9, $cols = 30)
{
$txt_area = '<textarea class="'.$this->CSS_format['textarea'].'" name="'.$name.'" rows="'.$rows.'" cols="'.$cols.'">'.$value.'</textarea>';
$this->fields[sizeof($this->fields)] = $this->html_format_field($txt_area,$label);
}
// Function to add radio button group to form
// ----------------------------------------------
// name - name
// elements - array of associative arrays with elements. Structure
// array('id'=>'blah','text'=>'dah')
// label - label of the field
// checked_val - value of the selected element
function addRadioGroup($name, $elements, $label, $checked_val='')
{
foreach($elements as $item)
{
$radio_group .= '<input class="'.$this->CSS_format['radio'].'" type="radio" name="'.$name.'" value="'.$item['id'].'"';
if ($checked_val == $item['id'])
{
$radio_group .= 'checked="checked"';
}
$radio_group .= ' /> '.$item['text']."<br />\n";
}
$this->fields[sizeof($this->fields)] = $this->html_format_field($radio_group,$label);
}
// Function to add checkbox group to form
// ----------------------------------------------
// name - name
// elements - array of associative arrays with elements. Structure
// array('id'=>'blah','text'=>'dah')
// label - label
// checked_val - array with selected values. structure array(1,2,4)
function addCheckGroup($name, $elements, $label, $checked_val=array())
{
foreach($elements as $item)
{
$radio_group .= '<input class="'.$this->CSS_format['checkbox'].'" type="checkbox" name="'.$name.'[]" value="'.$item['id'].'"';
if (in_array( $item['id'],$checked_val))
{
$radio_group .= 'checked="checked"';
}
$radio_group .= ' /> '.$item['text']."<br />\n";
}
$this->fields[sizeof($this->fields)] = $this->html_format_field($radio_group,$label);
}
// Function to add single checkbox to form
// ----------------------------------------------
// name - name
// value - value
// label - field label
// checked - whether this checkbox is checked or not
function addCheckBox($name, $value, $label, $checked=False)
{
$ch_box = '<input class="'.$this->CSS_format['checkbox'].'" type="checkbox" name="'.$name.'" value="'.$value.'"';
if ($checked)
{
$ch_box .= ' selected="selected" ';
}
$ch_box .= '/>';
$this->fields[sizeof($this->fields)] = $this->html_format_field($ch_box,$label);
}
// Function to add text input field to form
// ----------------------------------------------
// name - name
// value - value
// label - field label
// type - type, one of: text|password|file
function addTextinput($name, $value, $label, $type='text')
{
$text_input = '<input class="'.$this->CSS_format['input'].'" type="'.$type.'" value="'.$value.'" name="'.$name.'" />';
$this->fields[sizeof($this->fields)] = $this->html_format_field($text_input,$label);
}
// Function to return form
// ----------------------------------------------
// title - Form title
// submit_value - value of the submit button
// submit_name - name of the submit button
// table_width - width of the table, if HTML formating is used
// cellspacing - HTML attribute of table
// cellpadding - HTML attribute of table
function showform($title,$submit_value='Submit', $submit_name='submit',$table_width='100%',$cellspacing=1,$cellpadding=2)
{
$form = '<form class="'.$this->CSS_format['form'].'" action="'.$this->form_action.'" method="'.$this->form_method.'">';
if ($this->tr)
{
$form .= '<table class="'.$this->CSS_format['table'].'" width="'.$table_width.'" cellspacing="'.$cellspacing.'" cellpadding="'.$cellpadding.'">';
$form .= '<tr class="'.$this->CSS_format['tr'].'"><th class="'.$this->CSS_format['th'].'" colspan="2">'.$title.'</th></tr>';
}
else
{
$form .= $title."\n";
}
// loop through the array of fields
foreach($this->fields as $field)
{
$form .= $field."\n";
}
if ($this->tr)
{
$form .= '<tr class="'.$this->CSS_format['tr'].'"><td colspan="2" class="'.$this->CSS_format['td'].'">
<center><input type="submit" name="'.$submit_name.'" value="'.$submit_value.'" /> <input type="reset" name="reset" value="Reset" /></center>
</td></tr>';
$form .= '</table></form>';
}
else
{
$form .= '<input type="submit" name="'.$submit_name.'" value="'.$submit_value.'" /> <input type="reset" name="reset" value="Reset" /></form>';
$form = '<p align="right">'.nl2br($form).'</p>';
}
$form = str_replace(' class=""','',$form);
return $form;
}
}
?>