tp_servicedesk/app/extensions/BulmaFormHelper.php

267 lines
9.2 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;
const FIELD_INPUT = 10;
const FIELD_TEXTAREA = 11;
const FIELD_SELECT = 13;
static public function render($node) {
$attr = $node['@attrib'] ?? [];
$type = strtoupper($attr['type']) ?? null;
// all *
$label = $attr['label'] ?? '';
$name = $attr['name'] ?? '';
$value = $attr['value'] ?? '';
$class = $attr['class'] ?? '';
// select
$options = $attr['options'] ?? [];
$selected = $attr['selected'] ?? [];
// textarea
$rows = $attr['rows'] ?? '';
$label = \Template::instance()->build($label);
$name = \Template::instance()->build($name);
$value = \Template::instance()->build($value);
$selected = \Template::instance()->build($selected);
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;
case BulmaFormHelper::FIELD_INPUT:
return BulmaFormHelper::build_field_input($label, $name, $value, $class);
break;
case BulmaFormHelper::FIELD_TEXTAREA:
return BulmaFormHelper::build_field_textarea($label, $name, $value, $class, $rows);
break;
case BulmaFormHelper::FIELD_SELECT:
return BulmaFormHelper::build_field_select($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_field_input($label, $name, $value, $class, $rows=10){
$string_label = $label !== '' ? sprintf('<label class="label">%1$s</label>', $label) : '';
$string = '
<div class="field %4$s">
%1$s
<div class="control">
<input class="input" id="%2$s" name="%2$s" type="text" placeholder="" value="%3$s">
</div>
</div>
';
return sprintf($string, $string_label, $name, $value, $class, $rows);
}
static function build_field_textarea($label, $name, $value, $class, $rows=10)
{
$string_label = $label !== '' ? sprintf('<label class="label">%1$s</label>', $label) : '';
$string = '
<div class="field %4$s">
%1$s
<div class="control">
<textarea class="textarea" id="%2$s" name="%2$s" rows="%5$s">%3$s</textarea>
</div>
</div>
';
return sprintf($string, $string_label, $name, $value, $class,$rows);
}
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;
}
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;
}
/**
* build_field_select_new
*
* `<bulma type="H_FIELD_SELECT" label="Priority:" name="priority_id"
* options="priorities" option_value="id" option_name="name"
* selected="{{@ticket.priority_id}}"></bulma>`
*
* @param mixed $attr
* @return void
*/
static function build_field_select($attr)
{
$f3 = \Base::instance();
$class = $attr['class'] ?? '';
$label = $attr['label'] ?? '';
$name = $attr['name'] ?? '';
// $options_arr = $attr['options'] ?? [];
$option_value = $attr['option_value'] ?? 'id';
$option_name = $attr['option_name'] ?? 'name';
$options = \Template::instance()->token($attr['options']);
$selected = \Template::instance()->token($attr['selected']);
// TODO: label - this could be moved into a seperate function
$html_label = $label !== '' ? sprintf('<label class="label">%1$s</label>', $label) : '';
$tmp_options = '<?php echo \BulmaFormHelper::instance()->field_select('.
$options.', '.$selected.', "'.$option_value.'", "'.$option_name.'"); ?>';
$html = '
<div class="field %4$s">
%1$s
<div class="control">
<div class="select">
<select name="%3$s" id="%3$s">
%2$s
</select>
</div>
</div>
</div>
';
return sprintf($html, $html_label, $tmp_options, $name, $class);
}
function field_select($options, $selected, $option_value, $option_name){
$html_options = '';
foreach ($options as $option) {
$value = $option[$option_value] ?? '';
$text = $option[$option_name] ?? '';
$html_selected = ((string)$value === (string)$selected) ? ' selected="selected"' : '';
$html_option = '<option value="%s"%s>%s</option>';
$html_options .= sprintf($html_option, $value, $html_selected, $text);
}
echo $html_options;
}
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_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;
}
}
\Template::instance()->extend('bulma', 'BulmaFormHelper::render');