|
|
@@ -157,17 +157,36 @@ class Form {
|
|
|
*/
|
|
|
public static function renderField(object $options) {
|
|
|
|
|
|
+ $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;
|
|
|
@@ -210,18 +229,16 @@ class Form {
|
|
|
$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 class="form-group">
|
|
|
+ <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
|
|
|
+ <input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
|
|
|
+ <?php
|
|
|
+ if($help_text) {
|
|
|
?>
|
|
|
- </div>
|
|
|
+ <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+ ?>
|
|
|
</div>
|
|
|
<?php
|
|
|
}
|
|
|
@@ -243,29 +260,27 @@ class Form {
|
|
|
$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>
|
|
|
+ <div class="form-group">
|
|
|
+ <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
|
|
|
+ <div class="input-group">
|
|
|
<?php
|
|
|
- if($help_text) {
|
|
|
+ if(!empty($prepend_text)) {
|
|
|
?>
|
|
|
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
|
|
|
+ <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>
|
|
|
<?php
|
|
|
}
|
|
|
@@ -281,28 +296,26 @@ class Form {
|
|
|
$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
|
|
|
- }
|
|
|
+ <div class="form-group">
|
|
|
+ <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></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 === strval($value)) ? "selected" : "";
|
|
|
?>
|
|
|
- </select>
|
|
|
+ <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
|
|
|
<?php
|
|
|
- if($help_text) {
|
|
|
- ?>
|
|
|
- <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
|
|
|
- <?php
|
|
|
- }
|
|
|
+ }
|
|
|
+ ?>
|
|
|
+ </select>
|
|
|
+ <?php
|
|
|
+ if($help_text) {
|
|
|
?>
|
|
|
- </div>
|
|
|
+ <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+ ?>
|
|
|
</div>
|
|
|
<?php
|
|
|
}
|
|
|
@@ -315,18 +328,16 @@ class Form {
|
|
|
$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 class="form-group">
|
|
|
+ <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
|
|
|
+ <input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
|
|
|
+ <?php
|
|
|
+ if($help_text) {
|
|
|
?>
|
|
|
- </div>
|
|
|
+ <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+ ?>
|
|
|
</div>
|
|
|
<?php
|
|
|
}
|
|
|
@@ -343,18 +354,16 @@ class Form {
|
|
|
$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 class="form-group">
|
|
|
+ <label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
|
|
|
+ <textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
|
|
|
+ <?php
|
|
|
+ if($help_text) {
|
|
|
?>
|
|
|
- </div>
|
|
|
+ <small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+ ?>
|
|
|
</div>
|
|
|
<?php
|
|
|
}
|
|
|
@@ -365,6 +374,75 @@ class Form {
|
|
|
<?php
|
|
|
}
|
|
|
|
|
|
+ private static function renderCheckboxField(object $options) {
|
|
|
+ $id = str_replace("_", "-", $options->name);
|
|
|
+ $checked = !empty($options->value) ? "checked" : "";
|
|
|
+ $required = ($options->required ?? false) ? "required" : "";
|
|
|
+ $help_text = $options->helpText ?? false;
|
|
|
+ ?>
|
|
|
+ <div class="form-group">
|
|
|
+ <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
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static function renderRadioField(object $options) {
|
|
|
+ $id = str_replace("_", "-", $options->name);
|
|
|
+ $current_value = $options->value ?? http::value($options->name);
|
|
|
+ $required = ($options->required ?? false) ? "required" : "";
|
|
|
+ $help_text = $options->helpText ?? false;
|
|
|
+ $inline = ($options->type == "inline-radio") ? "custom-control-inline" : "";
|
|
|
+
|
|
|
+ ?>
|
|
|
+ <div class="form-group">
|
|
|
+ <label>
|
|
|
+ <?=htmlspecialchars($options->label)?>
|
|
|
+ <?php
|
|
|
+ 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 === strval($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
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
?>
|