1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
trait Tabs |
6
|
|
|
{ |
7
|
|
|
public function enableTabs() |
8
|
|
|
{ |
9
|
|
|
$this->setOperationSetting('tabsEnabled', true); |
|
|
|
|
10
|
|
|
$this->setOperationSetting('tabsType', config('backpack.crud.operations.'.$this->getCurrentOperation().'.tabsType', 'horizontal')); |
|
|
|
|
11
|
|
|
|
12
|
|
|
return $this->tabsEnabled(); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function disableTabs() |
16
|
|
|
{ |
17
|
|
|
$this->setOperationSetting('tabsEnabled', false); |
18
|
|
|
|
19
|
|
|
return $this->tabsEnabled(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
|
|
public function tabsEnabled() |
26
|
|
|
{ |
27
|
|
|
return $this->getOperationSetting('tabsEnabled'); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function tabsDisabled() |
34
|
|
|
{ |
35
|
|
|
return ! $this->tabsEnabled(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setTabsType($type) |
39
|
|
|
{ |
40
|
|
|
$this->enableTabs(); |
41
|
|
|
$this->setOperationSetting('tabsType', $type); |
42
|
|
|
|
43
|
|
|
return $this->getOperationSetting('tabsType'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getTabsType() |
50
|
|
|
{ |
51
|
|
|
return $this->getOperationSetting('tabsType'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function enableVerticalTabs() |
55
|
|
|
{ |
56
|
|
|
return $this->setTabsType('vertical'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function disableVerticalTabs() |
60
|
|
|
{ |
61
|
|
|
return $this->setTabsType('horizontal'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function enableHorizontalTabs() |
65
|
|
|
{ |
66
|
|
|
return $this->setTabsType('horizontal'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function disableHorizontalTabs() |
70
|
|
|
{ |
71
|
|
|
return $this->setTabsType('vertical'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $label |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function tabExists($label) |
79
|
|
|
{ |
80
|
|
|
$tabs = $this->getTabs(); |
81
|
|
|
|
82
|
|
|
return in_array($label, $tabs); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return bool|string |
87
|
|
|
*/ |
88
|
|
|
public function getLastTab() |
89
|
|
|
{ |
90
|
|
|
$tabs = $this->getTabs(); |
91
|
|
|
|
92
|
|
|
if (count($tabs)) { |
93
|
|
|
return last($tabs); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $label |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function isLastTab($label) |
104
|
|
|
{ |
105
|
|
|
return $this->getLastTab() == $label; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @deprecated Do not use this method as it will be removed in future versions! |
110
|
|
|
* Instead, use $this->getElementsWithoutATab($this->getCurrentFields()) |
111
|
|
|
* |
112
|
|
|
* @return \Illuminate\Support\Collection |
113
|
|
|
*/ |
114
|
|
|
public function getFieldsWithoutATab() |
115
|
|
|
{ |
116
|
|
|
return $this->getElementsWithoutATab($this->getCurrentFields()); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return \Illuminate\Support\Collection |
121
|
|
|
*/ |
122
|
|
|
public function getElementsWithoutATab(array $elements) |
123
|
|
|
{ |
124
|
|
|
return collect($elements)->filter(function ($value) { |
|
|
|
|
125
|
|
|
return ! isset($value['tab']); |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @deprecated Do not use this method as it will be removed in future versions! |
131
|
|
|
* Instead, use $this->getTabItems($tabLabel, 'fields') |
132
|
|
|
* |
133
|
|
|
* @return array|\Illuminate\Support\Collection |
134
|
|
|
*/ |
135
|
|
|
public function getTabFields(string $tabLabel) |
136
|
|
|
{ |
137
|
|
|
return $this->getTabItems($tabLabel, 'fields'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array|\Illuminate\Support\Collection |
142
|
|
|
*/ |
143
|
|
|
public function getTabItems(string $tabLabel, string $source) |
144
|
|
|
{ |
145
|
|
|
if (in_array($tabLabel, $this->getUniqueTabNames($source))) { |
146
|
|
|
$items = $this->getCurrentItems($source); |
147
|
|
|
|
148
|
|
|
return collect($items)->filter(function ($value) use ($tabLabel) { |
|
|
|
|
149
|
|
|
return isset($value['tab']) && $value['tab'] == $tabLabel; |
150
|
|
|
}); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return []; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getTabs(): array |
157
|
|
|
{ |
158
|
|
|
return $this->getUniqueTabNames('fields'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* $source could be `fields` or `columns` for now. |
163
|
|
|
*/ |
164
|
|
|
public function getUniqueTabNames(string $source): array |
165
|
|
|
{ |
166
|
|
|
$tabs = []; |
167
|
|
|
$items = $this->getCurrentItems($source); |
168
|
|
|
|
169
|
|
|
collect($items) |
|
|
|
|
170
|
|
|
->filter(function ($value) { |
171
|
|
|
return isset($value['tab']); |
172
|
|
|
}) |
173
|
|
|
->each(function ($value) use (&$tabs) { |
174
|
|
|
if (! in_array($value['tab'], $tabs)) { |
175
|
|
|
$tabs[] = $value['tab']; |
176
|
|
|
} |
177
|
|
|
}); |
178
|
|
|
|
179
|
|
|
return $tabs; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
private function getCurrentItems(string $source): array |
183
|
|
|
{ |
184
|
|
|
$items = []; |
185
|
|
|
|
186
|
|
|
switch ($source) { |
187
|
|
|
case 'fields': |
188
|
|
|
$items = $this->getCurrentFields(); |
189
|
|
|
break; |
190
|
|
|
case 'columns': |
191
|
|
|
$items = $this->columns(); |
|
|
|
|
192
|
|
|
break; |
193
|
|
|
default: |
194
|
|
|
break; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $items; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|