Location: PHPKode > scripts > phpFormBuilder > php-form-builder-2010-02-01/FormDisplay.php
<?
/**
*
* @package luc
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @author Emily Brand
* 
* Minimum Requirement: PHP 5.2
*/
require_once('database.class.php');

class FormDisplay
{
  public $values = array();  //Holds submitted form field values
  public $errors = array();  //Holds submitted form error messages
  public $num_errors;        //The number of errors in submitted form
  public $tablename;         //The name of the form that will be inserted into the same named table
  public $tablestructure = array();    //The table structure to make it possible to add a new table or fields if necessary

  /* Class constructor */
  function FormDisplay($tablename){
    
    $this->tablename = $tablename;
    
    /**
     * Get form value and error arrays, used when there
     * is an error with a user-submitted form.
     */
    if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){
      $this->values = $_SESSION['value_array'];
      $this->errors = $_SESSION['error_array'];
      $this->num_errors = count($this->errors);
      $this->user = $_SESSION['username'];
    }
    else if(isset($_SESSION['value_array'])){
      $this->values = $_SESSION['value_array'];
      $this->user = $_SESSION['username'];
    }
    else if(isset($_SESSION['varray']) && isset($_SESSION['earray'])){
      $this->values = $_SESSION['varray'];
      $this->errors = $_SESSION['earray'];
      $this->num_errors = count($this->errors);
      $this->user = $_SESSION['username'];

      unset($_SESSION['varray']);
      unset($_SESSION['earray']);
    }
    else if(isset($_SESSION['varray'])){
      $this->values = $_SESSION['varray'];
      unset($_SESSION['varray']);
    }
    else if(isset($_SESSION['varray2'])){
      $this->values = $_SESSION['varray2'];

      unset($_SESSION['varray2']);
    }
    else{
      $this->num_errors = 0;
    }
  }

  /**
   * setValue - Records the value typed into the given
   * form field by the user.
   */
  function setValue($field, $value){
    $this->values[$field] = $value;
  }

  /**
   * setError - Records new form error given the form
   * field name and the error message attached to it.
   */
  function setError($field, $errmsg){
    $this->errors[$field] = $errmsg;
    $this->num_errors = count($this->errors);
  }

  /**
   * value - Returns the value attached to the given
   * field, if none exists, the empty string is returned.
   */
  function value($field){
    if(array_key_exists($field,$this->values)){
      return htmlspecialchars(stripslashes($this->values[$field]));
    }else{
      return "";
    }
  }

  /**
   * error - Returns the error message attached to the
   * given field, if none exists, the empty string is returned.
   */
  function error($field){
    if(array_key_exists($field,$this->errors)){
      return "<font color=\"#ff0000\">".$this->errors[$field]."</font>";
    }else{
      return "";
    }
  }

  /* getErrorArray - Returns the array of error messages */
  function getErrorArray(){
    return $this->errors;
  }

  /* getValueArray - Returns the array of error messages */
  function getValueArray(){
    return $this->values;
  }
  
  /**
   * Add the field structure to an array for future use of building and editing
   * the structure of the form's database table.
   * @param $id
   * @param $type
   * @param $size
   * @return unknown_type
   */
  function addFieldStructure($id, $type, $size) {
    $fields = new FieldStructure($id, $type, $size);
    $this->tablestructure[$id] = $fields;
  }

  /**
   * createTextBox - Returns a text box with the specified
   * parameters
   *
   * @param id the id & name for the textbox
   * @param value the value of the textbox
   * @param size default 20
   * @param maxsize default 50
   * @return String the html version of the textbox
   */
  function createTextBox($name, $id, $value, $size, $maxsize, $type = "VARCHAR"){
    $this->addFieldStructure($id, $type, $maxsize);
    return $name.$this->insertUnderlines($value)."&nbsp;";
  }

  /**
   * createInputFile - Returns a input file box with the specified parameters
   *
   * @param id the id & name for the textbox
   * @param value the value of the textbox
   * @return String the html version of the textbox
   */
  function createInputFile($name, $id, $value, $type = "VARCHAR"){
    $this->addFieldStructure($id, $type, $maxsize);
    return "$value File Uploaded!";
  }

  /**
   * createTextArea - Returns a textarea box with the specified parameters
   *
   * @param id the id & name for the textbox
   * @param value the value of the textbox default NULL
   * @param cols default 10
   * @param rows default 10
   * @return String the html version of the textbox
   */
  function createTextArea($name, $id, $value = NULL, $cols = 10, $rows = 10, $script = NULL){
    $this->addFieldStructure($id, "MEDIUMTEXT", null);
    return nl2br($value);
  }

  /**
   * createPasswordBox - Returns a password box with the specified parameters
   *
   * @param id the id & name for the textbox
   * @param value the value of the textbox
   * @param size default 20
   * @param maxsize default 50
   * @return String the html version of the textbox
   */
  function createPasswordBox($name, $id, $value, $size=20, $maxsize=50){
    $this->addFieldStructure($id, "VARCHAR", $maxsize+50);
    return "******";
  }

  /**
   * createHidden - Returns a hidden box with the specified parameters
   *
   * @param id - the id & name for the textbox
   * @param value - the value of the textbox
   * @return - the html version of the hidden input box
   */
  function createHidden($id, $value){
    $this->addFieldStructure($id, "VARCHAR", length($value)+50);
    return "";
  }

  /**
   * createRadioButton - Returns a radio button with the specified parameters
   *
   * @param nameId - the id & name for the radio
   * @param value - the value of the radio button
   * @param checked - the value of the radio button
   * @param onchangeform - (optional) onchange command
   * @param formname - (optional) the formname for the onchange command
   * @return - the html version of the radio button
   */
  function createRadioButton($nameId, $id, $checkedValue, $type = "VARCHAR", $size = null){
    $size = 0;
    foreach($nameId as $name=>$value) {
      $radio .= (($value == $checkedValue) ? "&#9746;" : "&#9744;")."&nbsp;$name";
      $size = ($size < strlen($value)) ? strlen($value) : $size;
    }
    $this->addFieldStructure($id, $type, $size+10);
    return $radio;
  }

  /**
   * createCheckBox - Returns a checkbox with the specified parameters
   *
   * @param id - the id & name for the checkbox
   * @param value - the value of the checkbox
   * @param checked - true or false
   * @return - the html version of the checkbox
   */
  function createCheckBox($nameId, $id, $checkedValue, $onchangeform = NULL, $formname = NULL){
    $size = 0;
    foreach($nameId as $name=>$value) {
      $radio .= ((in_array($value,$checkedValue)) ? "&#9746;" : "&#9744;")."&nbsp;$name";
      $size = ($size < strlen($value)) ? strlen($value) : $size;
    }
    $this->addFieldStructure($id, $type, $size+10);
    return $radio;
  }

  /**
   * createSubmit - Returns a hidden box with the specified parameters
   *
   * @param id - the id & name for the submit button & hidden input box
   * @param value - the value of the submit button & hidden input box
   * @return - the html version of the hidden input box & the submit button
   */
  function createSubmit($id, $value){
    return "";
  }
   
  function text($text) {
    return $text;
  }
   
  function linebreak() {
    return "<br /><br />";
  }

  /**
   * To insert underlines if no value is present.
   *
   * @param String $value
   * @return String
   */
  function insertUnderlines($value) {
    if(!$value) return "__________";
    else return "<span style='text-decoration: underline'>&nbsp;&nbsp;$value&nbsp;&nbsp;</span>";
  }

  function finalizeForm($content) {
    global $database;
//    // Set up parameters
//    $to = "hide@address.com";
//    $subject = "Undergraduate Excel Application";
//    
//    $from = "hide@address.com";
//    $headers = "MIME-Version: 1.0" . "\n";
//    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\n";
//    $headers .= "From: $from" . "\n";
//    
//    // Send email
//    mail($to,$subject,$content,$headers);
    if($database->insertIntoDatabase($this->tablename, $this->tablestructure, $this->values, $content))
      return $content;
    else
      return "There was an error with your request. Please contact the administrator.";
  }
};

?>
Return current item: phpFormBuilder