public function select_box()
{
return _class('html')->select_box('input_name', ['k1' => 'key1', 'k2' => 'key2']);
}
public function select_box($name, $values = [], $selected = '', $show_text = false, $type = 2, $add_str = '', $translate = 0, $level = 0)
{
// Passing params as array
if (is_array($name)) {
$extra = (array) $extra + $name;
$name = $extra['name'];
}
$values = isset($extra['values']) ? $extra['values'] : (array) $values; // Required
$translate = isset($extra['translate']) ? $extra['translate'] : $translate;
if ($extra['no_translate']) {
$translate = 0;
}
$selected = isset($extra['selected']) ? $extra['selected'] : $selected;
$show_text = isset($extra['show_text']) ? $extra['show_text'] : ($show_text !== null ? $show_text : false);
$type = isset($extra['type']) ? $extra['type'] : ($type !== null ? $type : 2);
$level = isset($extra['level']) ? $extra['level'] : $level;
// (example: $add_str = 'size=6')
$add_str = isset($extra['add_str']) ? $extra['add_str'] : $add_str;
$extra['class'] = isset($extra['class']) ? $extra['class'] : $this->CLASS_SELECT_BOX;
$extra['class_add'] && $extra['class'] = trim($extra['class'] . ' ' . $extra['class_add']);
if ( ! $values && @ ! $extra['ajax']) {
return false;
}
if ($extra['disabled']) {
$extra['disabled'] = 'disabled';
}
$body = [];
if ($level == 0) {
$extra['force_id'] && $id = $extra['force_id'];
$id = $id ?: __FUNCTION__ . '_' . ++$this->_ids[__FUNCTION__];
if ($this->AUTO_ASSIGN_IDS) {
$extra['id'] = $id;
}
if ($extra['outer_label']) {
$body[] = '<label class="outer-label">' . $extra['outer_label'] . '</label>';
}
$extra['name'] = $name;
$body[] = '<select' . _attrs($extra, ['name', 'id', 'class', 'style', 'disabled', 'required']) . ($add_str ? ' ' . $add_str : '') . '>';
}
$selected = (string) $selected;
if ($show_text && $level == 0) {
$def_opt_text = $show_text;
if ($show_text == 1) {
$def_opt_text = str_replace(['%name%', '%select%'], [t($name), t('Select')], $this->SELECT_BOX_DEF_OPT_TPL);
}
$body[] = '<option value="" class="' . $this->CLASS_SELECT_OPTION_DEFAULT . '">' . $def_opt_text . '</option>';
}
$self_func = __FUNCTION__;
$option_callback = $extra['option_callback'];
$use_option_callback = is_callable($option_callback);
foreach ((array) $values as $key => $cur_value) {
if (is_array($cur_value)) {
$body[] = '<optgroup label="' . $key . '" title="' . ($translate ? t($key) : $key) . '">';
$body[] = $this->$self_func($name, $cur_value, $selected, $show_text, $type, $add_str, $translate, $level + 1);
$body[] = '</optgroup>';
} else {
$_what_compare = (string) ($type == 1 ? $cur_value : $key);
$is_selected = ($_what_compare == $selected);
$text = $translate ? t($cur_value) : $cur_value;
if ($use_option_callback) {
$body[] = $option_callback($key, $cur_value, $is_selected, $text, $extra);
} else {
$body[] = '<option value="' . $key . '"' . ($is_selected ? ' selected="selected"' : '') . '>' . $text . '</option>';
}
}
}
$body[] = $level == 0 ? '</select>' : '';
return implode(PHP_EOL, $body);
}