Version: current

     1 <?php
     2 /**
     3  * ComplexTableField with a radio button column, designed to edit a has_one join.
     4  * @package forms
     5  * @subpackage fields-relational
     6  */
     7 class HasOneComplexTableField extends HasManyComplexTableField {
     8
     9     public $itemClass = 'HasOneComplexTableField_Item';
    10
    11     public $isOneToOne = false;
    12
    13     function getParentIdName($parentClass, $childClass) {
    14         return $this->getParentIdNameRelation($parentClass, $childClass, 'has_one');
    15     }
    16
    17     function getControllerJoinID() {
    18         return $this->controller->{$this->joinField};
    19     }
    20
    21     function saveInto(DataObject $record) {
    22         $fieldName = $this->name;
    23         $fieldNameID = $fieldName . 'ID';
    24
    25         $record->$fieldNameID = 0;
    26         if($val = $this->value[ $this->htmlListField ]) {
    27             if($val != 'undefined')
    28                 $record->$fieldNameID = $val;
    29         }
    30
    31         $record->write();
    32     }
    33
    34     function setOneToOne() {
    35         $this->isOneToOne = true;
    36     }
    37
    38     function isChildSet($childID) {
    39         return DataObject::get($this->controllerClass(), '"' . $this->joinField . "\" = '$childID'");
    40     }
    41
    42     function ExtraData() {
    43         $val = $this->getControllerJoinID() ? $this->getControllerJoinID() : '';
    44         $inputId = $this->id() . '_' . $this->htmlListEndName;
    45         return <<<HTML
    46         <input id="$inputId" name="{$this->name}[{$this->htmlListField}]" type="hidden" value="$val"/>
    47 HTML;
    48     }
    49 }
    50
    51 /**
    52  * Single record of a {@link HasOneComplexTableField} field.
    53  * @package forms
    54  * @subpackage fields-relational
    55  */
    56 class HasOneComplexTableField_Item extends ComplexTableField_Item {
    57
    58     function MarkingCheckbox() {
    59         $name = $this->parent->Name() . '[]';
    60
    61         $isOneToOne = $this->parent->isOneToOne;
    62         $joinVal = $this->parent->getControllerJoinID();
    63         $childID = $this->item->ID;
    64
    65         if($this->parent->IsReadOnly || ($isOneToOne && $joinVal != $childID && $this->parent->isChildSet($childID)))
    66             return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
    67         else if($joinVal == $childID)
    68             return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\"/>";
    69         else
    70             return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\"/>";
    71     }
    72 }
    73
    74 ?>

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