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
|
|
|
protected $icon = null; |
35
|
|
|
|
36
|
|
|
protected $baseUrl = null; |
37
|
|
|
|
38
|
|
|
protected $visibleIn = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Section constructor. |
42
|
|
|
* @param array $data |
43
|
|
|
*/ |
44
|
5 |
|
public function __construct($data = []) |
45
|
|
|
{ |
46
|
5 |
|
$this->writeData($data); |
47
|
5 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $data |
51
|
|
|
*/ |
52
|
5 |
|
public function writeData($data) |
53
|
|
|
{ |
54
|
5 |
|
foreach ($data as $key => $value) { |
55
|
4 |
|
$this->{$key} = $value; |
56
|
|
|
} |
57
|
5 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getSubdomain() |
63
|
|
|
{ |
64
|
|
|
return $this->subdomain; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
1 |
|
public function printIcon() |
71
|
|
|
{ |
72
|
1 |
|
if (empty($this->icon)) { |
73
|
|
|
return ''; |
|
|
|
|
74
|
|
|
} |
75
|
1 |
|
if (Str::contains($this->icon,'<path')) { |
76
|
|
|
return '<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" focusable="false" aria-hidden="true" role="presentation">' |
|
|
|
|
77
|
1 |
|
. $this->icon |
78
|
1 |
|
. '</svg>'; |
79
|
|
|
} |
80
|
|
|
return $this->icon; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param bool $url |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1 |
|
public function getURL($url = false) |
88
|
|
|
{ |
89
|
1 |
|
$url = $url ? $url : request()->root(); |
90
|
1 |
|
$http = request()->getHttp(); |
91
|
1 |
|
return str_replace( |
92
|
1 |
|
'://' . $http->getSubdomain() . '.' . $http->getRootDomain(), |
93
|
1 |
|
'://' . $this->subdomain . '.' . $http->getRootDomain(), |
94
|
1 |
|
$url |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string|null |
100
|
|
|
*/ |
101
|
|
|
public function getBaseUrl() |
102
|
|
|
{ |
103
|
|
|
if ($this->baseUrl === null) { |
104
|
|
|
$this->initBaseUrl(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->baseUrl; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function initBaseUrl() |
111
|
|
|
{ |
112
|
|
|
$baseUrl = request()->root(); |
113
|
|
|
$http = request()->getHttp(); |
114
|
|
|
|
115
|
|
|
$baseUrl = str_replace( |
116
|
|
|
'://' . $this->getManager()->getCurrent()->getSubdomain() . '.' . $http->getRootDomain(), |
117
|
|
|
'://' . $this->getSubdomain() . '.' . $http->getRootDomain(), |
118
|
|
|
$baseUrl |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$this->baseUrl = $baseUrl; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param bool $url |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function assembleURL($url = false, $params = []) |
129
|
|
|
{ |
130
|
|
|
$url = $url ? $url : '/'; |
131
|
|
|
|
132
|
|
|
$url = $this->getBaseUrl() . $url; |
|
|
|
|
133
|
|
|
if (count($params)) { |
134
|
|
|
$url .= '?' . http_build_query($params); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $url; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Compile path for this section from a given path of current section |
142
|
|
|
* |
143
|
|
|
* @param bool $path |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function compilePath($path = false) |
147
|
|
|
{ |
148
|
|
|
$currentBasePath = $this->getManager()->getCurrent()->getPath(); |
149
|
|
|
$path = str_replace($currentBasePath, $this->getPath(), $path); |
150
|
|
|
return $path; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return the path for section |
155
|
|
|
* |
156
|
|
|
* @return null|string |
157
|
|
|
*/ |
158
|
|
|
public function getPath() |
159
|
|
|
{ |
160
|
|
|
if ($this->path === null) { |
161
|
|
|
$this->initPath(); |
162
|
|
|
} |
163
|
|
|
return $this->path; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function initPath() |
167
|
|
|
{ |
168
|
|
|
$this->path = $this->generatePath(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
protected function generatePath() |
175
|
|
|
{ |
176
|
|
|
$path = app('path.base'); |
177
|
|
|
if (!$this->isCurrent()) { |
178
|
|
|
$path = str_replace( |
179
|
|
|
DIRECTORY_SEPARATOR . $this->getManager()->getCurrent()->getFolder() . '', |
180
|
|
|
DIRECTORY_SEPARATOR . $this->getFolder() . '', |
181
|
|
|
$path |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
return $path; |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function isCurrent() |
191
|
|
|
{ |
192
|
|
|
return $this->getName() == $this->getManager()->getCurrent()->getName(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getName() |
199
|
|
|
{ |
200
|
|
|
return $this->name; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return SectionsManager |
205
|
|
|
*/ |
206
|
|
|
protected function getManager() |
207
|
|
|
{ |
208
|
|
|
return app('mvc.sections'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getFolder() |
215
|
|
|
{ |
216
|
|
|
return $this->folder; |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $place |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
2 |
|
public function visibleIn($place) |
224
|
|
|
{ |
225
|
2 |
|
return in_array($place, $this->visibleIn); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|