|
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 SleepingOwl\Admin\Traits\VisibleCondition; |
|
10
|
|
|
use SleepingOwl\Admin\Contracts\Form\FormInterface; |
|
11
|
|
|
use SleepingOwl\Admin\Contracts\Display\TabInterface; |
|
12
|
|
|
use SleepingOwl\Admin\Contracts\Display\DisplayInterface; |
|
13
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @property TabInterface[]|DisplayTabsCollection $elements |
|
17
|
|
|
*/ |
|
18
|
|
|
class DisplayTabbed implements DisplayInterface, FormInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use FormElements, VisibleCondition, \SleepingOwl\Admin\Traits\Renderable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $view = 'display.tabbed'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* DisplayTabbed constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Closure|TabInterface[] $tabs |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($tabs = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->elements = new DisplayTabsCollection(); |
|
35
|
|
|
|
|
36
|
|
|
if (is_array($tabs) or is_callable($tabs)) { |
|
|
|
|
|
|
37
|
|
|
$this->setTabs($tabs); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Initialize tabbed interface. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function initialize() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->initializeElements(); |
|
47
|
|
|
|
|
48
|
|
|
$tabs = $this->getTabs(); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($tabs as $tab) { |
|
51
|
|
|
if (method_exists($tab->getContent(), 'getElements')) { |
|
52
|
|
|
$elements = $tab->getContent()->getElements(); |
|
53
|
|
|
foreach ($elements as $element) { |
|
54
|
|
|
if ($element instanceof self) { |
|
55
|
|
|
foreach ($element->getTabs() as $subTab) { |
|
56
|
|
|
if ($subTab->getName() == session('sleeping_owl_tab_id')) { |
|
57
|
|
|
$tab->setActive(true); |
|
58
|
|
|
$subTab->setActive(true); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($tab->getName() == session('sleeping_owl_tab_id')) { |
|
66
|
|
|
$tab->setActive(true); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$activeTabs = $this->getTabs()->filter(function (TabInterface $tab) { |
|
71
|
|
|
return $tab->isActive(); |
|
72
|
|
|
})->count(); |
|
73
|
|
|
|
|
74
|
|
|
if ($activeTabs === 0 and $firstTab = $this->getTabs()->first()) { |
|
|
|
|
|
|
75
|
|
|
$firstTab->setActive(true); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return Model $model|null |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getModel() |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($this->getTabs() as $tab) { |
|
85
|
|
|
if ($tab->getContent() instanceof FormInterface) { |
|
86
|
|
|
return $tab->getContent()->getModel(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $class |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setModelClass($class) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->getTabs()->each(function (TabInterface $tab) use ($class) { |
|
99
|
|
|
if ($tab instanceof DisplayInterface) { |
|
100
|
|
|
$tab->setModelClass($class); |
|
101
|
|
|
} |
|
102
|
|
|
}); |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return TabInterface[]|DisplayTabsCollection |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getTabs() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->getElements(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param Closure|TabInterface[] $tabs |
|
117
|
|
|
* |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setTabs($tabs) |
|
121
|
|
|
{ |
|
122
|
|
|
if (is_callable($tabs)) { |
|
123
|
|
|
$tabs = call_user_func($tabs, $this); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this->setElements($tabs); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param array $elements |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function setElements(array $elements) |
|
135
|
|
|
{ |
|
136
|
|
|
foreach ($elements as $label => $tab) { |
|
137
|
|
|
if ($tab instanceof TabInterface) { |
|
138
|
|
|
$this->addElement($tab); |
|
139
|
|
|
} else { |
|
140
|
|
|
$this->appendTab($tab, $label); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param Renderable $display |
|
149
|
|
|
* @param string $label |
|
150
|
|
|
* @param bool|false $active |
|
151
|
|
|
* |
|
152
|
|
|
* @return TabInterface |
|
153
|
|
|
*/ |
|
154
|
|
|
public function appendTab(Renderable $display, $label, $active = false) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->addElement( |
|
157
|
|
|
$tab = app('sleeping_owl.display')->tab($display, $label)->setActive($active) |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
return $tab; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param TabInterface $element |
|
165
|
|
|
* |
|
166
|
|
|
* @return $this |
|
167
|
|
|
*/ |
|
168
|
|
|
public function addElement(TabInterface $element) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->elements->push($element); |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param string $action |
|
177
|
|
|
* |
|
178
|
|
|
* @return $this |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setAction($action) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->getTabs()->each(function (TabInterface $tab) use ($action) { |
|
183
|
|
|
if ($tab instanceof FormInterface) { |
|
184
|
|
|
$tab->setAction($action); |
|
185
|
|
|
} |
|
186
|
|
|
}); |
|
187
|
|
|
|
|
188
|
|
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param int $id |
|
193
|
|
|
* |
|
194
|
|
|
* @return $this |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setId($id) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->getTabs()->each(function (TabInterface $tab) use ($id) { |
|
199
|
|
|
if ($tab instanceof FormInterface) { |
|
200
|
|
|
$tab->setId($id); |
|
201
|
|
|
} |
|
202
|
|
|
}); |
|
203
|
|
|
|
|
204
|
|
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param \Illuminate\Http\Request $request |
|
209
|
|
|
* @param ModelConfigurationInterface $model |
|
210
|
|
|
* |
|
211
|
|
|
* @return void |
|
212
|
|
|
*/ |
|
213
|
|
View Code Duplication |
public function validateForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null) |
|
|
|
|
|
|
214
|
|
|
{ |
|
215
|
|
|
$this->getTabs()->each(function ($tab) use ($request, $model) { |
|
216
|
|
|
$tabId = $request->get('sleeping_owl_tab_id'); |
|
217
|
|
|
|
|
218
|
|
|
if ($tab instanceof FormInterface && $tab->getName() == $tabId) { |
|
|
|
|
|
|
219
|
|
|
$tab->validateForm($request, $model); |
|
220
|
|
|
} |
|
221
|
|
|
}); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param \Illuminate\Http\Request $request |
|
226
|
|
|
* @param ModelConfigurationInterface $model |
|
227
|
|
|
* |
|
228
|
|
|
* @return void |
|
229
|
|
|
*/ |
|
230
|
|
View Code Duplication |
public function saveForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null) |
|
|
|
|
|
|
231
|
|
|
{ |
|
232
|
|
|
$this->getTabs()->each(function (TabInterface $tab) use ($request, $model) { |
|
233
|
|
|
$tabId = $request->get('sleeping_owl_tab_id'); |
|
234
|
|
|
|
|
235
|
|
|
if ($tab instanceof FormInterface && $tab->getName() == $tabId) { |
|
236
|
|
|
$tab->saveForm($request, $model); |
|
237
|
|
|
} |
|
238
|
|
|
}); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return null |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getValue() |
|
245
|
|
|
{ |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return bool |
|
250
|
|
|
*/ |
|
251
|
|
|
public function isReadonly() |
|
252
|
|
|
{ |
|
253
|
|
|
return false; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
|
|
public function isValueSkipped() |
|
260
|
|
|
{ |
|
261
|
|
|
return false; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return array |
|
266
|
|
|
*/ |
|
267
|
|
|
public function toArray() |
|
268
|
|
|
{ |
|
269
|
|
|
return [ |
|
270
|
|
|
'tabs' => $this->getTabs()->onlyVisible(), |
|
271
|
|
|
]; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Using in trait FormElements;. |
|
276
|
|
|
* |
|
277
|
|
|
* @param $object |
|
278
|
|
|
* |
|
279
|
|
|
* @return mixed |
|
280
|
|
|
*/ |
|
281
|
|
|
protected function getElementContainer($object) |
|
282
|
|
|
{ |
|
283
|
|
|
return $object->getContent(); |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
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.