form.php 15 KB

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