Posted By

LoRd1990 on 01/15/08


Tagged

forms php5 automatisation factorizing


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

LoRd1990


Functions package to SpeedUp web-forms creating process. V.0.2Beta


 / Published in: PHP
 

URL: http://e-code.tnt43.com

If you had seen some errors in the code, please send me notice about it on the: [email protected]

Thanks, K.Karpenko

  1. <?php
  2. class forms implements iforms{
  3.  
  4. var $_inited=false;
  5. var $_form=array();
  6. var $_elems=array();
  7. /**_elems: array(
  8. array('id'=>'','name'=>'login','value'=>'','validate'=>true,'params'=>'','on'=>array('click'=>'event'))
  9. );**/
  10. /**
  11. _fields: array('id'=>'','label'=>"Enter your login:","value"=>"el:21551","type"=>"pair","style"=>"");
  12. **/
  13. var $_fields=array();
  14. var $_patterns=array();
  15. var $_css=array();
  16. var $_js=array();
  17. var $_data='';
  18.  
  19.  
  20. /**
  21. * Constructor. Make new instance of the form container.
  22. * @return
  23. * @param $method Object
  24. * @param $action Object
  25. * @param $encoding Object
  26. * @param $params Object[optional]
  27. */
  28.  
  29. public function init($method,$action,$encoding='',$name='',$id='',$params=array())
  30. {
  31. $id=($id)?'f_'.md5($time.$name.$action).'_'.mt_rand(10,99):'';
  32. if($this->correctMethod($method) && !$this->_inited){
  33. $this->_data='';
  34. $this->_form['method']=$method;
  35. $this->_form['action']=$action;
  36. $this->_form['encoding']=$encoding;
  37. $this->_form['params']=$params;
  38. $this->_form['id']=$id;
  39. $this->_form['name']=$name;
  40. $this->initOthers();
  41. $this->initPatterns();
  42. $this->_inited=true;
  43. }else{
  44. return IFORM_WRONG_METHOD;
  45. }
  46. }
  47.  
  48. protected function initOthers(){
  49. //CSS
  50. $this->css['pairFieldLabelClass']='label';
  51. $this->css['singleFieldLabelClass']='label label1 center';
  52. $this->css['sinleFieldLabelStyle']='width:100%;clear:both;';
  53. $this->css['formLegendClass']='legend';
  54. $this->css['pairFieldValueClass']='value value1';
  55. $this->css['pairFieldLabelClass']='label';
  56. $this->css['rowClass']='row';
  57. $this->css['formMainClass']='form';
  58. $this->css['submit']='submit center';
  59.  
  60. //JS
  61. $this->js['validateFunct']='Invis.tools.checkValue(this.id);';
  62. }
  63.  
  64. public function addLegend($text){
  65. $this->_form['legend']=sprintf($this->_patterns['formLegend'],$text);
  66. }
  67. protected function correctMethod($method)
  68. {
  69. switch($method){
  70. case 'GET':
  71. case 'POST':
  72. case 'PUT':
  73. return true;
  74. }
  75. return false;
  76. }
  77.  
  78. /**
  79. * Append eventHandler to added to form input element
  80. * @return integer
  81. * @param $name String
  82. * @param $event String
  83. * @param $handler String
  84. */
  85. public function addEventHandler($name,$event,$handler){
  86. if(trim($name)!='' && trim($event)!='' && trim($handler)!=''){
  87. for($i=0;$i<count($this->_elems);$i++){
  88. if($this->_elems['name']==$name){
  89. $this->_elems[$i]['on'][]=array($event=>$handler);
  90. break;
  91. }
  92. }
  93. }else{
  94. return IFORM_WRONG_PARAMS;
  95. }
  96. return 1;
  97. }
  98.  
  99. /**
  100. * Add submit control to the form
  101. * @return void
  102. * @param $name Object
  103. * @param $value Object
  104. * @param $style Object[optional]
  105. */
  106. public function addSubmit($name,$value,$params=array())
  107. {
  108. if(trim($name)!=''){
  109. $this->_submit=sprintf($this->_patterns['submit'],$name,$value,$this->_prepareParams($params));
  110. }else{
  111. return IFROM_WRONG_PARAMS;
  112. }
  113. }
  114.  
  115. /**
  116. * Check that element with value of type $type equals $value
  117. * @return integer
  118. * @param $type String
  119. * @param $value String
  120. */
  121. protected function elemExists($type,$value)
  122. {
  123. for($i=0;$i<count($this->_elems);$i++){
  124. switch($type)
  125. {
  126. case 'name':
  127. if($this->_elems[$i]['name']==$value)return 1;
  128. break;
  129. case 'id':
  130. if($this->_elems[$i]['id']==$value)return 1;
  131. break;
  132. }
  133. }
  134. return 0;
  135. }
  136.  
  137. /**
  138. * Set "important" parameter to the $id - input element
  139. * @return integer
  140. * @param $name String
  141. */
  142. public function validateElement($id)
  143. {
  144. if(trim($name)==''){
  145. return IFORM_WRONG_PARAMS;
  146. }else{
  147. if(!$this->elemExists("id",$id)){
  148. return IFORM_WRONG_ELEM;
  149. }else{
  150. //FIX ERROR
  151. $this->_elems[array_search($name,$this->_elems)]['validate']=true;
  152. }
  153. }
  154. return 1;
  155. }
  156.  
  157. /**
  158. * Add input element to the current form
  159. * @return String
  160. * @param $type String
  161. * @param $name String
  162. * @param $parent String
  163. * @param $id String[optional]
  164. * @param $value String[optional]
  165. * @param $validate Boolean[optional]
  166. * @param $params String[optional]
  167. */
  168. public function addInput($type,$name,$parent,$id='',$value='',$validate=false,$params='')
  169. {
  170. if(trim($type)!='' && trim($name)!='' && trim($parent)!=''){
  171. $id=(!$id)?'i'.md5($name).'_'.count($this->_elems):$id;
  172. $this->_elems[]=array(
  173. 'id'=>$id,
  174. 'type'=>$type,
  175. 'parent'=>$parent,
  176. 'name'=>$name,
  177. 'value'=>$value,
  178. 'validate'=>$validate,
  179. 'on'=>array(),
  180. 'params'=>$params
  181. );
  182. }else{
  183. return IFROM_WRONG_PARAMS;
  184. }
  185. return $id;
  186. }
  187.  
  188. /**
  189. * Add field to the form
  190. * @return String
  191. * @param $label String
  192. * @param $type String[optional]
  193. * @param $el String[optional]
  194. * @param $id String[optional]
  195. * @param $params String[optional]
  196. */
  197. public function addField($label,$type="pair",$value="",$id='',$params="")
  198. {
  199. $id=(!$id)?'f'.md5($label).'_'.count($this->_fields):$id;
  200. $this->_fields[]=array("id"=>$id,"label"=>$label,"type"=>$type,"value"=>$value,"type"=>$type,"style"=>$params);
  201. return $id;
  202. }
  203.  
  204.  
  205. /**
  206. * Initialize system patterns
  207. * @return void
  208. */
  209. protected function initPatterns()
  210. {
  211. $this->_patterns['form']='<form action="%s" method="%s" encoding="%s" %s>';
  212. $this->_patterns['inputI']='<input type="%s" name="%s" id="%s" %s %s %s/>';
  213. $this->_patterns['inputD']='<%s name="%s" id="%s" %s %s %s>%s</%s>';
  214. $this->_patterns['fieldsingle']='<div class="%s" style="width:100%;clear:both;" id="%s" %s%s</div>';
  215. $this->_patterns['fieldpair']='<div class="%s" %s id="%s"><span class="%s" style="%s">%s</span><span style="%s">%s</span></div>';
  216. $this->_patterns['formLegend']='<div class="'.$this->css['formLegendClass'].'">%s</div>';
  217. $this->_patterns['mainContainer']='<div class="'.$this->css['formMainClass'].'">';
  218. $this->_patterns['endContainer']='</div></form>';
  219. $this->_patterns['submit']='<div class="row"><span class=" '.$this->css['submitClass'].'" style="clear:both;"><input type="submit" name="%s" value="%s" %s/></span></div>';
  220. }
  221.  
  222.  
  223. /**
  224. * Get inputs which referers to $parent-field
  225. * @return String
  226. * @param $parent Object
  227. */
  228. protected function _getChildsInputs($parent)
  229. {
  230. $res='';
  231. for($i=0;$i<count($this->_elems);$i++)
  232. {
  233. if($this->_elems[$i]['parent']==$parent){
  234. switch($this->_elems[$i]['type']){
  235. case 'password':
  236. case 'checkbox':
  237. case 'text':
  238. $res.=sprintf(
  239. $this->_patterns['inputI'],
  240. $this->_elems[$i]['type'],
  241. $this->_elems[$i]['name'],
  242. $this->_elems[$i]['id'],
  243. ($this->_elems[$i]['validate'])?'onclick="'.$this->js['validateFunct'].'"':'',
  244. $this->_getEvents($i),
  245. $this->_prepareParams($this->_elems[$i]['params'])
  246. );
  247. break;
  248. case 'select':
  249. $res.=sprintf(
  250. $this->_patterns['inputI'],
  251. $this->_elems[$i]['type'],
  252. $this->_elems[$i]['name'],
  253. $this->_elems[$i]['id'],
  254. ($this->_elems[$i]['validate'])?'onclick="'.$this->js['validateFunct'].'"':'',
  255. $this->_getEvents($i),
  256. $this->_prepareParams($this->_elems[$i]['params'])
  257. );
  258. break;
  259. default:
  260. $res=IFORM_WRONG_TYPE;
  261. }
  262. }
  263. }
  264. return $res;
  265. }
  266.  
  267.  
  268. /**
  269. *
  270. * @return String
  271. * @param $elId Integer
  272. */
  273. protected function _getEvents($elId){
  274. $result=null;
  275. if(array_search($elId,$this->_elems)!=-1){
  276. foreach($this->_elems[$elId]['on'] as $k=>$v)
  277. {
  278. $result.=' on'.$k.'="return '.$v.'();" ';
  279. }
  280. }else{
  281. $result=IFORM_NOT_EXISTS;
  282. }
  283. return $result;
  284. }
  285.  
  286. private function _prepareParams($data)
  287. {
  288. $res=null;
  289. if(is_array($data)){
  290. foreach($data as $k=>$v)
  291. {
  292. $res.=$k."=\"".$data."\"";
  293. }
  294. }
  295. return $res;
  296. }
  297.  
  298. /**
  299. *
  300. * @return String
  301. */
  302. public function render($output=true)
  303. {//<form action="%s" method="%s" encoding="%s" style="%s" %s>
  304. #print_rbr($this->_elems);
  305. #die_br($this->_fields);
  306. $this->_data=sprintf($this->_patterns['form'],$this->_form['action'],$this->_form['method'],$this->_form['encoding'],$this->_prepareParams($this->_form['params']));
  307. $this->_data.=sprintf($this->_patterns['mainContainer']);
  308. $this->_data.=($this->_form['legend'])?$this->_form['legend']:'';
  309. #die($this->_data);
  310. $this->_data.='<input type="hidden" name="genericDATA" id="fX'.md5(time()).'" value="'.time().'"/>';
  311. for($i=0;$i<count($this->_fields);$i++)
  312. {#die_r($this->_fields[$i]);
  313. switch($this->_fields[$i]['type'])
  314. {
  315.  
  316. case 'pair':
  317. //<div class="%s" id="%s"><span class="%s" style="%s">%s</span><span style="%s">%s(value)</span></div>'
  318. $this->_data.=sprintf(
  319. $this->_patterns['fieldpair'],
  320. $this->css['rowClass'],
  321. $this->_fields[$i]['params'],
  322. $this->_fields[$i]['id'],
  323. $this->css['pairFieldLabelClass'],
  324. $this->css['pairFieldLabelStyle'],
  325. $this->_fields[$i]['label'],
  326. $this->css['pairFieldValueClass'],
  327. ($this->_fields[$i]['value']=='')?$this->_getChildsInputs($this->_fields[$i]['id']):$this->_fields[$i]['value']
  328. );
  329.  
  330. break;
  331. case 'single':
  332. break;
  333. }
  334. }
  335. $this->_data.=$this->_submit;
  336. $this->_data.=sprintf($this->_patterns['endContainer']);
  337. return ($output)?$this->_data:'';
  338. }
  339.  
  340. function getData()
  341. {
  342. return $this->_data;
  343. }
  344.  
  345. }
  346. ?>

Report this snippet  

You need to login to post a comment.