Passed
Push — master ( 47e01d...760836 )
by Thomas
12:05
created

HasOneTabulatorField::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 11
rs 10
cc 2
nc 1
nop 4
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
    /**
61
     */
62
    public function getRecord(): DataObject
63
    {
64
        return $this->record;
65
    }
66
67
    /**
68
     */
69
    public function setRecord(DataObject $record = null)
70
    {
71
        $this->record = $record ?: singleton(get_class($this->record));
72
    }
73
74
    /**
75
     * Get the current parent
76
     */
77
    public function getParent(): DataObject
78
    {
79
        return $this->parent;
80
    }
81
82
    /**
83
     * Set the current parent
84
     *
85
     * @param DataObject $parent parent of the relationship
86
     */
87
    public function setParent(DataObject $parent): self
88
    {
89
        $this->parent = $parent;
90
        return $this;
91
    }
92
93
    /**
94
     * Get the name of the relation this field is managing
95
     */
96
    public function getRelation(): string
97
    {
98
        return $this->relation;
99
    }
100
101
    /**
102
     * Set the name of the relation this field is managing
103
     *
104
     * @param string $relation The relation name
105
     */
106
    public function setRelation(string $relation): self
107
    {
108
        $this->relation = $relation;
109
        return $this;
110
    }
111
112
    public function FieldHolder($properties = [])
113
    {
114
        return FormField::FieldHolder($properties);
115
    }
116
117
    public function Field($properties = [])
118
    {
119
        return FormField::Field($properties);
120
    }
121
122
    public function getEditLink(): string
123
    {
124
        return Controller::join_links($this->Link('item'), $this->record->ID);
125
    }
126
}
127