|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Mvc\Sections; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Utility\Str; |
|
6
|
|
|
use Nip\Utility\Traits\DynamicPropertiesTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Section |
|
10
|
|
|
* @package Nip\Mvc\Sections |
|
11
|
|
|
* |
|
12
|
|
|
* @property $menu |
|
13
|
|
|
* @property $folder |
|
14
|
|
|
*/ |
|
15
|
|
|
class Section |
|
16
|
|
|
{ |
|
17
|
|
|
use DynamicPropertiesTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $name; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $subdomain; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $path = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $folder = null; |
|
38
|
|
|
|
|
39
|
|
|
protected $icon = null; |
|
40
|
|
|
|
|
41
|
|
|
protected $baseUrl = null; |
|
42
|
|
|
|
|
43
|
|
|
protected $visibleIn = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Section constructor. |
|
47
|
|
|
* @param array $data |
|
48
|
|
|
*/ |
|
49
|
7 |
|
public function __construct($data = []) |
|
50
|
|
|
{ |
|
51
|
7 |
|
$this->writeData($data); |
|
52
|
7 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritDoc |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function __call(string $name, array $arguments) |
|
58
|
|
|
{ |
|
59
|
1 |
|
if (strpos($name, 'is') ===0) { |
|
60
|
1 |
|
$section = substr($name, 2); |
|
61
|
1 |
|
$section = inflector()->underscore($section); |
|
62
|
1 |
|
return $this->visibleIn($section); |
|
63
|
|
|
} |
|
64
|
|
|
throw new \Exception("Invalid call " . $name . " on " . __CLASS__ . ""); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $data |
|
69
|
|
|
*/ |
|
70
|
11 |
|
public function writeData($data) |
|
71
|
|
|
{ |
|
72
|
11 |
|
foreach ($data as $key => $value) { |
|
73
|
11 |
|
$this->{$key} = $value; |
|
74
|
|
|
} |
|
75
|
11 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
6 |
|
public function getSubdomain() |
|
81
|
|
|
{ |
|
82
|
6 |
|
return $this->subdomain; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function printIcon() |
|
89
|
|
|
{ |
|
90
|
1 |
|
if (empty($this->icon)) { |
|
91
|
|
|
return ''; |
|
92
|
|
|
} |
|
93
|
1 |
|
if (Str::contains($this->icon, '<path')) { |
|
94
|
|
|
return '<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" focusable="false" aria-hidden="true" role="presentation">' |
|
95
|
1 |
|
. $this->icon |
|
96
|
1 |
|
. '</svg>'; |
|
97
|
|
|
} |
|
98
|
|
|
return $this->icon; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param bool $url |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
3 |
|
public function getURL($url = false) |
|
106
|
|
|
{ |
|
107
|
3 |
|
$url = $url ? $url : request()->root(); |
|
108
|
|
|
|
|
109
|
3 |
|
if (strpos($url, 'http') === 0) { |
|
|
|
|
|
|
110
|
2 |
|
return $this->getManager()->getUrlTransformer()->transform($url, $this); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
return $this->getBaseUrl() . $url; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string|null |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function getBaseUrl() |
|
120
|
|
|
{ |
|
121
|
2 |
|
if ($this->baseUrl === null) { |
|
122
|
2 |
|
$this->initBaseUrl(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
2 |
|
return $this->baseUrl; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
2 |
|
protected function initBaseUrl() |
|
129
|
|
|
{ |
|
130
|
2 |
|
$baseUrl = request()->root(); |
|
131
|
|
|
|
|
132
|
2 |
|
$this->baseUrl = $this->getManager()->getUrlTransformer()->transform($baseUrl, $this); |
|
133
|
2 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param bool $url |
|
137
|
|
|
* @param array $params |
|
138
|
|
|
* @return mixed |
|
139
|
|
|
*/ |
|
140
|
|
|
public function assembleURL($url = false, $params = []) |
|
141
|
|
|
{ |
|
142
|
|
|
$url = $url ? $url : '/'; |
|
143
|
|
|
|
|
144
|
|
|
$url = $this->getBaseUrl() . $url; |
|
|
|
|
|
|
145
|
|
|
if (count($params)) { |
|
146
|
|
|
$url .= '?' . http_build_query($params); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $url; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Compile path for this section from a given path of current section |
|
154
|
|
|
* |
|
155
|
|
|
* @param bool $path |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function compilePath($path = false) |
|
159
|
|
|
{ |
|
160
|
|
|
$currentBasePath = $this->getManager()->getCurrent()->getPath(); |
|
161
|
|
|
$path = str_replace($currentBasePath, $this->getPath(), $path); |
|
162
|
|
|
return $path; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Return the path for section |
|
167
|
|
|
* |
|
168
|
|
|
* @return null|string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getPath() |
|
171
|
|
|
{ |
|
172
|
|
|
if ($this->path === null) { |
|
173
|
|
|
$this->initPath(); |
|
174
|
|
|
} |
|
175
|
|
|
return $this->path; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
protected function initPath() |
|
179
|
|
|
{ |
|
180
|
|
|
$this->path = $this->generatePath(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function generatePath() |
|
187
|
|
|
{ |
|
188
|
|
|
$path = app('path.base'); |
|
189
|
|
|
if (!$this->isCurrent()) { |
|
190
|
|
|
$path = str_replace( |
|
191
|
|
|
DIRECTORY_SEPARATOR . $this->getManager()->getCurrent()->getFolder() . '', |
|
192
|
|
|
DIRECTORY_SEPARATOR . $this->getFolder() . '', |
|
193
|
|
|
$path |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
return $path; |
|
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return bool |
|
201
|
|
|
*/ |
|
202
|
|
|
public function isCurrent() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->getName() == $this->getManager()->getCurrent()->getName(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getName() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->name; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return SectionsManager |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function getManager() |
|
219
|
|
|
{ |
|
220
|
|
|
return app('mvc.sections'); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getFolder() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->folder; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param string $place |
|
233
|
|
|
* @return bool |
|
234
|
|
|
*/ |
|
235
|
3 |
|
public function visibleIn($place) |
|
236
|
|
|
{ |
|
237
|
3 |
|
return in_array($place, $this->visibleIn); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|