|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use KodiComponents\Support\HtmlAttributes; |
|
7
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
8
|
|
|
use SleepingOwl\Admin\Display\Column\Control; |
|
9
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
|
10
|
|
|
use SleepingOwl\Admin\Contracts\ColumnInterface; |
|
11
|
|
|
|
|
12
|
|
|
class Tree extends Extension implements Initializable, Renderable |
|
13
|
|
|
{ |
|
14
|
|
|
use HtmlAttributes, \SleepingOwl\Admin\Traits\Renderable; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $controlActive = true; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string|\Illuminate\View\View |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $view = 'display.columns'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Control |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $controlColumn; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->columns = new Collection(); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$this->setControlColumn(app('sleeping_owl.table.column')->treeControl()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param ColumnInterface $controlColumn |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setControlColumn(ColumnInterface $controlColumn) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->controlColumn = $controlColumn; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return Control |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getControlColumn() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->controlColumn; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
public function isControlActive() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->controlActive; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function enableControls() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->controlActive = true; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function disableControls() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->controlActive = true; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return Collection|\SleepingOwl\Admin\Contracts\ColumnInterface[] |
|
88
|
|
|
*/ |
|
89
|
|
|
public function all() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->columns; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return Collection|\SleepingOwl\Admin\Contracts\ColumnInterface[] |
|
96
|
|
|
*/ |
|
97
|
|
|
public function allWithControl() |
|
98
|
|
|
{ |
|
99
|
|
|
$columns = $this->all(); |
|
100
|
|
|
|
|
101
|
|
|
if ($this->isControlActive()) { |
|
102
|
|
|
$columns->push($this->getControlColumn()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $columns; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param ColumnInterface $column |
|
110
|
|
|
* |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
|
|
public function push(ColumnInterface $column) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->columns->push($column); |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the instance as an array. |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
public function toArray() |
|
126
|
|
|
{ |
|
127
|
|
|
return [ |
|
128
|
|
|
'columns' => $this->allWithControl(), |
|
129
|
|
|
'attributes' => $this->htmlAttributesToString(), |
|
130
|
|
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function initialize() |
|
134
|
|
|
{ |
|
135
|
|
|
$this->allWithControl()->each(function (ColumnInterface $column) { |
|
136
|
|
|
$column->initialize(); |
|
137
|
|
|
}); |
|
138
|
|
|
|
|
139
|
|
|
$this->setHtmlAttribute('class', 'table table-striped'); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: