| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- <?php /** @noinspection DuplicatedCode */
- class Form {
- public const BOOLEAN_OPTIONS = [
- 0 => NO,
- 1 => YES,
- ];
- /**
- * @param $value
- * @return string
- */
- public static function checked($value): string {
- return !empty($value) ? "checked" : "";
- }
- /**
- * @param float $required_score
- * @return bool
- * @throws Exception
- */
- public static function checkRecaptcha(float $required_score = 0.5): bool {
- if(!defined("GOOGLE_RECAPTCHA_SECRET")) return true;
- $json = http::post("https://www.google.com/recaptcha/api/siteverify", [
- "secret" => GOOGLE_RECAPTCHA_SECRET,
- "response" => http::value("g-recaptcha-response"),
- "remoteip" => $_SERVER["REMOTE_ADDR"]
- ]);
- if(!$json) return false;
- $response = json_decode($json);
- if(!$response) return false;
- if(isset($response->score)) {
- return $response->score >= $required_score;
- }
- return $response->success;
- }
- public static function renderYearOptions(?int $min = null, ?int $max = null, ?int $selected_value = null): void {
- if($min === null) $min = date("Y") - 100;
- if($max === null) $max = date("Y") - 16;
- for($i = $min; $i <= $max; $i++) {
- $selected = ((int) $selected_value === $i) ? "selected" : "";
- ?>
- <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
- <?php
- }
- }
- public static function renderMonthOptions($selected_value = null): void {
- for($i = 1; $i <= 12; $i++) {
- $selected = ((int) $selected_value === $i) ? "selected" : "";
- $display = date("M", mktime(0, 0, 0, $i, 15));
- ?>
- <option value="<?=$i?>" <?=$selected?>><?=$display?></option>
- <?php
- }
- }
- public static function renderDayOptions($selected_value = null): void {
- for($i = 1; $i <= 31; $i++) {
- $selected = ((int) $selected_value === $i) ? "selected" : "";
- ?>
- <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
- <?php
- }
- }
- public static function renderCountryOptions($selected_value = null): void {
- $file = ROOT_DIR . "/files/countries.json";
- $threshold = time() - (7 * 86400);
- if(file_exists($file) && filemtime($file) > $threshold) {
- $countries_json = @file_get_contents($file);
- $countries = json_decode($countries_json);
- }
- else {
- $url = "http://country.io/names.json";
- $countries_json = @file_get_contents($url);
- $countries = json_decode($countries_json);
- if($countries) {
- file_put_contents($file, $countries_json);
- }
- }
- if(!$countries) return;
- $country_pairs = get_object_vars($countries);
- sort($country_pairs);
- $countries = array_values($country_pairs);
- ?>
- <option value="">Select Country</option>
- <?php
- foreach($countries as $country) {
- $selected = ($selected_value === $country) ? "selected" : "";
- ?>
- <option value="<?=htmlspecialchars($country)?>" <?=$selected?>><?=htmlspecialchars($country)?></option>
- <?php
- }
- }
- /**
- * @param object $options
- * @throws Exception
- */
- public static function renderField(object $options): void {
- $required_key = $options->requiredKey ?? null;
- /** @var Template $template */
- $template = $options->template ?? null;
- if($required_key && $template) {
- $keys = $template->getKeys();
- if(!in_array($required_key, $keys)) return;
- }
- switch($options->type) {
- case "header":
- self::renderHeader($options);
- break;
- case "checkbox":
- self::renderCheckboxField($options);
- break;
- case "dropdown":
- case "select":
- self::renderSelectField($options);
- break;
- case "radio":
- case "inline-radio":
- self::renderRadioField($options);
- break;
- case "text":
- self::renderTextField($options);
- break;
- case "number":
- self::renderNumberField($options);
- break;
- case "textarea":
- self::renderTextareaField($options);
- break;
- case "file":
- self::renderFileField($options);
- break;
- case "hr":
- self::renderHorizontalBar($options);
- break;
- case "time":
- self::renderTimeField($options);
- break;
- }
- }
- private static function renderHeader($options): void {
- $hidden = !empty($options->hidden) ? "hidden" : "";
- $requirements = $options->requirements ?? [];
- ?>
- <h3 <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>"><?=htmlspecialchars($options->content)?></h3>
- <?php
- }
- private static function renderRequiredBadge(bool $shown): void {
- $hidden = $shown ? "" : "hidden";
- ?>
- <span class="badge badge-danger badge-required" <?=$hidden?>>
- <?=htmlspecialchars(__("Required"))?>
- </span>
- <?php
- }
- /**
- * @param object $options
- * @throws Exception
- */
- private static function renderTextField(object $options): void {
- $id = str_replace("_", "-", $options->name);
- $value = $options->value ?? http::value($options->name);
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
- $hidden = !empty($options->hidden) ? "hidden" : "";
- $requirements = $options->requirements ?? [];
- $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
- ?>
- <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
- <label for="<?=$id?>">
- <?=htmlspecialchars($options->label)?>
- <?php
- self::renderRequiredBadge($required);
- ?>
- </label>
- <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- <?php
- }
- /**
- * @param object $options
- * @throws Exception
- */
- private static function renderNumberField(object $options): void {
- $id = str_replace("_", "-", $options->name);
- $value = $options->value ?? http::value($options->name);
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- $min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
- $max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
- $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
- $step = $options->step ?? 1;
- $prepend_text = $options->prependText ?? "";
- $hidden = !empty($options->hidden) ? "hidden" : "";
- $requirements = $options->requirements ?? [];
- $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
- ?>
- <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
- <label for="<?=$id?>">
- <?=htmlspecialchars($options->label)?>
- <?php
- self::renderRequiredBadge($required);
- ?>
- </label>
- <div class="input-group">
- <?php
- if(!empty($prepend_text)) {
- ?>
- <div class="input-group-prepend">
- <span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
- </div>
- <?php
- }
- ?>
- <input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter number" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
- </div>
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- <?php
- }
- /**
- * @param object $options
- * @throws Exception
- */
- private static function renderSelectField(object $options): void {
- $id = str_replace("_", "-", $options->name);
- $default_value = $options->defaultValue ?? null;
- $selected_value = $options->value ?? http::value($options->name);
- if($selected_value === null) $selected_value = $default_value;
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- $requirements = $options->requirements ?? [];
- $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
- ?>
- <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
- <label for="<?=$id?>">
- <?=htmlspecialchars($options->label)?>
- <?php
- self::renderRequiredBadge($required);
- ?>
- </label>
- <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
- <?php
- foreach($options->values as $index => $value) {
- $display = $options->displayStrings[$index];
- $selected = ($selected_value === (string) $value) ? "selected" : "";
- ?>
- <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
- <?php
- }
- ?>
- </select>
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- <?php
- }
- /**
- * @param object $options
- */
- private static function renderFileField(object $options): void {
- $id = str_replace("_", "-", $options->name);
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- $requirements = $options->requirements ?? [];
- $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
- ?>
- <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
- <label for="<?=$id?>">
- <?=htmlspecialchars($options->label)?>
- <?php
- self::renderRequiredBadge($required);
- ?>
- </label>
- <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- <?php
- }
- /**
- * @param object $options
- * @throws Exception
- */
- private static function renderTextareaField(object $options): void {
- $id = str_replace("_", "-", $options->name);
- $value = $options->value ?? http::value($options->name);
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- $max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
- $requirements = $options->requirements ?? [];
- $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
- ?>
- <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
- <label for="<?=$id?>">
- <?=htmlspecialchars($options->label)?>
- <?php
- self::renderRequiredBadge($required);
- ?>
- </label>
- <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- <?php
- }
- private static function renderHorizontalBar(object $options): void {
- $hidden = !empty($options->hidden) ? "hidden" : "";
- $requirements = $options->requirements ?? [];
- ?>
- <hr <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" />
- <?php
- }
- private static function renderCheckboxField(object $options): void {
- $id = str_replace("_", "-", $options->name);
- $checked = !empty($options->value) ? "checked" : "";
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- $requirements = $options->requirements ?? [];
- ?>
- <div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>">
- <div class="custom-control custom-switch">
- <input type="checkbox" class="custom-control-input" id="<?=htmlspecialchars($id)?>" name="<?=htmlspecialchars($options->name)?>" value="1" <?=$checked?> <?=$required?> />
- <label for="<?=htmlspecialchars($id)?>" class="custom-control-label">
- <?=htmlspecialchars($options->label)?>
- </label>
- </div>
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- <?php
- }
- /**
- * @param object $options
- * @throws Exception
- */
- private static function renderRadioField(object $options): void {
- $id = str_replace("_", "-", $options->name);
- $default_value = $options->defaultValue ?? null;
- $current_value = $options->value ?? http::value($options->name);
- if($current_value === null) $current_value = $default_value;
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- $inline = ($options->type === "inline-radio") ? "custom-control-inline" : "";
- $hidden = !empty($options->hidden) ? "hidden" : "";
- $requirements = $options->requirements ?? [];
- ?>
- <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>">
- <label>
- <?=htmlspecialchars($options->label)?>
- <?php
- self::renderRequiredBadge($required);
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </label>
- <br />
- <?php
- foreach($options->values as $index => $value) {
- $display = $options->displayStrings[$index];
- $checked = ($current_value === (string) $value) ? "checked" : "";
- $radio_id = sprintf("%s-%s", $id, $value);
- if($index === 0 && $current_value === null) {
- $checked = "checked";
- }
- ?>
- <div class="custom-control custom-radio <?=$inline?>">
- <input id="<?=$radio_id?>" name="<?=$options->name?>" type="radio" class="custom-control-input" value="<?=htmlspecialchars($value)?>" <?=$checked?> <?=$required?> />
- <label for="<?=$radio_id?>" class="custom-control-label">
- <?=htmlspecialchars($display)?>
- </label>
- </div>
- <?php
- }
- ?>
- </div>
- <?php
- }
- /**
- * @param object $options
- * @throws Exception
- */
- private static function renderTimeField(object $options): void {
- $id = str_replace("_", "-", $options->name);
- $value = $options->value ?? http::value($options->name);
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- $hidden = !empty($options->hidden) ? "hidden" : "";
- $requirements = $options->requirements ?? [];
- $required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
- ?>
- <div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
- <label for="<?=$id?>">
- <?=htmlspecialchars($options->label)?>
- <?php
- self::renderRequiredBadge($required);
- ?>
- </label>
- <input class="form-control" type="time" name="<?=$options->name?>" id="<?=$id?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- <?php
- }
- }
|