Location: PHPKode > scripts > ooform > ooform/class_form.inc
<?php

  /**
  *
  * Include definition
  * So that PHP doen's throw a duplicate function redefine error
  *
  */
if (!defined("__CLASS_FORM_PHP__")) {

  define ("__CLASS_FORM_PHP__", true);

  /**
  * This class controls the form function
  * Imagine a form tags with all the form field.
  * This class is the container.
  *
  * Extends this class to suits your needs
  *
  * @author Herdian ferdianto <hide@address.com>
  *
  */
  class ooform {
    /**
    * Name of form
    *
    * @var string
    * @acess public
    */
       var $name;

    /**
    * The form's elements
    *
    * @var array
    * @acess public
    */
       var $elements=array();

    /**
    * The form's attribute
    *
    * @var string
    * @acess public
    */
       var $attributes;
       
    /**
    * Form id for check if the form has been submitted
    * need some thoughts about it.
    *
    * @var array
    * @acess public
    */
       var $id="";

    /**
    * Constructor of the class
    *
    * Initializes variables.
    *
    * @param $a_name string Name of the form
    * @param $a_attr string Form HTML attributes include methods, action, enctype
    * @return void
    */
       function ooform($a_name,$a_attr="") {
            $this->name=$a_name;
            $this->attributes=$a_attr;
            $this->id = "myform_data_".$this->name;
       }

    /**
    * Add a form control object for this class to handle
    *
    * @param $obj object FormControl instance
    * @return void
    */
       function add_elements($obj) {
            if (!is_object($obj)) { return false; }
            if (!isset($obj->name)) { return false; }
            $this->elements[$obj->name] = $obj;
            $name = $obj->name;
            $this->$name = $obj;
       }

    /**
    * Render the form's open tags
    *
    * @return string
    */
       function start() {
            return sprintf("<form name=\"%s\" %s>",$this->name,$this->attributes);
       }

    /**
    * Render the form's id fields and close tags
    * Maybe could be added with some js Validation scripts for the forms
    *
    * @return string
    */
       function end() {
            return "<input type=\"hidden\" name=\"form_id\" value=\"".$this->id."\">\n</form>";
       }
  } // end of ooforms class



  /**
  * A small hierarchy of classes that serve as object wrappers for HTML form's inputs
  */

  /**
  * Base generic form control class
  *
  * @author Herdian Ferdianto <hide@address.com>
  */

  class formControl {

    /**
    * Form Control name
    *
    * Also becomes the property of the ooforms element after add_elements methods
    *
    * @var string
    * @access private
    * @see $form
    */
       var $name="";
       
    /**
    * References the containing form's object
    *
    * Agregation of the ooforms class
    *
    * @var object ooforms
    * @access private
    */
       var $form;

    /**
    * The form control value after submittal
    *
    * @var string
    * @access public
    */
       var $value="";

    /**
    * The form control value before submittal
    *
    * @var string
    * @access public
    */
       var $initvalue="";

    /**
    * The form control html attributes
    *
    * @var string
    * @access public
    */
       var $attributes="";

    /**
    * If true the value of the forms is save
    * So that user didn't have to fill the form back again
    *
    * @var string
    * @access public
    */
       var $reuse=false;

    /**
    * True if the form's has not been submitted
    *
    * @var string
    * @access public
    */
       var $first=false;

    /**
    * Constructor of the class
    *
    * Initialize the variables, check if it's first time (before submittal or
    * after submittal
    *
    * @param $a_form object ooform A reference to the object representing containing form
    * @param $a_name string Control name
    * @param $a_value string initial form value
    * @param $a_attributes string Form object HTML attributes
    * @param $a_reuse bool Form reuseable value flags
    * @return void
    * @access public
    *
    */
       function formControl($a_form,$a_name,$a_value="",$a_attributes="",$a_reuse=false) {
            $this->form = $a_form;
            $this->name = $a_name;
            $this->attributes=$a_attributes;

            $this->reuse= $a_reuse;
            $this->first = !($GLOBALS["HTTP_POST_VARS"]["form_id"]==$this->form->id);
            $this->value = $GLOBALS["HTTP_POST_VARS"][$this->name];

            if (!$this->first && $this->reuse) {
                $this->initvalue = $this->value;
                }
            else {
                $this->initvalue = $a_value;
            }
        }

    /**
    * Defines the validation object
    *
    * @param $objVal object CustomValidation A reference to the object representing custom validation
    * @return bool
    * @access public
    *
    */
        function validate($objVal) {
            $this->Check = new $objVal($this->value);
            return $this->Check->result;
        }

    /**
    * Echoes the validation status after submittal
    *
    * @return string
    * @access public
    *
    */
        function showError() {
          if ($this->first) {
                return;
          }
          if (!$this->Check->result) {
                return $this->Check->errorMsg;
                }
     }
  } // end of formControl Class



  /**
  * text control class
  *
  */
  class textbox extends formControl {
     // return the html for textbox html forms.
     function render() {
          return sprintf("<input type=\"text\" name=\"%s\" value=\"%s\" %s>",$this->name,$this->initvalue,$this->attributes);
     }
  } // end of textbox class

  /**
  * password control class
  *
  */
  class password extends formControl {
     // return the html for textbox html forms.
     function render() {
          return sprintf("<input type=\"password\" name=\"%s\" value=\"%s\" %s>",$this->name,$this->initvalue,$this->attributes);
     }
  } // end of textbox class

  /**
  * textarea control class
  *
  */
  class textarea extends formControl {
     function render() {
          return sprintf("<textarea name=\"%s\" %s>%s</textarea>",$this->name,$this->attributes,$this->initvalue);
     }
  } // end of textarea class

  /**
  * submit buttons control class
  *
  */
  class submit extends formControl {
     function render() {
          return sprintf("<input type=\"submit\" name=\"%s\" value=\"%s\" %s>",$this->name,$this->initvalue,$this->attributes);
     }
  } // end of submit class

  /**
  * reset buttons control class
  *
  */
  class reset extends formControl {
     function render() {
          return sprintf("<input type=\"reset\" value=\"%s\" %s>",$this->initvalue,$this->attributes);
     }
  } // end if reset class


  /**
  * A custom validation class
  * Extends this class to any validation code you need
  * Some attributes that must in validation class
  *  - $result
  *  - $errorMsg
  *
  * @author Herdian ferdianto <hide@address.com>
  *
  */
  class CustomValidation {
    /**
    * Holds the validation result
    *
    * @var bool
    * @acess public
    */
      var $result;

    /**
    * Holds the error messages
    *
    * @var string
    * @acess public
    */
      var $errorMsg='';
    
    /**
    * Constructor of the class
    *
    * Initialize the variables, and validate the parameters
    *
    * @param $things any value to validate
    * @return void
    * @access public
    *
    */
    function customValidation($things) {
      $this->errorMsg='Error!!';
      $this->result = false;
      }
  } // end of custom check class

}// end of definition
?>
Return current item: ooform