form.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. class Form {
  3. const BOOLEAN_OPTIONS = [
  4. 0 => "No",
  5. 1 => "Yes",
  6. ];
  7. public static function checked($value) {
  8. return !empty($value) ? "checked" : "";
  9. }
  10. public static function checkRecaptcha() {
  11. if(!defined("GOOGLE_RECAPTCHA_SECRET_KEY")) return true;
  12. $json = http::post("https://www.google.com/recaptcha/api/siteverify", [
  13. "secret" => GOOGLE_RECAPTCHA_SECRET_KEY,
  14. "response" => http::value("g-recaptcha-response"),
  15. "remoteip" => $_SERVER["REMOTE_ADDR"]
  16. ]);
  17. if($json) {
  18. $response = json_decode($json);
  19. if($response) {
  20. return $response->success;
  21. }
  22. }
  23. return false;
  24. }
  25. public static function renderYearOptions($min = null, $max = null, $selected_value = null) {
  26. if(empty($min)) $min = date("Y") - 100;
  27. if(empty($max)) $max = date("Y") - 16;
  28. for($i = $min; $i <= $max; $i++) {
  29. $selected = ($selected_value == $i) ? "selected" : "";
  30. ?>
  31. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  32. <?php
  33. }
  34. }
  35. public static function renderYearOptionsDescending($min = null, $max = null, $selected_value = null) {
  36. if(empty($min)) $min = date("Y") - 100;
  37. if(empty($max)) $max = date("Y") - 16;
  38. for($i = $max; $i >= $min; $i--) {
  39. $selected = ($selected_value == $i) ? "selected" : "";
  40. ?>
  41. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  42. <?php
  43. }
  44. }
  45. public static function renderMonthOptions($selected_value = null) {
  46. for($i = 1; $i <= 12; $i++) {
  47. $selected = ($selected_value == $i) ? "selected" : "";
  48. $display = date("M", mktime(0, 0, 0, $i, 15));
  49. ?>
  50. <option value="<?=$i?>" <?=$selected?>><?=$display?></option>
  51. <?php
  52. }
  53. }
  54. public static function renderDayOptions($selected_value = null) {
  55. for($i = 1; $i <= 31; $i++) {
  56. $selected = ($selected_value == $i) ? "selected" : "";
  57. ?>
  58. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  59. <?php
  60. }
  61. }
  62. public static function renderHourOptions($selected_value = null) {
  63. for($i = 0; $i <= 23; $i++) {
  64. $selected = ($selected_value === $i) ? "selected" : "";
  65. ?>
  66. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  67. <?php
  68. }
  69. }
  70. public static function renderMinuteOptions($selected_value = null) {
  71. for($i = 0; $i <= 59; $i++) {
  72. $selected = ($selected_value === $i) ? "selected" : "";
  73. ?>
  74. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  75. <?php
  76. }
  77. }
  78. public static function renderSecondOptions($selected_value = null) {
  79. for($i = 0; $i <= 59; $i++) {
  80. $selected = ($selected_value === $i) ? "selected" : "";
  81. ?>
  82. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  83. <?php
  84. }
  85. }
  86. public static function renderField($options) {
  87. switch($options->type) {
  88. case "header":
  89. self::renderHeader($options);
  90. break;
  91. case "dropdown":
  92. case "select":
  93. self::renderSelectField($options);
  94. break;
  95. case "text":
  96. self::renderTextField($options);
  97. break;
  98. case "number":
  99. self::renderNumberField($options);
  100. break;
  101. case "textarea":
  102. self::renderTextareaField($options);
  103. break;
  104. case "file":
  105. self::renderFileField($options);
  106. break;
  107. }
  108. }
  109. private static function renderHeader($options) {
  110. ?>
  111. <h3><?=htmlspecialchars($options->content)?></h3>
  112. <?php
  113. }
  114. private static function renderTextField($options) {
  115. $id = str_replace("_", "-", $options->name);
  116. $placeholder = "Enter " . strtolower($options->label);
  117. $value = $options->value ?? http::value($options->name);
  118. $required = ($options->required ?? false) ? "required" : "";
  119. $help_text = $options->helpText ?? false;
  120. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  121. ?>
  122. <div class="row mb-1">
  123. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  124. <div class="col-sm">
  125. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  126. <?php
  127. if($help_text) {
  128. ?>
  129. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  130. <?php
  131. }
  132. ?>
  133. </div>
  134. </div>
  135. <?php
  136. }
  137. private static function renderNumberField($options) {
  138. $id = str_replace("_", "-", $options->name);
  139. $placeholder = "Enter " . strtolower($options->label);
  140. $value = $options->value ?? http::value($options->name);
  141. $required = ($options->required ?? false) ? "required" : "";
  142. $help_text = $options->helpText ?? false;
  143. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  144. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  145. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  146. $step = $options->step ?? 1;
  147. $prepend_text = $options->prependText ?? "";
  148. ?>
  149. <div class="row mb-1">
  150. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  151. <div class="col-sm">
  152. <div class="input-group">
  153. <?php
  154. if(!empty($prepend_text)) {
  155. ?>
  156. <div class="input-group-prepend">
  157. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  158. </div>
  159. <?php
  160. }
  161. ?>
  162. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  163. </div>
  164. <?php
  165. if($help_text) {
  166. ?>
  167. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  168. <?php
  169. }
  170. ?>
  171. </div>
  172. </div>
  173. <?php
  174. }
  175. private static function renderSelectField($options) {
  176. $id = str_replace("_", "-", $options->name);
  177. $selected_value = $options->value ?? http::value($options->name);
  178. $required = ($options->required ?? false) ? "required" : "";
  179. $help_text = $options->helpText ?? false;
  180. ?>
  181. <div class="row mb-1">
  182. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  183. <div class="col-sm">
  184. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  185. <?php
  186. foreach($options->values as $index => $value) {
  187. $display = $options->displayStrings[$index];
  188. $selected = ($selected_value === strval($value)) ? "selected" : "";
  189. ?>
  190. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  191. <?php
  192. }
  193. ?>
  194. </select>
  195. <?php
  196. if($help_text) {
  197. ?>
  198. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  199. <?php
  200. }
  201. ?>
  202. </div>
  203. </div>
  204. <?php
  205. }
  206. private static function renderFileField($options) {
  207. $id = str_replace("_", "-", $options->name);
  208. $placeholder = "Enter " . strtolower($options->label);
  209. $required = ($options->required ?? false) ? "required" : "";
  210. $help_text = $options->helpText ?? false;
  211. ?>
  212. <div class="row mb-1">
  213. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  214. <div class="col-sm">
  215. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  216. <?php
  217. if($help_text) {
  218. ?>
  219. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  220. <?php
  221. }
  222. ?>
  223. </div>
  224. </div>
  225. <?php
  226. }
  227. private static function renderTextareaField($options) {
  228. $id = str_replace("_", "-", $options->name);
  229. $placeholder = "Enter " . strtolower($options->label);
  230. $value = $options->value ?? http::value($options->name);
  231. $required = ($options->required ?? false) ? "required" : "";
  232. $help_text = $options->helpText ?? false;
  233. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  234. ?>
  235. <div class="row mb-1">
  236. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  237. <div class="col-sm">
  238. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  239. <?php
  240. if($help_text) {
  241. ?>
  242. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  243. <?php
  244. }
  245. ?>
  246. </div>
  247. </div>
  248. <?php
  249. }
  250. }
  251. ?>