Version: current

     1 <?php
     2
     3 class HasOneFileDataObjectManager extends HasManyFileDataObjectManager {
     4
     5     /**
     6      * Most of the code below was copied from HasOneComplexTableField.
     7      * Painful, but necessary, until PHP supports multiple inheritance.
     8      */
     9
    10     public $itemClass = 'HasOneFileDataObjectManager_Item';
    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 = trim($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
    53 class HasOneFileDataObjectManager_Item extends FileDataObjectManager_Item {
    54
    55     function MarkingCheckbox() {
    56         $name = $this->parent->Name() . '[]';
    57
    58         $isOneToOne = $this->parent->isOneToOne;
    59         $joinVal = $this->parent->getControllerJoinID();
    60         $childID = $this->item->ID;
    61         $disabled = $this->parent->hasMarkingPermission() ? "" : "disabled='disabled'";
    62
    63         if($this->parent->IsReadOnly || ($isOneToOne && $joinVal != $childID && $this->parent->isChildSet($childID)))
    64             return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
    65         else if($joinVal == $childID)
    66             return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\" $disabled />";
    67         else
    68             return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\" $disabled />";
    69     }
    70 }
    71
    72
    73 ?>

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