"Fossies" - the Fresh Open Source Software Archive

Member "openmailadmin-1.0.1/inc/lib/HTMLInputTagGenerator.php" (19 Aug 2006, 5308 Bytes) of package /linux/privat/old/openmailadmin-1.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "HTMLInputTagGenerator.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * Regard this as huge factory for generating strings of HTML "input" tags.
    4  *
    5  * @see     http://www.w3.org/TR/html401/interact/forms.html
    6  */
    7 class HTMLInputTagGenerator
    8 {
    9     /** Key of this array contains tag's name and value its CSS class. */
   10     public      $arrClass   = array();
   11     /** Key of this array contains tag's name and value its tag-properties. */
   12     public      $arrProperties  = array();
   13     public static   $arrDontSetValuesFor    = array('hidden', 'password');
   14 
   15     public function __construct() {
   16         $this->arrClass['text']     = 'text';
   17         $this->arrClass['password'] = 'password';
   18         $this->arrClass['image']    = 'image';
   19         $this->arrClass['submit']   = 'submit';
   20         $this->arrClass['checkbox'] = 'checkbox';
   21         $this->arrClass['textarea'] = 'textarea';
   22         $this->arrClass['select']   = 'select';
   23         $this->arrProperties['image']   = array('border'    => '0',
   24                             'alt'       => ''
   25                             );
   26         $this->arrProperties['input']   = array();
   27         $this->arrProperties['textarea']    = array();
   28         $this->arrProperties['select']  = array();
   29     }
   30 
   31     /**
   32      * For synthesizing generic tags.
   33      * SGML container can enclose text and thus consist of opening and closing sequence. I.e. B of HTML.
   34      *
   35      * @param   tag     Tag's name. I.e. "input".
   36      * @param   arrProperties   Array of properties of the tag.
   37      * @param   strContent  Text enclosed byy the tag.
   38      * @param   container   Boolean. True if tag is SGML container.
   39      * @param   part        "a" for "all parts", "s" for "opening/start sequence" and "e" for "closing/ending sequence"
   40      */
   41     private function _generic($tag, $arrProperties, $strContent = '', $container = true, $part = 'a') {
   42         $ret = '';
   43         if($part != 'e') {
   44             $arrProperties  = array_merge(isset($this->arrProperties[$tag])
   45                                 ? $this->arrProperties[$tag]
   46                                 : $this->arrProperties,
   47                             $arrProperties);
   48             $ret    = '<'.$tag;
   49             foreach($arrProperties as $key => $value)
   50                 $ret    .= ' '.strtolower($key).'="'.$value.'"';
   51             unset($arrProperties);
   52             if($container) {
   53                 $ret    .= '>';
   54                 $ret    .= $strContent;
   55             } else {
   56                 $ret    .= ' />';
   57             }
   58         }
   59         if($part != 's' && $container) {
   60             $ret    .= '</'.$tag.'>';
   61         }
   62         return $ret;
   63     }
   64 
   65     /** For generating input tags. */
   66     public function _generate($type, $name, $value, $arrProperties) {
   67         $arrProperties['type']  = $type;
   68         $arrProperties['name']  = $name;
   69         if($type == 'checkbox' || $type == 'radio') {
   70             if(isset($_POST[$name]) && $_POST[$name] == $value)
   71                 $arrProperties['checked'] = '1';
   72             $arrProperties['value'] = $value;
   73         } else {
   74             if(!in_array($type, self::$arrDontSetValuesFor) && isset($_POST[$name]))
   75                 $arrProperties['value'] = $_POST[$name];
   76             else if(!is_null($value))
   77                 $arrProperties['value'] = $value;
   78         }
   79         if(!isset($arrProperties['class']) && isset($this->arrClass[$type]))
   80             $arrProperties['class'] = $this->arrClass[$type];
   81 
   82         if(isset($this->arrProperties[$type]))
   83             $arrProperties  = array_merge($this->arrProperties[$type], $arrProperties);
   84         return($this->_generic('input', $arrProperties, '', false));
   85     }
   86 
   87     public function checkbox($name, $value = null, $prop = array()) {
   88         return($this->_generate('checkbox', $name, $value, $prop));
   89     }
   90 
   91     public function radio($name, $value, $prop = array()) {
   92         return($this->_generate('radio', $name, $value, array_merge(array('id' => $name.'_'.$value), $prop)));
   93     }
   94 
   95     public function submit($name) {
   96         return($this->_generate('submit', $name, $name, array()));
   97     }
   98 
   99     public function text($name, $maxlength = '', $default = null) {
  100         if($maxlength != '' && is_numeric($maxlength))
  101             return($this->_generate('text', $name, $default, array('maxlength' => $maxlength)));
  102         else
  103             return($this->_generate('text', $name, $default, array()));
  104     }
  105 
  106     public function textarea($name, $rows = 2, $cols=49) {
  107         return($this->_generic('textarea',
  108                     array('rows' => $rows, 'cols' => $cols, 'name' => $name, 'id' => $name),
  109                     isset($_POST[$name]) ? $_POST[$name] : '', true));
  110     }
  111 
  112     /**
  113      * @param   name        Name of input field.
  114      * @param   arr_names   Array with names of options which the use can chose from. These will be displayed.
  115      * @param   arr_values  Corresponding values. If none are given an option's value will be equal to its name.
  116      * @param   size        rows visible
  117      * @param   multiple    Set to 1 to allow multiple selection.
  118      */
  119     public function select($name, $arr_names, $arr_values = array(), $size = '1', $multiple = 0) {
  120         $props = array('size' => $size, 'name' => $name);
  121         if($multiple == 1)
  122             $props['multiple'] = 1;
  123         $select_value = '';
  124         foreach($arr_names as $key => $value) {
  125             $select_value .= '<option';
  126             if(isset($arr_values[$key])) {
  127                 $select_value .= ' value="'.$arr_values[$key].'"';
  128                 if(isset($_POST[$name]) && $_POST[$name] == $arr_values[$key])
  129                     $select_value .= ' selected="selected"';
  130             } else {
  131                 if(isset($_POST[$name]) && $_POST[$name] == $value)
  132                     $select_value .= ' selected="selected"';
  133             }
  134             $select_value .= ">$value</option>";
  135         }
  136         return($this->_generic('select', $props, $select_value, true));
  137     }
  138 
  139     public function password($name, $maxlength = '') {
  140         if($maxlength != '' && is_numeric($maxlength))
  141             return($this->_generate('password', $name, null, array('maxlength' => $maxlength)));
  142         else
  143             return($this->_generate('password', $name, null, array()));
  144     }
  145 
  146     public function hidden($name, $value) {
  147         return($this->_generate('hidden', $name, $value, array()));
  148     }
  149 
  150 }
  151 ?>