-
Notifications
You must be signed in to change notification settings - Fork 3
/
Module.php
234 lines (183 loc) · 6.42 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace wdmg\pages;
/**
* Yii2 Pages
*
* @category Module
* @version 1.3.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-pages
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
use wdmg\translations\components\UrlManager;
use Yii;
use wdmg\base\BaseModule;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* Pages module definition class
*/
class Module extends BaseModule
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'wdmg\pages\controllers';
/**
* {@inheritdoc}
*/
public $defaultRoute = "pages/index";
/**
* @var string, the name of module
*/
public $name = "Pages";
/**
* @var string, the description of module
*/
public $description = "Static Page Manager";
/**
* @var string the module version
*/
private $version = "1.3.0";
/**
* @var integer, priority of initialization
*/
private $priority = 2;
/**
* @var string or array, the default routes to rendered page (use "/" - for root)
*/
public $baseRoute = "/pages";
/**
* @var string, the default controller for pages in @frontend
*/
public $defaultController = "admin/pages/default";
/**
* @var string, the default layout to rendered page
*/
public $baseLayout = "@app/views/layouts/main";
/**
* @var array, the list of support locales for multi-language versions of page.
* @note This variable will be override if you use the `wdmg\yii2-translations` module.
*/
public $supportLocales = ['ru-RU', 'uk-UA', 'en-US'];
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// Set version of current module
$this->setVersion($this->version);
// Set priority of current module
$this->setPriority($this->priority);
if (isset(Yii::$app->params["pages.baseRoute"]))
$this->baseRoute = Yii::$app->params["pages.baseRoute"];
if (isset(Yii::$app->params["pages.defaultController"]))
$this->defaultController = Yii::$app->params["pages.defaultController"];
if (isset(Yii::$app->params["pages.supportLocales"]))
$this->supportLocales = Yii::$app->params["pages.supportLocales"];
if (!isset($this->baseRoute))
throw new InvalidConfigException("Required module property `baseRoute` isn't set.");
// Process and normalize route for news in frontend
$this->baseRoute = self::normalizeRoute($this->baseRoute);
}
/**
* Normalization to normal path/route
*
* @param string or array $routes
* @return string or array of normalized route`s
*/
public function normalizePagesRoute($routes)
{
if (is_array($routes)) {
$routes = array_unique($routes);
foreach ($routes as &$route) {
$route = self::normalizeRoute($route);
}
} else {
$routes = self::normalizeRoute($routes);
}
return $routes;
}
/**
* {@inheritdoc}
*/
public function dashboardNavItems($options = false)
{
$items = [
'label' => $this->name,
'url' => [$this->routePrefix . '/'. $this->id],
'icon' => 'fa fa-fw fa-layer-group',
'active' => in_array(\Yii::$app->controller->module->id, [$this->id])
];
if (!is_null($options)) {
if (isset($options['count'])) {
$items['label'] .= '<span class="badge badge-default float-right">' . $options['count'] . '</span>';
unset($options['count']);
}
if (is_array($options))
$items = \wdmg\helpers\ArrayHelper::merge($items, $options);
}
return $items;
}
/**
* {@inheritdoc}
*/
public function bootstrap($app)
{
parent::bootstrap($app);
if (!$this->isBackend() && !is_null($this->defaultController)) {
// Get language scheme if available
$custom = false;
$hide = false;
$scheme = null;
if (isset(Yii::$app->translations)) {
$custom = true;
$hide = Yii::$app->translations->module->hideDefaultLang;
$scheme = Yii::$app->translations->module->languageScheme;
}
$baseRoute = ltrim($this->baseRoute, '/');
// Add routes for frontend
switch ($scheme) {
case "after":
$app->getUrlManager()->addRules([
'/<route:' . $baseRoute . '.*?>/<alias:[\w-]+>/<lang:\w+>' => $this->defaultController . '/view',
], true);
if ($hide) {
$app->getUrlManager()->addRules([
'/<route:' . $baseRoute . '.*?>/<alias:[\w-]+>' => $this->defaultController . '/view',
], true);
}
break;
case "query":
$app->getUrlManager()->addRules([
'/<route:' . $baseRoute . '.*?>/<alias:[\w-]+>' => $this->defaultController . '/view',
], true);
/*if ($hide) {
}*/
break;
case "subdomain":
if ($host = $app->getRequest()->getHostName()) {
$app->getUrlManager()->addRules([
'http(s)?://' . $host. '/<route:' . $baseRoute . '.*?>/<alias:[\w-]+>' => $this->defaultController . '/view',
], true);
}
/*if ($hide) {
}*/
break;
default:
$app->getUrlManager()->addRules([
'/<lang:\w+>/<route:' . $baseRoute . '.*?>/<alias:[\w-]+>' => $this->defaultController . '/view',
], true);
if ($hide || !$custom) {
$app->getUrlManager()->addRules([
'/<route:' . $baseRoute . '.*?>/<alias:[\w-]+>' => $this->defaultController . '/view',
], true);
}
break;
}
}
}
}