form.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. $min_length = ($options->minLength ?? false) ? "minlength=\"$options->minLength\"" : "";
  311. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  312. $requirements = $options->requirements ?? [];
  313. $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
  314. ?>
  315. <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
  316. <label for="<?=$id?>">
  317. <?=htmlspecialchars($options->label)?>
  318. <?php
  319. self::renderRequiredBadge($required);
  320. ?>
  321. </label>
  322. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" rows="4" <?=$required?> <?=$min_length?> <?=$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. $requirements = $options->requirements ?? [];
  336. ?>
  337. <hr <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" />
  338. <?php
  339. }
  340. private static function renderCheckboxField(object $options): void {
  341. $id = str_replace("_", "-", $options->name);
  342. $checked = !empty($options->value) ? "checked" : "";
  343. $required = ($options->required ?? false) ? "required" : "";
  344. $help_text = $options->helpText ?? false;
  345. $requirements = $options->requirements ?? [];
  346. ?>
  347. <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>">
  348. <div class="custom-control custom-switch">
  349. <input type="checkbox" class="custom-control-input" id="<?=htmlspecialchars($id)?>" name="<?=htmlspecialchars($options->name)?>" value="1" <?=$checked?> <?=$required?> />
  350. <label for="<?=htmlspecialchars($id)?>" class="custom-control-label">
  351. <?=htmlspecialchars($options->label)?>
  352. </label>
  353. </div>
  354. <?php
  355. if($help_text) {
  356. ?>
  357. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  358. <?php
  359. }
  360. ?>
  361. </div>
  362. <?php
  363. }
  364. /**
  365. * @param object $options
  366. * @throws Exception
  367. */
  368. private static function renderRadioField(object $options): void {
  369. $id = str_replace("_", "-", $options->name);
  370. $default_value = $options->defaultValue ?? null;
  371. $current_value = $options->value ?? http::value($options->name);
  372. if($current_value === null) $current_value = $default_value;
  373. $required = ($options->required ?? false) ? "required" : "";
  374. $help_text = $options->helpText ?? false;
  375. $inline = ($options->type === "inline-radio") ? "custom-control-inline" : "";
  376. $hidden = !empty($options->hidden) ? "hidden" : "";
  377. $requirements = $options->requirements ?? [];
  378. ?>
  379. <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>">
  380. <label>
  381. <?=htmlspecialchars($options->label)?>
  382. <?php
  383. self::renderRequiredBadge($required);
  384. if($help_text) {
  385. ?>
  386. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  387. <?php
  388. }
  389. ?>
  390. </label>
  391. <br />
  392. <?php
  393. foreach($options->values as $index => $value) {
  394. $display = $options->displayStrings[$index];
  395. $checked = ($current_value === (string) $value) ? "checked" : "";
  396. $radio_id = sprintf("%s-%s", $id, $value);
  397. if($index === 0 && $current_value === null) {
  398. $checked = "checked";
  399. }
  400. ?>
  401. <div class="custom-control custom-radio <?=$inline?>">
  402. <input id="<?=$radio_id?>" name="<?=$options->name?>" type="radio" class="custom-control-input" value="<?=htmlspecialchars($value)?>" <?=$checked?> <?=$required?> />
  403. <label for="<?=$radio_id?>" class="custom-control-label">
  404. <?=htmlspecialchars($display)?>
  405. </label>
  406. </div>
  407. <?php
  408. }
  409. ?>
  410. </div>
  411. <?php
  412. }
  413. /**
  414. * @param object $options
  415. * @throws Exception
  416. */
  417. private static function renderTimeField(object $options): void {
  418. $id = str_replace("_", "-", $options->name);
  419. $value = $options->value ?? http::value($options->name);
  420. $required = ($options->required ?? false) ? "required" : "";
  421. $help_text = $options->helpText ?? false;
  422. $hidden = !empty($options->hidden) ? "hidden" : "";
  423. $requirements = $options->requirements ?? [];
  424. $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
  425. ?>
  426. <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
  427. <label for="<?=$id?>">
  428. <?=htmlspecialchars($options->label)?>
  429. <?php
  430. self::renderRequiredBadge($required);
  431. ?>
  432. </label>
  433. <input class="form-control" type="time" name="<?=$options->name?>" id="<?=$id?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />
  434. <?php
  435. if($help_text) {
  436. ?>
  437. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  438. <?php
  439. }
  440. ?>
  441. </div>
  442. <?php
  443. }
  444. }