Fixed formatting of code, and added some documentation on what the source for this field should be

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@48319 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2008-01-20 08:58:04 +00:00
parent 20da7e8f95
commit e28361a313

View File

@ -6,36 +6,48 @@
*/ */
/** /**
* Grouped dropdown, using <optgroup>s. * Grouped dropdown, using <optgroup> tags.
* Returns a <select> tag containing all the appropriate <option> tags, with <optgroup> tags around the <option> tags as required *
* $source parameter (from DropdownField) must be a two dimensional array.
* The first level of the array is used for the <optgroup>, and the second
* level are the <options> for each group.
*
* Returns a <select> tag containing all the appropriate <option> tags, with
* <optgroup> tags around the <option> tags as required.
*
* @package forms * @package forms
* @subpackage fields-basic * @subpackage fields-basic
*/ */
class GroupedDropdownField extends DropdownField { class GroupedDropdownField extends DropdownField {
function Field() {
function Field() {
// Initialisations // Initialisations
$options = ''; $options = '';
$classAttr = ''; $classAttr = '';
if($extraClass = trim($this->extraClass())) {
if($extraClass = trim($this->extraClass())) {
$classAttr = "class=\"$extraClass\""; $classAttr = "class=\"$extraClass\"";
} }
foreach($this->source as $value => $title) { if($this->source) {
if(is_array($title)) { foreach($this->source as $value => $title) {
$options .= "<optgroup label=\"$value\">"; if(is_array($title)) {
foreach($title as $value2 => $title2) { $options .= "<optgroup label=\"$value\">";
$selected = $value2 == $this->value ? " selected=\"selected\"" : ""; foreach($title as $value2 => $title2) {
$options .= "<option$selected value=\"$value2\">$title2</option>"; $selected = $value2 == $this->value ? " selected=\"selected\"" : "";
} $options .= "<option$selected value=\"$value2\">$title2</option>";
$options .= "</optgroup>"; }
} else { // Fall back to the standard dropdown field $options .= "</optgroup>";
$selected = $value == $this->value ? " selected=\"selected\"" : ""; } else { // Fall back to the standard dropdown field
$options .= "<option$selected value=\"$value\">$title</option>"; $selected = $value == $this->value ? " selected=\"selected\"" : "";
} $options .= "<option$selected value=\"$value\">$title</option>";
}
}
} }
$id = $this->id(); $id = $this->id();
return "<select $classAttr name=\"$this->name\" id=\"$id\">$options</select>"; return "<select $classAttr name=\"$this->name\" id=\"$id\">$options</select>";
} }
} }
?>
?>