Hello DMZ-Users,
i have made a quick/small checkbox-multicolumn hack for the “htmlform”-extension.
$form_fields = array(
'id',
'tag' => array(
'input_separator' => '',
'checkbox_cols' => 3
)
);
‘checkbox_cols’ is the new feature.
The changes in the htmlforms.php are in the _checkbox function:
// renders one or more checkboxes or radio buttons
function _checkbox($type, $id, $value, $options, $sub_id = '', $label = '')
{
if(isset($options['value']))
{
$value = $options['value'];
unset($options['value']);
}
// if there is a list in options, render our multiple checkboxes.
if(isset($options['list']))
{
$list = $options['list'];
unset($options['list']);
$ret = '';
if( ! is_array($value))
{
if(is_null($value) || $value === FALSE || $value === '')
{
$value = array();
}
else
{
$value = array($value);
}
}
$sep = '<br>';
if(isset($options['input_separator']))
{
$sep = $options['input_separator'];
unset($options['input_separator']);
}
$col = 0; // Standard 1 Column
if(isset($options['checkbox_cols']))
{
$col = $options['checkbox_cols']-1;
unset($options['checkbox_cols']);
}
$num = 0;
foreach($list as $k => $v)
{
if( ! empty($ret))
{
// put each node on one line.
$ret .= $sep;
}
if($col!=0){
if($num==0){
$ret .= '<tr><td>'.$this->_checkbox($type, $id, $value, $options, $k, $v).'</td>';
$num++;
}elseif ($num%$col == 0){
$ret .= '<td>'.$this->_checkbox($type, $id, $value, $options, $k, $v).'</td></tr>';
$num=0;
}else {
$ret .= '<td>'.$this->_checkbox($type, $id, $value, $options, $k, $v).'</td>';
$num++;
}
}else{
$ret .= $this->_checkbox($type, $id, $value, $options, $k, $v);
}
}
return '<table class="cb">'.$ret.'</table>';
}
else
{
// just render the single checkbox.
$node_id = $id;
if( ! empty($sub_id))
{
// there are multiple nodes with this id, append the sub_id
$node_id .= '_' . $sub_id;
$field_value = $sub_id;
}
else
{
// sub_id is the same as the node's id
$sub_id = $id;
$field_value = '1';
}
$name = $id;
if(is_array($value))
{
// allow for multiple results
$name .= '[]';
}
// node attributes
$a = $options + array(
'type' => $type,
'id' => $node_id,
'name' => $name,
'value' => $field_value
);
// if checked wasn't overridden
if( ! isset($a['checked']))
{
// determine if this is a multiple checkbox or not.
$checked = $value;
if(is_array($checked))
{
$checked = in_array($sub_id, $value);
}
if($checked)
{
$a['checked'] = 'checked';
}
}
$ret = $this->_render_node('input', $a);
if( ! empty($label))
{
$ret .= ' ' . $this->_render_node('label', array('for' => $node_id), $label);
}
return $ret;
}
}
Greetings