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
|
|
|
protected $icon; |
13
|
|
|
|
14
|
|
|
protected $parentPageId; |
15
|
|
|
|
16
|
|
|
protected $priority = 100; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public function getIcon() |
22
|
|
|
{ |
23
|
|
|
return $this->icon; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param mixed $icon |
28
|
|
|
* |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function setIcon($icon) |
32
|
|
|
{ |
33
|
|
|
$this->icon = $icon; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function getParentPageId() |
42
|
|
|
{ |
43
|
|
|
return $this->parentPageId; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed $parentPageId |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function setParentPageId($parentPageId) |
52
|
|
|
{ |
53
|
|
|
$this->parentPageId = $parentPageId; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function hasParentPageId() |
59
|
|
|
{ |
60
|
|
|
return $this->parentPageId !== null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
public function getPriority(): int |
67
|
|
|
{ |
68
|
|
|
return $this->priority; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $priority |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setPriority(int $priority) |
77
|
|
|
{ |
78
|
|
|
$this->priority = $priority; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function getNavigation() |
87
|
|
|
{ |
88
|
|
|
return $this->app['admin.navigation']; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function addToNavigation($badge = null) |
95
|
|
|
{ |
96
|
|
|
$nav = $this->getNavigation(); |
97
|
|
|
if ($this->hasParentPageId()) { |
98
|
|
|
$nav = $nav->getPages()->findById($this->getParentPageId()); |
99
|
|
|
if (is_null($nav)) { |
100
|
|
|
throw new InvalidArgumentException( |
101
|
|
|
sprintf( |
102
|
|
|
'Not Found "%s" navigation', |
103
|
|
|
$this->getParentPageId() |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$page = $this->makePage($badge); |
110
|
|
|
|
111
|
|
|
$nav->addPage($page); |
112
|
|
|
|
113
|
|
|
return $page; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* page |
118
|
|
|
* |
119
|
|
|
* @param null $badge |
120
|
|
|
* |
121
|
|
|
* @return \Sco\Admin\Navigation\Page |
122
|
|
|
*/ |
123
|
|
|
protected function makePage($badge = null) |
124
|
|
|
{ |
125
|
|
|
$page = new Page($this); |
126
|
|
|
$page->setPriority($this->getPriority()) |
127
|
|
|
->setIcon($this->getIcon()) |
128
|
|
|
->setAccessLogic(function () { |
129
|
|
|
return $this->isView(); |
|
|
|
|
130
|
|
|
}); |
131
|
|
|
|
132
|
|
|
if ($badge) { |
133
|
|
|
if (!($badge instanceof BadgeInterface)) { |
134
|
|
|
$badge = new Badge($badge); |
135
|
|
|
} |
136
|
|
|
$page->addBadge($badge); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$page->setIcon($this->getIcon()); |
140
|
|
|
|
141
|
|
|
return $page; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $parameters |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getViewUrl(array $parameters = []) |
150
|
|
|
{ |
151
|
|
|
array_unshift($parameters, $this->getName()); |
|
|
|
|
152
|
|
|
|
153
|
|
|
return route('admin.model.index', $parameters, false); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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: