forms.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 'multi':
  52. $r = "<div class='row'><label for='{$field['name']}'>{$field['label']}</label><span><div>";
  53. foreach($field['values'] as $k => $opt){
  54. $a = '';
  55. if(isset($opt['attributes']) && is_array($opt['attributes'])){
  56. foreach($opt['attributes'] as $attribute => $value){
  57. $a .= " {$attribute}=\"{$value}\"";
  58. }
  59. }
  60. if(isset($field['value'])&&$field['value']==$opt['value']){
  61. $a .= "selected=\"selected\"";
  62. }
  63. $r .= "<input type='checkbox' name='{$field['name']}[{$opt['value']}]'{$a}/> {$opt['label']}<br/>";
  64. }
  65. $r .= "</div></span></div>";
  66. break;
  67. case 'hidden':
  68. $r = "<input type='hidden' name='{$field['name']}'{$v}{$a}/>";
  69. break;
  70. case 'custom':
  71. $r = $field['html'];
  72. break;
  73. case 'section':
  74. $r = "<div class='form_section'{$a}>";
  75. if(isset($field['fields'])){
  76. foreach($field['fields'] as $k => $subfield){
  77. $r .= get_field_html($subfield);
  78. }
  79. }
  80. $r .= "</div>";
  81. break;
  82. case 'submit':
  83. $r = "<input type='submit' {$v}{$a}/>";
  84. break;
  85. case 'text':default:
  86. $r = "<div class='row'><label for='{$field['name']}'>{$field['label']}</label><span><input type='{$field['type']}' name='{$field['name']}'{$v}{$a}/></span></div>";
  87. }
  88. return $r."\n";
  89. }
  90. ?>