2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Convert;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set of radio buttons designed to emulate a dropdown.
|
2014-08-15 08:53:05 +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},
|
2010-10-15 05:55:22 +02:00
|
|
|
* so the constructor and arguments are in the same format.
|
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 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>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* You can use the helper functions on data object set to create the source array. eg:
|
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* //Database request for the object
|
2012-05-28 11:12:59 +02:00
|
|
|
* $map = FooBar::get()->map();
|
2016-09-09 08:43:05 +02:00
|
|
|
* // returns a Map object containing an array of ID => Title
|
2012-05-28 11:12:59 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* // Instantiate the OptionsetField
|
2012-05-28 11:12:59 +02:00
|
|
|
* $FieldList = new FieldList(
|
|
|
|
* new OptionsetField(
|
|
|
|
* $name = "Foobar",
|
|
|
|
* $title = "FooBar's optionset",
|
|
|
|
* $source = $map,
|
|
|
|
* $value = $map[0]
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* // Pass the fields to the form constructor. etc
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
class OptionsetField extends SingleSelectField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-22 07:11:58 +01:00
|
|
|
/**
|
2015-02-13 05:35:39 +01:00
|
|
|
* Build a field option for template rendering
|
|
|
|
*
|
|
|
|
* @param mixed $value Value of the option
|
|
|
|
* @param string $title Title of the option
|
|
|
|
* @param boolean $odd True if this should be striped odd. Otherwise it should be striped even
|
|
|
|
* @return ArrayData Field option
|
2010-02-22 07:11:58 +01:00
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
protected function getFieldOption($value, $title, $odd) {
|
|
|
|
return new ArrayData(array(
|
2015-12-21 05:57:07 +01:00
|
|
|
'ID' => $this->getOptionID($value),
|
|
|
|
'Class' => $this->getOptionClass($value, $odd),
|
|
|
|
'Name' => $this->getOptionName(),
|
2015-02-13 05:35:39 +01:00
|
|
|
'Value' => $value,
|
|
|
|
'Title' => $title,
|
2015-12-21 05:57:07 +01:00
|
|
|
'isChecked' => $this->isSelectedValue($value, $this->Value()),
|
|
|
|
'isDisabled' => $this->isDisabledValue($value)
|
2015-02-13 05:35:39 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-12-21 05:57:07 +01:00
|
|
|
/**
|
|
|
|
* Generate an ID property for a single option
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getOptionID($value) {
|
|
|
|
return $this->ID() . '_' . Convert::raw2htmlid($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the "name" property for each item in the list
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getOptionName() {
|
2015-02-13 05:35:39 +01:00
|
|
|
return $this->getName();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-12-21 05:57:07 +01:00
|
|
|
/**
|
|
|
|
* Get extra classes for each item in the list
|
|
|
|
*
|
|
|
|
* @param string $value Value of this item
|
|
|
|
* @param bool $odd If this item is odd numbered in the list
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getOptionClass($value, $odd) {
|
|
|
|
$oddClass = $odd ? 'odd' : 'even';
|
|
|
|
$valueClass = ' val' . Convert::raw2htmlid($value);
|
|
|
|
return $oddClass . $valueClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
public function Field($properties = array()) {
|
|
|
|
$options = array();
|
|
|
|
$odd = false;
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// Add all options striped
|
|
|
|
foreach($this->getSourceEmpty() as $value => $title) {
|
|
|
|
$odd = !$odd;
|
|
|
|
$options[] = $this->getFieldOption($value, $title, $odd);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
$properties = array_merge($properties, array(
|
|
|
|
'Options' => new ArrayList($options)
|
|
|
|
));
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2016-07-07 20:55:37 +02:00
|
|
|
return FormField::Field($properties);
|
2007-11-23 02:10:19 +01:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2015-12-14 17:41:18 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function validate($validator) {
|
2015-02-13 05:35:39 +01:00
|
|
|
if (!$this->Value()) {
|
2015-12-14 17:41:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::validate($validator);
|
|
|
|
}
|
|
|
|
|
2016-01-11 23:38:03 +01:00
|
|
|
public function getAttributes() {
|
|
|
|
$attributes = parent::getAttributes();
|
|
|
|
unset($attributes['name']);
|
|
|
|
unset($attributes['required']);
|
|
|
|
unset($attributes['role']);
|
2015-02-13 05:35:39 +01:00
|
|
|
|
2016-01-11 23:38:03 +01:00
|
|
|
return $attributes;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|