Skip to content

Commit

Permalink
Checkbox sets
Browse files Browse the repository at this point in the history
As alternative to multiple selects
  • Loading branch information
bloatware committed May 2, 2016
1 parent e6b344d commit 802e054
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions textpattern/lib/txplib_forms.php
Expand Up @@ -169,8 +169,8 @@ function selectInput($name = '', $array = array(), $value = '', $blank_first = f
$atts .= ' '.trim($onchange);
}

return n.'<select'.$atts.$multiple.'>'.n.join(n, $out).n.'</select>'
.($multiple ? n.'<input type="hidden" value="" name="'.txpspecialchars($name).'" />'.n : ''); // TODO: use jQuery UI selectmenu?
return n.'<select'.$atts.$multiple.'>'.n.join(n, $out).n.'</select>'.n
.($multiple ? hInput($name, '').n : ''); // TODO: use jQuery UI selectmenu?
}

/**
Expand Down Expand Up @@ -410,6 +410,40 @@ function checkbox2($name, $value, $tabindex = 0, $id = '')
return checkbox($name, 1, $value, $tabindex, $id);
}

/**
* A checkbox set.
*
* @param string $name The field
* @param array $array The values => labels array
* @param array $value Checked values
* @param string $wraptag An HTML wrapper tag for the set
* @param string $break Checkboxes wrapper/separator
* @param string $class Class applied to the wraptag
* @param string $breakclass Class applied to break tag
* @param string $atts HTML attributes applied to the wraptag
* @param string $breakatts HTML attributes applied to the break tag
* @param string $id HTML id applied to the wraptag
* @return string HTML input
* @example
* echo checkboxSet('fruit', array('a'=>'Apple', 'b'=>'Banana'), array('b'));
*/

This comment has been minimized.

Copy link
@Bloke

Bloke May 3, 2016

Member

Nice start. I've always made this myself in plugins, so to have a core widget is ace.

I know that many other functions in this file have convoluted and awkward parameter lists, but I'd really like to move towards an API that's less cumbersome. Perhaps longer-term it'll become a class-based approach, but for now is there any chance this function's signature can be distilled a little?

Even (perhaps) employing associative arrays for similar options might be preferable, as they can be extended in future. Though they are more difficult to document. See this guy's checkbox or multicheck for examples. Notice the catch-all $args, which could be used in our case for $atts and maybe $breakatts if it's needed.

As a rule of thumb, I'd say that any more than five args is getting unwieldy, especially when something like id is delegated to the last parameter.

Side note: I think we've deprecated breakclass throughout (there were only a couple of tags that used it).

function checkboxSet($name = '', $array = array(), $value = array(), $wraptag = '', $break = '', $class = '', $breakclass = '', $atts = '', $breakatts = '', $id = '')
{
$out = array();

if ($name) {
$name .= '[]';
}

foreach ($array as $avalue => $alabel) {
$out[] = checkbox($name, $avalue, in_array($avalue, $value)).n
.'<label>'.txpspecialchars($alabel).'</label>';
}

return $out ? n.doWrap($out, $wraptag, $break, $class, $breakclass, $atts, $breakatts, $id).n.hInput($name, '') : '';
}

/**
* A single radio button.
*
Expand Down

0 comments on commit 802e054

Please sign in to comment.