PHP Classes

File: form_class.php

Recommend this page to a friend!
  Classes of Daniel Froz Costa   Form Class   form_class.php   Download  
File: form_class.php
Role: ???
Content type: text/plain
Description: The form class file
Class: Form Class
Author: By
Last change:
Date: 22 years ago
Size: 8,015 bytes
 

Contents

Class file image Download
<? /* * Form Class * * Copyright (C) 2002 Daniel Fróz Costa * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA * * * $Id: form_class.php,v 1.3 2002/02/28 10:57:27 daniel Exp $ */ if(!defined(PHP_FORM_CLASS)) { define(PHP_FORM_CLASS, 1); define(FORM_GMETHOD_ARRAY, 0); define(FORM_GMETHOD_QUEUE, 1); define(FORM_SELECT_IS_MULTIPLE, 0); define(FORM_SELECT_NOT_MULTIPLE, 1); define(FORM_OK, 0); define(FORM_EARG, 1); define(FORM_ECALL, 2); define(FORM_ETYPE, 3); define(FORM_EIND, 4); class form { var $action; var $errno; var $field; /* field buffer */ var $method; /* form method */ var $gmethod; /* get field method */ /* * void form( string action [, string method ] ) */ function form($action, $method="GET") { $this->action = $action; $this->errno = FORM_OK; $this->field = array(); $this->method = $method; $this->gmethod = FORM_GMETHOD_ARR; } /* * string end( void ) */ function end() { return "</form>"; } /* * boolean freeField( [ mixed key ] ) */ function freeField($key = 0) { $idx = $this->getIndice($key); if($idx < 0) { $this->errno = FORM_EARG; return false; } switch($this->gmethod) { case FORM_GMETHOD_ARRAY: unset($this->field[$idx]); $this->field[$idx] = 0; break; case FORM_GMETHOD_QUEUE: $nfield = array(); while(list($k, $v) = each($this->field)) { if($k == $idx) continue; $nfield[] = $this->field[$k]; } unset($this->field); $this->field = $nfield; break; } return true; } /* * getIndice( [ mixed key ] ) */ function getIndice($key = 0) { if(is_numeric($key)) return $key; for($i=0; $i < count($this->field); $i++) if(!strcmp($this->field[$i][name], $key)) return $i; return -1; } /* * string getField ( [ mixed key ] ) */ function getField($key=0) { $formtag = NULL; $idx = $this->getIndice($key); if($idx < 0) { $this->errno = FORM_EIND; return false; } $name =& $this->field[$idx][name]; $value =& $this->field[$idx][value]; $type =& $this->field[$idx][type]; $attr =& $this->field[$idx][attr]; $sattr =& $this->field[$idx][sattr]; /* special attribute */ switch($type) { case "radio": if(count($attr) > 0) { $formtag = "<input type=\"$type\" name=\"$name\" "; $formtag .= "value=\"".$attr[0]."\""; if(!strcmp($value, $attr[0])) $formtag .= " checked"; $formtag .= ">"; array_shift($attr); if(!$attr[0]) $this->freeField($idx); } break; case "checkbox": if(count($attr)>0) { $formtag = "<input type=\"$type\" name=\"$name\""; $formtag .= " value=\"".$attr[0]."\""; if(is_array($value)) { if(!strcmp($value[0], $attr[0])) { $formtag .= " checked"; array_shift($value); } } else { if(!strcmp($value, $attr[0])) $formtag .= " checked"; } $formtag .= ">"; array_shift($attr); if(!$attr[0]) $this->freeField($idx); } break; case "textarea": $formtag = "<textarea name=\"$name\""; if($attr) { if(is_array($attr)) $formtag .= " ".implode($attr, " "); else $formtag .= " $attr"; } $formtag .= ">$value</textarea>"; $this->freeField($idx); break; case "select": $formtag = "<select name=\"$name\""; if(is_array($sattr)) $formtag .= " ".implode($sattr, " "); else $formtag .= " $sattr"; $formtag .= ">\n"; while(list($optv, $optdes) = each($attr)) { $formtag .= "<option value=\"$optv\""; if(!strcmp($value, $optv)) $formtag .= " selected"; $formtag .= ">$optdes</option>\n"; } $formtag .= "</select>"; $this->freeField($idx); break; case "selectmultiple": $formtag = "<select name=\"$name\" multiple"; if(is_array($sattr)) $formtag .= " ".implode($sattr, " "); else $formtag .= " $sattr"; $formtag .= ">\n"; while(list($optv, $optdes) = each($attr)) { $formtag .= "<option value=\"$optv\""; if(is_array($value)) { if(in_array($optv, $value)) $formtag .= " selected"; } else { if(!strcmp($value, $optv)) $formtag .= " selected"; } $formtag .= ">$optdes</option>\n"; } $formtag .= "</select>"; $this->freeField($idx); break; default: $formtag = "<input type=\"$type\" name=\"$name\""; $formtag .= " value=\"$value\""; if($attr) { if(is_array($attr)) $formtag .= " ".implode($attr, " "); else $formtag .= " $attr"; } $formtag .= ">"; $this->freeField($idx); break; } return $formtag; } /* * boolean setField ( string name [, string value [, string type [, mixed attributes [, mixed special_attributes ]]]]) */ function setField($name, $value=0, $type="text", $attr=NULL, $sattr=NULL) { switch($type) { case "radio": if(!is_array($attr)) { $this->errno = FORM_EARG; return false; } break; case "select": case "selectmultiple": if(!is_array($attr)) { $this->errno = FORM_EARG; return false; } break; case "checkbox": case "hidden": case "password": case "submit": case "text": case "textarea": break; default: $this->strerr = FORM_ETYPE; return false; } array_push($this->field, array( "name" => $name, "value"=> $value, "type" => $type, "attr" => $attr, "sattr" => $sattr, ) ); return true; } /* * boolean setGetFieldMethod ( integer gmethod ) */ function setGetFieldMethod($gmethod) { if($gmethod != FORM_GMETHOD_QUEUE && $gmethod != FORM_GMETHOD_ARRAY) { $this->errno = FORM_EARG; return false; } $this->gmethod = $gmethod; return true; } /* * string start( void ) */ function start() { return "<form action=\"$this->action\" method=\"$this->method\">"; } /* * string strError( void ) */ function strError() { switch($this->errno) { case FORM_EARG: return "invalid argument"; case FORM_ECALL: return "invalid call order"; case FORM_EIND: return "invalid index"; case FORM_ETYPE: return "invalid element type"; default: return "success"; } } } /* ! class form */ } /* ! PHP_FORM_CLASS */ ?>