2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
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;
|
|
|
|
|
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(
|
|
|
|
* new SelectionGroup_Item(
|
|
|
|
* 'one',
|
|
|
|
* new LiteralField('one', 'one view'),
|
|
|
|
* 'one title'
|
|
|
|
* ),
|
|
|
|
* new SelectionGroup_Item(
|
|
|
|
* 'two',
|
|
|
|
* new LiteralField('two', 'two view'),
|
|
|
|
* 'two title'
|
|
|
|
* ),
|
|
|
|
* );
|
|
|
|
* $field = new SelectionGroup('MyGroup', $items);
|
|
|
|
* </code>
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-structural
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class SelectionGroup extends CompositeField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new selection group.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-12-21 02:55:17 +01:00
|
|
|
* @param string $name The field name of the selection group.
|
|
|
|
* @param array $items The list of {@link SelectionGroup_Item}
|
|
|
|
* @param mixed $value
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-12-21 02:55:17 +01:00
|
|
|
public function __construct($name, $items, $value = null) {
|
|
|
|
if($value !== null) {
|
|
|
|
$this->setValue($value);
|
|
|
|
}
|
|
|
|
|
2012-11-30 15:06:49 +01:00
|
|
|
$selectionItems = array();
|
2013-12-21 02:55:17 +01:00
|
|
|
|
2012-11-30 15:06:49 +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;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-11-30 15:06:49 +01:00
|
|
|
$selectionItems[] = new SelectionGroup_Item($key, $item, $title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($selectionItems);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
$this->setName($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-12-22 12:38:51 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FieldSet() {
|
2011-12-22 12:38:51 +01:00
|
|
|
return $this->FieldList();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FieldList() {
|
2011-12-22 12:38:51 +01:00
|
|
|
$items = parent::FieldList()->toArray();
|
2007-07-19 12:40:28 +02:00
|
|
|
$count = 0;
|
2012-08-10 08:53:33 +02:00
|
|
|
$newItems = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-30 15:06:49 +01:00
|
|
|
foreach($items as $item) {
|
|
|
|
if($this->value == $item->getValue()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$firstSelected = " class=\"selected\"";
|
2012-11-30 15:06:49 +01:00
|
|
|
$checked = true;
|
|
|
|
} else {
|
|
|
|
$firstSelected = "";
|
|
|
|
$checked = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$itemID = $this->ID() . '_' . (++$count);
|
|
|
|
$extra = array(
|
2016-06-03 10:51:02 +02:00
|
|
|
"RadioButton" => DBField::create_field('HTMLFragment', FormField::create_tag(
|
2014-08-15 08:53:05 +02:00
|
|
|
'input',
|
2012-11-30 15:06:49 +01:00
|
|
|
array(
|
|
|
|
'class' => 'selector',
|
|
|
|
'type' => 'radio',
|
|
|
|
'id' => $itemID,
|
|
|
|
'name' => $this->name,
|
|
|
|
'value' => $item->getValue(),
|
2014-08-15 08:53:05 +02:00
|
|
|
'checked' => $checked
|
2012-11-30 15:06:49 +01:00
|
|
|
)
|
2016-06-03 10:51:02 +02:00
|
|
|
)),
|
|
|
|
"RadioLabel" => DBField::create_field('HTMLFragment', FormField::create_tag(
|
2014-08-15 08:53:05 +02:00
|
|
|
'label',
|
2012-11-30 15:06:49 +01:00
|
|
|
array('for' => $itemID),
|
|
|
|
$item->getTitle()
|
2016-06-03 10:51:02 +02:00
|
|
|
)),
|
2007-07-19 12:40:28 +02:00
|
|
|
"Selected" => $firstSelected,
|
|
|
|
);
|
2012-11-30 15:06:49 +01:00
|
|
|
$newItems[] = $item->customise($extra);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-05 12:40:24 +02:00
|
|
|
return new ArrayList($newItems);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function hasData() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FieldHolder($properties = array()) {
|
2009-12-13 06:22:31 +01:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR .'/jquery/jquery.js');
|
2016-04-19 11:04:43 +02:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/client/dist/js/SelectionGroup.js');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/SelectionGroup.css');
|
2012-04-11 08:07:55 +02:00
|
|
|
|
|
|
|
$obj = $properties ? $this->customise($properties) : $this;
|
2012-04-14 12:08:00 +02:00
|
|
|
|
|
|
|
return $obj->renderWith($this->getTemplates());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 15:06:49 +01:00
|
|
|
class SelectionGroup_Item extends CompositeField {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $value Form field identifier
|
2016-07-06 06:34:09 +02:00
|
|
|
* @param FormField|array $fields Contents of the option
|
2012-11-30 15:06:49 +01:00
|
|
|
* @param String $title Title to show for the radio button option
|
|
|
|
*/
|
|
|
|
function __construct($value, $fields = null, $title = null) {
|
2016-07-06 06:34:09 +02:00
|
|
|
$this->setValue($value);
|
|
|
|
if($fields && !is_array($fields)) {
|
|
|
|
$fields = array($fields);
|
|
|
|
}
|
2012-11-30 15:06:49 +01:00
|
|
|
|
|
|
|
parent::__construct($fields);
|
2016-07-06 06:34:09 +02:00
|
|
|
|
|
|
|
$this->setTitle($title ?: $value);
|
2012-11-30 15:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTitle() {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTitle($title) {
|
|
|
|
$this->title = $title;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getValue() {
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setValue($Value) {
|
|
|
|
$this->value = $Value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|