2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
protected $template = "SelectionGroup";
|
|
|
|
|
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
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::css(SAPPHIRE_DIR . '/css/SelectionGroup.css');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-11-23 02:10:19 +01:00
|
|
|
/**
|
|
|
|
* Return a readonly version of this field. Keeps the composition but returns readonly
|
|
|
|
* versions of all the children
|
|
|
|
*/
|
|
|
|
public function performDisabledTransformation($trans) {
|
|
|
|
$newChildren = array();
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
if($clone->children) foreach($clone->getChildren() as $idx => $child) {
|
2007-11-23 02:10:19 +01:00
|
|
|
if(is_object($child)) {
|
|
|
|
$child = $child->transform($trans);
|
|
|
|
}
|
|
|
|
$newChildren[$idx] = $child;
|
|
|
|
}
|
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$clone->setChildren(new FieldList($newChildren));
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone->setReadonly(true);
|
|
|
|
return $clone;
|
2007-11-23 02:10:19 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function FieldSet() {
|
|
|
|
$items = parent::FieldSet()->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);
|
|
|
|
|
|
|
|
$firstSelected = $checked ="";
|
|
|
|
}
|
2011-05-05 12:40:24 +02:00
|
|
|
return new ArrayList($newItems);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasData() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function FieldHolder() {
|
2009-12-13 06:22:31 +01:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR .'/jquery/jquery.js');
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/javascript/SelectionGroup.js');
|
2008-11-12 05:31:33 +01:00
|
|
|
Requirements::css(SAPPHIRE_DIR . '/css/SelectionGroup.css');
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2011-04-16 05:09:30 +02:00
|
|
|
return $this->renderWith($this->template);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|