1 <?php
2 3 4 5 6 7 8
9 class SelectionGroup extends CompositeField {
10 11 12 13 14 15 16 17 18 19
20 function __construct($name, $items) {
21 $this->name = $name;
22
23 parent::__construct($items);
24
25 Requirements::css(SAPPHIRE_DIR . '/css/SelectionGroup.css');
26 }
27
28 29 30 31
32 public function performDisabledTransformation($trans) {
33 $newChildren = array();
34 $clone = clone $this;
35 if($clone->children) foreach($clone->getChildren() as $idx => $child) {
36 if(is_object($child)) {
37 $child = $child->transform($trans);
38 }
39 $newChildren[$idx] = $child;
40 }
41
42 $clone->setChildren(new FieldSet($newChildren));
43 $clone->setReadonly(true);
44 return $clone;
45 }
46
47 function FieldSet() {
48 $items = parent::FieldSet()->toArray();
49
50 $count = 0;
51 $firstSelected = $checked ="";
52 foreach($items as $key => $item) {
53 if(strpos($key,'//') !== false) {
54 list($key,$title) = explode('//', $key,2);
55 } else {
56 $title = $key;
57 }
58 if($this->value == $key) {
59 $firstSelected = " class=\"selected\"";
60 $checked = " checked=\"checked\"";
61 }
62
63 $itemID = $this->ID() . '_' . (++$count);
64 $extra = array(
65 "RadioButton" => "<input class=\"selector\" type=\"radio\" id=\"$itemID\" name=\"$this->name\" value=\"$key\"$checked />",
66 "RadioLabel" => "<label class=\"selector\" for=\"$itemID\">$title</label>",
67 "Selected" => $firstSelected,
68 );
69 if(is_object($item)) $newItems[] = $item->customise($extra);
70 else $newItems[] = new ArrayData($extra);
71
72 $firstSelected = $checked ="";
73 }
74 return new DataObjectSet($newItems);
75 }
76
77 function hasData() {
78 return true;
79 }
80
81 function FieldHolder() {
82 Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
83 Requirements::javascript(SAPPHIRE_DIR . '/javascript/SelectionGroup.js');
84 Requirements::css(SAPPHIRE_DIR . '/css/SelectionGroup.css');
85
86 return $this->renderWith("SelectionGroup");
87 }
88 }
89
90 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-