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;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-06-03 10:51:02 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
2016-06-03 10:51:02 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Represents a number of fields which are selectable by a radio
|
|
|
|
* button that appears at the beginning of each item. Using CSS, you can
|
|
|
|
* configure the field to only display its contents if the corresponding radio
|
2012-11-30 15:06:49 +01:00
|
|
|
* button is selected. Each item is defined through {@link SelectionGroup_Item}.
|
|
|
|
*
|
|
|
|
* @example <code>
|
|
|
|
* $items = array(
|
2016-11-29 00:31:16 +01:00
|
|
|
* new SelectionGroup_Item(
|
|
|
|
* 'one',
|
|
|
|
* new LiteralField('one', 'one view'),
|
|
|
|
* 'one title'
|
|
|
|
* ),
|
|
|
|
* new SelectionGroup_Item(
|
|
|
|
* 'two',
|
|
|
|
* new LiteralField('two', 'two view'),
|
|
|
|
* 'two title'
|
|
|
|
* ),
|
2012-11-30 15:06:49 +01:00
|
|
|
* );
|
|
|
|
* $field = new SelectionGroup('MyGroup', $items);
|
|
|
|
* </code>
|
2016-09-15 04:48:38 +02:00
|
|
|
*
|
|
|
|
* Caution: The form field does not include any JavaScript or CSS when used outside of the CMS context,
|
|
|
|
* since the required frontend dependencies are included through CMS bundling.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class SelectionGroup extends CompositeField
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Create a new selection group.
|
|
|
|
*
|
|
|
|
* @param string $name The field name of the selection group.
|
|
|
|
* @param array $items The list of {@link SelectionGroup_Item}
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __construct($name, $items, $value = null)
|
|
|
|
{
|
|
|
|
if ($value !== null) {
|
|
|
|
$this->setValue($value);
|
|
|
|
}
|
2013-12-21 02:55:17 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$selectionItems = array();
|
2013-12-21 02:55:17 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($items as $key => $item) {
|
|
|
|
if ($item instanceof SelectionGroup_Item) {
|
|
|
|
$selectionItems[] = $item;
|
|
|
|
} else {
|
|
|
|
// Convert legacy format
|
|
|
|
if (strpos($key, '//') !== false) {
|
|
|
|
list($key,$title) = explode('//', $key, 2);
|
|
|
|
} else {
|
|
|
|
$title = null;
|
|
|
|
}
|
|
|
|
$selectionItems[] = new SelectionGroup_Item($key, $item, $title);
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 15:06:49 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
parent::__construct($selectionItems);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$this->setName($name);
|
|
|
|
}
|
2011-12-22 12:38:51 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function FieldSet()
|
|
|
|
{
|
|
|
|
return $this->FieldList();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function FieldList()
|
|
|
|
{
|
|
|
|
$items = parent::FieldList()->toArray();
|
|
|
|
$count = 0;
|
|
|
|
$newItems = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/** @var SelectionGroup_Item $item */
|
|
|
|
foreach ($items as $item) {
|
|
|
|
if ($this->value == $item->getValue()) {
|
|
|
|
$firstSelected = true;
|
|
|
|
$checked = true;
|
|
|
|
} else {
|
|
|
|
$firstSelected = false;
|
|
|
|
$checked = false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$itemID = $this->ID() . '_' . (++$count);
|
|
|
|
// @todo Move into SelectionGroup_Item.ss template at some point.
|
|
|
|
$extra = array(
|
|
|
|
"RadioButton" => DBField::create_field('HTMLFragment', FormField::create_tag(
|
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'class' => 'selector',
|
|
|
|
'type' => 'radio',
|
|
|
|
'id' => $itemID,
|
|
|
|
'name' => $this->name,
|
|
|
|
'value' => $item->getValue(),
|
|
|
|
'checked' => $checked,
|
|
|
|
'aria-labelledby' => "title-{$itemID}",
|
|
|
|
)
|
|
|
|
)),
|
|
|
|
"RadioLabel" => DBField::create_field('HTMLFragment', FormField::create_tag(
|
|
|
|
'label',
|
|
|
|
array(
|
|
|
|
'id' => "title-{$itemID}",
|
|
|
|
'for' => $itemID
|
|
|
|
),
|
|
|
|
$item->getTitle()
|
|
|
|
)),
|
|
|
|
"Selected" => $firstSelected,
|
|
|
|
);
|
|
|
|
$newItems[] = $item->customise($extra);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return new ArrayList($newItems);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function hasData()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function FieldHolder($properties = array())
|
|
|
|
{
|
|
|
|
return parent::FieldHolder($properties);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|