forms.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. require_once(dirname(dirname(__FILE__))."/header.php");
  3. function get_form_html($id,$fields,$sublabel){
  4. array_push($fields,Array(
  5. 'type'=>'submit',
  6. 'value'=>$sublabel
  7. ));
  8. return get_form_html_advanced(Array(
  9. 'id'=>$id
  10. ),$fields);
  11. }
  12. function get_form_html_advanced($attributes,$fields){
  13. $r = "<form";
  14. foreach($attributes as $attribute => $value){
  15. $r .= " {$attribute}=\"{$value}\"";
  16. }
  17. $r.= ">\n";
  18. foreach($fields as $k => $field){
  19. $r .= get_field_html($field);
  20. }
  21. return $r."</form>\n";
  22. }
  23. function get_field_html($field){
  24. $a = '';
  25. if(isset($field['attributes'])){
  26. foreach($field['attributes'] as $attribute => $value){
  27. $a .= " {$attribute}=\"{$value}\"";
  28. }
  29. }
  30. $v = '';
  31. if(isset($field['value'])&&!is_null($field['value'])&&$field['value']!=''){
  32. $v = "value='{$field['value']}'";
  33. }
  34. switch($field['type']){
  35. case 'select':
  36. $r = "<div class='row'><label for='{$field['name']}'>{$field['label']}</label><span><select name='{$field['name']}'{$a}>";
  37. foreach($field['values'] as $k => $opt){
  38. $a = '';
  39. if(isset($opt['attributes']) && is_array($opt['attributes'])){
  40. foreach($opt['attributes'] as $attribute => $value){
  41. $a .= " {$attribute}=\"{$value}\"";
  42. }
  43. }
  44. if(isset($field['value'])&&$field['value']==$opt['value']){
  45. $a .= "selected=\"selected\"";
  46. }
  47. $r .= "<option value='{$opt['value']}'{$a}>{$opt['label']}</option>";
  48. }
  49. $r .= "</select></span></div>";
  50. break;
  51. case 'hidden':
  52. $r = "<input type='hidden' name='{$field['name']}'{$v}{$a}/>";
  53. break;
  54. case 'custom':
  55. $r = $field['html'];
  56. break;
  57. case 'section':
  58. $r = "<div class='form_section'{$a}>";
  59. if(isset($field['fields'])){
  60. foreach($field['fields'] as $k => $subfield){
  61. $r .= get_field_html($subfield);
  62. }
  63. }
  64. $r .= "</div>";
  65. break;
  66. case 'submit':
  67. $r = "<input type='submit' {$v}{$a}/>";
  68. break;
  69. case 'text':default:
  70. $r = "<div class='row'><label for='{$field['name']}'>{$field['label']}</label><span><input type='{$field['type']}' name='{$field['name']}'{$v}{$a}/></span></div>";
  71. }
  72. return $r."\n";
  73. }
  74. ?>