form.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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?>">
  155. <?=htmlspecialchars($options->label)?>
  156. <?php
  157. if($required) {
  158. ?>
  159. <span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
  160. <?php
  161. }
  162. ?>
  163. </label>
  164. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  165. <?php
  166. if($help_text) {
  167. ?>
  168. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  169. <?php
  170. }
  171. ?>
  172. </div>
  173. <?php
  174. }
  175. /**
  176. * @param object $options
  177. * @throws Exception
  178. */
  179. private static function renderNumberField(object $options): void {
  180. $id = str_replace("_", "-", $options->name);
  181. $placeholder = "Enter " . strtolower($options->label);
  182. $value = $options->value ?? http::value($options->name);
  183. $required = ($options->required ?? false) ? "required" : "";
  184. $help_text = $options->helpText ?? false;
  185. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  186. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  187. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  188. $step = $options->step ?? 1;
  189. $prepend_text = $options->prependText ?? "";
  190. $hidden = !empty($options->hidden) ? "hidden" : "";
  191. ?>
  192. <div class="form-group" <?=$hidden?>>
  193. <label for="<?=$id?>">
  194. <?=htmlspecialchars($options->label)?>
  195. <?php
  196. if($required) {
  197. ?>
  198. <span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
  199. <?php
  200. }
  201. ?>
  202. </label>
  203. <div class="input-group">
  204. <?php
  205. if(!empty($prepend_text)) {
  206. ?>
  207. <div class="input-group-prepend">
  208. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  209. </div>
  210. <?php
  211. }
  212. ?>
  213. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  214. </div>
  215. <?php
  216. if($help_text) {
  217. ?>
  218. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  219. <?php
  220. }
  221. ?>
  222. </div>
  223. <?php
  224. }
  225. /**
  226. * @param object $options
  227. * @throws Exception
  228. */
  229. private static function renderSelectField(object $options): void {
  230. $id = str_replace("_", "-", $options->name);
  231. $default_value = $options->defaultValue ?? null;
  232. $selected_value = $options->value ?? http::value($options->name);
  233. if($selected_value === null) $selected_value = $default_value;
  234. $required = ($options->required ?? false) ? "required" : "";
  235. $help_text = $options->helpText ?? false;
  236. ?>
  237. <div class="form-group">
  238. <label for="<?=$id?>">
  239. <?=htmlspecialchars($options->label)?>
  240. <?php
  241. if($required) {
  242. ?>
  243. <span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
  244. <?php
  245. }
  246. ?>
  247. </label>
  248. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  249. <?php
  250. foreach($options->values as $index => $value) {
  251. $display = $options->displayStrings[$index];
  252. $selected = ($selected_value === (string) $value) ? "selected" : "";
  253. ?>
  254. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  255. <?php
  256. }
  257. ?>
  258. </select>
  259. <?php
  260. if($help_text) {
  261. ?>
  262. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  263. <?php
  264. }
  265. ?>
  266. </div>
  267. <?php
  268. }
  269. /**
  270. * @param object $options
  271. */
  272. private static function renderFileField(object $options): void {
  273. $id = str_replace("_", "-", $options->name);
  274. $required = ($options->required ?? false) ? "required" : "";
  275. $help_text = $options->helpText ?? false;
  276. ?>
  277. <div class="form-group">
  278. <label for="<?=$id?>">
  279. <?=htmlspecialchars($options->label)?>
  280. <?php
  281. if($required) {
  282. ?>
  283. <span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
  284. <?php
  285. }
  286. ?>
  287. </label>
  288. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  289. <?php
  290. if($help_text) {
  291. ?>
  292. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  293. <?php
  294. }
  295. ?>
  296. </div>
  297. <?php
  298. }
  299. /**
  300. * @param object $options
  301. * @throws Exception
  302. */
  303. private static function renderTextareaField(object $options): void {
  304. $id = str_replace("_", "-", $options->name);
  305. $placeholder = $options->placeholder ?? "Enter " . strtolower($options->label);
  306. $value = $options->value ?? http::value($options->name);
  307. $required = ($options->required ?? false) ? "required" : "";
  308. $help_text = $options->helpText ?? false;
  309. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  310. ?>
  311. <div class="form-group">
  312. <label for="<?=$id?>">
  313. <?=htmlspecialchars($options->label)?>
  314. <?php
  315. if($required) {
  316. ?>
  317. <span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
  318. <?php
  319. }
  320. ?>
  321. </label>
  322. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  323. <?php
  324. if($help_text) {
  325. ?>
  326. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  327. <?php
  328. }
  329. ?>
  330. </div>
  331. <?php
  332. }
  333. private static function renderHorizontalBar(object $options): void {
  334. $hidden = !empty($options->hidden) ? "hidden" : "";
  335. ?>
  336. <hr <?=$hidden?> />
  337. <?php
  338. }
  339. private static function renderCheckboxField(object $options): void {
  340. $id = str_replace("_", "-", $options->name);
  341. $checked = !empty($options->value) ? "checked" : "";
  342. $required = ($options->required ?? false) ? "required" : "";
  343. $help_text = $options->helpText ?? false;
  344. ?>
  345. <div class="form-group">
  346. <div class="custom-control custom-switch">
  347. <input type="checkbox" class="custom-control-input" id="<?=htmlspecialchars($id)?>" name="<?=htmlspecialchars($options->name)?>" value="1" <?=$checked?> <?=$required?> />
  348. <label for="<?=htmlspecialchars($id)?>" class="custom-control-label">
  349. <?=htmlspecialchars($options->label)?>
  350. </label>
  351. </div>
  352. <?php
  353. if($help_text) {
  354. ?>
  355. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  356. <?php
  357. }
  358. ?>
  359. </div>
  360. <?php
  361. }
  362. /**
  363. * @param object $options
  364. * @throws Exception
  365. */
  366. private static function renderRadioField(object $options): void {
  367. $id = str_replace("_", "-", $options->name);
  368. $default_value = $options->defaultValue ?? null;
  369. $current_value = $options->value ?? http::value($options->name);
  370. if($current_value === null) $current_value = $default_value;
  371. $required = ($options->required ?? false) ? "required" : "";
  372. $help_text = $options->helpText ?? false;
  373. $inline = ($options->type === "inline-radio") ? "custom-control-inline" : "";
  374. $hidden = !empty($options->hidden) ? "hidden" : "";
  375. ?>
  376. <div class="form-group" <?=$hidden?>>
  377. <label>
  378. <?=htmlspecialchars($options->label)?>
  379. <?php
  380. if($required) {
  381. ?>
  382. <span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
  383. <?php
  384. }
  385. if($help_text) {
  386. ?>
  387. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  388. <?php
  389. }
  390. ?>
  391. </label>
  392. <br />
  393. <?php
  394. foreach($options->values as $index => $value) {
  395. $display = $options->displayStrings[$index];
  396. $checked = ($current_value === (string) $value) ? "checked" : "";
  397. $radio_id = sprintf("%s-%s", $id, $value);
  398. if($index === 0 && $current_value === null) {
  399. $checked = "checked";
  400. }
  401. ?>
  402. <div class="custom-control custom-radio <?=$inline?>">
  403. <input id="<?=$radio_id?>" name="<?=$options->name?>" type="radio" class="custom-control-input" value="<?=htmlspecialchars($value)?>" <?=$checked?> <?=$required?> />
  404. <label for="<?=$radio_id?>" class="custom-control-label">
  405. <?=htmlspecialchars($display)?>
  406. </label>
  407. </div>
  408. <?php
  409. }
  410. ?>
  411. </div>
  412. <?php
  413. }
  414. }