The easiest form handling I've seen with PHP so far.
LGPL license
<?php /*
Example:
<? $form = new form($_POST); ?>
<?=$form->open();?>
<?=$form->text('name');?>
<?=$form->submit('Show with value!');
<?=$form->close();?>
*/ class form {
public $feed;
function __construct($feed)
{
$this->feed = $feed;
}
/*To use this method the fckeditor class must be included.*/
function editor($name,$attributes=array())
{
$oFCKeditor = new FCKeditor($this->getName($name));
$oFCKeditor->BasePath = '/FCKeditor/';//http:// path to the fckeditor dir.
$oFCKeditor->Value = html_entity_decode($this->getValue($name));
$oFCKeditor->Width='100%';
$oFCKeditor->Height='100%';
return '<div '.$this->doAttributes($attributes).'>'.$oFCKeditor->CreateHtml().'</div>';
}
function getValue($name)
{
if(is_array($this->feed))
{
return htmlspecialchars($this->feed[$name]);
}
elseif(is_object($this->feed))
{
$classname = get_class($this->feed);
try
{
return call_user_func(array($this->feed, 'display'.ucfirst($name)));
}
catch(exception $e)
{
$e = new exception("$feed returned: ".$e->getMessage()." in form object",2);
throw $e;
exit;
}
}
else
{
return '';
}
}
function doAttributes($attributes = array())
{
$str = '';
foreach($attributes as $k => $v)
{
$str .= ' '.$k.'="'.$v.'" ';
}
return $str;
}
function text($name,$attributes=array())
{
return '<input type="text" name="'.$name.'" value="'.$this->getValue($name).'" '.$this->doAttributes($attributes).'>';
}
function password($name,$attributes=array())
{
return '<input type="password" name="'.$name.'" value="" '.$this->doAttributes($attributes).'>';
}
function file($name,$attributes=array())
{
return '<input type="file" name="'.$name.'" value="" '.$this->doAttributes($attributes).'>';
}
function textarea($name,$attributes=array())
{
return '<textarea name="'.$name.'" '.$this->doAttributes($attributes).'>'.$this->getValue($name).'</textarea>';
}
function checkbox($name,$value,$attributes=array())
{
$checked = $this->getValue($name)==$value?'checked':'';
return '<input type="checkbox" name="'.$name.'" value="'.$value.'" '.$checked.' '.$this->doAttributes($attributes).'>';
}
function radio($name,$value,$attributes=array())
{
$checked = $this->getValue($name)==$value?'checked':'';
return '<input type="radio" name="'.$name.'" value="'.$value.'" '.$checked.' '.$this->doAttributes($attributes).'>';
}
function open($method="POST",$action1 = "")
{
if($method=="POST")
{
$action = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
else
{
$method="GET";
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$a = parse_url($url);
$action = 'http://'.$a['host'].$a['path'];
}
if($action1!="")
{
$action = $action1;
}
return '<form method="'.$method.'" action="'.entity($action).'" enctype="multipart/form-data">';
}
function close()
{
return '</form>';
}
function hidden($name,$attributes=array())
{
return '<input type="hidden" name="'.$name.'" value="'.$this->getValue($name).'" '.$this->doAttributes($attributes).'>';
}
function select($name,$source=null,$attributes=array())
{
if(is_array($source))
{
$dropdown = "<select name="$name">n";
$value = $this->getValue($name);
foreach($source as $id => $name1)
{
$dropdown .= "<option value="$id"";
if($value == $id){ $dropdown .= " SELECTED";}
$dropdown .= ">".$name1."</option>";
}
$dropdown .= "</select>n";
return $dropdown;
}
elseif(is_string($source))
{
$result = mysql_query($source);
$rows = mysql_num_rows($result);
$dropdown = "<select name="$name">n";
$value = $this->getValue($name);
while($rows > 0 && ($IdName = mysql_fetch_array($result)))
{
$id = $IdName[0];
$name1 = $IdName[1];
$dropdown .= "<option value="$id"";
if($value == $id){ $dropdown .= " SELECTED";}
$dropdown .= ">".$name1."</option>";
}
$dropdown .= "</select>n";
return $dropdown;
}
elseif($source === null)
{
if($this->feed->hasOne($name))
{
$className = substr($name,0,-2);
$object = newObject($className);
global $_CLASSES;
$string = null;
$i = 0;
foreach($_CLASSES[$className] as $k => $v)
{
if($v == 'varchar')
{
$string = $k;
break;
}
if($i==1)
{
$secondary = $k;
}
$i++;
}
if($v===null)
{
$string = $secondary;
}
$sql = "SELECT id, ".$string." FROM ".$object->getTableName()." WHERE 1";
return $this->select($name,$sql,$attributes);
}
}
}
function submit($value,$attributes=array(),$name="")
{
return '<input type="submit" name="'.$name.'" value="'.$value.'" '.$this->doAttributes($attributes).'>';
}
/*
$valuesToSet true = set all values
or a comma separated string mentioning the values in $feed that should be set.
*/
function setValues($valuesToSet=true)
{
$array = array();
if($valuesToSet===true)
{
if(is_object($this->feed))
{
foreach($this->feed as $k => $v)
{
if($k!="errors"&&$k!='id')
{
$array[] = $k;
}
}
}
else
{
foreach($this->feed as $k => $v)
{
$array[] = $k;
}
}
}
else
{
$array = explode(',',$valuesToSet);
foreach($array as $k => $v)
{
$array[$k] = trim($v);
}
}
foreach($array as $v)
{
$name = $v;
$name[0] = strtoupper($v[0]);
if(is_object($this->feed))
{
try
{
call_user_func(array($this->feed, 'set'.$name),$_POST[$v]);
}
catch(exception $e)
{
throw new exception('$feed returned: '.$e->getMessage().' on form object->setValues().',1);
}
}
}
}
function getName($name)
{
return $name;
}
function displayError($varName, $message,$value=null)
{
if(is_object($this->feed))
{
if($this->feed->getError($varName)!=_ok&&$this->feed->getError($varName)!=""&&$value===null)
{
return $message;
}
elseif($value!==null&&$value==$this->feed->getError($varName)&&$this->feed->getError($varName)!="")
{
return $message;
}
}
else
{
throw new exception("form::displayError() cannot be called when form->feed is not an object.",1);
}
}
} ?>
Sign up to add your own comment here!
|
|