/
FrontendModule.php
128 lines (95 loc) · 2.71 KB
/
FrontendModule.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
<?php
/*!
* yii2 - module - frontend
* 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/9/3
* since: 0.0.1
*/
namespace yii\cms;
use Yii;
use yii\components\Module;
use yii\cms\models\Site;
class FrontendModule extends Module {
public $siteId;
public $backendHost;
public $backendModuleId;
public $controllerNamespace = 'yii\cms\controllers\frontend';
public $defaultRoute = 'home';
public $layout = 'frontend';
public $customViewsPath = '@vendor/xiewulong/yii2-cms/views/frontend';
public $backendEntrance = false;
public $statisticsEnable = false;
public $hooks = [];
public $hookShowKey = 'show';
public $messageCategory = 'cms';
private $_site;
public function init() {
parent::init();
$this->setSite();
$this->setAttachmentModule('attachment', [
'unsupportTypes' => ['Image'],
]);
$this->setAttachmentModule('image', [
'lockTypes' => ['Image'],
]);
}
public function hook($position) {
if(YII_ENV != 'prod' && $this->hookShowKey && \Yii::$app->request->get('hook') == $this->hookShowKey) {
echo $position;
}
if(!isset($this->hooks[$position])) {
return null;
}
ob_start();
ob_implicit_flush(false);
try {
$out = \Yii::createObject($this->hooks[$position])->run();
} catch (\Exception $e) {
if (ob_get_level() > 0) {
ob_end_clean();
}
throw $e;
}
return ob_get_clean() . $out;
}
public function imageRoute($id) {
return [$this->url('image'), 'id' => $id];
}
public function attachmentRoute($id) {
return [$this->url('attachment'), 'id' => $id];
}
public function getBackendUrl($url = null) {
$backendModuleId = $this->siteId ? : $this->id;
return rtrim($this->backendHost ? : null, '/') . \Yii::$app->urlManager->createUrl([$this->backendModuleId ? : $backendModuleId]) . ($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',
'statisticsEnable' => $this->statisticsEnable,
];
$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([
'id' => $this->siteId,
'status' => Site::STATUS_ENABLED,
]);
if(!$this->_site || !$this->_site->name || !$this->_site->logo_id) {
\Yii::$app->end(\Yii::t('yii', 'Page not found.'));
}
}
}