Form Class

Here is the Form class. It may run out to the right a bit.


<?php
/*
##################################################
#
# Filename..........: $RCSfile: Form.php,v $
# Original Author...: Anthony L. Awtrey
# Version...........: $Revision: 0.1 $
# Last Modified By..: $Author: aawtrey $
# Last Modified.....: $Date: 2006/09/21 18:15:56 $
#
# Copyright 2006 Anthony Awtrey
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
*/

/*
 * This class provides methods to build HTML forms.
 */
class Form {

  var 
$action;
  var 
$method;
  var 
$hidden = array();
  var 
$input  = array();
  var 
$submit = array();
  var 
$reset;

  
/*
   * Class initialization
   */
  
function  __construct($action='index.php'$method='GET') {
    global 
$_SERVER;
    global 
$_SESSION;
    global 
$_REQUEST;
    global 
$_POST;
    global 
$_GET;
    
$this->action $action;
    
$this->method $method;
  }

  function 
__destruct()
  {
  }

  
/*
   * Add a hidden form input
   */
  
function hidden($name,$value) {
    
$c count($this->hidden);
    
$this->hidden[$c]['name']      = $name;
    
$this->hidden[$c]['value']     = $value;
  }

  
/*
   * Add a text form input
   */
  
function text($label,$name,$value,$size='30',$maxlength='') {
    
$c count($this->input);
    
$this->input[$c]['type']      = 'text';
    
$this->input[$c]['label']     = $label;
    
$this->input[$c]['name']      = $name;
    
$this->input[$c]['value']     = $value;
    
$this->input[$c]['size']      = $size;
    if ( 
$maxlength == '' ) {
      
$this->input[$c]['maxlength'] = $size;
    } else {
      
$this->input[$c]['maxlength'] = $maxlength;
    }
  }

  
/*
   * Add a password form input
   */
  
function password($label,$name,$value,$size='30',$maxlength='') {
    
$c count($this->input);
    
$this->input[$c]['type']      = 'password';
    
$this->input[$c]['label']     = $label;
    
$this->input[$c]['name']      = $name;
    
$this->input[$c]['value']     = $value;
    
$this->input[$c]['size']      = $size;
    if ( 
$maxlength == '' ) {
      
$this->input[$c]['maxlength'] = $size;
    } else {
      
$this->input[$c]['maxlength'] = $maxlength;
    }
  }

  
/*
   * Add a file form input
   */
  
function file($label,$name,$value,$size='30',$maxlength='') {
    
$c count($this->input);
    
$this->input[$c]['type']      = 'file';
    
$this->input[$c]['label']     = $label;
    
$this->input[$c]['name']      = $name;
    
$this->input[$c]['value']     = $value;
    
$this->input[$c]['size']      = $size;
    if ( 
$maxlength == '' ) {
      
$this->input[$c]['maxlength'] = $size;
    } else {
      
$this->input[$c]['maxlength'] = $maxlength;
    }
  }

  
/*
   * Add a textarea form input
   */
  
function textarea($label,$name,$value,$rows='5',$cols='60') {
    
$c count($this->input);
    
$this->input[$c]['type']      = 'textarea';
    
$this->input[$c]['label']     = $label;
    
$this->input[$c]['name']      = $name;
    
$this->input[$c]['value']     = $value;
    
$this->input[$c]['rows']      = $rows;
    
$this->input[$c]['cols']      = $cols;
  }

  
/*
   * Add a select form input
   * $options must be a key/value array
   */
  
function select($label,$name,$options,$selected='',$size='1') {
    
$c count($this->input);
    
$this->input[$c]['type']      = 'select';
    
$this->input[$c]['label']     = $label;
    
$this->input[$c]['name']      = $name;
    
$this->input[$c]['options']   = $options;
    
$this->input[$c]['selected']  = $selected;
    
$this->input[$c]['size']      = $size;
  }

  
/*
   * Add a captcha form input. This is an input test used to prevent spam.
   */
  
function captcha() {
    
$c count($this->input);
    
$this->input[$c]['type']      = 'captcha';
  }

  
/*
   * Add a submit button
   */
  
function submit($name,$value) {
    
$c count($this->submit);
    
$this->submit[$c]['name']     = $name;
    
$this->submit[$c]['value']    = $value;
  }

  
/*
   * Add a reset button
   */
  
function reset($name) {
    
$this->reset $name;
  }

  
/*
   * Generate a captcha image for the form and set it to the session
   */
  
function generateCaptcha() {
    
$image    imagecreate(10030);
    
$white    imagecolorallocate($image0xFF0xFF0xFF);
    
$gray     imagecolorallocate($image0xC00xC00xC0);
    
$darkgray imagecolorallocate($image0x500x500x50);
    
srand((double)microtime()*1000000);
    for (
$i 0$i 30$i++) {
      
$x1 rand(0,100);
      
$y1 rand(0,30);
      
$x2 rand(0,100);
      
$y2 rand(0,30);
      
imageline($image$x1$y1$x2$y2 $gray);
    }
    for (
$i 0$i 5$i++) {
      
$cnum[$i] = rand(0,9);
    }
    for (
$i 0$i 5$i++) {
      
$fnt rand(3,5);
      
$x $x rand(12 20);
      
$y rand(12);
      
imagestring($image$fnt$x$y$cnum[$i] , $darkgray);
    }
    
$digit "$cnum[0]$cnum[1]$cnum[2]$cnum[3]$cnum[4]";
    
$_SESSION['inCaptcha'] = $digit;
    
header('Content-type: image/png');
    
imagepng($image);
    
imagedestroy($image);
    exit;
  }

  
/*
   * Render the defined form into HTML
   */
  
function render() {
    
$output "<form action="" $this->action "" method="" $this->method "" enctype="multipart/form-data">n";
    foreach (
$this->hidden as $key => $val) {
      
$output .= "<input type="hidden" name="" $val['name'] . "" value="" $val['value'] . "" />n";
    }
    foreach (
$this->input as $key => $val) {
      switch (
$val['type']) {
        case 
'text':
          
$output .= "  <p>n";
          
$output .= "    <label>" $val['label'] . "</label>n";
          
$output .= "    <input type="text" name="" $val['name'] . "" value="" $val['value'] . "" size="" $val['size'] . "" maxlength="" $val['maxlength'] . "" />n";
          
$output .= "  </p>n";
          break;
        case 
'password':
          
$output .= "  <p>n";
          
$output .= "    <label>" $val['label'] . "</label>n";
          
$output .= "    <input type="password" name="" $val['name'] . "" value="" $val['value'] . "" size="" $val['size'] . "" maxlength="" $val['maxlength'] . "" />n";
          
$output .= "  </p>n";
          break;
        case 
'file':
          
$output .= "  <p>n";
          
$output .= "    <label>" $val['label'] . "</label>n";
          
$output .= "    <input type="file" name="" $val['name'] . "" value="" $val['value'] . "" size="" $val['size'] . "" maxlength="" $val['maxlength'] . "" />n";
          
$output .= "  </p>n";
          break;
        case 
'textarea':
          
$output .= "  <p>n";
          
$output .= "    <label>" $val['label'] . "</label>n";
          
$output .= "    <textarea name="" $val['name'] . "" rows="" $val['rows'] . "" cols="" $val['cols'] . "">n" .  $val['value'] . "n</textarea>n";
          
$output .= "  </p>n";
          break;
        case 
'select':
          
$output .= "  <p>n";
          
$output .= "    <label>" $val['label'] . "</label>n";
          
$output .= "    <select name="" $val['name'] . "" size="" $val['size'] . "">n";
          foreach ( 
$val['options'] as $k => $v) {
            
$selected '';
            if ( 
$k == $val['selected'] ) {
              
$selected 'SELECTED';
            }
            
$output .= "      <option value=""$k "" " $selected ">" $v "</option>n";
          }
          
$output .= "    </select>n";
          
$output .= "  </p>n";
          break;
        case 
'captcha':
          
$output .= "  <p>n";
          
$output .= "    <label>Enter numbers:</span>n";
          
$output .= "    <img src="".RELPATH."common/captcha.php"><input type="text" name="inCaptcha" value="" size="5" maxlength="5" />n";
          
$output .= "  </p>n";
          break;
        default:
          break;
      }
    }
    
$output .= "  <p>n";
    foreach (
$this->submit as $key => $val) {
      
$output .= "    <button name="" $val['name'] . "">" $val['value'] . "</button>n";
    }
    if ( 
$this->reset ) {
      
$output .= "    <button type="reset">" $this->reset "</button>n";
    }
    
$output .= "  </p>n";
    
$output .= "</form>n";
    return 
$output;
  }

}
?>