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
|
|
|
}
|
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
|
|
|
}
|
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');
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/javascript/SelectionGroup.js');
|
2008-11-12 05:31:33 +01:00
|
|
|
Requirements::css(SAPPHIRE_DIR . '/css/SelectionGroup.css');
|
2012-04-11 08:07:55 +02:00
|
|
|
|
|
|
|
$obj = $properties ? $this->customise($properties) : $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-04-11 08:07:55 +02:00
|
|
|
return $obj->renderWith($this->template);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|