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 |
16
|
|
|
*/ |
17
|
|
|
protected $pageId; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The page icon class name |
21
|
|
|
* |
22
|
|
|
* @var string |
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
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function getPageId() |
44
|
|
|
{ |
45
|
|
|
return $this->pageId; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param mixed $pageId |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function setPageId($pageId) |
54
|
|
|
{ |
55
|
|
|
$this->pageId = $pageId; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function getIcon() |
64
|
|
|
{ |
65
|
|
|
return $this->icon; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $icon |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function setIcon($icon) |
74
|
|
|
{ |
75
|
|
|
$this->icon = $icon; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function getParentPageId() |
84
|
|
|
{ |
85
|
|
|
return $this->parentPageId; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param mixed $parentPageId |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setParentPageId($parentPageId) |
94
|
|
|
{ |
95
|
|
|
$this->parentPageId = $parentPageId; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function hasParentPageId() |
101
|
|
|
{ |
102
|
|
|
return $this->parentPageId !== null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function getPriority(): int |
109
|
|
|
{ |
110
|
|
|
return $this->priority; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $priority |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function setPriority(int $priority) |
119
|
|
|
{ |
120
|
|
|
$this->priority = $priority; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function getNavigation() |
129
|
|
|
{ |
130
|
|
|
return $this->app['admin.navigation']; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function addToNavigation($badge = null) |
137
|
|
|
{ |
138
|
|
|
$nav = $this->getNavigation(); |
139
|
|
|
if ($this->hasParentPageId()) { |
140
|
|
|
$nav = $nav->getPages()->findById($this->getParentPageId()); |
141
|
|
|
if (is_null($nav)) { |
142
|
|
|
throw new InvalidArgumentException( |
143
|
|
|
sprintf( |
144
|
|
|
'Not Found "%s" navigation', |
145
|
|
|
$this->getParentPageId() |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$page = $this->makePage($badge); |
152
|
|
|
|
153
|
|
|
$nav->addPage($page); |
154
|
|
|
|
155
|
|
|
return $page; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* page |
160
|
|
|
* |
161
|
|
|
* @param null $badge |
162
|
|
|
* |
163
|
|
|
* @return \Sco\Admin\Navigation\Page |
164
|
|
|
*/ |
165
|
|
|
protected function makePage($badge = null) |
166
|
|
|
{ |
167
|
|
|
$page = new Page($this); |
168
|
|
|
$page->setPriority($this->getPriority()) |
|
|
|
|
169
|
|
|
->setIcon($this->getIcon()) |
170
|
|
|
->serId($this->getPageId()) |
171
|
|
|
->setAccessLogic(function () { |
172
|
|
|
return $this->isView(); |
|
|
|
|
173
|
|
|
}); |
174
|
|
|
|
175
|
|
|
if ($badge) { |
176
|
|
|
if (!($badge instanceof BadgeInterface)) { |
177
|
|
|
$badge = new Badge($badge); |
178
|
|
|
} |
179
|
|
|
$page->addBadge($badge); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$page->setIcon($this->getIcon()); |
183
|
|
|
|
184
|
|
|
return $page; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param array $parameters |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getViewUrl(array $parameters = []) |
193
|
|
|
{ |
194
|
|
|
array_unshift($parameters, $this->getName()); |
|
|
|
|
195
|
|
|
|
196
|
|
|
return route('admin.model.index', $parameters, false); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
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: