1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\cms\actions; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Action; |
7
|
|
|
use yii\helpers\ArrayHelper; |
8
|
|
|
use yii\web\NotFoundHttpException; |
9
|
|
|
use yii2mod\cms\models\CmsModel; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PageAction |
13
|
|
|
* @package yii2mod\cms\actions |
14
|
|
|
*/ |
15
|
|
|
class PageAction extends Action |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string custom layout |
19
|
|
|
*/ |
20
|
|
|
public $layout; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string Page path |
24
|
|
|
*/ |
25
|
|
|
public $view = '@vendor/yii2mod/yii2-cms/views/page'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var mixed pageId |
29
|
|
|
*/ |
30
|
|
|
public $pageId; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array base template params |
34
|
|
|
*/ |
35
|
|
|
public $baseTemplateParams = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initializes the object. |
39
|
|
|
*/ |
40
|
|
|
public function init() |
41
|
|
|
{ |
42
|
|
|
if (empty($this->pageId)) { |
43
|
|
|
$this->pageId = Yii::$app->request->get('pageId'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!empty($this->layout)) { |
47
|
|
|
$this->controller->layout = $this->layout; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
parent::init(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Run action |
55
|
|
|
* |
56
|
|
|
* @throws \yii\web\NotFoundHttpException |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function run() |
60
|
|
|
{ |
61
|
|
|
$model = $this->findModel(); |
62
|
|
|
$model->content = $this->parseBaseTemplateParams($model->content); |
63
|
|
|
|
64
|
|
|
return $this->controller->render($this->view, [ |
65
|
|
|
'model' => $model, |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Parse base template params, like {homeUrl} |
71
|
|
|
* |
72
|
|
|
* @param $pageContent |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function parseBaseTemplateParams($pageContent) |
76
|
|
|
{ |
77
|
|
|
$params = $this->getBaseTemplateParams(); |
78
|
|
|
$p = []; |
79
|
|
|
foreach ($params as $name => $value) { |
80
|
|
|
$p['{' . $name . '}'] = $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return strtr($pageContent, $p); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return base template params |
88
|
|
|
* |
89
|
|
|
* If one of this params exist in page content, it will be parsed |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function getBaseTemplateParams() |
94
|
|
|
{ |
95
|
|
|
return ArrayHelper::merge($this->baseTemplateParams, [ |
96
|
|
|
'homeUrl' => Yii::$app->urlManager->baseUrl, |
97
|
|
|
'siteName' => Yii::$app->name |
98
|
|
|
]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Find CmsModel |
103
|
|
|
* |
104
|
|
|
* @return null|CmsModel |
105
|
|
|
* @throws NotFoundHttpException |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
protected function findModel() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
if (($model = CmsModel::findOne($this->pageId)) !== null) { |
110
|
|
|
return $model; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
throw new NotFoundHttpException(Yii::t('yii2mod.cms', 'The requested page does not exist.')); |
114
|
|
|
} |
115
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.