form.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. class Form {
  3. public static function renderYearOptions($min = null, $max = null, $selected_value = null) {
  4. if(empty($min)) $min = date("Y") - 100;
  5. if(empty($max)) $max = date("Y") - 16;
  6. for($i = $min; $i <= $max; $i++) {
  7. $selected = ($selected_value == $i) ? "selected" : "";
  8. ?>
  9. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  10. <?php
  11. }
  12. }
  13. public static function renderMonthOptions($selected_value = null) {
  14. for($i = 1; $i <= 12; $i++) {
  15. $selected = ($selected_value == $i) ? "selected" : "";
  16. $display = date("M", mktime(0, 0, 0, $i));
  17. ?>
  18. <option value="<?=$i?>" <?=$selected?>><?=$display?></option>
  19. <?php
  20. }
  21. }
  22. public static function renderDayOptions($selected_value = null) {
  23. for($i = 1; $i <= 31; $i++) {
  24. $selected = ($selected_value == $i) ? "selected" : "";
  25. ?>
  26. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  27. <?php
  28. }
  29. }
  30. public static function renderField($options) {
  31. switch($options->type) {
  32. case "header":
  33. self::renderHeader($options);
  34. break;
  35. case "dropdown":
  36. case "select":
  37. self::renderSelectField($options);
  38. break;
  39. case "text":
  40. self::renderTextField($options);
  41. break;
  42. case "textarea":
  43. self::renderTextareaField($options);
  44. break;
  45. case "file":
  46. self::renderFileField($options);
  47. break;
  48. }
  49. }
  50. private static function renderHeader($options) {
  51. ?>
  52. <h3><?=htmlspecialchars($options->content)?></h3>
  53. <?php
  54. }
  55. private static function renderTextField($options) {
  56. $id = str_replace("_", "-", $options->name);
  57. $placeholder = "Enter " . strtolower($options->label);
  58. $value = $options->value ?? http::value($options->name);
  59. $required = ($options->required ?? false) ? "required" : "";
  60. ?>
  61. <div class="row mb-1">
  62. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  63. <div class="col-sm">
  64. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />
  65. </div>
  66. </div>
  67. <?php
  68. }
  69. private static function renderSelectField($options) {
  70. $id = str_replace("_", "-", $options->name);
  71. $selected_value = $options->value ?? http::value($options->name);
  72. $required = ($options->required ?? false) ? "required" : "";
  73. ?>
  74. <div class="row mb-1">
  75. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  76. <div class="col-sm">
  77. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  78. <?php
  79. foreach($options->values as $index => $value) {
  80. $display = $options->displayStrings[$index];
  81. $selected = ($selected_value === strval($value)) ? "selected" : "";
  82. ?>
  83. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  84. <?php
  85. }
  86. ?>
  87. </select>
  88. </div>
  89. </div>
  90. <?php
  91. }
  92. private static function renderFileField($options) {
  93. $id = str_replace("_", "-", $options->name);
  94. $placeholder = "Enter " . strtolower($options->label);
  95. $required = ($options->required ?? false) ? "required" : "";
  96. ?>
  97. <div class="row mb-1">
  98. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  99. <div class="col-sm">
  100. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" capture="camera" <?=$required?> />
  101. </div>
  102. </div>
  103. <?php
  104. }
  105. private static function renderTextareaField($options) {
  106. $id = str_replace("_", "-", $options->name);
  107. $placeholder = "Enter " . strtolower($options->label);
  108. $value = $options->value ?? http::value($options->name);
  109. $required = ($options->required ?? false) ? "required" : "";
  110. ?>
  111. <div class="row mb-1">
  112. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  113. <div class="col-sm">
  114. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?>><?=htmlspecialchars($value)?></textarea>
  115. </div>
  116. </div>
  117. <?php
  118. }
  119. }
  120. ?>