tp_servicedesk/app/extensions/BulmaFormHelper.php

163 lines
5.5 KiB
PHP

<?php
class BulmaFormHelper extends \Prefab {
const H_FIELD_INPUT = 1;
const H_FIELD_TEXTAREA = 2;
const H_FIELD_SELECT = 3;
const H_FIELD_SELECT_NEW = 4;
static public function render($node) {
$attr = $node['@attrib'] ?? [];
$type = strtoupper($attr['type']) ?? null;
// all *
$label = $attr['label'];
$name = $attr['name'];
$value = isset($attr['value']) ? $attr['value'] : '';
// select
$options = isset($attr['options']) ? $attr['options'] : '';
$selected = isset($attr['selected']) ? $attr['selected'] : '';
//
$label = \Template::instance()->build($label);
$name = \Template::instance()->build($name);
$value = \Template::instance()->build($value);
$options_array = \Template::instance()->token($options);
if(defined("BulmaFormHelper::$type")){
$type_const = constant("BulmaFormHelper::$type");
switch( $type_const ){
case BulmaFormHelper::H_FIELD_INPUT:
return BulmaFormHelper::build_h_field_input($label, $name, $value);
break;
case BulmaFormHelper::H_FIELD_TEXTAREA:
return BulmaFormHelper::build_h_field_textarea($label, $name, $value);
break;
case BulmaFormHelper::H_FIELD_SELECT:
return BulmaFormHelper::build_h_field_select($label, $name, $options, $selected);
break;
case BulmaFormHelper::H_FIELD_SELECT_NEW:
return BulmaFormHelper::build_h_field_select_new($attr);
break;
default:
return '<div class="notification is-danger">Error: Bulma CSS Form TYPE ('.$type.') not defined.</div>';
break;
}
} else {
return '<div class="notification is-danger">Error: Bulma CSS Form TYPE not defined.</div>';
}
}
static function build_h_field_select_new($attr)
{
$f3 = \Base::instance();
$label = $attr['label'] ?? '';
$name = $attr['name'] ?? '';
$options_arr = $attr['options'] ?? [];
$optionValue = $attr['option_value'] ?? 'id';
$optionName = $attr['option_name'] ?? 'name';
$selected = $attr['selected'] ?? '';
$options = $f3->get($options_arr);
$html = '<div class="field is-horizontal"><div class="field-label is-normal">';
if (!empty($label)) {
$html .= '<label class="label">'.$label.'</label>';
}
$html .= '</div><div class="field-body"><div class="field">';
$html .= '<div class="select">';
$html .= '<select name="'.$name.'">';
foreach ($options as $option) {
$value = $option[$optionValue] ?? '';
$text = $option[$optionName] ?? '';
$sel = ((string)$value === (string)$selected) ? ' selected="selected"' : '';
$html .= '<option value="'.$value.'"'.$sel.'>'.$text.'</option>';
}
$html .= '</select>';
$html .= '</div></div></div></div>';
return $html;
}
static function build_h_field_input($label, $name, $value){
$string = '
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">'.$label.'</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<input class="input" type="text" id="'.$name.'" name="'.
$name.'" value="'.
$value.'">
</div>
</div>
</div>
</div>
';
return $string;
}
static function build_h_field_select($label, $name, $options, $selected){
$opts = json_decode(str_replace("'", '"', $options));
$opts_string = "";
foreach($opts as $k => $v){
if($v == $selected){
$selected_str = " selected";
} else {
$selected_str = "";
}
$opts_string .= '<option'.$selected_str.'>'.$v.'</option>';
}
$string =
'<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">'.$label.'</label>
</div>
<div class="field-body">
<div class="field">
<div class="select">
<select id="'.$name.'" name="'.$name.'">
'.$opts_string.'
</select>
</div>
</div>
</div>
</div>
';
return $string;
}
static function build_h_field_textarea($label, $name, $value){
$string = '
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">'.$label.'</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<textarea class="textarea" id="'.$name.'" name="'.
$name.'">'.$value.'</textarea>
</div>
</div>
</div>
</div>
';
return $string;
}
}
\Template::instance()->extend('bulma', 'BulmaFormHelper::render');