public function multi_check_box()
{
return _class('html')->multi_check_box('input_name', ['k1' => 'key1', 'k2' => 'key2']);
}
public function multi_check_box($name, $values = [], $selected = [], $horizontal = true, $type = 2, $add_str = '', $translate = 0, $name_as_array = false)
{
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'] : 0;
if ($extra['no_translate']) {
$translate = 0;
}
$selected = $extra['selected'];
$type = isset($extra['type']) ? $extra['type'] : ($type !== null ? $type : 2);
$horizontal = isset($extra['horizontal']) ? $extra['horizontal'] : $horizontal;
$name_as_array = isset($extra['name_as_array']) ? $extra['name_as_array'] : false;
$add_str = isset($extra['add_str']) ? $extra['add_str'] : '';
if ($extra['class']) {
$add_str .= ' class="' . trim($extra['class'] . ' ' . $extra['class_add']) . '" ';
}
if ($extra['style']) {
$add_str .= ' style="' . $extra['style'] . '" ';
}
if ( ! $values) {
return false;
}
if ( ! is_array($selected)) {
$selected = (string) $selected;
}
$body = [];
if ($extra['outer_label']) {
$body[] = '<label class="outer-label">' . $extra['outer_label'] . '</label>';
}
foreach ((array) $values as $key => $value) {
$sel_text = '';
// Selected value could be an array
if (is_array($selected)) {
if ($type == 1) {
$sel_text = in_array($value, $selected) ? 'checked="checked"' : '';
} else {
$sel_text = isset($selected[$key]) ? 'checked="checked"' : '';
}
} elseif (strlen($selected)) {
$_what_compare = (string) ($type == 1 ? $value : $key);
$sel_text = $_what_compare == $selected ? 'checked="checked"' : '';
} else {
$sel_text = '';
}
$is_selected = strlen($sel_text) ? 1 : 0;
if ($name_as_array) {
$val_name = $name . '[' . $key . ']';
} else {
$val_name = $name . '_' . $key;
}
$label_extra = $extra['label_extra'];
$label_extra['class'] = isset($label_extra['class']) ? $label_extra['class'] : (isset($extra['class_label_checkbox']) ? $extra['class_label_checkbox'] : $this->CLASS_LABEL_CHECKBOX);
if ($horizontal) {
$label_extra['class'] .= ' ' . $this->CLASS_LABEL_CHECKBOX_INLINE;
}
if ($extra['class_add_label_checkbox']) {
$label_extra['class'] .= ' ' . $extra['class_add_label_checkbox'];
}
if ($is_selected) {
$label_extra['class'] .= ' ' . $this->CLASS_LABEL_CHECKBOX_SELECTED;
}
$id = __FUNCTION__ . '_' . ++$this->_ids[__FUNCTION__];
if ($extra['no_desc']) {
$desc = '';
} else {
$desc = ($translate ? t($value) : $value);
}
if ($this->BOXES_USE_STPL) {
$body[] = tpl()->parse('system/common/check_box_item', [
'name' => $val_name,
'value' => $key,
'desc' => $desc,
'selected' => $sel_text,
'add_str' => $add_str,
'label' => $translate ? t($value) : $value,
'id' => $id,
'extra' => $extra,
]);
} else {
$body[] = '<label' . _attrs($label_extra, ['id', 'class', 'style']) . '>'
. '<input type="checkbox" name="' . $val_name . '" id="' . $id . '" value="' . $key . '"'
. ($is_selected ? ' ' . $sel_text : '') . ($add_str ? ' ' . trim($add_str) : '')
. '> ' . '<span>' . $desc . '</span>' // Please do not remove whitespace :)
. '</label>';
}
}
return implode(PHP_EOL, $body);
}