/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/tag_utils/form_utils.inc
ViewVC logotype

Diff of /nfo/php/libs/com.newsblob.phphtmllib/tag_utils/form_utils.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by jonen, Thu Jan 30 03:29:42 2003 UTC revision 1.2 by jonen, Sat Feb 22 21:08:05 2003 UTC
# Line 4  Line 4 
4   * This file contains various helper functions   * This file contains various helper functions
5   * for building forms and form elements.   * for building forms and form elements.
6   *   *
7   * $Id$   * $Id$
8   *   *
9   * @author Walter A. Boring IV <waboring@buildabetterweb.com>   * @author Walter A. Boring IV <waboring@buildabetterweb.com>
10   * @package phpHtmlLib   * @package phpHtmlLib
11   *   *
12   *   *
13   */   */
14    
15    
16  /**  /**
# Line 27  Line 27 
27   */   */
28  function html_form( $name, $action, $method="GET", $attributes=array()) {  function html_form( $name, $action, $method="GET", $attributes=array()) {
29    
30          $attribs = array("name" => $name, "action" => $action,      $attribs = array("name" => $name, "action" => $action,
31                                           "method" => $method);                       "method" => $method);
32          $attributes = array_merge( $attribs, $attributes);      $attributes = array_merge( $attribs, $attributes);
33    
34          return new FORMtag( $attributes );      return new FORMtag( $attributes );
35  }  }
36    
37  /**  /**
# Line 47  function html_form( $name, $action, $met Line 47  function html_form( $name, $action, $met
47   * @return  string  returns the raw form tag.   * @return  string  returns the raw form tag.
48   */   */
49  function form_open( $name, $action, $method="GET", $attributes=array(), $indent_level=0 ) {  function form_open( $name, $action, $method="GET", $attributes=array(), $indent_level=0 ) {
50          $form = html_form( $name, $action, $method, $attributes);      $form = html_form( $name, $action, $method, $attributes);
51          return $form->_render_tag( $indent_level );      return $form->_render_tag( $indent_level );
52  }  }
53    
54    
# Line 58  function form_open( $name, $action, $met Line 58  function form_open( $name, $action, $met
58   * @return  string - the </form> tag.   * @return  string - the </form> tag.
59   */   */
60  function form_close( $indent_level=0 ) {  function form_close( $indent_level=0 ) {
61          $form = new FORMtag;      $form = new FORMtag;
62    
63          //just render the close tag.      //just render the close tag.
64          return $form->_render_close_tag( $indent_level );      return $form->_render_close_tag( $indent_level );
65  }  }
66    
67    
# Line 78  function form_close( $indent_level=0 ) { Line 78  function form_close( $indent_level=0 ) {
78   * @return  mixed   either returns an input object (default) or raw html.   * @return  mixed   either returns an input object (default) or raw html.
79   */   */
80  function form_text($name, $value=NULL, $size=NULL, $maxlength=NULL,  function form_text($name, $value=NULL, $size=NULL, $maxlength=NULL,
81                                     $attributes = array(), $render_flag=FALSE) {                     $attributes = array(), $render_flag=FALSE) {
82    
83          if ( $size != NULL ) {      if ($size != NULL) {
84                  $attributes["size"] = $size;          $attributes["size"] = $size;
85          }      }
86          if ( $maxlength != NULL ) {      if ($maxlength != NULL) {
87                  $attributes["maxlength"] = $maxlength;          $attributes["maxlength"] = $maxlength;
88          }      }
89    
90          $input = html_input("text", $name, $value, $attributes);      $input = html_input("text", $name, $value, $attributes);
91    
92          if ( $render_flag ) {      if ($render_flag) {
93                  return $input->render();          return $input->render();
94          } else {      }
95                  //user wants the object      else {
96                  return $input;          //user wants the object
97          }          return $input;
98        }
99  }  }
100    
101    
# Line 110  function form_text($name, $value=NULL, $ Line 111  function form_text($name, $value=NULL, $
111   * @return  mixed   either returns an input object (default) or raw html.   * @return  mixed   either returns an input object (default) or raw html.
112   */   */
113  function form_password($name, $value=NULL, $size=NULL, $maxlength=NULL,  function form_password($name, $value=NULL, $size=NULL, $maxlength=NULL,
114                                             $attributes = array(), $render_flag=FALSE) {                         $attributes = array(), $render_flag=FALSE) {
115    
116        if ($size != NULL) {
117            $attributes["size"] = $size;
118        }
119        if ($maxlength != NULL) {
120            $attributes["maxlength"] = $maxlength;
121        }
122    
123        $input = html_input( "password", $name, $value, $attributes );
124    
125          if ( $size != NULL ) {      if ($render_flag) {
126                  $attributes["size"] = $size;          return $input->render();
127          }      }
128          if ( $maxlength != NULL ) {      else {
129                  $attributes["maxlength"] = $maxlength;          //user wants the object
130          }          return $input;
131        }
         $input = html_input( "password", $name, $value, $attributes );  
   
         if ( $render_flag ) {  
                 return $input->render();  
         } else {  
                 //user wants the object  
                 return $input;  
         }  
132  }  }
133    
134  /**  /**
# Line 140  function form_password($name, $value=NUL Line 142  function form_password($name, $value=NUL
142   */   */
143  function form_button($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {  function form_button($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {
144    
145          $input = html_input( "button", $name, $value, $attributes );      $input = html_input( "button", $name, $value, $attributes );
146    
147          if ( $render_flag ) {      if ($render_flag) {
148                  return $input->render();          return $input->render();
149          } else {      }
150                  return $input;      else {
151          }          return $input;
152        }
153    
154  }  }
155    
# Line 161  function form_button($name, $value=NULL, Line 164  function form_button($name, $value=NULL,
164   */   */
165  function form_submit($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {  function form_submit($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {
166    
167          $input = html_input( "submit", $name, $value, $attributes );      $input = html_input( "submit", $name, $value, $attributes );
168    
169          if ( $render_flag ) {      if ($render_flag) {
170                  return $input->render();          return $input->render();
171          } else {      }
172                  return $input;      else {
173          }          return $input;
174        }
175  }  }
176    
177  /**  /**
# Line 182  function form_submit($name, $value=NULL, Line 186  function form_submit($name, $value=NULL,
186   */   */
187  function form_image($name, $value=NULL, $src, $attributes=array(), $render_flag=FALSE ) {  function form_image($name, $value=NULL, $src, $attributes=array(), $render_flag=FALSE ) {
188    
189          $attributes["src"] = $src;      $attributes["src"] = $src;
190          $input = html_input( "image", $name, $value, $attributes );          $input = html_input( "image", $name, $value, $attributes );
191    
192          if ( $render_flag ) {      if ($render_flag) {
193                  return $input->render();          return $input->render();
194          } else {      }
195                  return $input;      else {
196          }          return $input;
197        }
198  }  }
199    
200    
# Line 204  function form_image($name, $value=NULL, Line 209  function form_image($name, $value=NULL,
209   */   */
210  function form_radio($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {  function form_radio($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {
211    
212          $input = html_input( "radio", $name, $value, $attributes );      $input = html_input( "radio", $name, $value, $attributes );
213    
214          if ( $render_flag ) {      if ($render_flag) {
215                  return $input->render();          return $input->render();
216          } else {      }
217                  return $input;      else {
218          }          return $input;
219        }
220  }  }
221    
222  /**  /**
# Line 225  function form_radio($name, $value=NULL, Line 231  function form_radio($name, $value=NULL,
231   * @return  object returns a container object   * @return  object returns a container object
232   * @author Suren Markossian   * @author Suren Markossian
233   */   */
234  function form_active_radio($formname, $name, $list, $value, $line_break = TRUE, $attribs = FALSE) {  function form_active_radio( $name, $list, $value, $line_break = TRUE, $attribs = FALSE) {
235    
236      $container = new ContainerWidget();      $container = new ContainerWidget();
237      $i=0;      $i=0;
238      while (list($text,$val) = each($list)) {      while (list($text,$val) = each($list)) {
239          if ($attribs[$val]) {          if ($attribs[$val]) {
240                          $attr = $attribs[$val];              $attr = $attribs[$val];
241                  } else {          }
242                          $attr = array();          else {
243                  }              $attr = array();
244            }
245    
246          if ($val == $value) $attr[] = "checked";          if ($val == $value) $attr[] = "checked";
   
247          $container->push(form_radio($name, $val, $attr));          $container->push(form_radio($name, $val, $attr));
248    
249          if (count($list)>1) {          if (count($list)>1) {
250              if ($attribs[$val]) {              if ($attribs[$val]) {
251                                  $attr = $attribs[$val];                  $attr = $attribs[$val];
252                          } else {              }
253                                  $attr = array();              else {
254                          }                  $attr = array();
255                }
256    
257              $attr["class"] ="form_link";              $attr["class"] ="form_link";
258                          $js  = "javascript:if (!document.".$formname.".elements['".$name."'][".$i."]) ";              $attr["href"] = "#";
259                          $js .= "document.".$formname.".elements['".$name."'].checked = true; ";              $js = "javascript: function check(item){item.click();} check(".$name.".item($i));";
260                          $js .= "else if (!document.".$formname.".elements['".$name."'][".$i."].checked) ";              $attr["onclick"] = $js;
                         $js .= "document.".$formname.".elements['".$name."'][".$i."].checked=true;";  
                         $attr["href"] = "javascript:";  
                         $attr["onclick"] = $js;  
261    
262              $link = new Atag($attr, $text, FALSE);              $link = new Atag($attr, $text, FALSE);
263          }          }
# Line 278  function form_active_radio($formname, $n Line 282  function form_active_radio($formname, $n
282   */   */
283  function form_hidden($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {  function form_hidden($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {
284    
285          $input = html_input( "hidden", $name, $value, $attributes );      $input = html_input( "hidden", $name, $value, $attributes );
286    
287          if ( $render_flag ) {      if ($render_flag) {
288                  return $input->render();          return $input->render();
289          } else {      }
290                  return $input;      else {
291          }          return $input;
292        }
293  }  }
294    
295  /**  /**
# Line 298  function form_hidden($name, $value=NULL, Line 303  function form_hidden($name, $value=NULL,
303   */   */
304  function form_checkbox($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {  function form_checkbox($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {
305    
306          $input = html_input( "checkbox", $name, $value, $attributes );      $input = html_input( "checkbox", $name, $value, $attributes );
307    
308          if ( $render_flag ) {      if ($render_flag) {
309                  return $input->render();          return $input->render();
310          } else {      }
311                  return $input;      else {
312          }          return $input;
313        }
314  }  }
315    
316  /**  /**
317   * build/render an input tag of type CHECKBOX   * build/render an input tag of type CHECKBOX
318   * with onClick javaScript selection   * with onClick javaScript selection
319   *   *
  * @param       string  $formname - the name of the form that  
  *                  the checkbox lives  
320   * @param   string  $name - the name of the input tag.   * @param   string  $name - the name of the input tag.
321   * @param   string  $value - the value of the tag   * @param   string  $value - the value of the tag
322   * @param   string $text - text label for the checkbox   * @param   string $text - text label for the checkbox
323     * @param   string class - CSS class of the text
324   * @return  object returns a container object   * @return  object returns a container object
325   * @author Suren Markossian   * @author Suren Markossian
326   */   */
327  function form_active_checkbox($formname, $name, $text=NULL, $value=NULL, $onClick = NULL, $disabled=FALSE) {  function form_active_checkbox($name, $text=NULL, $value=NULL, $class = NULL, $onClick = NULL, $disabled=FALSE) {
328    
329      $container = container();      $container = container();
330    
331      if ($value) {      if ($value) {
332                  $attrib = array("style"=>"vertical-align: middle;","checked");          $attrib = array("style"=>"vertical-align: middle;","checked");
333          } else {      }
334                  $attrib = array("style"=>"vertical-align: middle;");      else {
335          }          $attrib = array("style"=>"vertical-align: middle;");
336          $check = form_checkbox($name, 1, $attrib);      }
337        $check = form_checkbox($name, 1, $attrib);
338    
339      if ($onClick) {      if ($onClick) {
340                  $check->set_tag_attribute("onClick",$onClick);          $check->set_tag_attribute("onClick",$onClick);
341          }      }
342      if ($disabled) {      if ($disabled) {
343                  $check->set_tag_attribute("disabled");          $check->set_tag_attribute("disabled");
344          }          $container->add( $check, $text );
345    
346      $container->push($check);      }
347        else {
348            $container->add($check);
349    
350          $action = "javascript:document.".$formname.".elements['".$name."'].checked = !document.".$formname.".elements['".$name."'].checked;";          $js_action = NULL;
351      if ($onClick) {          if (!$disabled) {
352                  $action .= $onClick;              $js_action = "javascript:" . $name . ".checked=!" . $name . ".checked;";
353          }          }
354    
355            if ($onClick) {
356                $js_action .= $onClick;
357            }
358    
359            $attributes = array("href"=>"#",
360                                "onClick"=>$js_action
361                               );
362    
363      $attributes = array("href"=>"javascript:",          if ($class != NULL)
364                          "onMouseOver"=>"status='" . $text . "';return true;",              $attributes["class"] = $class;
365                          "onMouseOut"=>"status='';return true;",  
366                                                  "onClick"=>$action,  
367                                                  "style" => "text-decoration:none;color: #000000;"          $link = new Atag($attributes, $text, FALSE);
368                         );          $container->add($link);
369        }
370    
     $link = new Atag($attributes, $text, FALSE);  
371    
     $container->push($link);  
372    
373      return $container;      return $container;
374  }  }
# Line 370  function form_active_checkbox($formname, Line 385  function form_active_checkbox($formname,
385   */   */
386  function form_file($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {  function form_file($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) {
387    
388          $input = html_input( "file", $name, $value, $attributes );      $input = html_input( "file", $name, $value, $attributes );
389    
390          if ( $render_flag ) {      if ($render_flag) {
391                  return $input->render();          return $input->render();
392          } else {      }
393                  return $input;      else {
394          }          return $input;
395        }
396  }  }
397    
398    
# Line 396  function form_file($name, $value=NULL, $ Line 412  function form_file($name, $value=NULL, $
412   */   */
413  function form_textarea($name, $value=NULL, $attributes=array(), $render_flag=FALSE) {  function form_textarea($name, $value=NULL, $attributes=array(), $render_flag=FALSE) {
414    
415          $attrib = array("name" => $name );      $attrib = array("name" => $name );
416          $attributes = array_merge( $attrib, $attributes );      $attributes = array_merge( $attrib, $attributes );
417    
418          $textarea = new TEXTAREAtag( $attributes );      $textarea = new TEXTAREAtag( $attributes );
419    
420          $textarea->newline_after_opentag = FALSE;      $textarea->newline_after_opentag = FALSE;
421          $textarea->push( $value );      $textarea->push( $value );
422    
423          return $textarea;      return $textarea;
424  }  }
425    
426  /**  /**
# Line 421  function form_textarea($name, $value=NUL Line 437  function form_textarea($name, $value=NUL
437   *                             of <option value="foo"> test </option>   *                             of <option value="foo"> test </option>
438   * @param   mixed  $selected - This can be either a string or an array.   * @param   mixed  $selected - This can be either a string or an array.
439   *                             If its a string then, it will be the selected   *                             If its a string then, it will be the selected
440   *                             option value   *                             option value
441   *                              <option value="foo" SELECTED>foo</option>   *                              <option value="foo" SELECTED>foo</option>
442   *                             If it is an array, then all of the option   *                             If it is an array, then all of the option
443   *                             values will be marked as SELECTED.  This only   *                             values will be marked as SELECTED.  This only
444   *                             makes sense to do if the multiple_flag  is true.   *                             makes sense to do if the multiple_flag  is true.
445   * @param   boolean $multiple_flag - is this a multiple selection select box?   * @param   boolean $multiple_flag - is this a multiple selection select box?
446     * @param   array   $attribs - additionnal attributes to the select tag
447   * @return a SELECTtag object.   * @return a SELECTtag object.
448   */   */
449  function form_select($name, $options=array(), $selected="", $multiple_flag=FALSE) {  function form_select($name, $options=array(), $selected="", $multiple_flag=FALSE, $attribs=false) {
450    
451        if (!$attribs)
452            $attribs = array();
453        $attribs["name"] = $name;
454        $select = new SELECTtag( $attribs );
455        if ($multiple_flag) {
456            $select->set_tag_attribute("MULTIPLE");
457        }
458    
459          $select = new SELECTtag( array("name" => $name) );      while (list($label, $value) = each($options)) {
460          if ( $multiple_flag ) {          $selected_value = "";
461                  $select->set_tag_attribute("MULTIPLE");  
462          }          if (is_array($selected)) {
463                //looks like this is a multiple select box
464          while ( list($label, $value) = each($options) ) {              //lets see if the value is in the array
465                  $selected_value = "";              if (in_array($value, $selected)) {
466                    $selected_value = "SELECTED";
467                  if ( is_array($selected) ) {              }
468                          //looks like this is a multiple select box          }
469                          //lets see if the value is in the array          else {
470                          if ( in_array($value, $selected) ) {              //must be a string
471                                  $selected_value = "SELECTED";              if ($value == $selected) {
472                          }                  $selected_value = "SELECTED";
473                  } else {              }
474                          //must be a string          }
475                          if ( $value == $selected ) {  
476                                  $selected_value = "SELECTED";  
477                          }          $attributes = array( "value" => $value, $selected_value );
478                  }          $option = new OPTIONtag( $attributes );
479            $option->push( $label );
480            $select->push( $option );
481                  $attributes = array( "value" => $value, $selected_value );      }
482                  $option = new OPTIONtag( $attributes );      return $select;
                 $option->push( $label );  
                 $select->push( $option );  
         }  
         return $select;  
483  }  }
484    
485  ?>  ?>

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed