form.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php /** @noinspection DuplicatedCode */
  2. class Form {
  3. public const BOOLEAN_OPTIONS = [
  4. 0 => NO,
  5. 1 => YES,
  6. ];
  7. /**
  8. * @param $value
  9. * @return string
  10. */
  11. public static function checked($value): string {
  12. return !empty($value) ? "checked" : "";
  13. }
  14. /**
  15. * @param float $required_score
  16. * @return bool
  17. * @throws Exception
  18. */
  19. public static function checkRecaptcha(float $required_score = 0.5): bool {
  20. if(!defined("GOOGLE_RECAPTCHA_SECRET")) return true;
  21. $json = http::post("https://www.google.com/recaptcha/api/siteverify", [
  22. "secret" => GOOGLE_RECAPTCHA_SECRET,
  23. "response" => http::value("g-recaptcha-response"),
  24. "remoteip" => $_SERVER["REMOTE_ADDR"]
  25. ]);
  26. if(!$json) return false;
  27. $response = json_decode($json);
  28. if(!$response) return false;
  29. if(isset($response->score)) {
  30. return $response->score >= $required_score;
  31. }
  32. return $response->success;
  33. }
  34. public static function renderYearOptions(?int $min = null, ?int $max = null, ?int $selected_value = null): void {
  35. if($min === null) $min = date("Y") - 100;
  36. if($max === null) $max = date("Y") - 16;
  37. for($i = $min; $i <= $max; $i++) {
  38. $selected = ((int) $selected_value === $i) ? "selected" : "";
  39. ?>
  40. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  41. <?php
  42. }
  43. }
  44. public static function renderMonthOptions($selected_value = null): void {
  45. for($i = 1; $i <= 12; $i++) {
  46. $selected = ((int) $selected_value === $i) ? "selected" : "";
  47. $display = date("M", mktime(0, 0, 0, $i, 15));
  48. ?>
  49. <option value="<?=$i?>" <?=$selected?>><?=$display?></option>
  50. <?php
  51. }
  52. }
  53. public static function renderDayOptions($selected_value = null): void {
  54. for($i = 1; $i <= 31; $i++) {
  55. $selected = ((int) $selected_value === $i) ? "selected" : "";
  56. ?>
  57. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  58. <?php
  59. }
  60. }
  61. public static function renderCountryOptions($selected_value = null): void {
  62. $file = ROOT_DIR . "/files/countries.json";
  63. $threshold = time() - (7 * 86400);
  64. if(file_exists($file) && filemtime($file) > $threshold) {
  65. $countries_json = @file_get_contents($file);
  66. $countries = json_decode($countries_json);
  67. }
  68. else {
  69. $url = "http://country.io/names.json";
  70. $countries_json = @file_get_contents($url);
  71. $countries = json_decode($countries_json);
  72. if($countries) {
  73. file_put_contents($file, $countries_json);
  74. }
  75. }
  76. if(!$countries) return;
  77. $country_pairs = get_object_vars($countries);
  78. sort($country_pairs);
  79. $countries = array_values($country_pairs);
  80. ?>
  81. <option value="">Select Country</option>
  82. <?php
  83. foreach($countries as $country) {
  84. $selected = ($selected_value === $country) ? "selected" : "";
  85. ?>
  86. <option value="<?=htmlspecialchars($country)?>" <?=$selected?>><?=htmlspecialchars($country)?></option>
  87. <?php
  88. }
  89. }
  90. /**
  91. * @param object $options
  92. * @throws Exception
  93. */
  94. public static function renderField(object $options): void {
  95. $required_key = $options->requiredKey ?? null;
  96. /** @var Template $template */
  97. $template = $options->template ?? null;
  98. if($required_key && $template) {
  99. $keys = $template->getKeys();
  100. if(!in_array($required_key, $keys)) return;
  101. }
  102. switch($options->type) {
  103. case "header":
  104. self::renderHeader($options);
  105. break;
  106. case "checkbox":
  107. self::renderCheckboxField($options);
  108. break;
  109. case "dropdown":
  110. case "select":
  111. self::renderSelectField($options);
  112. break;
  113. case "radio":
  114. case "inline-radio":
  115. self::renderRadioField($options);
  116. break;
  117. case "text":
  118. self::renderTextField($options);
  119. break;
  120. case "number":
  121. self::renderNumberField($options);
  122. break;
  123. case "textarea":
  124. self::renderTextareaField($options);
  125. break;
  126. case "file":
  127. self::renderFileField($options);
  128. break;
  129. case "hr":
  130. self::renderHorizontalBar($options);
  131. break;
  132. }
  133. }
  134. private static function renderHeader($options): void {
  135. $hidden = !empty($options->hidden) ? "hidden" : "";
  136. ?>
  137. <h3 <?=$hidden?>><?=htmlspecialchars($options->content)?></h3>
  138. <?php
  139. }
  140. /**
  141. * @param object $options
  142. * @throws Exception
  143. */
  144. private static function renderTextField(object $options): void {
  145. $id = str_replace("_", "-", $options->name);
  146. $placeholder = "Enter " . strtolower($options->label);
  147. $value = $options->value ?? http::value($options->name);
  148. $required = ($options->required ?? false) ? "required" : "";
  149. $help_text = $options->helpText ?? false;
  150. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  151. $hidden = !empty($options->hidden) ? "hidden" : "";
  152. ?>
  153. <div class="form-group" <?=$hidden?>>
  154. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  155. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  156. <?php
  157. if($help_text) {
  158. ?>
  159. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  160. <?php
  161. }
  162. ?>
  163. </div>
  164. <?php
  165. }
  166. /**
  167. * @param object $options
  168. * @throws Exception
  169. */
  170. private static function renderNumberField(object $options): void {
  171. $id = str_replace("_", "-", $options->name);
  172. $placeholder = "Enter " . strtolower($options->label);
  173. $value = $options->value ?? http::value($options->name);
  174. $required = ($options->required ?? false) ? "required" : "";
  175. $help_text = $options->helpText ?? false;
  176. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  177. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  178. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  179. $step = $options->step ?? 1;
  180. $prepend_text = $options->prependText ?? "";
  181. $hidden = !empty($options->hidden) ? "hidden" : "";
  182. ?>
  183. <div class="form-group" <?=$hidden?>>
  184. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  185. <div class="input-group">
  186. <?php
  187. if(!empty($prepend_text)) {
  188. ?>
  189. <div class="input-group-prepend">
  190. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  191. </div>
  192. <?php
  193. }
  194. ?>
  195. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  196. </div>
  197. <?php
  198. if($help_text) {
  199. ?>
  200. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  201. <?php
  202. }
  203. ?>
  204. </div>
  205. <?php
  206. }
  207. /**
  208. * @param object $options
  209. * @throws Exception
  210. */
  211. private static function renderSelectField(object $options): void {
  212. $id = str_replace("_", "-", $options->name);
  213. $default_value = $options->defaultValue ?? null;
  214. $selected_value = $options->value ?? http::value($options->name);
  215. if($selected_value === null) $selected_value = $default_value;
  216. $required = ($options->required ?? false) ? "required" : "";
  217. $help_text = $options->helpText ?? false;
  218. ?>
  219. <div class="form-group">
  220. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  221. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  222. <?php
  223. foreach($options->values as $index => $value) {
  224. $display = $options->displayStrings[$index];
  225. $selected = ($selected_value === (string) $value) ? "selected" : "";
  226. ?>
  227. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  228. <?php
  229. }
  230. ?>
  231. </select>
  232. <?php
  233. if($help_text) {
  234. ?>
  235. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  236. <?php
  237. }
  238. ?>
  239. </div>
  240. <?php
  241. }
  242. /**
  243. * @param object $options
  244. */
  245. private static function renderFileField(object $options): void {
  246. $id = str_replace("_", "-", $options->name);
  247. $required = ($options->required ?? false) ? "required" : "";
  248. $help_text = $options->helpText ?? false;
  249. ?>
  250. <div class="form-group">
  251. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  252. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  253. <?php
  254. if($help_text) {
  255. ?>
  256. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  257. <?php
  258. }
  259. ?>
  260. </div>
  261. <?php
  262. }
  263. /**
  264. * @param object $options
  265. * @throws Exception
  266. */
  267. private static function renderTextareaField(object $options): void {
  268. $id = str_replace("_", "-", $options->name);
  269. $placeholder = "Enter " . strtolower($options->label);
  270. $value = $options->value ?? http::value($options->name);
  271. $required = ($options->required ?? false) ? "required" : "";
  272. $help_text = $options->helpText ?? false;
  273. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  274. ?>
  275. <div class="form-group">
  276. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  277. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  278. <?php
  279. if($help_text) {
  280. ?>
  281. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  282. <?php
  283. }
  284. ?>
  285. </div>
  286. <?php
  287. }
  288. private static function renderHorizontalBar(object $options): void {
  289. $hidden = !empty($options->hidden) ? "hidden" : "";
  290. ?>
  291. <hr <?=$hidden?> />
  292. <?php
  293. }
  294. private static function renderCheckboxField(object $options): void {
  295. $id = str_replace("_", "-", $options->name);
  296. $checked = !empty($options->value) ? "checked" : "";
  297. $required = ($options->required ?? false) ? "required" : "";
  298. $help_text = $options->helpText ?? false;
  299. ?>
  300. <div class="form-group">
  301. <div class="custom-control custom-switch">
  302. <input type="checkbox" class="custom-control-input" id="<?=htmlspecialchars($id)?>" name="<?=htmlspecialchars($options->name)?>" value="1" <?=$checked?> <?=$required?> />
  303. <label for="<?=htmlspecialchars($id)?>" class="custom-control-label">
  304. <?=htmlspecialchars($options->label)?>
  305. </label>
  306. </div>
  307. <?php
  308. if($help_text) {
  309. ?>
  310. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  311. <?php
  312. }
  313. ?>
  314. </div>
  315. <?php
  316. }
  317. /**
  318. * @param object $options
  319. * @throws Exception
  320. */
  321. private static function renderRadioField(object $options): void {
  322. $id = str_replace("_", "-", $options->name);
  323. $default_value = $options->defaultValue ?? null;
  324. $current_value = $options->value ?? http::value($options->name);
  325. if($current_value === null) $current_value = $default_value;
  326. $required = ($options->required ?? false) ? "required" : "";
  327. $help_text = $options->helpText ?? false;
  328. $inline = ($options->type === "inline-radio") ? "custom-control-inline" : "";
  329. $hidden = !empty($options->hidden) ? "hidden" : "";
  330. ?>
  331. <div class="form-group" <?=$hidden?>>
  332. <label>
  333. <?=htmlspecialchars($options->label)?>
  334. <?php
  335. if($help_text) {
  336. ?>
  337. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  338. <?php
  339. }
  340. ?>
  341. </label>
  342. <br />
  343. <?php
  344. foreach($options->values as $index => $value) {
  345. $display = $options->displayStrings[$index];
  346. $checked = ($current_value === (string) $value) ? "checked" : "";
  347. $radio_id = sprintf("%s-%s", $id, $value);
  348. if($index === 0 && $current_value === null) {
  349. $checked = "checked";
  350. }
  351. ?>
  352. <div class="custom-control custom-radio <?=$inline?>">
  353. <input id="<?=$radio_id?>" name="<?=$options->name?>" type="radio" class="custom-control-input" value="<?=htmlspecialchars($value)?>" <?=$checked?> <?=$required?> />
  354. <label for="<?=$radio_id?>" class="custom-control-label">
  355. <?=htmlspecialchars($display)?>
  356. </label>
  357. </div>
  358. <?php
  359. }
  360. ?>
  361. </div>
  362. <?php
  363. }
  364. }
  365. ?>