|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
|
4
|
|
|
|
|
5
|
|
|
trait Boxes |
|
6
|
|
|
{ |
|
7
|
|
|
public $sideBoxesEnabled = false; |
|
8
|
|
|
public $boxesOptions = []; |
|
9
|
|
|
|
|
10
|
|
|
public function enableSideBoxes() |
|
11
|
|
|
{ |
|
12
|
|
|
$this->sideBoxesEnabled = true; |
|
13
|
|
|
|
|
14
|
|
|
return $this->sideBoxesEnabled; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function disableSideBoxes() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->sideBoxesEnabled = false; |
|
20
|
|
|
|
|
21
|
|
|
return $this->sideBoxesEnabled; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function sideBoxesEnabled() |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
return $this->sideBoxesEnabled; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function sideBoxesDisabled() |
|
30
|
|
|
{ |
|
31
|
|
|
return ! $this->sideBoxesEnabled; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function boxExists($label) |
|
35
|
|
|
{ |
|
36
|
|
|
$boxes = $this->getBoxes(); |
|
37
|
|
|
|
|
38
|
|
|
return in_array($label, $boxes); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getLastBox() |
|
42
|
|
|
{ |
|
43
|
|
|
$boxes = $this->getBoxes(); |
|
44
|
|
|
|
|
45
|
|
|
if (count($boxes)) { |
|
46
|
|
|
return last($boxes); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function isLastBox($label) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->getLastBox() == $label; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getBoxFields($label) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->boxExists($label)) { |
|
60
|
|
|
$all_fields = $this->getCurrentFields(); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$fields_for_current_box = collect($all_fields)->filter(function ($value, $key) use ($label) { |
|
|
|
|
|
|
63
|
|
|
return isset($value['box']) && $value['box'] == $label; |
|
64
|
|
|
}); |
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
if ($this->isLastBox($label)) { |
|
|
|
|
|
|
67
|
|
|
$fields_without_a_box = collect($all_fields)->filter(function ($value, $key) { |
|
|
|
|
|
|
68
|
|
|
return ! isset($value['box']); |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
$fields_for_current_box = $fields_for_current_box->merge($fields_without_a_box); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $fields_for_current_box; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return []; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function setBoxOptions($label, array $options) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->boxesOptions[$label] = $options; |
|
83
|
|
|
|
|
84
|
|
|
// if a "side" box was mentioned, we should enable it |
|
85
|
|
|
if (! empty($options['side'])) { |
|
86
|
|
|
$this->enableSideBoxes(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getBoxOptions($label) |
|
91
|
|
|
{ |
|
92
|
|
|
$boxOptions = [ |
|
93
|
|
|
'side' => false, |
|
94
|
|
|
'class' => '', |
|
95
|
|
|
'collapsed' => false, |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
if (isset($this->boxesOptions[$label])) { |
|
99
|
|
|
$boxOptions = array_merge($boxOptions, $this->boxesOptions[$label]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($boxOptions['collapsed']) { |
|
103
|
|
|
$boxOptions['class'] = trim($boxOptions['class'].' collapsed-box'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $boxOptions; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getBoxes($columnFilter = null) |
|
110
|
|
|
{ |
|
111
|
|
|
$boxes = []; |
|
112
|
|
|
$fields = $this->getCurrentFields(); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
$fields_with_boxes = collect($fields) |
|
|
|
|
|
|
115
|
|
|
->filter(function ($value, $key) { |
|
|
|
|
|
|
116
|
|
|
return isset($value['box']); |
|
117
|
|
|
}) |
|
118
|
|
|
->each(function ($value, $key) use (&$boxes, $columnFilter) { |
|
|
|
|
|
|
119
|
|
|
if (! isset($columnFilter) || ($columnFilter === 'side' xor ! $this->getBoxOptions($value['box'])['side'])) { |
|
|
|
|
|
|
120
|
|
|
if (! in_array($value['box'], $boxes)) { |
|
121
|
|
|
$boxes[] = $value['box']; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
}); |
|
125
|
|
|
|
|
126
|
|
|
// Default Box |
|
127
|
|
|
if (empty($boxes) && $columnFilter !== 'side') { |
|
128
|
|
|
$boxes[] = ucfirst($this->entity_name); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $boxes; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.