public static function checkBoxList($name, $select, $data, $htmlOptions = array())
{
$inline = TbArray::popValue('inline', $htmlOptions, false);
$separator = TbArray::popValue('separator', $htmlOptions, ' ');
$container = TbArray::popValue('container', $htmlOptions, 'div');
$containerOptions = TbArray::popValue('containerOptions', $htmlOptions, array());
$labelOptions = TbArray::popValue('labelOptions', $htmlOptions, array());
if (substr($name, -2) !== '[]') {
$name .= '[]';
}
$checkAll = TbArray::popValue('checkAll', $htmlOptions);
$checkAllLast = TbArray::popValue('checkAllLast', $htmlOptions);
if ($checkAll !== null) {
$checkAllLabel = $checkAll;
$checkAllLast = $checkAllLast !== null;
}
$items = array();
$baseID = $containerOptions['id'] = TbArray::popValue('baseID', $htmlOptions, parent::getIdByName($name));
$id = 0;
$checkAll = true;
foreach ($data as $value => $label) {
$checked = !is_array($select) && !strcmp($value, $select) || is_array($select) && in_array($value, $select);
$checkAll = $checkAll && $checked;
$htmlOptions['value'] = $value;
$htmlOptions['id'] = $baseID . '_' . $id++;
if ($inline) {
$htmlOptions['label'] = $label;
self::addCssClass('checkbox-inline', $labelOptions);
$htmlOptions['labelOptions'] = $labelOptions;
$items[] = self::checkBox($name, $checked, $htmlOptions);
} else {
$option = self::checkBox($name, $checked, $htmlOptions);
$items[] = self::tag('div', array('class' => 'checkbox'), self::label($option . ' ' . $label, false, $labelOptions));
}
}
if (isset($checkAllLabel)) {
$htmlOptions['value'] = 1;
$htmlOptions['id'] = $id = $baseID . '_all';
$htmlOptions['label'] = $checkAllLabel;
$htmlOptions['labelOptions'] = $labelOptions;
$item = self::checkBox($id, $checkAll, $htmlOptions);
if ($inline) {
self::addCssClass('checkbox-inline', $labelOptions);
} else {
$item = self::checkBox($id, $checkAll, $htmlOptions);
$item = self::tag('div', array('class' => 'checkbox'), $item);
}
if ($checkAllLast) {
$items[] = $item;
} else {
array_unshift($items, $item);
}
$name = strtr($name, array('[' => '\\[', ']' => '\\]'));
$js = <<<EOD
jQuery('#{$id}').on('click', function() {
\tjQuery("input[name='{$name}']").prop('checked', this.checked);
});
jQuery("input[name='{$name}']").on('click', function() {
\tjQuery('#{$id}').prop('checked', !jQuery("input[name='{$name}']:not(:checked)").length);
});
jQuery('#{$id}').prop('checked', !jQuery("input[name='{$name}']:not(:checked)").length);
EOD;
$cs = Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerScript($id, $js);
}
$inputs = implode($separator, $items);
return !empty($container) ? self::tag($container, $containerOptions, $inputs) : $inputs;
}