1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Tabulator; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\Forms\FormField; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Inspired by https://github.com/silvershop/silverstripe-hasonefield |
11
|
|
|
* but using TabulatorGrid under the hood |
12
|
|
|
*/ |
13
|
|
|
class HasOneTabulatorField extends TabulatorGrid |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The related object to the parent |
17
|
|
|
*/ |
18
|
|
|
protected DataObject $record; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The current parent of the relationship (the base object we are editing) |
22
|
|
|
*/ |
23
|
|
|
protected DataObject $parent; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The name of the relation this field is managing |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected string $relation; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* HasOneButtonField constructor. |
34
|
|
|
* @param \SilverStripe\ORM\DataObject $parent |
35
|
|
|
* @param string $relationName |
36
|
|
|
* @param string|null $fieldName |
37
|
|
|
* @param string|null $title |
38
|
|
|
*/ |
39
|
|
|
public function __construct(DataObject $parent, string $relationName, string $fieldName = null, string $title = null) |
40
|
|
|
{ |
41
|
|
|
$record = $parent->{$relationName}(); |
42
|
|
|
$this->setRecord($record); |
43
|
|
|
$this->parent = $parent; |
44
|
|
|
$this->relation = $relationName; |
45
|
|
|
|
46
|
|
|
$list = HasOneButtonRelationList::create($parent, $this->record, $relationName); |
47
|
|
|
|
48
|
|
|
parent::__construct($fieldName ?: $relationName, $title, $list); |
49
|
|
|
$this->setModelClass($record->ClassName); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setValue($value, $data = null) |
53
|
|
|
{ |
54
|
|
|
if ($value instanceof DataObject) { |
55
|
|
|
$value = HasOneButtonRelationList::create($this->parent, $value, $this->relation); |
56
|
|
|
} |
57
|
|
|
return parent::setValue($value, $data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function isViewOnly(): bool |
61
|
|
|
{ |
62
|
|
|
return !$this->record->canEdit(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
*/ |
67
|
|
|
public function getRecord(): DataObject |
68
|
|
|
{ |
69
|
|
|
return $this->record; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
*/ |
74
|
|
|
public function setRecord(DataObject $record = null) |
75
|
|
|
{ |
76
|
|
|
$this->record = $record ?: singleton(get_class($this->record)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the current parent |
81
|
|
|
*/ |
82
|
|
|
public function getParent(): DataObject |
83
|
|
|
{ |
84
|
|
|
return $this->parent; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the current parent |
89
|
|
|
* |
90
|
|
|
* @param DataObject $parent parent of the relationship |
91
|
|
|
*/ |
92
|
|
|
public function setParent(DataObject $parent): self |
93
|
|
|
{ |
94
|
|
|
$this->parent = $parent; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the name of the relation this field is managing |
100
|
|
|
*/ |
101
|
|
|
public function getRelation(): string |
102
|
|
|
{ |
103
|
|
|
return $this->relation; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the name of the relation this field is managing |
108
|
|
|
* |
109
|
|
|
* @param string $relation The relation name |
110
|
|
|
*/ |
111
|
|
|
public function setRelation(string $relation): self |
112
|
|
|
{ |
113
|
|
|
$this->relation = $relation; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function FieldHolder($properties = []) |
118
|
|
|
{ |
119
|
|
|
return FormField::FieldHolder($properties); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function Field($properties = []) |
123
|
|
|
{ |
124
|
|
|
return FormField::Field($properties); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getEditLink(): string |
128
|
|
|
{ |
129
|
|
|
return Controller::join_links($this->Link('item'), $this->record->ID); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|