form.php 8.1 KB

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