|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use SleepingOwl\Admin\Traits\FormElements; |
|
8
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
9
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
10
|
|
|
use SleepingOwl\Admin\Contracts\FormInterface; |
|
11
|
|
|
use SleepingOwl\Admin\Traits\VisibleCondition; |
|
12
|
|
|
use SleepingOwl\Admin\Contracts\DisplayInterface; |
|
13
|
|
|
use SleepingOwl\Admin\Contracts\Display\TabInterface; |
|
14
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @property TabInterface[]|DisplayTabsCollection $elements |
|
18
|
|
|
*/ |
|
19
|
|
|
class DisplayTabbed implements DisplayInterface, FormInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use FormElements, VisibleCondition; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $view = 'display.tabbed'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* DisplayTabbed constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param Closure|TabInterface[] $tabs |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($tabs = null) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->elements = new DisplayTabsCollection(); |
|
36
|
|
|
|
|
37
|
|
|
if (is_array($tabs) or is_callable($tabs)) { |
|
|
|
|
|
|
38
|
|
|
$this->setTabs($tabs); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function initialize() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->initializeElements(); |
|
45
|
|
|
|
|
46
|
|
|
$activeTabs = $this->getTabs()->filter(function (TabInterface $tab) { |
|
47
|
|
|
return $tab->isActive(); |
|
48
|
|
|
})->count(); |
|
49
|
|
|
|
|
50
|
|
|
if ($activeTabs === 0 and $firstTab = $this->getTabs()->first()) { |
|
|
|
|
|
|
51
|
|
|
$firstTab->setActive(true); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $class |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setModelClass($class) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->getTabs()->each(function (TabInterface $tab) use ($class) { |
|
61
|
|
|
if ($tab instanceof DisplayInterface) { |
|
62
|
|
|
$tab->setModelClass($class); |
|
63
|
|
|
} |
|
64
|
|
|
}); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return TabInterface[]|DisplayTabsCollection |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getTabs() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->getElements(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param Closure|TabInterface[] $tabs |
|
77
|
|
|
* |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setTabs($tabs) |
|
81
|
|
|
{ |
|
82
|
|
|
if (is_callable($tabs)) { |
|
83
|
|
|
$tabs = call_user_func($tabs, $this); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->setElements($tabs); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param array $elements |
|
91
|
|
|
* |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setElements(array $elements) |
|
95
|
|
|
{ |
|
96
|
|
|
foreach ($elements as $label => $tab) { |
|
97
|
|
|
if ($tab instanceof TabInterface) { |
|
98
|
|
|
$this->addElement($tab); |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->appendTab($tab, $label); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param Renderable $display |
|
109
|
|
|
* @param string $label |
|
110
|
|
|
* @param bool|false $active |
|
111
|
|
|
* |
|
112
|
|
|
* @return TabInterface |
|
113
|
|
|
*/ |
|
114
|
|
|
public function appendTab(Renderable $display, $label, $active = false) |
|
115
|
|
|
{ |
|
116
|
|
|
$tab = app('sleeping_owl.display')->tab($display)->setLabel($label)->setActive($active); |
|
117
|
|
|
|
|
118
|
|
|
$this->addElement($tab); |
|
119
|
|
|
|
|
120
|
|
|
return $tab; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param TabInterface $element |
|
125
|
|
|
* |
|
126
|
|
|
* @return $this |
|
127
|
|
|
*/ |
|
128
|
|
|
public function addElement(TabInterface $element) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->elements->push($element); |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $action |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setAction($action) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->getTabs()->each(function (TabInterface $tab) use ($action) { |
|
141
|
|
|
if ($tab instanceof FormInterface) { |
|
142
|
|
|
$tab->setAction($action); |
|
143
|
|
|
} |
|
144
|
|
|
}); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param int $id |
|
149
|
|
|
*/ |
|
150
|
|
|
public function setId($id) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->getTabs()->each(function (TabInterface $tab) use ($id) { |
|
153
|
|
|
if ($tab instanceof FormInterface) { |
|
154
|
|
|
$tab->setId($id); |
|
155
|
|
|
} |
|
156
|
|
|
}); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param ModelConfigurationInterface $model |
|
161
|
|
|
* |
|
162
|
|
|
* @return Validator|null |
|
163
|
|
|
*/ |
|
164
|
|
|
public function validateForm(ModelConfigurationInterface $model) |
|
165
|
|
|
{ |
|
166
|
|
|
foreach ($this->getTabs() as $tab) { |
|
167
|
|
|
if ($tab instanceof FormInterface) { |
|
168
|
|
|
$result = $tab->validateForm($model); |
|
169
|
|
|
if (! is_null($result)) { |
|
170
|
|
|
return $result; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param ModelConfigurationInterface $model |
|
178
|
|
|
*/ |
|
179
|
|
|
public function saveForm(ModelConfigurationInterface $model) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->getTabs()->each(function (TabInterface $tab) use ($model) { |
|
182
|
|
|
if ($tab instanceof FormInterface) { |
|
183
|
|
|
$tab->saveForm($model); |
|
184
|
|
|
} |
|
185
|
|
|
}); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return mixed |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getValue() |
|
192
|
|
|
{ |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return bool |
|
197
|
|
|
*/ |
|
198
|
|
|
public function isReadonly() |
|
199
|
|
|
{ |
|
200
|
|
|
return false; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getView() |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->view; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
|
|
public function toArray() |
|
215
|
|
|
{ |
|
216
|
|
|
return [ |
|
217
|
|
|
'tabs' => $this->getTabs()->onlyVisible(), |
|
218
|
|
|
]; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
|
223
|
|
|
*/ |
|
224
|
|
|
public function render() |
|
225
|
|
|
{ |
|
226
|
|
|
return app('sleeping_owl.template')->view( |
|
227
|
|
|
$this->getView(), |
|
228
|
|
|
$this->toArray() |
|
229
|
|
|
); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
|
|
public function __toString() |
|
236
|
|
|
{ |
|
237
|
|
|
return (string) $this->render(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Using in trait FormElements;. |
|
242
|
|
|
* |
|
243
|
|
|
* @param $object |
|
244
|
|
|
* |
|
245
|
|
|
* @return mixed |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function getElementContainer($object) |
|
248
|
|
|
{ |
|
249
|
|
|
return $object->getContent(); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return Model $model |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getModel() |
|
256
|
|
|
{ |
|
257
|
|
|
foreach ($this->getTabs() as $tab) { |
|
258
|
|
|
if ($tab->getContent() instanceof FormInterface) { |
|
259
|
|
|
return $tab->getContent()->getModel(); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.