121 lines
4.1 KiB
PHP
121 lines
4.1 KiB
PHP
<?php
|
|
|
|
class BulmaFormHelper extends \Prefab {
|
|
|
|
const H_FIELD_INPUT = 1;
|
|
const H_FIELD_TEXTAREA = 2;
|
|
const H_FIELD_SELECT = 3;
|
|
|
|
static public function render($args) {
|
|
|
|
$type = strtoupper($args['@attrib']['type']);
|
|
// all *
|
|
$label = $args['@attrib']['label'];
|
|
$name = $args['@attrib']['name'];
|
|
$value = isset($args['@attrib']['value']) ? $args['@attrib']['value'] : '';
|
|
// select
|
|
$options = isset($args['@attrib']['options']) ? $args['@attrib']['options'] : '';
|
|
$selected = isset($args['@attrib']['selected']) ? $args['@attrib']['selected'] : '';
|
|
//
|
|
$label = \Template::instance()->build($label);
|
|
$name = \Template::instance()->build($name);
|
|
$value = \Template::instance()->build($value);
|
|
|
|
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;
|
|
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_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'); |