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