form.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 renderField($options) {
  75. switch($options->type) {
  76. case "header":
  77. self::renderHeader($options);
  78. break;
  79. case "dropdown":
  80. case "select":
  81. self::renderSelectField($options);
  82. break;
  83. case "text":
  84. self::renderTextField($options);
  85. break;
  86. case "number":
  87. self::renderNumberField($options);
  88. break;
  89. case "textarea":
  90. self::renderTextareaField($options);
  91. break;
  92. case "file":
  93. self::renderFileField($options);
  94. break;
  95. }
  96. }
  97. private static function renderHeader($options) {
  98. ?>
  99. <h3><?=htmlspecialchars($options->content)?></h3>
  100. <?php
  101. }
  102. private static function renderTextField($options) {
  103. $id = str_replace("_", "-", $options->name);
  104. $placeholder = "Enter " . strtolower($options->label);
  105. $value = $options->value ?? http::value($options->name);
  106. $required = ($options->required ?? false) ? "required" : "";
  107. $help_text = $options->helpText ?? false;
  108. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  109. ?>
  110. <div class="row mb-1">
  111. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  112. <div class="col-sm">
  113. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  114. <?php
  115. if($help_text) {
  116. ?>
  117. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  118. <?php
  119. }
  120. ?>
  121. </div>
  122. </div>
  123. <?php
  124. }
  125. private static function renderNumberField($options) {
  126. $id = str_replace("_", "-", $options->name);
  127. $placeholder = "Enter " . strtolower($options->label);
  128. $value = $options->value ?? http::value($options->name);
  129. $required = ($options->required ?? false) ? "required" : "";
  130. $help_text = $options->helpText ?? false;
  131. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  132. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  133. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  134. $step = $options->step ?? 1;
  135. $prepend_text = $options->prependText ?? "";
  136. ?>
  137. <div class="row mb-1">
  138. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  139. <div class="col-sm">
  140. <div class="input-group">
  141. <?php
  142. if(!empty($prepend_text)) {
  143. ?>
  144. <div class="input-group-prepend">
  145. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  146. </div>
  147. <?php
  148. }
  149. ?>
  150. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  151. </div>
  152. <?php
  153. if($help_text) {
  154. ?>
  155. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  156. <?php
  157. }
  158. ?>
  159. </div>
  160. </div>
  161. <?php
  162. }
  163. private static function renderSelectField($options) {
  164. $id = str_replace("_", "-", $options->name);
  165. $selected_value = $options->value ?? http::value($options->name);
  166. $required = ($options->required ?? false) ? "required" : "";
  167. $help_text = $options->helpText ?? false;
  168. ?>
  169. <div class="row mb-1">
  170. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  171. <div class="col-sm">
  172. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  173. <?php
  174. foreach($options->values as $index => $value) {
  175. $display = $options->displayStrings[$index];
  176. $selected = ($selected_value === strval($value)) ? "selected" : "";
  177. ?>
  178. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  179. <?php
  180. }
  181. ?>
  182. </select>
  183. <?php
  184. if($help_text) {
  185. ?>
  186. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  187. <?php
  188. }
  189. ?>
  190. </div>
  191. </div>
  192. <?php
  193. }
  194. private static function renderFileField($options) {
  195. $id = str_replace("_", "-", $options->name);
  196. $placeholder = "Enter " . strtolower($options->label);
  197. $required = ($options->required ?? false) ? "required" : "";
  198. $help_text = $options->helpText ?? false;
  199. ?>
  200. <div class="row mb-1">
  201. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  202. <div class="col-sm">
  203. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  204. <?php
  205. if($help_text) {
  206. ?>
  207. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  208. <?php
  209. }
  210. ?>
  211. </div>
  212. </div>
  213. <?php
  214. }
  215. private static function renderTextareaField($options) {
  216. $id = str_replace("_", "-", $options->name);
  217. $placeholder = "Enter " . strtolower($options->label);
  218. $value = $options->value ?? http::value($options->name);
  219. $required = ($options->required ?? false) ? "required" : "";
  220. $help_text = $options->helpText ?? false;
  221. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  222. ?>
  223. <div class="row mb-1">
  224. <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  225. <div class="col-sm">
  226. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  227. <?php
  228. if($help_text) {
  229. ?>
  230. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  231. <?php
  232. }
  233. ?>
  234. </div>
  235. </div>
  236. <?php
  237. }
  238. }
  239. ?>