Location: PHPKode > scripts > formbuilder > formbuilder/form.inc.php
<?

	/*
	table v.1.0.1b
		
	By Llorenç Herrera [hide@address.com]
	Please do not remove this credits
	
	Version history:
	. Multiple select improvement by Wolfsmutter.
	*/
	
	define ("FORM_METHOD_GET",				"get");
	define ("FORM_METHOD_POST",				"post");
	
	define ("FORM_GROUP_MAIN",				"%%main%%");
	define ("FORM_GROUP_HIDDEN",			"%%hidden%%");
	
	class form_group
	{
		var			$name;
		var			$title;
		
		function form_group ($name, $title)
		{
			$this->name		= $name;
			$this->title	= $title;
		}
	}
	
	class form_element
	{
		var			$title;
		var			$name;
		var			$value;
		
		function form_element ($title, $name, $value)
		{
			$this->title	= $title;
			$this->name		= $name;
			$this->value	= $value;
		}
	}
	
	class form_element_text extends form_element
	{
		var			$style = "formText";
		var			$size;
		var			$maxlength;
		var			$isreadonly = false;
		
		function form_element_text ($title, $name, $value, $style, $size, $maxlength, $isreadonly)
		{
			$this->form_element		($title, $name, $value);
			if ($style != "") $this->style			= $style;
			$this->size				= $size;
			$this->maxlength		= $maxlength;
			$this->isreadonly		= $isreadonly;
		}
		
		function getTag ()
		{
			return "<input type=\"text\" name=\"".$this->name."\" value=\"".$this->value."\" class=\"".$this->style."\" size=\"".$this->size."\" maxlength=\"".$this->maxlength."\" ".($this->isreadonly ? " readonly" : "")."><BR>";
		}
	}
	
	class form_element_onlytext extends form_element
	{
		var			$style = "formTextOnly";
		
		function form_element_onlytext ($title, $value, $style)
		{
			$this->form_element		($title, "", $value);
			if ($style != "") $this->style			= $style;
		}
		
		function getTag ()
		{
			return "<span class=\"".$this->style."\">".$this->value."</span><br>";
		}
	}
	
	class form_element_password extends form_element
	{
		var			$style = "formText";
		var			$size;
		var			$maxlength;
		
		function form_element_password ($title, $name, $value, $style, $size, $maxlength)
		{
			$this->form_element		($title, $name, $value);
			if ($style != "") $this->style			= $style;
			$this->size				= $size;
			$this->maxlength		= $maxlength;
		}
		
		function getTag ()
		{
			return "<input type=\"password\" name=\"".$this->name."\" value=\"".$this->value."\" class=\"".$this->style."\" size=\"".$this->size."\" maxlength=\"".$this->maxlength."\"><BR>";
		}
	}
	
	class form_element_combo extends form_element
	{
		var			$style = "formCombo";
		var			$size;
		var			$values;
		var			$multiple;
		
		function form_element_combo ($title, $name, $value, $style, $size, $values, $multiple)
		{
			$this->form_element		($title, $name, $value);
			if ($style != "") $this->style			= $style;
			$this->size				= $size;
			$this->values			= $values;
			$this->multiple			= $multiple;
		}
		
		function getTag ()
		{
			$r = "";
			$r .= "<select name=\"".$this->name."\" class=\"".$this->style."\"".( $this->size != 0 ? "size=\"".$this->size."\"" : "").($this->multiple ? " multiple" : "").">";
			while (list ($value, $title) = each ($this->values))
				$r .= "<option value=\"$value\"".($value == $this->value ? " selected" : "").">$title</option>";
			$r .= "</select>";
			return $r;
		}
	}
	
	class form_element_radio extends form_element
	{
		var			$style = "formRadio";
		var			$size;
		var			$values;
		
		function form_element_radio ($title, $name, $value, $style, $size, $values)
		{
			$this->form_element		($title, $name, $value);
			if ($style != "") $this->style			= $style;
			$this->size				= $size;
			$this->values			= $values;
		}
		
		function getTag ()
		{
			$r = "";
			$r .= "<span class=\"".$this->style."\">";
			while (list ($value, $title) = each ($this->values))
				$r .= "<input class=\"".$this->style."\" type=radio name=\"".$this->name."\" value=\"$value\"".($value == $this->value ? " checked" : "").">$title<br>\n";
			return $r;
		}
	}
	
	class form_element_checkbox extends form_element
	{
		var			$style = "formCheckbox";
		var			$size;
		var			$values;
		
		function form_element_checkbox ($title, $name, $value, $style)
		{
			$this->form_element		($title, $name, $value);
			if ($style != "") $this->style			= $style;
			$this->size				= $size;
			$this->values			= $values;
		}
		
		function getTag ()
		{
			$r = "";
			$r .= "<span class=\"".$this->style."\">";
			$r .= "<input type=checkbox name=\"".$this->name."\" value=\"1\"".($this->value ? " checked" : "").">$title<br>\n";
			return $r;
		}
	}
	
	class form_element_textarea extends form_element
	{
		var			$style = "formTextarea";
		var			$cols;
		var			$rows;
		
		function form_element_textarea ($title, $name, $value, $style, $cols, $rows)
		{
			$this->form_element		($title, $name, $value);
			if ($style != "") $this->style			= $style;
			$this->cols				= $cols;
			$this->rows				= $rows;
		}
		
		function getTag ()
		{
			return "<textarea cols=".$this->cols." rows=".$this->rows." name=\"".$this->name."\" class=\"".$this->style."\">".$this->value."</textarea><BR>";
		}
		/*
			<textarea cols="1" rows="2" name="name"></textarea>
		*/
	}
	
	class form_element_hidden extends form_element
	{
		function form_element_hidden ($name, $value)
		{
			$this->form_element		("", $name, $value);
		}
		
		function getTag ()
		{
			return "<input type=\"hidden\" name=\"".$this->name."\" value=\"".$this->value."\">";
		}
	}
	
	class form
	{
		var			$title;
		var			$name;
		var			$method;
		var			$action;
		
		var			$groups;
		var			$elements;
		
		// Styles
		var			$width					= "100%";		
		
		var			$title_bgcolor			= "#004BAB";
		var			$title_style			= "title_style";
		
		var			$group_bgcolor			= "#B9D1F0";
		var			$group_style			= "group_style";
		
		var			$element_bgcolor		= array ("#E6E7E8", "#F1F2F2");
		var			$element_style			= "element_style";
		var			$element_separator		= "<img src=lib/form/item.gif>";
		
		var			$issubmit				= true;
		var			$submit_bgcolor			= "#B9D1F0";
		var			$submit_title			= ".:    enviar    .:";
		var			$submit_style			= "formbutton";
		
		var			$isreset				= false;
		var			$reset_title			= ".: borrar .:";
		var			$reset_style			= "formreset";
		
		function form ($title, $name, $method, $action)
		{
			$this->title	= $title;
			$this->name		= $name;
			$this->method	= $method;
			$this->action	= $action;
			$this->addGroup	(FORM_GROUP_HIDDEN, "");
			$this->addGroup	(FORM_GROUP_MAIN, "");
		}
		
		function addGroup ($name, $title)
		{
			$this->groups[] = new form_group ($name, $title);
		}
		
		function addElement ($group, $element)
		{
			$this->elements[$group][] = $element;
		}
		
		function getForm ()
		{
			$r = "";
			$r .= "<form method=\"".$this->method."\" name=\"".$this->name."\" action=\"".$this->action."\" style=\"margin: 0px;\">\n";
			$r .= "<table border=0 width=".$this->width." cellpadding=3 cellspacing=0>\n";
			$r .= "<tr>\n\t<td colspan=3 bgcolor=".$this->title_bgcolor."><span class=\"".$this->title_style."\">".$this->title."</td>\n</tr>\n";
			
			for ($group_i=0; $group_i<sizeof ($this->groups); $group_i++)
			{
				$group = $this->groups[$group_i];
				
				if ($group->name != FORM_GROUP_MAIN && $group->name != FORM_GROUP_HIDDEN)
				{
					$r .= "<tr>\n\t<td colspan=3 height=2></td>\n</tr>\n";
					$r .= "<tr>\n\t<td colspan=3 bgcolor=".$this->group_bgcolor."><span class=\"".$this->group_style."\">".$group->title."</td>\n</tr>\n";
				}
				
				$color = 0;
				for ($element_i=0; $element_i<sizeof ($this->elements[$this->groups[$group_i]->name]); $element_i++)
					switch ($group->name)
					{
						case FORM_GROUP_HIDDEN:
							$r .= $this->elements[$this->groups[$group_i]->name][$element_i]->getTag ()."\n";
							break;
						default:
							$element = $this->elements[$this->groups[$group_i]->name][$element_i];
							$bgcolor = $this->element_bgcolor[$color];
							if ($color >= sizeof ($this->element_bgcolor)-1) $color = 0; else $color ++;
							$r .= "<tr bgcolor=$bgcolor>\n\t<td valign=top><div align=right class=\"".$this->element_style."\">".$element->title."</td>\n\t<td width=1 valign=top>".$this->element_separator."</td>\n\t<td>";
							$r .= $element->getTag ()."\n";
							$r .= "</td>\n</tr>\n";
							break;
					}
			}
			
			$r .= "<tr>\n\t<td colspan=3 height=2></td>\n</tr>\n";
			if ($this->issubmit || $this->isreset)
			{
				$r .= "<tr bgcolor=\"".$this->submit_bgcolor."\">\n\t<td colspan=3 valign=center><div align=center>";
				if ($this->issubmit)
					$r .= "<input type=submit value=\"".$this->submit_title."\" class=\"".$this->submit_style."\">";
				if ($this->isreset)
					$r.= "&nbsp;<input type=reset value=\"".$this->reset_title."\" class=\"".$this->reset_style."\">";
				$r .= "</td>\n</tr>";
			}
			$r .= "</form>\n";
			$r .= "</table>\n";
			
			return $r;
		}

	}

?>
Return current item: formbuilder