form.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php /** @noinspection DuplicatedCode */
  2. class Form {
  3. 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) {
  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. else {
  33. return $response->success;
  34. }
  35. }
  36. public static function renderYearOptions($min = null, $max = null, $selected_value = null) {
  37. if(empty($min)) $min = date("Y") - 100;
  38. if(empty($max)) $max = date("Y") - 16;
  39. for($i = $min; $i <= $max; $i++) {
  40. $selected = ($selected_value == $i) ? "selected" : "";
  41. ?>
  42. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  43. <?php
  44. }
  45. }
  46. public static function renderYearOptionsDescending($min = null, $max = null, $selected_value = null) {
  47. if(empty($min)) $min = date("Y") - 100;
  48. if(empty($max)) $max = date("Y") - 16;
  49. for($i = $max; $i >= $min; $i--) {
  50. $selected = ($selected_value == $i) ? "selected" : "";
  51. ?>
  52. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  53. <?php
  54. }
  55. }
  56. public static function renderMonthOptions($selected_value = null) {
  57. for($i = 1; $i <= 12; $i++) {
  58. $selected = ($selected_value == $i) ? "selected" : "";
  59. $display = date("M", mktime(0, 0, 0, $i, 15));
  60. ?>
  61. <option value="<?=$i?>" <?=$selected?>><?=$display?></option>
  62. <?php
  63. }
  64. }
  65. public static function renderDayOptions($selected_value = null) {
  66. for($i = 1; $i <= 31; $i++) {
  67. $selected = ($selected_value == $i) ? "selected" : "";
  68. ?>
  69. <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
  70. <?php
  71. }
  72. }
  73. public static function renderHourOptions($selected_value = null) {
  74. for($i = 0; $i <= 23; $i++) {
  75. $selected = ($selected_value === $i) ? "selected" : "";
  76. ?>
  77. <option value="<?=$i?>" <?=$selected?>><?=str_pad($i, 2, "0", STR_PAD_LEFT)?></option>
  78. <?php
  79. }
  80. }
  81. public static function renderMinuteOptions($selected_value = null) {
  82. for($i = 0; $i <= 59; $i++) {
  83. $selected = ($selected_value === $i) ? "selected" : "";
  84. ?>
  85. <option value="<?=$i?>" <?=$selected?>><?=str_pad($i, 2, "0", STR_PAD_LEFT)?></option>
  86. <?php
  87. }
  88. }
  89. public static function renderSecondOptions($selected_value = null) {
  90. for($i = 0; $i <= 59; $i++) {
  91. $selected = ($selected_value === $i) ? "selected" : "";
  92. ?>
  93. <option value="<?=$i?>" <?=$selected?>><?=str_pad($i, 2, "0", STR_PAD_LEFT)?></option>
  94. <?php
  95. }
  96. }
  97. public static function renderCountryOptions($selected_value = null) {
  98. $file = ROOT_DIR . "/files/countries.json";
  99. $threshold = time() - (7 * 86400);
  100. if(file_exists($file) && filemtime($file) > $threshold) {
  101. $countries_json = @file_get_contents($file);
  102. $countries = json_decode($countries_json);
  103. }
  104. else {
  105. $url = "http://country.io/names.json";
  106. $countries_json = @file_get_contents($url);
  107. $countries = json_decode($countries_json);
  108. if($countries) {
  109. file_put_contents($file, $countries_json);
  110. }
  111. }
  112. if(!$countries) return;
  113. $country_pairs = get_object_vars($countries);
  114. sort($country_pairs);
  115. $countries = array_values($country_pairs);
  116. ?>
  117. <option value="">Select Country</option>
  118. <?php
  119. foreach($countries as $country) {
  120. $selected = ($selected_value === $country) ? "selected" : "";
  121. ?>
  122. <option value="<?=htmlspecialchars($country)?>" <?=$selected?>><?=htmlspecialchars($country)?></option>
  123. <?php
  124. }
  125. }
  126. /**
  127. * @param object $options
  128. * @throws Exception
  129. */
  130. public static function renderField(object $options) {
  131. $required_key = $options->requiredKey ?? null;
  132. /** @var Template $template */
  133. $template = $options->template ?? null;
  134. if($required_key && $template) {
  135. $keys = $template->getKeys();
  136. if(!in_array($required_key, $keys)) return;
  137. }
  138. switch($options->type) {
  139. case "header":
  140. self::renderHeader($options);
  141. break;
  142. case "checkbox":
  143. self::renderCheckboxField($options);
  144. break;
  145. case "dropdown":
  146. case "select":
  147. self::renderSelectField($options);
  148. break;
  149. case "radio":
  150. case "inline-radio":
  151. self::renderRadioField($options);
  152. break;
  153. case "text":
  154. self::renderTextField($options);
  155. break;
  156. case "number":
  157. self::renderNumberField($options);
  158. break;
  159. case "textarea":
  160. self::renderTextareaField($options);
  161. break;
  162. case "file":
  163. self::renderFileField($options);
  164. break;
  165. case "hr":
  166. self::renderHorizontalBar($options);
  167. break;
  168. }
  169. }
  170. private static function renderHeader($options) {
  171. ?>
  172. <h3><?=htmlspecialchars($options->content)?></h3>
  173. <?php
  174. }
  175. /**
  176. * @param object $options
  177. * @throws Exception
  178. */
  179. private static function renderTextField(object $options) {
  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. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  186. ?>
  187. <div class="form-group">
  188. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  189. <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
  190. <?php
  191. if($help_text) {
  192. ?>
  193. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  194. <?php
  195. }
  196. ?>
  197. </div>
  198. <?php
  199. }
  200. /**
  201. * @param object $options
  202. * @throws Exception
  203. */
  204. private static function renderNumberField(object $options) {
  205. $id = str_replace("_", "-", $options->name);
  206. $placeholder = "Enter " . strtolower($options->label);
  207. $value = $options->value ?? http::value($options->name);
  208. $required = ($options->required ?? false) ? "required" : "";
  209. $help_text = $options->helpText ?? false;
  210. $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
  211. $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
  212. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  213. $step = $options->step ?? 1;
  214. $prepend_text = $options->prependText ?? "";
  215. ?>
  216. <div class="form-group">
  217. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  218. <div class="input-group">
  219. <?php
  220. if(!empty($prepend_text)) {
  221. ?>
  222. <div class="input-group-prepend">
  223. <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
  224. </div>
  225. <?php
  226. }
  227. ?>
  228. <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
  229. </div>
  230. <?php
  231. if($help_text) {
  232. ?>
  233. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  234. <?php
  235. }
  236. ?>
  237. </div>
  238. <?php
  239. }
  240. /**
  241. * @param object $options
  242. * @throws Exception
  243. */
  244. private static function renderSelectField(object $options) {
  245. $id = str_replace("_", "-", $options->name);
  246. $selected_value = $options->value ?? http::value($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. <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 = ($selected_value === strval($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) {
  277. $id = str_replace("_", "-", $options->name);
  278. $required = ($options->required ?? false) ? "required" : "";
  279. $help_text = $options->helpText ?? false;
  280. ?>
  281. <div class="form-group">
  282. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  283. <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
  284. <?php
  285. if($help_text) {
  286. ?>
  287. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  288. <?php
  289. }
  290. ?>
  291. </div>
  292. <?php
  293. }
  294. /**
  295. * @param object $options
  296. * @throws Exception
  297. */
  298. private static function renderTextareaField(object $options) {
  299. $id = str_replace("_", "-", $options->name);
  300. $placeholder = "Enter " . strtolower($options->label);
  301. $value = $options->value ?? http::value($options->name);
  302. $required = ($options->required ?? false) ? "required" : "";
  303. $help_text = $options->helpText ?? false;
  304. $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
  305. ?>
  306. <div class="form-group">
  307. <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
  308. <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
  309. <?php
  310. if($help_text) {
  311. ?>
  312. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  313. <?php
  314. }
  315. ?>
  316. </div>
  317. <?php
  318. }
  319. private static function renderHorizontalBar(object $options) {
  320. ?>
  321. <hr />
  322. <?php
  323. }
  324. private static function renderCheckboxField(object $options) {
  325. $id = str_replace("_", "-", $options->name);
  326. $checked = !empty($options->value) ? "checked" : "";
  327. $required = ($options->required ?? false) ? "required" : "";
  328. $help_text = $options->helpText ?? false;
  329. ?>
  330. <div class="form-group">
  331. <div class="custom-control custom-switch">
  332. <input type="checkbox" class="custom-control-input" id="<?=htmlspecialchars($id)?>" name="<?=htmlspecialchars($options->name)?>" value="1" <?=$checked?> <?=$required?> />
  333. <label for="<?=htmlspecialchars($id)?>" class="custom-control-label">
  334. <?=htmlspecialchars($options->label)?>
  335. </label>
  336. </div>
  337. <?php
  338. if($help_text) {
  339. ?>
  340. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  341. <?php
  342. }
  343. ?>
  344. </div>
  345. <?php
  346. }
  347. private static function renderRadioField(object $options) {
  348. $id = str_replace("_", "-", $options->name);
  349. $current_value = $options->value ?? http::value($options->name);
  350. $required = ($options->required ?? false) ? "required" : "";
  351. $help_text = $options->helpText ?? false;
  352. $inline = ($options->type == "inline-radio") ? "custom-control-inline" : "";
  353. ?>
  354. <div class="form-group">
  355. <label>
  356. <?=htmlspecialchars($options->label)?>
  357. <?php
  358. if($help_text) {
  359. ?>
  360. <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
  361. <?php
  362. }
  363. ?>
  364. </label>
  365. <br />
  366. <?php
  367. foreach($options->values as $index => $value) {
  368. $display = $options->displayStrings[$index];
  369. $checked = ($current_value === strval($value)) ? "checked" : "";
  370. $radio_id = sprintf("%s-%s", $id, $value);
  371. if($index === 0 && $current_value === null) {
  372. $checked = "checked";
  373. }
  374. ?>
  375. <div class="custom-control custom-radio <?=$inline?>">
  376. <input id="<?=$radio_id?>" name="<?=$options->name?>" type="radio" class="custom-control-input" value="<?=htmlspecialchars($value)?>" <?=$checked?> <?=$required?> />
  377. <label for="<?=$radio_id?>" class="custom-control-label">
  378. <?=htmlspecialchars($display)?>
  379. </label>
  380. </div>
  381. <?php
  382. }
  383. ?>
  384. </div>
  385. <?php
  386. }
  387. }
  388. ?>