Login   Register  
PHP Classes
elePHPant
Icontem

File: HTML_Form.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of masa918  >  Form taglib classes  >  HTML_Form.inc  >  Download  
File: HTML_Form.inc
Role: ???
Content type: text/plain
Description: restore form value
Class: Form taglib classes
Simple HTML Form taglib
Author: By
Last change:
Date: 12 years ago
Size: 5,440 bytes
 

Contents

Class file image Download
<?
/**
*  HTML_Form Class
*  restore POST(GET) request value
*
*  @author Masahiro.Kobayashi <masa918@nifty.com>
*  @access public
*/
class HTML_Form
{
  /**
  *  HTTP Request Parameter
  *  @var copy of $_GET or $_POST
  *  @access private
  */
  var $params = null;
  /**
  *  magic quote enable
  *  @var 
  */
  var $magic_quote = false;
  /**
  *
  *  form_vars
  *
  */
  var $form_vars = array();
  /**
  *  Constructor
  *
  *  @access private
  *  @see instance
  */
  function HTML_Form()
  {
    if(get_magic_quotes_gpc() === 0 )
      $this->magic_quote = false;
    else
      $this->magic_quote = true;
    
    //  $set parameter
    $type = $_SERVER["REQUEST_METHOD"];
    if($type == "POST")
      $this->params = $_POST;
    elseif($type == "GET")
      $this->params = $_GET;
    
    if($this->magic_quote === true)
    {
      $this->stripslashes_param($this->params);
    }
  }
  /**
  *  get instance
  *
  *  @access public
  *  @return object HTML_Form
  */
  function &instance()
  {
    static $instance;
    if(isset($instance)){
      return $instance;
    }
    else{
      $instance = new HTML_Form();
      return $instance;
    }
  }
  /**
  *  clear $params
  *
  *  @access public
  */
  function clear()
  {
    $this->params = array();
  }
  /**
  *  print html specialized request parameter[$key]
  *
  *  @access public
  */ 
  function htmlValue($key,$default = "")
  {
    $ret = $this->params[$key];
    if(isset($ret))
      print htmlspecialchars($ret);
    else
      print htmlspecialchars($default);
  }
  function get($key)
  {
    return htmlspecialchars($this->params[$key]);
  }
  
  /**
  *  stripslashes_param
  *
  */
  function stripslashes_param(&$val)
  {
    if(is_string($val)){
      return stripslashes($val);
    }
    elseif(is_array($val))
    {
      foreach($val as $key => $value)
      {
        $val[$key] = $this->stripslashes_param($value);
      }
      
      return $val;
    }
  }
  function doAction($var_name , &$object, $method)
  {
    if(isset($this->params[$var_name])){
      return $object->$method($this);
    }
  }
  /**
  *
  *  Text Field
  *
  */
  function text($name,$default = "")
  {
    if(isset($this->params[$name])){
      $data = $this->params[$name];
    }
    else
      $data = $default;
    print('type="text" name="'.$name.'" value="');
    print(htmlspecialchars($data).'"');
  }
  /**
  *
  *  Password Field
  *
  */
  function password($name,$default = "")
  {
    if(isset($this->params[$name])){
      $data = $this->params[$name];
    }
    else
      $data = $default;
    print('type="password" name="'.$name.'" value="');
    print(htmlspecialchars($data).'"');
  }
  /**
  *
  *  [howto]
  *  <input type="radio" name="type" <? $form->radio("type","1","0"); ?>>
  */
  function radio($name,$value,$default = "")
  {
    if(isset($this->params[$name])){
      $data = $this->params[$name];
    }
    else
      $data = $default;
    
    print('type="radio" name="'.$name.'"');
    print ' value="'.$value.'"';
    if($data === $value)
      print ' checked';
  }
  /**
  *
  *  print checkbox value
  *
  *  @access public
  */
  function checkbox($name,$value,$default = "")
  {
    $keys = $this->_get_keys($name);
    $var_name = $keys[0];
    
    if(isset($this->params[$var_name])){
      $data = $this->params[$var_name];
      for($i=1; $i<count($keys); $i++){
        $key = $keys[$i];
        $data = $data[$key];
      }
    }
    else
      $data = $default;
    
    print('type="checkbox" name="'.$name.'"');
    print ' value="'.$value.'"';
    
    if(is_array($data)){
      if(in_array($value,$data))
      {
        print ' checked';
      }
    }
    else
    {
      if($data === $value)
        print ' checked';
    }
  }
  /**
  *
  *  select 
  *
  */
  function &getSelect($name)
  {
    $ret =& new HTML_Form_Select($this,$name);
    return $ret;
  }
  
  function _get_keys($name)
  {
    $ret = array();
    $name = str_replace("]","",$name);
    $tok = strtok($name,"[");
  
    while($tok){
      $ret[] = $tok;
      $tok = strtok("[");
    }
    return $ret;
  }
}
/**
*
*  HTML select tag class
*
*  @access private
*/
class HTML_Form_Select
{
  var $keys;
  var $form;
  var $name;
  
  function HTML_Form_Select(&$form,$name)
  {
    $this->name = $name;
    $this->form = $form;
    $this->keys = $this->form->_get_keys($name);
  }
  function setSize($size){
    $this->size = $size;
  }
  function setMultiple($bool)
  {
    $this->isMultiple = $bool;
  }
  
  function open($opt = null){
    print 'name="'.$this->name.'"';
  }
  
  function option($value,$default = "")
  {
    $form =& $this->form;
    $var_name = $this->keys[0];
    if(isset($form->params[$var_name])){
      $data = $form->params[$var_name];
      for($i=1; $i<count($this->keys); $i++){
        $key = $this->keys[$i];
        $data = $data[$key];
      }
    }
    else
      $data = $default;
    
    print 'value="'.$value.'"';
    
    if(is_array($data)){
      if(in_array($value,$data))
      {
        print ' selected';
      }
    }
    else
    {
      if($data === $value)
        print ' selected';
    }
  }
}
?>