Version: current

     1 <?php
     2 /**
     3  * SelectionGroup represents a number of fields that are selectable by a radio button that appears at
     4  * the beginning of each item.  Using CSS, you can configure the field to only display its contents if
     5  * the corresponding radio button is selected.
     6  * @package forms
     7  * @subpackage fields-structural
     8  */
     9 class SelectionGroup extends CompositeField {
    10     /**
    11      * Create a new selection group.
    12      * @param name The field name of the selection group.
    13      * @param items The list of items to show.  This should be a map.  The keys will be the radio
    14      * button option names, and the values should be the associated field to display.  If you want,
    15      * you can make this field a composite field.
    16      *
    17      * If you want to a have a title that is different from the value of the key, you can express it
    18      * as "InternalVal//This is the Title"
    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      * Return a readonly version of this field.  Keeps the composition but returns readonly
    30      * versions of all the children
    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. -