public function button_box()
{
return _class('html')->button_box('input_name', ['k1' => 'key1', 'k2' => 'key2']);
}
public function button_box($name, $values = [], $selected = '', $extra = [])
{
if (is_array($name)) {
$extra = (array) $extra + $name;
$name = $extra['name'];
}
$desc = isset($extra['desc']) ? $extra['desc'] : ucfirst(str_replace('_', '', $name));
$values = isset($extra['values']) ? $extra['values'] : (array) $values; // Required
$translate = isset($extra['translate']) ? $extra['translate'] : 0;
if ($extra['no_translate']) {
$translate = 0;
}
$selected = isset($extra['selected']) ? $extra['selected'] : $selected;
$extra['id'] = $extra['id'] ?: __FUNCTION__ . '_' . ++$this->_ids[__FUNCTION__];
if ( ! $values) {
return false;
}
$selected = (string) $selected;
$items = [];
$selected_val = '';
foreach ((array) $values as $key => $cur_value) {
$_what_compare = (string) ($type == 1 ? $cur_value : $key);
$is_selected = $_what_compare == $selected;
$val = $translate ? t($cur_value) : $cur_value;
$items[] = '<li class="dropdown' . ($is_selected ? ' active' : '') . '"><a data-value="' . $key . '"' . ($is_selected ? ' data-selected="selected"' : '') . '>' . $val . '</a></li>';
if ($is_selected) {
$selected_val = $val;
}
}
$class = $extra['class'] ?: 'btn dropdown-toggle';
$extra['class_add'] && $class .= ' ' . $extra['class_add'];
$text = $selected_val ?: $desc;
$body = [];
$body[] = '<div class="btn-group" id="' . $extra['id'] . '">';
if ($extra['button_split']) {
$body[] = '<button class="btn">' . $text . '</button>';
$body[] = '<button class="' . $class . '" data-toggle="dropdown"><span class="caret"></span></button>';
} else {
$body[] = '<button class="' . $class . '" data-toggle="dropdown">' . $text . ' <span class="caret"></span></button>';
}
$body[] = '<ul class="dropdown-menu">';
foreach ($items as $v) {
$body[] = $v;
}
$body[] = '</ul>';
$body[] = '</div>';
return implode(PHP_EOL, $body);
}