2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Set of radio buttons designed to emulate a dropdown.
|
|
|
|
* It even uses the same constructor as a dropdown field.
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
|
|
|
* This field allows you to ensure that a form element is submitted is not optional and is part of a fixed set of
|
|
|
|
* data. This field uses the input type of radio. It's a direct subclass of {@link DropdownField},
|
|
|
|
* so the constructor and arguments are in the same format.
|
|
|
|
*
|
|
|
|
* <b>Usage</b>
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* new OptionsetField(
|
|
|
|
* $name = "Foobar",
|
|
|
|
* $title = "FooBar's optionset",
|
|
|
|
* $source = array(
|
|
|
|
* "1" => "Option 1",
|
|
|
|
* "2" => "Option 2",
|
|
|
|
* "3" => "Option 3",
|
|
|
|
* "4" => "Option 4",
|
|
|
|
* "5" => "Option 5"
|
|
|
|
* ),
|
|
|
|
* $value = "1"
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* You can use the helper functions on data object set to create the source array. eg:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* //Database request for the object
|
|
|
|
* $myDoSet = DataObject::get("FooBars","");
|
|
|
|
* if($myDoSet){
|
|
|
|
* // This returns an array of ID => Title
|
|
|
|
* $map = $myDoSet->toDropDownMap();
|
|
|
|
*
|
|
|
|
* // Instantiate the OptionsetField
|
|
|
|
* $fieldset = new Fieldset(
|
|
|
|
* new OptionsetField(
|
|
|
|
* $name = "Foobar",
|
|
|
|
* $title = "FooBar's optionset",
|
|
|
|
* $source = $map,
|
|
|
|
* $value = $map[0]
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // Pass the fields to the form constructor. etc
|
|
|
|
* </code>
|
|
|
|
*
|
2010-10-19 00:50:48 +02:00
|
|
|
* @see CheckboxSetField for multiple selections through checkboxes instead.
|
|
|
|
* @see DropdownField for a simple <select> field with a single element.
|
|
|
|
* @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class OptionsetField extends DropdownField {
|
|
|
|
|
2010-02-22 07:11:58 +01:00
|
|
|
/**
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected $disabledItems = array();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Creates a new optionset field.
|
|
|
|
* @param name The field name
|
|
|
|
* @param title The field title
|
|
|
|
* @param source An map of the dropdown items
|
|
|
|
* @param value The current value
|
|
|
|
* @param form The parent form
|
|
|
|
*/
|
|
|
|
function __construct($name, $title = "", $source = array(), $value = "", $form = null) {
|
|
|
|
parent::__construct($name, $title, $source, $value, $form);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-09 05:18:36 +01:00
|
|
|
* Create a UL tag containing sets of radio buttons and labels. The IDs are set to
|
2007-07-19 12:40:28 +02:00
|
|
|
* FieldID_ItemKey, where ItemKey is the key with all non-alphanumerics removed.
|
2008-08-11 01:03:35 +02:00
|
|
|
*
|
|
|
|
* @todo Should use CheckboxField FieldHolder rather than constructing own markup.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function Field() {
|
|
|
|
$options = '';
|
|
|
|
$odd = 0;
|
2008-10-15 14:39:09 +02:00
|
|
|
$source = $this->getSource();
|
|
|
|
foreach($source as $key => $value) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$itemID = $this->id() . "_" . ereg_replace('[^a-zA-Z0-9]+','',$key);
|
|
|
|
|
|
|
|
if($key == $this->value/* || $useValue */) {
|
|
|
|
$useValue = false;
|
|
|
|
$checked = " checked=\"checked\"";
|
|
|
|
} else {
|
|
|
|
$checked="";
|
|
|
|
}
|
|
|
|
|
|
|
|
$odd = ($odd + 1) % 2;
|
|
|
|
$extraClass = $odd ? "odd" : "even";
|
2008-04-15 11:21:45 +02:00
|
|
|
$extraClass .= " val" . preg_replace('/[^a-zA-Z0-9\-\_]/','_', $key);
|
2010-02-23 05:48:56 +01:00
|
|
|
$disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? 'disabled="disabled"' : '';
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-11 01:03:35 +02:00
|
|
|
$options .= "<li class=\"".$extraClass."\"><input id=\"$itemID\" name=\"$this->name\" type=\"radio\" value=\"$key\"$checked $disabled class=\"radio\" /> <label for=\"$itemID\">$value</label></li>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
$id = $this->id();
|
2007-11-25 22:19:30 +01:00
|
|
|
return "<ul id=\"$id\" class=\"optionset {$this->extraClass()}\">\n$options</ul>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-11-23 02:10:19 +01:00
|
|
|
|
|
|
|
protected $disabled = false;
|
|
|
|
function setDisabled($val) {
|
|
|
|
$this->disabled = $val;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function performReadonlyTransformation() {
|
|
|
|
// Source and values are DataObject sets.
|
2008-10-15 14:39:09 +02:00
|
|
|
$items = $this->getSource();
|
2007-07-19 12:40:28 +02:00
|
|
|
$field = new LookupField($this->name,$this->title ? $this->title : "" ,$items,$this->value);
|
|
|
|
$field->setForm($this->form);
|
2008-08-12 04:58:48 +02:00
|
|
|
$field->setReadonly(true);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2010-02-22 07:11:58 +01:00
|
|
|
/**
|
|
|
|
* Mark certain elements as disabled,
|
|
|
|
* regardless of the {@link setDisabled()} settings.
|
|
|
|
*
|
|
|
|
* @param array $items Collection of array keys, as defined in the $source array
|
|
|
|
*/
|
|
|
|
function setDisabledItems($items) {
|
|
|
|
$this->disabledItems = $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
function getDisabledItems() {
|
|
|
|
return $this->disabledItems;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function ExtraOptions() {
|
|
|
|
return new DataObjectSet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|