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
|
2012-03-19 03:27:52 +01:00
|
|
|
* $myDoSet = DataList::create("FooBars","");
|
2010-10-15 05:55:22 +02:00
|
|
|
* if($myDoSet){
|
|
|
|
* // This returns an array of ID => Title
|
2012-03-19 03:27:52 +01:00
|
|
|
* $map = $myDoSet->map();
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
|
|
|
* // Instantiate the OptionsetField
|
2011-10-28 03:37:27 +02:00
|
|
|
* $FieldList = new FieldList(
|
2010-10-15 05:55:22 +02:00
|
|
|
* 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 {
|
2011-12-21 17:35:42 +01:00
|
|
|
|
|
|
|
protected $template = 'OptionsetField';
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2010-02-22 07:11:58 +01:00
|
|
|
/**
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected $disabledItems = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2011-03-23 05:12:25 +01:00
|
|
|
function Field($properties = array()) {
|
2008-10-15 14:39:09 +02:00
|
|
|
$source = $this->getSource();
|
2011-03-23 05:12:25 +01:00
|
|
|
$odd = 0;
|
|
|
|
$options = array();
|
|
|
|
if($source) {
|
|
|
|
foreach($source as $value => $title) {
|
|
|
|
$itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value);
|
|
|
|
$odd = ($odd + 1) % 2;
|
|
|
|
$extraClass = $odd ? 'odd' : 'even';
|
|
|
|
$extraClass .= ' val' . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $value);
|
|
|
|
|
|
|
|
$options[] = new ArrayData(array(
|
|
|
|
'ID' => $itemID,
|
|
|
|
'Class' => $extraClass,
|
|
|
|
'Name' => $this->name,
|
|
|
|
'Value' => $value,
|
|
|
|
'Title' => $title,
|
|
|
|
'isChecked' => $value == $this->value,
|
|
|
|
'isDisabled' => $this->disabled || in_array($value, $this->disabledItems),
|
|
|
|
));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
|
|
|
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
|
|
|
|
|
2011-12-21 17:35:42 +01:00
|
|
|
return $this->customise($properties)->renderWith($this->getTemplate());
|
2007-11-23 02:10:19 +01:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
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();
|
2011-03-23 05:12:25 +01:00
|
|
|
$field = new LookupField($this->name, $this->title ? $this->title : '', $items, $this->value);
|
2007-07-19 12:40:28 +02:00
|
|
|
$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;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2010-02-22 07:11:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
function getDisabledItems() {
|
|
|
|
return $this->disabledItems;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function ExtraOptions() {
|
2011-05-05 12:40:24 +02:00
|
|
|
return new ArrayList();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|