Version: current

     1 <?php
     2 /**
     3  * Set of radio buttons designed to emulate a dropdown.
     4  * It even uses the same constructor as a dropdown field.
     5  * @package forms
     6  * @subpackage fields-basic
     7  */
     8 class OptionsetField extends DropdownField {
     9
    10     /**
    11      * @var Array
    12      */
    13     protected $disabledItems = array();
    14
    15     /**
    16      * Creates a new optionset field.
    17      * @param name The field name
    18      * @param title The field title
    19      * @param source An map of the dropdown items
    20      * @param value The current value
    21      * @param form The parent form
    22      */
    23     function __construct($name, $title = "", $source = array(), $value = "", $form = null) {
    24         parent::__construct($name, $title, $source, $value, $form);
    25     }
    26
    27     /**
    28      * Create a UL tag containing sets of radio buttons and labels.  The IDs are set to
    29      * FieldID_ItemKey, where ItemKey is the key with all non-alphanumerics removed.
    30      *
    31      * @todo Should use CheckboxField FieldHolder rather than constructing own markup.
    32      */
    33     function Field() {
    34         $options = array();
    35         $odd = 0;
    36         $source = $this->getSource();
    37         foreach($source as $value => $title) {
    38             $value_lat = Convert::rus2lat($value);
    39             $itemID = $this->id() . "_" . ereg_replace('[^a-zA-Z0-9]+','', $value_lat);
    40
    41             $odd = ($odd + 1) % 2;
    42             $extraClass = $odd ? "odd" : "even";
    43             $extraClass .= " val" . preg_replace('/[^a-zA-Z0-9\-\_]/','_', $value_lat);
    44             $checked = ($value == $this->value) ? 1 : 0;
    45             $disabled = ($this->disabled || in_array($value, $this->disabledItems)) ? 1 : 0;
    46
    47             $options[] = new ArrayData(array(
    48                 'ID' => $itemID,
    49                 'Class' => $extraClass,
    50                 'Name' => $this->name,
    51                 'Value' => $value,
    52                 'Title' => $title,
    53                 'isChecked' => $value == $this->value,
    54                 'isDisabled' => $disabled,
    55             ));
    56         }
    57         $attributes = array(
    58             'class' => "optionset {$this->extraClass()}",
    59             'id' => $this->id(),
    60         );
    61         return $this->createTag('input', $attributes, new DataObjectSet($options));
    62     }
    63
    64     function performReadonlyTransformation() {
    65         // Source and values are DataObject sets.
    66         $items = $this->getSource();
    67         $field = new LookupField($this->name,$this->title ? $this->title : "" ,$items,$this->value);
    68         $field->setForm($this->form);
    69         $field->setReadonly(true);
    70         return $field;
    71     }
    72
    73     /**
    74      * Mark certain elements as disabled,
    75      * regardless of the {@link setDisabled()} settings.
    76      *
    77      * @param array $items Collection of array keys, as defined in the $source array
    78      */
    79     function setDisabledItems($items) {
    80         $this->disabledItems = $items;
    81     }
    82
    83     /**
    84      * @return Array
    85      */
    86     function getDisabledItems() {
    87         return $this->disabledItems;
    88     }
    89
    90     function ExtraOptions() {
    91         return new DataObjectSet();
    92     }
    93 }
    94 ?>

[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. -