Revision: 4679
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 15, 2008 10:08 by LoRd1990
Initial Code
<?php class forms implements iforms{ var $_inited=false; var $_form=array(); var $_elems=array(); /**_elems: array( array('id'=>'','name'=>'login','value'=>'','validate'=>true,'params'=>'','on'=>array('click'=>'event')) );**/ /** _fields: array('id'=>'','label'=>"Enter your login:","value"=>"el:21551","type"=>"pair","style"=>""); **/ var $_fields=array(); var $_patterns=array(); var $_css=array(); var $_js=array(); var $_data=''; /** * Constructor. Make new instance of the form container. * @return * @param $method Object * @param $action Object * @param $encoding Object * @param $params Object[optional] */ public function init($method,$action,$encoding='',$name='',$id='',$params=array()) { $id=($id)?'f_'.md5($time.$name.$action).'_'.mt_rand(10,99):''; if($this->correctMethod($method) && !$this->_inited){ $this->_data=''; $this->_form['method']=$method; $this->_form['action']=$action; $this->_form['encoding']=$encoding; $this->_form['params']=$params; $this->_form['id']=$id; $this->_form['name']=$name; $this->initOthers(); $this->initPatterns(); $this->_inited=true; }else{ return IFORM_WRONG_METHOD; } } protected function initOthers(){ //CSS $this->css['pairFieldLabelClass']='label'; $this->css['singleFieldLabelClass']='label label1 center'; $this->css['sinleFieldLabelStyle']='width:100%;clear:both;'; $this->css['formLegendClass']='legend'; $this->css['pairFieldValueClass']='value value1'; $this->css['pairFieldLabelClass']='label'; $this->css['rowClass']='row'; $this->css['formMainClass']='form'; $this->css['submit']='submit center'; //JS $this->js['validateFunct']='Invis.tools.checkValue(this.id);'; } public function addLegend($text){ $this->_form['legend']=sprintf($this->_patterns['formLegend'],$text); } protected function correctMethod($method) { switch($method){ case 'GET': case 'POST': case 'PUT': return true; } return false; } /** * Append eventHandler to added to form input element * @return integer * @param $name String * @param $event String * @param $handler String */ public function addEventHandler($name,$event,$handler){ if(trim($name)!='' && trim($event)!='' && trim($handler)!=''){ for($i=0;$i<count($this->_elems);$i++){ if($this->_elems['name']==$name){ $this->_elems[$i]['on'][]=array($event=>$handler); break; } } }else{ return IFORM_WRONG_PARAMS; } return 1; } /** * Add submit control to the form * @return void * @param $name Object * @param $value Object * @param $style Object[optional] */ public function addSubmit($name,$value,$params=array()) { if(trim($name)!=''){ $this->_submit=sprintf($this->_patterns['submit'],$name,$value,$this->_prepareParams($params)); }else{ return IFROM_WRONG_PARAMS; } } /** * Check that element with value of type $type equals $value * @return integer * @param $type String * @param $value String */ protected function elemExists($type,$value) { for($i=0;$i<count($this->_elems);$i++){ switch($type) { case 'name': if($this->_elems[$i]['name']==$value)return 1; break; case 'id': if($this->_elems[$i]['id']==$value)return 1; break; } } return 0; } /** * Set "important" parameter to the $id - input element * @return integer * @param $name String */ public function validateElement($id) { if(trim($name)==''){ return IFORM_WRONG_PARAMS; }else{ if(!$this->elemExists("id",$id)){ return IFORM_WRONG_ELEM; }else{ //FIX ERROR $this->_elems[array_search($name,$this->_elems)]['validate']=true; } } return 1; } /** * Add input element to the current form * @return String * @param $type String * @param $name String * @param $parent String * @param $id String[optional] * @param $value String[optional] * @param $validate Boolean[optional] * @param $params String[optional] */ public function addInput($type,$name,$parent,$id='',$value='',$validate=false,$params='') { if(trim($type)!='' && trim($name)!='' && trim($parent)!=''){ $id=(!$id)?'i'.md5($name).'_'.count($this->_elems):$id; $this->_elems[]=array( 'id'=>$id, 'type'=>$type, 'parent'=>$parent, 'name'=>$name, 'value'=>$value, 'validate'=>$validate, 'on'=>array(), 'params'=>$params ); }else{ return IFROM_WRONG_PARAMS; } return $id; } /** * Add field to the form * @return String * @param $label String * @param $type String[optional] * @param $el String[optional] * @param $id String[optional] * @param $params String[optional] */ public function addField($label,$type="pair",$value="",$id='',$params="") { $id=(!$id)?'f'.md5($label).'_'.count($this->_fields):$id; $this->_fields[]=array("id"=>$id,"label"=>$label,"type"=>$type,"value"=>$value,"type"=>$type,"style"=>$params); return $id; } /** * Initialize system patterns * @return void */ protected function initPatterns() { $this->_patterns['form']='<form action="%s" method="%s" encoding="%s" %s>'; $this->_patterns['inputI']='<input type="%s" name="%s" id="%s" %s %s %s/>'; $this->_patterns['inputD']='<%s name="%s" id="%s" %s %s %s>%s</%s>'; $this->_patterns['fieldsingle']='<div class="%s" style="width:100%;clear:both;" id="%s" %s%s</div>'; $this->_patterns['fieldpair']='<div class="%s" %s id="%s"><span class="%s" style="%s">%s</span><span style="%s">%s</span></div>'; $this->_patterns['formLegend']='<div class="'.$this->css['formLegendClass'].'">%s</div>'; $this->_patterns['mainContainer']='<div class="'.$this->css['formMainClass'].'">'; $this->_patterns['endContainer']='</div></form>'; $this->_patterns['submit']='<div class="row"><span class=" '.$this->css['submitClass'].'" style="clear:both;"><input type="submit" name="%s" value="%s" %s/></span></div>'; } /** * Get inputs which referers to $parent-field * @return String * @param $parent Object */ protected function _getChildsInputs($parent) { $res=''; for($i=0;$i<count($this->_elems);$i++) { if($this->_elems[$i]['parent']==$parent){ switch($this->_elems[$i]['type']){ case 'password': case 'checkbox': case 'text': $res.=sprintf( $this->_patterns['inputI'], $this->_elems[$i]['type'], $this->_elems[$i]['name'], $this->_elems[$i]['id'], ($this->_elems[$i]['validate'])?'onclick="'.$this->js['validateFunct'].'"':'', $this->_getEvents($i), $this->_prepareParams($this->_elems[$i]['params']) ); break; case 'select': $res.=sprintf( $this->_patterns['inputI'], $this->_elems[$i]['type'], $this->_elems[$i]['name'], $this->_elems[$i]['id'], ($this->_elems[$i]['validate'])?'onclick="'.$this->js['validateFunct'].'"':'', $this->_getEvents($i), $this->_prepareParams($this->_elems[$i]['params']) ); break; default: $res=IFORM_WRONG_TYPE; } } } return $res; } /** * * @return String * @param $elId Integer */ protected function _getEvents($elId){ $result=null; if(array_search($elId,$this->_elems)!=-1){ foreach($this->_elems[$elId]['on'] as $k=>$v) { $result.=' on'.$k.'="return '.$v.'();" '; } }else{ $result=IFORM_NOT_EXISTS; } return $result; } private function _prepareParams($data) { $res=null; if(is_array($data)){ foreach($data as $k=>$v) { $res.=$k."=\"".$data."\""; } } return $res; } /** * * @return String */ public function render($output=true) {//<form action="%s" method="%s" encoding="%s" style="%s" %s> #print_rbr($this->_elems); #die_br($this->_fields); $this->_data=sprintf($this->_patterns['form'],$this->_form['action'],$this->_form['method'],$this->_form['encoding'],$this->_prepareParams($this->_form['params'])); $this->_data.=sprintf($this->_patterns['mainContainer']); $this->_data.=($this->_form['legend'])?$this->_form['legend']:''; #die($this->_data); $this->_data.='<input type="hidden" name="genericDATA" id="fX'.md5(time()).'" value="'.time().'"/>'; for($i=0;$i<count($this->_fields);$i++) {#die_r($this->_fields[$i]); switch($this->_fields[$i]['type']) { case 'pair': //<div class="%s" id="%s"><span class="%s" style="%s">%s</span><span style="%s">%s(value)</span></div>' $this->_data.=sprintf( $this->_patterns['fieldpair'], $this->css['rowClass'], $this->_fields[$i]['params'], $this->_fields[$i]['id'], $this->css['pairFieldLabelClass'], $this->css['pairFieldLabelStyle'], $this->_fields[$i]['label'], $this->css['pairFieldValueClass'], ($this->_fields[$i]['value']=='')?$this->_getChildsInputs($this->_fields[$i]['id']):$this->_fields[$i]['value'] ); break; case 'single': break; } } $this->_data.=$this->_submit; $this->_data.=sprintf($this->_patterns['endContainer']); return ($output)?$this->_data:''; } function getData() { return $this->_data; } } ?>
Initial URL
http://e-code.tnt43.com
Initial Description
If you had seen some errors in the code, please send me notice about it on the: [email protected] Thanks, K.Karpenko
Initial Title
Functions package to SpeedUp web-forms creating process. V.0.2Beta
Initial Tags
forms
Initial Language
PHP