1
|
|
|
<?php namespace Arcanesoft\Sidebar; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Sidebar\Contracts\Manager as ManagerContract; |
4
|
|
|
use Arcanesoft\Sidebar\Entities\Item; |
5
|
|
|
use Arcanesoft\Sidebar\Entities\ItemCollection; |
6
|
|
|
use Illuminate\Support\HtmlString; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Manager |
10
|
|
|
* |
11
|
|
|
* @package Arcanesoft\Sidebar |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Manager implements ManagerContract |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Properties |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The view name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $view; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The current name. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $currentName; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The sidebar items collection. |
37
|
|
|
* |
38
|
|
|
* @var \Arcanesoft\Sidebar\Entities\ItemCollection |
39
|
|
|
*/ |
40
|
|
|
protected $items; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The authenticated user. |
44
|
|
|
* |
45
|
|
|
* @var \Arcanesoft\Contracts\Auth\Models\User |
46
|
|
|
*/ |
47
|
|
|
protected $user; |
48
|
|
|
|
49
|
|
|
/* ----------------------------------------------------------------- |
50
|
|
|
| Constructor |
51
|
|
|
| ----------------------------------------------------------------- |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Manager constructor. |
56
|
|
|
*/ |
57
|
18 |
|
public function __construct() |
58
|
|
|
{ |
59
|
18 |
|
$this->items = new ItemCollection; |
60
|
18 |
|
} |
61
|
|
|
|
62
|
|
|
/* ----------------------------------------------------------------- |
63
|
|
|
| Getters & Setters |
64
|
|
|
| ----------------------------------------------------------------- |
65
|
|
|
*/ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the view name. |
69
|
|
|
* |
70
|
|
|
* @param string $view |
71
|
|
|
* |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
6 |
|
public function setView($view) |
75
|
|
|
{ |
76
|
6 |
|
if ( ! is_null($view)) |
77
|
6 |
|
$this->view = $view; |
78
|
|
|
|
79
|
6 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the current item name. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
3 |
|
public function getCurrent() |
88
|
|
|
{ |
89
|
3 |
|
return $this->currentName; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the current item name. |
94
|
|
|
* |
95
|
|
|
* @param string $currentName |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
3 |
|
public function setCurrent($currentName) |
100
|
|
|
{ |
101
|
3 |
|
$this->currentName = $currentName; |
102
|
|
|
|
103
|
3 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the sidebar items. |
108
|
|
|
* |
109
|
|
|
* @return \Arcanesoft\Sidebar\Entities\ItemCollection |
110
|
|
|
*/ |
111
|
15 |
|
public function getItems() |
112
|
|
|
{ |
113
|
15 |
|
return $this->items; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/* ----------------------------------------------------------------- |
117
|
|
|
| Main Methods |
118
|
|
|
| ----------------------------------------------------------------- |
119
|
|
|
*/ |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add a routed item. |
123
|
|
|
* |
124
|
|
|
* @param string $name |
125
|
|
|
* @param string $title |
126
|
|
|
* @param string $route |
127
|
|
|
* @param array $parameters |
128
|
|
|
* @param string|null $icon |
129
|
|
|
* |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
3 |
|
public function addRouteItem($name, $title, $route, array $parameters = [], $icon = null) |
133
|
|
|
{ |
134
|
3 |
|
return $this->addItem($name, $title, route($route, $parameters), $icon); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Add an item. |
139
|
|
|
* |
140
|
|
|
* @param string $name |
141
|
|
|
* @param string $title |
142
|
|
|
* @param string $url |
143
|
|
|
* @param string|null $icon |
144
|
|
|
* |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
9 |
|
public function addItem($name, $title, $url = '#', $icon = null) |
148
|
|
|
{ |
149
|
9 |
|
return $this->add(compact('name', 'title', 'url', 'icon')); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add an item from array. |
154
|
|
|
* |
155
|
|
|
* @param array $array |
156
|
|
|
* |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
9 |
|
public function add(array $array) |
160
|
|
|
{ |
161
|
9 |
|
$item = Item::makeFromArray($array); |
162
|
|
|
|
163
|
9 |
|
if ($item->allowed()) |
164
|
9 |
|
$this->items->push($item); |
165
|
|
|
|
166
|
9 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Load items from multiple config keys. |
171
|
|
|
* |
172
|
|
|
* @param string $key |
173
|
|
|
* |
174
|
|
|
* @return self |
175
|
|
|
*/ |
176
|
|
|
public function loadItemsFromConfig($key) |
177
|
|
|
{ |
178
|
|
|
foreach (config($key, []) as $configKey) { |
179
|
|
|
$this->loadItemFromConfig($configKey); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Load sidebar item from config file. |
187
|
|
|
* |
188
|
|
|
* @param string $key |
189
|
|
|
* |
190
|
|
|
* @return self |
191
|
|
|
*/ |
192
|
|
|
public function loadItemFromConfig($key) |
193
|
|
|
{ |
194
|
|
|
if (config()->has($key)) |
195
|
|
|
$this->add(config($key)); |
196
|
|
|
else |
197
|
|
|
// Throw an exception ?? |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Render the sidebar. |
204
|
|
|
* |
205
|
|
|
* @param string|null $view |
206
|
|
|
* @param array|null $data |
207
|
|
|
* |
208
|
|
|
* @return \Illuminate\Support\HtmlString |
209
|
|
|
*/ |
210
|
6 |
|
public function render($view = '_includes.sidebar.default', array $data = []) |
211
|
|
|
{ |
212
|
6 |
|
$this->syncCurrentName()->setView($view); |
213
|
|
|
|
214
|
6 |
|
return new HtmlString( |
215
|
6 |
|
view($this->view, array_merge($data, ['sidebarItems' => $this->getItems()]))->render() |
216
|
1 |
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/* ----------------------------------------------------------------- |
220
|
|
|
| Check Methods |
221
|
|
|
| ----------------------------------------------------------------- |
222
|
|
|
*/ |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Check if the sidebar has items. |
226
|
|
|
* |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
6 |
|
public function hasItems() |
230
|
|
|
{ |
231
|
6 |
|
return ! $this->items->isEmpty(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/* ----------------------------------------------------------------- |
235
|
|
|
| Other Methods |
236
|
|
|
| ----------------------------------------------------------------- |
237
|
|
|
*/ |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Sync the current name wih the sidebar items. |
241
|
|
|
* |
242
|
|
|
* @return self |
243
|
|
|
*/ |
244
|
6 |
|
private function syncCurrentName() |
245
|
|
|
{ |
246
|
6 |
|
$this->items->setCurrent($this->currentName); |
247
|
|
|
|
248
|
6 |
|
return $this; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|