-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackendModule.php
101 lines (72 loc) · 2.2 KB
/
BackendModule.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
<?php
/*!
* yii2 - module - backend
* xiewulong <xiewulong@vip.qq.com>
* https://github.com/xiewulong/yii2-cms
* https://raw.githubusercontent.com/xiewulong/yii2-cms/master/LICENSE
* create: 2016/8/7
* update: 2016/10/12
* since: 0.0.1
*/
namespace yii\cms;
use Yii;
use yii\components\Module;
use yii\cms\models\Site;
class BackendModule extends Module {
public $siteId;
public $attachmentPath;
public $attachmentUrl;
public $frontendHost;
public $frontendModuleId;
public $controllerNamespace = 'yii\cms\controllers\backend';
public $defaultRoute = 'dashboard';
public $layout = 'backend';
public $customViewsPath = '@vendor/xiewulong/yii2-cms/views/backend';
public $permissions = ['@'];
public $addMenuItems = [];
public $addSidebarItems = [];
public $messageCategory = 'cms';
private $_site;
public function init() {
parent::init();
$this->setSite();
$this->setAttachmentModule('attachment');
}
public function attachmentRoute($id) {
return [$this->url('attachment'), 'id' => $id];
}
public function getFrontendUrl($url = null) {
$frontendModuleId = $this->siteId ? : $this->id;
return rtrim($this->frontendHost ? : null, '/') . \Yii::$app->urlManager->createUrl([$this->frontendModuleId ? : $frontendModuleId]) . ($url ? \Yii::$app->urlManager->createUrl($url) : null);
}
public function url($url) {
return '/' . $this->uniqueId . '/' . $url;
}
private function setAttachmentModule($id, $options = []) {
$module = [
'class' => 'yii\attachment\Module',
'write' => $this->permissions,
];
$modules = $this->modules;
$modules[$id] = array_merge($module, isset($modules[$id]) ? $modules[$id] : [], $options);
$this->modules = $modules;
}
public function getSite() {
return $this->_site;
}
private function setSite() {
if(!$this->siteId) {
$this->siteId = $this->id;
}
$this->_site = Site::findOne($this->siteId);
if(!$this->_site) {
$this->_site = new Site;
$this->_site->scenario = 'add';
$this->_site->id = $this->siteId;
$this->_site->powered = \Yii::t($this->messageCategory, 'Nanning Automan Technology Co., Ltd.');
if(!$this->_site->commonHandler()) {
\Yii::$app->end($this->_site->getFirstError('id'));
}
}
}
}