Adding 'multiple' => 'checkbox' option to FormHelper::select() (Ticket #1901 and Ticket #3043)

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5992 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-11-14 05:51:49 +00:00
parent f5dc3c7655
commit 1f56ef2b7a
2 changed files with 45 additions and 8 deletions

View file

@ -985,8 +985,11 @@ class FormHelper extends AppHelper {
* @return string Formatted SELECT element * @return string Formatted SELECT element
*/ */
function select($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '') { function select($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '') {
$select = array();
$showParents = false; $showParents = false;
$escapeOptions = true; $escapeOptions = true;
$style = null;
$tag = null;
if (isset($attributes['escape'])) { if (isset($attributes['escape'])) {
$escapeOptions = $attributes['escape']; $escapeOptions = $attributes['escape'];
@ -1012,12 +1015,22 @@ class FormHelper extends AppHelper {
} }
if (isset($attributes) && array_key_exists('multiple', $attributes)) { if (isset($attributes) && array_key_exists('multiple', $attributes)) {
$tag = $this->Html->tags['selectmultiplestart']; if ($attributes['multiple'] === 'checkbox') {
if (isset($this->Html->tags['checkboxmultiplestart'])) {
$tag = $this->Html->tags['checkboxmultiplestart'];
}
$style = 'checkbox';
} else {
$tag = $this->Html->tags['selectmultiplestart'];
}
} else { } else {
$tag = $this->Html->tags['selectstart']; $tag = $this->Html->tags['selectstart'];
$this->__secure(); $this->__secure();
} }
$select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes($attributes, array('name', 'value')));
if (!empty($tag)) {
$select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes($attributes, array('name', 'value')));
}
if ($showEmpty !== null && $showEmpty !== false && !(empty($showEmpty) && (isset($attributes) && array_key_exists('multiple', $attributes)))) { if ($showEmpty !== null && $showEmpty !== false && !(empty($showEmpty) && (isset($attributes) && array_key_exists('multiple', $attributes)))) {
if ($showEmpty === true) { if ($showEmpty === true) {
@ -1027,8 +1040,16 @@ class FormHelper extends AppHelper {
$options[''] = $showEmpty; $options[''] = $showEmpty;
$options = array_reverse($options, true); $options = array_reverse($options, true);
} }
$select = am($select, $this->__selectOptions(array_reverse($options, true), $selected, array(), $showParents, array('escape' => $escapeOptions))); $select = am($select, $this->__selectOptions(array_reverse($options, true), $selected, array(), $showParents, array('escape' => $escapeOptions, 'style' => $style)));
$select[] = sprintf($this->Html->tags['selectend']);
if ($style == 'checkbox') {
if (isset($this->Html->tags['checkboxmultipleend'])) {
$select[] = $this->Html->tags['checkboxmultipleend'];
}
} else {
$select[] = $this->Html->tags['selectend'];
}
return $this->output(implode("\n", $select)); return $this->output(implode("\n", $select));
} }
/** /**
@ -1390,7 +1411,7 @@ class FormHelper extends AppHelper {
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) { function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {
$select = array(); $select = array();
$attributes = am(array('escape' => true), $attributes); $attributes = am(array('escape' => true, 'style' => null), $attributes);
$selectedIsEmpty = ($selected === '' || $selected === null); $selectedIsEmpty = ($selected === '' || $selected === null);
$selectedIsArray = is_array($selected); $selectedIsArray = is_array($selected);
@ -1414,11 +1435,26 @@ class FormHelper extends AppHelper {
} }
if ($name !== null) { if ($name !== null) {
if ((!$selectedIsEmpty && ($selected == $name)) || ($selectedIsArray && in_array($name, $selected))) { if ((!$selectedIsEmpty && ($selected == $name)) || ($selectedIsArray && in_array($name, $selected))) {
$htmlOptions['selected'] = 'selected'; if ($attributes['style'] === 'checkbox') {
$htmlOptions['checked'] = true;
} else {
$htmlOptions['selected'] = 'selected';
}
} }
if ($showParents || (!in_array($title, $parents))) { if ($showParents || (!in_array($title, $parents))) {
$title = ife($attributes['escape'], h($title), $title); $title = ife($attributes['escape'], h($title), $title);
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title); if ($attributes['style'] === 'checkbox') {
$htmlOptions['value'] = $name;
$label = array();
if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {
$label['class'] = 'selected';
}
list($name) = array_values($this->__name());
$select[] = sprintf($this->Html->tags['checkboxmultiple'], $name, $this->Html->_parseAttributes($htmlOptions)) . $this->label(null, $title, $label);
} else {
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title);
}
} }
} }
} }
@ -1467,7 +1503,7 @@ class FormHelper extends AppHelper {
break; break;
case 'month': case 'month':
for ($i = 1; $i <= 12; $i++) { for ($i = 1; $i <= 12; $i++) {
$data[sprintf("%02s", $i)] = strftime("%B", mktime(1,1,1,$i,1,1999)); $data[sprintf("%02s", $i)] = strftime("%B", mktime(1, 1, 1, $i, 1, 1999));
} }
break; break;
case 'year': case 'year':

View file

@ -55,6 +55,7 @@ class HtmlHelper extends AppHelper {
'hidden' => '<input type="hidden" name="%s" %s/>', 'hidden' => '<input type="hidden" name="%s" %s/>',
'textarea' => '<textarea name="%s" %s>%s</textarea>', 'textarea' => '<textarea name="%s" %s>%s</textarea>',
'checkbox' => '<input type="checkbox" name="%s" %s/>', 'checkbox' => '<input type="checkbox" name="%s" %s/>',
'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
'radio' => '<input type="radio" name="%s" id="%s" %s />%s', 'radio' => '<input type="radio" name="%s" id="%s" %s />%s',
'selectstart' => '<select name="%s"%s>', 'selectstart' => '<select name="%s"%s>',
'selectmultiplestart' => '<select name="%s[]"%s>', 'selectmultiplestart' => '<select name="%s[]"%s>',