form.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. class Form {
  3. public static function renderField($options) {
  4. switch($options->type) {
  5. case "header":
  6. self::renderHeader($options);
  7. break;
  8. case "dropdown":
  9. case "select":
  10. self::renderSelectField($options);
  11. break;
  12. case "text":
  13. self::renderTextField($options);
  14. break;
  15. case "textarea":
  16. self::renderTextareaField($options);
  17. break;
  18. case "file":
  19. self::renderFileField($options);
  20. break;
  21. }
  22. }
  23. private static function renderHeader($options) {
  24. ?>
  25. <h3><?=htmlspecialchars($options->content)?></h3>
  26. <?php
  27. }
  28. private static function renderTextField($options) {
  29. $id = str_replace("_", "-", $options->name);
  30. $placeholder = "Enter " . strtolower($options->label);
  31. $value = $options->value ?? http::value($options->name);
  32. $required = ($options->required ?? false) ? "required" : "";
  33. ?>
  34. <div class="form-group">
  35. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  36. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />
  37. </div>
  38. <?php
  39. }
  40. private static function renderSelectField($options) {
  41. $id = str_replace("_", "-", $options->name);
  42. $selected_value = $options->value ?? http::value($options->name);
  43. $required = ($options->required ?? false) ? "required" : "";
  44. ?>
  45. <div class="form-group">
  46. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  47. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  48. <?php
  49. foreach($options->values as $index => $value) {
  50. $display = $options->displayStrings[$index];
  51. $selected = ($selected_value === strval($value)) ? "selected" : "";
  52. ?>
  53. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  54. <?php
  55. }
  56. ?>
  57. </select>
  58. </div>
  59. <?php
  60. }
  61. private static function renderFileField($options) {
  62. $id = str_replace("_", "-", $options->name);
  63. $placeholder = "Enter " . strtolower($options->label);
  64. $required = ($options->required ?? false) ? "required" : "";
  65. ?>
  66. <div class="form-group">
  67. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  68. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" capture="camera" <?=$required?> />
  69. </div>
  70. <?php
  71. }
  72. private static function renderTextareaField($options) {
  73. $id = str_replace("_", "-", $options->name);
  74. $placeholder = "Enter " . strtolower($options->label);
  75. $value = $options->value ?? http::value($options->name);
  76. $required = ($options->required ?? false) ? "required" : "";
  77. ?>
  78. <div class="form-group">
  79. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  80. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?>><?=htmlspecialchars($value)?></textarea>
  81. </div>
  82. <?php
  83. }
  84. }
  85. ?>