form.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. case "alert":
  133. self::renderAlert($options);
  134. break;
  135. case "time":
  136. self::renderTimeField($options);
  137. break;
  138. }
  139. }
  140. private static function renderHeader($options): void {
  141. $hidden = !empty($options->hidden) ? "hidden" : "";
  142. $requirements = $options->requirements ?? [];
  143. ?>
  144. <h3 <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>"><?=htmlspecialchars($options->content)?></h3>
  145. <?php
  146. }
  147. private static function renderRequiredBadge(bool $shown): void {
  148. $hidden = $shown ? "" : "hidden";
  149. ?>
  150. <span class="badge badge-danger badge-required" <?=$hidden?>>
  151. <?=htmlspecialchars(__("Required"))?>
  152. </span>
  153. <?php
  154. }
  155. /**
  156. * @param object $options
  157. * @throws Exception
  158. */
  159. private static function renderTextField(object $options): void {
  160. $id = str_replace("_", "-", $options->name);
  161. $value = $options->value ?? http::value($options->name);
  162. $required = ($options->required ?? false) ? "required" : "";
  163. $help_text = $options->helpText ?? false;
  164. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  165. $hidden = !empty($options->hidden) ? "hidden" : "";
  166. $requirements = $options->requirements ?? [];
  167. $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
  168. ?>
  169. <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
  170. <label for="<?=$id?>">
  171. <?=htmlspecialchars($options->label)?>
  172. <?php
  173. self::renderRequiredBadge($required);
  174. ?>
  175. </label>
  176. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  177. <?php
  178. if($help_text) {
  179. ?>
  180. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  181. <?php
  182. }
  183. ?>
  184. </div>
  185. <?php
  186. }
  187. /**
  188. * @param object $options
  189. * @throws Exception
  190. */
  191. private static function renderNumberField(object $options): void {
  192. $id = str_replace("_", "-", $options->name);
  193. $value = $options->value ?? http::value($options->name);
  194. $required = ($options->required ?? false) ? "required" : "";
  195. $help_text = $options->helpText ?? false;
  196. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  197. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  198. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  199. $step = $options->step ?? 1;
  200. $prepend_text = $options->prependText ?? "";
  201. $hidden = !empty($options->hidden) ? "hidden" : "";
  202. $requirements = $options->requirements ?? [];
  203. $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
  204. ?>
  205. <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
  206. <label for="<?=$id?>">
  207. <?=htmlspecialchars($options->label)?>
  208. <?php
  209. self::renderRequiredBadge($required);
  210. ?>
  211. </label>
  212. <div class="input-group">
  213. <?php
  214. if(!empty($prepend_text)) {
  215. ?>
  216. <div class="input-group-prepend">
  217. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  218. </div>
  219. <?php
  220. }
  221. ?>
  222. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter number" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  223. </div>
  224. <?php
  225. if($help_text) {
  226. ?>
  227. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  228. <?php
  229. }
  230. ?>
  231. </div>
  232. <?php
  233. }
  234. /**
  235. * @param object $options
  236. * @throws Exception
  237. */
  238. private static function renderSelectField(object $options): void {
  239. $id = str_replace("_", "-", $options->name);
  240. $default_value = $options->defaultValue ?? null;
  241. $selected_value = $options->value ?? http::value($options->name);
  242. if($selected_value === null) $selected_value = $default_value;
  243. $required = ($options->required ?? false) ? "required" : "";
  244. $help_text = $options->helpText ?? false;
  245. $requirements = $options->requirements ?? [];
  246. $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
  247. ?>
  248. <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
  249. <label for="<?=$id?>">
  250. <?=htmlspecialchars($options->label)?>
  251. <?php
  252. self::renderRequiredBadge($required);
  253. ?>
  254. </label>
  255. <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
  256. <?php
  257. foreach($options->values as $index => $value) {
  258. $display = $options->displayStrings[$index];
  259. $selected = ((string) $selected_value === (string) $value) ? "selected" : "";
  260. ?>
  261. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  262. <?php
  263. }
  264. ?>
  265. </select>
  266. <?php
  267. if($help_text) {
  268. ?>
  269. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  270. <?php
  271. }
  272. ?>
  273. </div>
  274. <?php
  275. }
  276. /**
  277. * @param object $options
  278. */
  279. private static function renderFileField(object $options): void {
  280. $id = str_replace("_", "-", $options->name);
  281. $required = ($options->required ?? false) ? "required" : "";
  282. $help_text = $options->helpText ?? false;
  283. $requirements = $options->requirements ?? [];
  284. $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
  285. ?>
  286. <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
  287. <label for="<?=$id?>">
  288. <?=htmlspecialchars($options->label)?>
  289. <?php
  290. self::renderRequiredBadge($required);
  291. ?>
  292. </label>
  293. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  294. <?php
  295. if($help_text) {
  296. ?>
  297. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  298. <?php
  299. }
  300. ?>
  301. </div>
  302. <?php
  303. }
  304. /**
  305. * @param object $options
  306. * @throws Exception
  307. */
  308. private static function renderTextareaField(object $options): void {
  309. $id = str_replace("_", "-", $options->name);
  310. $value = $options->value ?? http::value($options->name);
  311. $required = ($options->required ?? false) ? "required" : "";
  312. $help_text = $options->helpText ?? false;
  313. $min_length = ($options->minLength ?? false) ? "minlength=\"$options->minLength\"" : "";
  314. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  315. $requirements = $options->requirements ?? [];
  316. $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
  317. ?>
  318. <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
  319. <label for="<?=$id?>">
  320. <?=htmlspecialchars($options->label)?>
  321. <?php
  322. self::renderRequiredBadge($required);
  323. ?>
  324. </label>
  325. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" rows="4" <?=$required?> <?=$min_length?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  326. <?php
  327. if($help_text) {
  328. ?>
  329. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  330. <?php
  331. }
  332. ?>
  333. </div>
  334. <?php
  335. }
  336. private static function renderHorizontalBar(object $options): void {
  337. $hidden = !empty($options->hidden) ? "hidden" : "";
  338. $requirements = $options->requirements ?? [];
  339. ?>
  340. <hr <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" />
  341. <?php
  342. }
  343. private static function renderAlert(object $options): void {
  344. $class = $options->class ?? "alert-info";
  345. $icon = $options->icon ?? "fa-info-circle";
  346. $heading = $options->heading ?? "";
  347. $content = $options->content;
  348. ?>
  349. <div class="alert <?=$class?>">
  350. <div class="mb-2">
  351. <strong>
  352. <i class="fa fa-fw <?=$icon?>"></i>
  353. <?=htmlspecialchars($heading)?>
  354. </strong>
  355. </div>
  356. <?=htmlspecialchars($content)?>
  357. </div>
  358. <?php
  359. }
  360. private static function renderCheckboxField(object $options): void {
  361. $id = str_replace("_", "-", $options->name);
  362. $checked = !empty($options->value) ? "checked" : "";
  363. $required = ($options->required ?? false) ? "required" : "";
  364. $help_text = $options->helpText ?? false;
  365. $requirements = $options->requirements ?? [];
  366. ?>
  367. <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>">
  368. <div class="custom-control custom-switch">
  369. <input type="checkbox" class="custom-control-input" id="<?=htmlspecialchars($id)?>" name="<?=htmlspecialchars($options->name)?>" value="1" <?=$checked?> <?=$required?> />
  370. <label for="<?=htmlspecialchars($id)?>" class="custom-control-label">
  371. <?=htmlspecialchars($options->label)?>
  372. </label>
  373. </div>
  374. <?php
  375. if($help_text) {
  376. ?>
  377. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  378. <?php
  379. }
  380. ?>
  381. </div>
  382. <?php
  383. }
  384. /**
  385. * @param object $options
  386. * @throws Exception
  387. */
  388. private static function renderRadioField(object $options): void {
  389. $id = str_replace("_", "-", $options->name);
  390. $default_value = $options->defaultValue ?? null;
  391. $current_value = $options->value ?? http::value($options->name);
  392. if($current_value === null) $current_value = $default_value;
  393. $required = ($options->required ?? false) ? "required" : "";
  394. $help_text = $options->helpText ?? false;
  395. $inline = ($options->type === "inline-radio") ? "custom-control-inline" : "";
  396. $hidden = !empty($options->hidden) ? "hidden" : "";
  397. $requirements = $options->requirements ?? [];
  398. ?>
  399. <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>">
  400. <label>
  401. <?=htmlspecialchars($options->label)?>
  402. <?php
  403. self::renderRequiredBadge($required);
  404. if($help_text) {
  405. ?>
  406. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  407. <?php
  408. }
  409. ?>
  410. </label>
  411. <br />
  412. <?php
  413. foreach($options->values as $index => $value) {
  414. $display = $options->displayStrings[$index];
  415. $checked = ($current_value === (string) $value) ? "checked" : "";
  416. $radio_id = sprintf("%s-%s", $id, $value);
  417. if($index === 0 && $current_value === null) {
  418. $checked = "checked";
  419. }
  420. ?>
  421. <div class="custom-control custom-radio <?=$inline?>">
  422. <input id="<?=$radio_id?>" name="<?=$options->name?>" type="radio" class="custom-control-input" value="<?=htmlspecialchars($value)?>" <?=$checked?> <?=$required?> />
  423. <label for="<?=$radio_id?>" class="custom-control-label">
  424. <?=htmlspecialchars($display)?>
  425. </label>
  426. </div>
  427. <?php
  428. }
  429. ?>
  430. </div>
  431. <?php
  432. }
  433. /**
  434. * @param object $options
  435. * @throws Exception
  436. */
  437. private static function renderTimeField(object $options): void {
  438. $id = str_replace("_", "-", $options->name);
  439. $value = $options->value ?? http::value($options->name);
  440. $required = ($options->required ?? false) ? "required" : "";
  441. $help_text = $options->helpText ?? false;
  442. $hidden = !empty($options->hidden) ? "hidden" : "";
  443. $requirements = $options->requirements ?? [];
  444. $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
  445. ?>
  446. <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
  447. <label for="<?=$id?>">
  448. <?=htmlspecialchars($options->label)?>
  449. <?php
  450. self::renderRequiredBadge($required);
  451. ?>
  452. </label>
  453. <input class="form-control" type="time" name="<?=$options->name?>" id="<?=$id?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />
  454. <?php
  455. if($help_text) {
  456. ?>
  457. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  458. <?php
  459. }
  460. ?>
  461. </div>
  462. <?php
  463. }
  464. }