2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-14 07:32:29 +02:00
|
|
|
* SelectionGroup represents a number of fields that 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
|
|
|
|
* button is selected.
|
|
|
|
*
|
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 {
|
2011-04-16 05:09:30 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new selection group.
|
|
|
|
* @param name The field name of the selection group.
|
|
|
|
* @param items The list of items to show. This should be a map. The keys will be the radio
|
|
|
|
* button option names, and the values should be the associated field to display. If you want,
|
|
|
|
* you can make this field a composite field.
|
|
|
|
*
|
|
|
|
* If you want to a have a title that is different from the value of the key, you can express it
|
|
|
|
* as "InternalVal//This is the Title"
|
|
|
|
*/
|
|
|
|
function __construct($name, $items) {
|
|
|
|
$this->name = $name;
|
2007-09-27 22:56:55 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct($items);
|
2007-09-27 22:56:55 +02:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/SelectionGroup.css');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-12-22 12:38:51 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function FieldSet() {
|
2011-12-22 12:38:51 +01:00
|
|
|
return $this->FieldList();
|
|
|
|
}
|
|
|
|
|
|
|
|
function FieldList() {
|
|
|
|
$items = parent::FieldList()->toArray();
|
2008-01-22 04:51:24 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$count = 0;
|
2008-01-22 04:51:24 +01:00
|
|
|
$firstSelected = $checked ="";
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($items as $key => $item) {
|
|
|
|
if(strpos($key,'//') !== false) {
|
|
|
|
list($key,$title) = explode('//', $key,2);
|
|
|
|
} else {
|
|
|
|
$title = $key;
|
|
|
|
}
|
|
|
|
if($this->value == $key) {
|
|
|
|
$firstSelected = " class=\"selected\"";
|
|
|
|
$checked = " checked=\"checked\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
$itemID = $this->ID() . '_' . (++$count);
|
|
|
|
$extra = array(
|
|
|
|
"RadioButton" => "<input class=\"selector\" type=\"radio\" id=\"$itemID\" name=\"$this->name\" value=\"$key\"$checked />",
|
|
|
|
"RadioLabel" => "<label for=\"$itemID\">$title</label>",
|
|
|
|
"Selected" => $firstSelected,
|
|
|
|
);
|
|
|
|
if(is_object($item)) $newItems[] = $item->customise($extra);
|
|
|
|
else $newItems[] = new ArrayData($extra);
|
|
|
|
|
2012-04-12 02:24:06 +02:00
|
|
|
$firstSelected = $checked ="";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-04-14 07:32:29 +02:00
|
|
|
|
2011-05-05 12:40:24 +02:00
|
|
|
return new ArrayList($newItems);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasData() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-11 08:07:55 +02:00
|
|
|
function FieldHolder($properties = array()) {
|
2009-12-13 06:22:31 +01:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR .'/jquery/jquery.js');
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/SelectionGroup.js');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/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
|
|
|
}
|
|
|
|
}
|
|
|
|
|