1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component\Concerns; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use KodiComponents\Navigation\Contracts\BadgeInterface; |
7
|
|
|
use Sco\Admin\Navigation\Badge; |
8
|
|
|
use Sco\Admin\Navigation\Page; |
9
|
|
|
|
10
|
|
|
trait HasNavigation |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The page id name. |
14
|
|
|
* |
15
|
|
|
* @var string|null |
16
|
|
|
*/ |
17
|
|
|
protected $pageId; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The page icon class name. |
21
|
|
|
* |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
protected $icon; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The page belong to page id name. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $parentPageId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The page priority. |
35
|
|
|
* |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $priority = 100; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the page id |
42
|
|
|
* |
43
|
|
|
* @return string|null |
44
|
|
|
*/ |
45
|
|
|
public function getPageId() |
46
|
|
|
{ |
47
|
|
|
return $this->pageId; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set the page id |
52
|
|
|
* |
53
|
|
|
* @param string $pageId |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function setPageId(string $pageId) |
58
|
|
|
{ |
59
|
|
|
$this->pageId = $pageId; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the page icon class name. |
66
|
|
|
* |
67
|
|
|
* @return string|null |
68
|
|
|
*/ |
69
|
|
|
public function getIcon() |
70
|
|
|
{ |
71
|
|
|
return $this->icon; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the page icon class name. |
76
|
|
|
* |
77
|
|
|
* @param string $icon |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setIcon(string $icon) |
82
|
|
|
{ |
83
|
|
|
$this->icon = $icon; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getParentPageId(): string |
93
|
|
|
{ |
94
|
|
|
return $this->parentPageId; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $parentPageId |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setParentPageId(string $parentPageId) |
103
|
|
|
{ |
104
|
|
|
$this->parentPageId = $parentPageId; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function hasParentPageId() |
110
|
|
|
{ |
111
|
|
|
return $this->parentPageId !== null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public function getPriority(): int |
118
|
|
|
{ |
119
|
|
|
return $this->priority; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param int $priority |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function setPriority(int $priority) |
128
|
|
|
{ |
129
|
|
|
$this->priority = $priority; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function getNavigation() |
138
|
|
|
{ |
139
|
|
|
return $this->app['admin.navigation']; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function addToNavigation($badge = null) |
146
|
|
|
{ |
147
|
|
|
$nav = $this->getNavigation(); |
148
|
|
|
if ($this->hasParentPageId()) { |
149
|
|
|
$nav = $nav->getPages()->findById($this->getParentPageId()); |
150
|
|
|
if (is_null($nav)) { |
151
|
|
|
throw new InvalidArgumentException( |
152
|
|
|
sprintf( |
153
|
|
|
'Not Found "%s" navigation', |
154
|
|
|
$this->getParentPageId() |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$page = $this->makePage($badge); |
161
|
|
|
|
162
|
|
|
$nav->addPage($page); |
163
|
|
|
|
164
|
|
|
return $page; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Make page |
169
|
|
|
* |
170
|
|
|
* @param string|Closure|null $badge |
171
|
|
|
* |
172
|
|
|
* @return \Sco\Admin\Navigation\Page |
173
|
|
|
*/ |
174
|
|
|
protected function makePage($badge = null) |
175
|
|
|
{ |
176
|
|
|
$page = new Page($this); |
177
|
|
|
$page->setPriority($this->getPriority()) |
178
|
|
|
->setIcon($this->getIcon()) |
179
|
|
|
->setId($this->getPageId()) |
180
|
|
|
->setAccessLogic(function () { |
181
|
|
|
return $this->isDisplay(); |
|
|
|
|
182
|
|
|
}); |
183
|
|
|
|
184
|
|
|
if ($badge) { |
185
|
|
|
if (! ($badge instanceof BadgeInterface)) { |
186
|
|
|
$badge = new Badge($badge); |
187
|
|
|
} |
188
|
|
|
$page->addBadge($badge); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $page; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the model display url. |
196
|
|
|
* |
197
|
|
|
* @param array $parameters |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public function getDisplayUrl(array $parameters = []) |
202
|
|
|
{ |
203
|
|
|
array_unshift($parameters, $this->getName()); |
|
|
|
|
204
|
|
|
|
205
|
|
|
return route('admin.model.index', $parameters, false); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
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: