1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Alireza Josheghani <[email protected]> |
5
|
|
|
* @version 1.0 |
6
|
|
|
* @package SideNav |
7
|
|
|
* @since 20 Sep 2016 |
8
|
|
|
* SideNav Menu class |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Anetwork\SideNav; |
12
|
|
|
|
13
|
|
|
class Menu |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
// the route name |
17
|
|
|
protected $route; |
18
|
|
|
|
19
|
|
|
// the title option |
20
|
|
|
protected $title; |
21
|
|
|
|
22
|
|
|
// the url option |
23
|
|
|
protected $link = [ |
24
|
|
|
'new_tab' => false, |
25
|
|
|
'value' => null, |
26
|
|
|
'regex' => [] |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
// the class-name option |
30
|
|
|
protected $class; |
31
|
|
|
|
32
|
|
|
// the icon option |
33
|
|
|
protected $icon = [ |
34
|
|
|
'tag' => 'i', |
35
|
|
|
'value' => null |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
// is_new icon option : boolean |
39
|
|
|
protected $isNew = false; |
40
|
|
|
|
41
|
|
|
// selected option |
42
|
|
|
protected $selected = false; |
43
|
|
|
|
44
|
|
|
// open child when item is active : boolean |
45
|
|
|
protected $openChildOnClick = true; |
46
|
|
|
|
47
|
|
|
// the sub menu array |
48
|
|
|
protected $submenu = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Attach sub menu array |
52
|
|
|
* |
53
|
|
|
* @author Alireza Josheghani <[email protected]> |
54
|
|
|
* @since 20 Sep 2016 |
55
|
|
|
* @param $route |
56
|
|
|
* @param $callback |
57
|
|
|
*/ |
58
|
|
|
public function sub($route, $callback) |
59
|
|
|
{ |
60
|
|
|
$sub = SideNav::addSub($route, $callback); |
61
|
|
|
array_push($this->submenu, $sub); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Attach sub menu array with checking status of item |
66
|
|
|
* |
67
|
|
|
* @author Alireza Josheghani <[email protected]> |
68
|
|
|
* @since 20 Sep 2016 |
69
|
|
|
* @param $route |
70
|
|
|
* @param $callback |
71
|
|
|
* @param $checkCallback |
72
|
|
|
*/ |
73
|
|
|
public function subWithCheck($route, $callback , $checkCallback) |
74
|
|
|
{ |
75
|
|
|
if($checkCallback()) { |
76
|
|
|
$sub = SideNav::addSub($route, $callback); |
77
|
|
|
array_push($this->submenu, $sub); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the icon option |
83
|
|
|
* |
84
|
|
|
* @author Alireza Josheghani <[email protected]> |
85
|
|
|
* @since 20 Sep 2016 |
86
|
|
|
* @param $icon |
87
|
|
|
*/ |
88
|
|
|
public function icon($icon) |
89
|
|
|
{ |
90
|
|
|
$this->icon['value'] = $icon; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the icon tag option |
96
|
|
|
* |
97
|
|
|
* @author Alireza Josheghani <[email protected]> |
98
|
|
|
* @since 20 Sep 2016 |
99
|
|
|
* @param $icon |
100
|
|
|
*/ |
101
|
|
|
public function tag($tag) |
102
|
|
|
{ |
103
|
|
|
$this->icon['tag'] = $tag; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the title option |
109
|
|
|
* |
110
|
|
|
* @author Alireza Josheghani <[email protected]> |
111
|
|
|
* @since 20 Sep 2016 |
112
|
|
|
* @param $title |
113
|
|
|
*/ |
114
|
|
|
public function title($title) |
115
|
|
|
{ |
116
|
|
|
$this->title = $title; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the class-name option |
121
|
|
|
* |
122
|
|
|
* @author Alireza Josheghani <[email protected]> |
123
|
|
|
* @since 20 Sep 2016 |
124
|
|
|
* @param $class_name |
125
|
|
|
*/ |
126
|
|
|
public function className($className) |
127
|
|
|
{ |
128
|
|
|
$this->class = $className; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the newTab option |
133
|
|
|
* |
134
|
|
|
* @author Alireza Josheghani <[email protected]> |
135
|
|
|
* @since 20 Sep 2016 |
136
|
|
|
* @param $newtab : boolean |
137
|
|
|
*/ |
138
|
|
|
public function newTab($newtab) |
139
|
|
|
{ |
140
|
|
|
$this->link['new_tab'] = $newtab; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set route name |
145
|
|
|
* |
146
|
|
|
* @author Alireza Josheghani <[email protected]> |
147
|
|
|
* @since 20 Sep 2016 |
148
|
|
|
* @param $route |
149
|
|
|
*/ |
150
|
|
|
public function routeName($route) |
151
|
|
|
{ |
152
|
|
|
$this->route = $route; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the link of menu |
157
|
|
|
* |
158
|
|
|
* @author Alireza Josheghani <[email protected]> |
159
|
|
|
* @since 20 Sep 2016 |
160
|
|
|
* @param $link |
161
|
|
|
*/ |
162
|
|
|
public function link($link) |
163
|
|
|
{ |
164
|
|
|
$this->link['value'] = $link; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set the regex link of menu |
169
|
|
|
* |
170
|
|
|
* @author Alireza Josheghani <[email protected]> |
171
|
|
|
* @since 16 Oxt 2016 |
172
|
|
|
* @param array $regex |
173
|
|
|
*/ |
174
|
|
|
public function regex(array $regex) |
175
|
|
|
{ |
176
|
|
|
$this->link['regex'] = $regex; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set target link of item |
181
|
|
|
* |
182
|
|
|
* @author Alireza Josheghani <[email protected]> |
183
|
|
|
* @since 20 Sep 2016 |
184
|
|
|
* @param $isNew |
185
|
|
|
*/ |
186
|
|
|
public function isNew($isNew) |
187
|
|
|
{ |
188
|
|
|
$this->isNew = $isNew; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Set item is selected |
193
|
|
|
* |
194
|
|
|
* @author Alireza Josheghani <[email protected]> |
195
|
|
|
* @since 20 Sep 2016 |
196
|
|
|
* @param $type |
197
|
|
|
*/ |
198
|
|
|
public function selected($type) |
199
|
|
|
{ |
200
|
|
|
$this->selected = $type; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Set open child when item is active |
205
|
|
|
* |
206
|
|
|
* @author Alireza Josheghani <[email protected]> |
207
|
|
|
* @since 20 Sep 2016 |
208
|
|
|
* @param $type |
209
|
|
|
*/ |
210
|
|
|
public function openChildOnClick($type) |
211
|
|
|
{ |
212
|
|
|
$this->openChildOnClick = $type; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* make the return item array |
217
|
|
|
* |
218
|
|
|
* @author Alireza Josheghani <[email protected]> |
219
|
|
|
* @since 20 Sep 2016 |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
public function make($route) |
223
|
|
|
{ |
224
|
|
|
// set route name of menu |
225
|
|
|
$this->routeName($route); |
226
|
|
|
|
227
|
|
|
$submenu = null; |
228
|
|
|
|
229
|
|
|
if(!empty($this->submenu)) { |
230
|
|
|
$submenu = $this->submenu; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$result = [ |
234
|
|
|
'name' => $this->route, |
235
|
|
|
'title' => $this->title, |
236
|
|
|
'class' => $this->class, |
237
|
|
|
'icon' => $this->icon, |
238
|
|
|
'link' => $this->link, |
239
|
|
|
'regex' => $this->regex, |
|
|
|
|
240
|
|
|
'selected' => $this->selected, |
241
|
|
|
'is_new' => $this->isNew, |
242
|
|
|
'open_child_on_click' => $this->openChildOnClick, |
243
|
|
|
'sub' => $submenu |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
if(empty($this->submenu)) { |
247
|
|
|
unset($result['sub']); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $result; |
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
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: