| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- class Form {
- public static function checkRecaptcha() {
- if(!defined("GOOGLE_RECAPTCHA_SECRET_KEY")) return true;
- $json = http::post("https://www.google.com/recaptcha/api/siteverify", [
- "secret" => GOOGLE_RECAPTCHA_SECRET_KEY,
- "response" => http::value("g-recaptcha-response"),
- "remoteip" => $_SERVER["REMOTE_ADDR"]
- ]);
- if($json) {
- $response = json_decode($json);
- if($response) {
- return $response->success;
- }
- }
- return false;
- }
- public static function renderYearOptions($min = null, $max = null, $selected_value = null) {
- if(empty($min)) $min = date("Y") - 100;
- if(empty($max)) $max = date("Y") - 16;
- for($i = $min; $i <= $max; $i++) {
- $selected = ($selected_value == $i) ? "selected" : "";
- ?>
- <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
- <?php
- }
- }
- public static function renderMonthOptions($selected_value = null) {
- for($i = 1; $i <= 12; $i++) {
- $selected = ($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) {
- for($i = 1; $i <= 31; $i++) {
- $selected = ($selected_value == $i) ? "selected" : "";
- ?>
- <option value="<?=$i?>" <?=$selected?>><?=$i?></option>
- <?php
- }
- }
- public static function renderField($options) {
- switch($options->type) {
- case "header":
- self::renderHeader($options);
- break;
- case "dropdown":
- case "select":
- self::renderSelectField($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;
- }
- }
- private static function renderHeader($options) {
- ?>
- <h3><?=htmlspecialchars($options->content)?></h3>
- <?php
- }
- private static function renderTextField($options) {
- $id = str_replace("_", "-", $options->name);
- $placeholder = "Enter " . strtolower($options->label);
- $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\"" : "";
- ?>
- <div class="row mb-1">
- <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
- <div class="col-sm">
- <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- </div>
- <?php
- }
- private static function renderNumberField($options) {
- $id = str_replace("_", "-", $options->name);
- $placeholder = "Enter " . strtolower($options->label);
- $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 ?? "";
- ?>
- <div class="row mb-1">
- <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
- <div class="col-sm">
- <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="<?=htmlspecialchars($placeholder)?>" 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>
- </div>
- <?php
- }
- private static function renderSelectField($options) {
- $id = str_replace("_", "-", $options->name);
- $selected_value = $options->value ?? http::value($options->name);
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- ?>
- <div class="row mb-1">
- <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
- <div class="col-sm">
- <select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
- <?php
- foreach($options->values as $index => $value) {
- $display = $options->displayStrings[$index];
- $selected = ($selected_value === strval($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>
- </div>
- <?php
- }
- private static function renderFileField($options) {
- $id = str_replace("_", "-", $options->name);
- $placeholder = "Enter " . strtolower($options->label);
- $required = ($options->required ?? false) ? "required" : "";
- $help_text = $options->helpText ?? false;
- ?>
- <div class="row mb-1">
- <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
- <div class="col-sm">
- <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>
- </div>
- <?php
- }
- private static function renderTextareaField($options) {
- $id = str_replace("_", "-", $options->name);
- $placeholder = "Enter " . strtolower($options->label);
- $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\"" : "";
- ?>
- <div class="row mb-1">
- <label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
- <div class="col-sm">
- <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
- <?php
- if($help_text) {
- ?>
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
- <?php
- }
- ?>
- </div>
- </div>
- <?php
- }
- }
- ?>
|