2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2008-01-20 09:58:04 +01:00
|
|
|
* Grouped dropdown, using <optgroup> tags.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-20 09:58:04 +01:00
|
|
|
* $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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-20 09:58:04 +01:00
|
|
|
* Returns a <select> tag containing all the appropriate <option> tags, with
|
|
|
|
* <optgroup> tags around the <option> tags as required.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Usage</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* new GroupedDropdownField(
|
|
|
|
* $name = "dropdown",
|
|
|
|
* $title = "Simple Grouped Dropdown",
|
|
|
|
* $source = array(
|
|
|
|
* "numbers" => array(
|
|
|
|
* "1" => "1",
|
|
|
|
* "2" => "2",
|
|
|
|
* "3" => "3",
|
|
|
|
* "4" => "4"
|
|
|
|
* ),
|
|
|
|
* "letters" => array(
|
|
|
|
* "1" => "A",
|
|
|
|
* "2" => "B",
|
|
|
|
* "3" => "C",
|
|
|
|
* "4" => "D",
|
|
|
|
* "5" => "E",
|
|
|
|
* "6" => "F"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-17 11:06:02 +02:00
|
|
|
* <b>Disabling individual items</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-17 11:06:02 +02:00
|
|
|
* <code>
|
2014-08-15 08:53:05 +02:00
|
|
|
* $groupedDrDownField->setDisabledItems(
|
2012-10-17 11:06:02 +02:00
|
|
|
* array(
|
|
|
|
* "numbers" => array(
|
|
|
|
* "1" => "1",
|
|
|
|
* "3" => "3"
|
|
|
|
* ),
|
|
|
|
* "letters" => array(
|
|
|
|
* "3" => "C"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class GroupedDropdownField extends DropdownField {
|
2008-01-20 09:58:04 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2008-08-26 01:32:12 +02:00
|
|
|
$options = '';
|
2008-10-15 14:39:09 +02:00
|
|
|
foreach($this->getSource() as $value => $title) {
|
|
|
|
if(is_array($title)) {
|
|
|
|
$options .= "<optgroup label=\"$value\">";
|
|
|
|
foreach($title as $value2 => $title2) {
|
2012-10-17 11:06:02 +02:00
|
|
|
$disabled = '';
|
|
|
|
if( array_key_exists($value, $this->disabledItems)
|
2014-08-15 08:53:05 +02:00
|
|
|
&& is_array($this->disabledItems[$value])
|
2012-10-17 11:06:02 +02:00
|
|
|
&& in_array($value2, $this->disabledItems[$value]) ){
|
|
|
|
$disabled = 'disabled="disabled"';
|
|
|
|
}
|
2008-10-15 14:39:09 +02:00
|
|
|
$selected = $value2 == $this->value ? " selected=\"selected\"" : "";
|
2012-10-17 11:06:02 +02:00
|
|
|
$options .= "<option$selected value=\"$value2\" $disabled>$title2</option>";
|
2008-08-26 01:32:12 +02:00
|
|
|
}
|
2008-10-15 14:39:09 +02:00
|
|
|
$options .= "</optgroup>";
|
|
|
|
} else { // Fall back to the standard dropdown field
|
2012-10-17 11:06:02 +02:00
|
|
|
$disabled = '';
|
|
|
|
if( in_array($value, $this->disabledItems) ){
|
|
|
|
$disabled = 'disabled="disabled"';
|
|
|
|
}
|
2008-10-15 14:39:09 +02:00
|
|
|
$selected = $value == $this->value ? " selected=\"selected\"" : "";
|
2012-10-17 11:06:02 +02:00
|
|
|
$options .= "<option$selected value=\"$value\" $disabled>$title</option>";
|
2008-08-26 01:32:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 02:19:57 +01:00
|
|
|
return FormField::create_tag('select', $this->getAttributes(), $options);
|
2008-01-20 09:58:04 +01:00
|
|
|
}
|
2012-08-16 23:31:13 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2012-08-16 23:31:13 +02:00
|
|
|
return 'groupeddropdown dropdown';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-21 04:15:03 +01:00
|
|
|
/**
|
|
|
|
* @todo Implement DropdownField::validate() with group validation support
|
|
|
|
*/
|
|
|
|
public function validate(Validator $validator) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-01-20 09:58:04 +01:00
|
|
|
|