Forms
|
|
|
|
<? /**
* FILE: block.autoform.php
*/
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {autoform}{/autoform} block plugin
*
* @author Borislav Nedelchev <snairl@yahoo.com>
* @copyright Copyright (c) 2006, Borislav Nedelchev
*
* Type: block function<br>
* Name: autoform<br>
* Purpose: Automatically sets value to the form controls via user provided callback function
* @version autoform 1.0
*
* @param array params
* <pre>
* Params: func:object
* </pre>
* @param string $content form content
* @param Smarty $smarty not used
* @param bool $repeat not used
* @return string $content with autofilled form controls
*/ function smarty_block_autoform($params,$content,&$smarty,&$repeat){
if($content=='')return ;
preg_match_all("/<W*input[^>]*/i",$content,$matches);
$field_info=array();
$output="";
$offset=0;
foreach ($matches[0] as $field){
if(preg_match("/type=['"]([^'"]*)/i",$field,$_type)){
$type=strtolower($_type[1]);
}else $type="";
if($type=="file")continue;
if(preg_match("/name=['"]([^'"]*)/i",$field,$_name)){
$name=$_name[1];
}else $name="";
$has_value=false;
if($type=="image"){
if(preg_match("/src=['"]([^'"]*)/i",$field,$_value)){
$value=$_name[1];
$has_value=true;
}else $value="";
}else{
if(preg_match("/value=['"]([^'"]*)/i",$field,$_value)){
$value=$_name[1];
$has_value=true;
}else $value="";
}
$id=isset($field_info[$name])?$field_info[$name]:0;
$field_info[$name]=++$id;
$default=false;
$new_value=$params['func']->_handle_form_fields($type,$name,$value,$id,$default);
$pos=strpos($content,$field,$offset);
if($pos===false){
$smarty->trigger_error("autoform: cannot substitute content");
}
$flen=strlen($field);
$output.=substr($content,$offset,$pos-$offset);
$offset=$pos+$flen;
if($has_value){
if($type=="image")$field=preg_replace("/(src=['"])([^'"]*)/i","${1}$new_value",$field);
else $field=preg_replace("/(value=['"])([^'"]*)/i","${1}$new_value",$field);
}else{
if($type=='image')$val_attr="src";
else $val_attr="value";
$pos=strrpos($field," ");
if($pos===false){
$field.=" {$val_attr}='$new_value'";
}else{
$field=substr($field,0,$pos)." {$val_attr}='$new_value'".substr($field,$pos);
}
}
if(($type=='checkbox'||$type=='radio')&&$default){
$pos=strrpos($field," ");
if($pos===false){
$field.=' checked="checked"';
}else{
$field=substr($field,0,$pos).' checked="checked"'.substr($field,$pos);
}
}
$output.=$field;
}
//textareas are handled separately
preg_match_all("/(<W*textarea[^>]*>)([^<]*)([^>]*)/i",$content,$matches);
for ($i=0;$i<count($matches[0]);$i++){
preg_match("/name=['"]([^'"]*)/i",$matches[1][$i],$_name);
$output=str_replace($matches[0][$i],$matches[1][$i].$params['func']->_handle_form_fields("textarea",$_name[1],$matches[2][$i],1,$default).$matches[3][$i],$output);
}
return $output;
}
?>
|
|
|
Usage Example
|
<?php /**
* FILE: index.php
*/
/**
* @author Borislav Nedelchev <snairl@yahoo.com>
* @copyright Copyright (c) 2006, Borislav Nedelchev
*/
/**
* Assuming that Smarty class is accessable via the php include_path directive
*/ require_once "Smarty.class.php"; /**
* Instantiating Smarty and setting up
*/ $smarty=new Smarty(); $smarty->template_dir="templates";
$smarty->compile_dir="templates_c";
/**
* Manually loading autoform plugin,
* unless we put it in the Smarty plugin folder
*/ require_once "block.autoform.php"; $smarty->register_block("autoform","smarty_block_autoform");
/**
* Load and instantiate page class responsible
* for page initialization.
*/ require_once "clsMyPage.php";
$page=new CSamplePage(); /**
* Do other stuff with page here
*/
/**
* Provide callback class for form autocompletition to Smarty
*/ $smarty->assign_by_ref("form_class",$page); /**
* Finally display page template
*/ $smarty->display("index.tpl"); ?>
<?php /**
* FILE: clsMyPage.php
*/
/**
* @author Borislav Nedelchev <snairl@yahoo.com>
* @copyright Copyright (c) 2006, Borislav Nedelchev
*/
/**
* Class to handle page functionality
* responsible to initialize page content
* and provide callback for autocomplete form fields
* with default|current values
*
*/ class CSamplePage{
/**
* Callback function called from autoform plugin
* respon
*
* @param string $field_type
* Describe what field is proccessed
* possible values (text|hidden|textarea|checkbox|radio|submit|button|image)
* @param string $field_name
* Name of the processed field which must be set
* @param string $current_value
* Current value of field
* @param int $count
* Basically used with the radiobutton groups to determine
* which radio control is currently processed.
* The first control is counted as 1, second as 2, etc.
* @param bool $default
* Basically used with checkbox and radio controls to determine state
* of control checked/not checked
* @return string
* Value that must be set to control
*/
function _handle_form_fields($field_type,$field_name,$current_value,$count,&$default){
/**
* Do anything to initialize form control here.
* For exmaple purposes we return the old value of the control
* to see it is work. Also we'll set the first radio|checkbox button in
* groups as default (aka checked).
*/
if($count==1)$default=true;
return "oldval=$current_value||value:$count";
}
} ?>
<? /**
* FILE: templates/index.tpl
*/ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
{literal}
<script type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
{/literal}
</head>
<body>
<form id="form1" name="form1" method="post" action="">
{autoform func=$form_class}
<p>
<input type="text" name="textfield" />
</p>
<p>
<input type="hidden" name="hiddenField" />
</p>
<p>
<textarea name="textarea"></textarea>
</p>
<p>
<input type="checkbox" name="checkbox" value="checkbox" />
</p>
<p>
<input type="checkbox" name="checkbox2" value="checkbox" />
</p>
<p>
<textarea name="textarea2">Some value left from the HTML editor</textarea>
</p>
<p>
</p>
<p>
<label>
<input type="radio" name="RadioGroup1" value="radio" />
Radio1</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="radio" />
Radio1</label>
<br />
</p>
<p>
<label>
<input type="radio" name="RadioGroup2" value="radio" />
Radio2</label>
<br />
<label>
<input type="radio" name="RadioGroup2" value="radio" />
Radio2</label>
<br />
</p>
<p>
<select name="select">
</select>
</p>
<p>
<input type="image" name="imageField" src="../images/button.gif" />
</p>
<p></p>
<p>
<input type="file" name="bobo">
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
<p> </p>
{/autoform}
</form>
</body>
</html>
|
|
|
Rate This Script
|
|
|
|