1 <?php
2 3 4 5 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 $fieldName = $this->name;
19 $fieldNameID = $fieldName . 'ID';
20 return $this->controller->{$fieldNameID};
21 }
22
23 function saveInto(DataObject $record) {
24 $fieldName = $this->name;
25 $fieldNameID = $fieldName . 'ID';
26 $record->$fieldNameID = 0;
27 if($val = $this->value[ $this->htmlListField ]) {
28 if($val != 'undefined')
29 $record->$fieldNameID = $val;
30 }
31
32 $record->write();
33 }
34
35 function setOneToOne() {
36 $this->isOneToOne = true;
37 }
38
39 function isChildSet($childID) {
40 return DataObject::get($this->controllerClass(), '"' . $this->joinField . "\" = '$childID'");
41 }
42
43 function () {
44 $val = $this->getControllerJoinID() ? $this->getControllerJoinID() : '';
45 $inputId = $this->id() . '_' . $this->htmlListEndName;
46 return <<<HTML
47 <input id="$inputId" name="{$this->name}[{$this->htmlListField}]" type="hidden" value="$val"/>
48 HTML;
49 }
50 }
51
52 53 54 55 56
57 class HasOneComplexTableField_Item extends ComplexTableField_Item {
58
59 function MarkingCheckbox() {
60 $name = $this->parent->Name() . '[]';
61
62 $isOneToOne = $this->parent->isOneToOne;
63 $joinVal = $this->parent->getControllerJoinID();
64 $childID = $this->item->ID;
65
66 if($this->parent->IsReadOnly || ($isOneToOne && $joinVal != $childID && $this->parent->isChildSet($childID)))
67 return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
68 else if($joinVal == $childID)
69 return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\"/>";
70 else
71 return "<input class=\"radio\" type=\"radio\" name=\"$name\" value=\"{$this->item->ID}\"/>";
72 }
73 }
74
75 ?>
[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.
-