Skip to content

Commit ef6cd29

Browse files
committed
add view page action #1
1 parent 189c2b1 commit ef6cd29

File tree

7 files changed

+133
-52
lines changed

7 files changed

+133
-52
lines changed

src/Bootstrap.php

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,18 @@
33
namespace nullref\cms;
44

55

6-
use yii\base\ActionEvent;
7-
use yii\base\Application;
86
use yii\base\BootstrapInterface;
9-
use yii\base\Controller;
10-
use yii\base\Event;
11-
use yii\web\ErrorAction;
12-
13-
class Bootstrap implements BootstrapInterface
14-
{
15-
public function bootstrap($app)
16-
{
17-
$prefix = $app->getModule('cms')->urlPrefix;
18-
$app->urlManager->addRules([
19-
$prefix.'/<route:[a-zA-Z0-9-/]+>' => '/cms/page/view'
20-
]);
21-
22-
<?php
23-
24-
namespace nullref\cms;
25-
26-
27-
use yii\base\ActionEvent;
28-
use yii\base\Application;
29-
use yii\base\BootstrapInterface;
30-
use yii\base\Controller;
31-
use yii\base\Event;
32-
use yii\web\ErrorAction;
337
use yii\web\Application as WebApplication;
348

359
class Bootstrap implements BootstrapInterface
3610
{
3711
public function bootstrap($app)
3812
{
39-
$prefix = $app->getModule('cms')->urlPrefix;
40-
$app->urlManager->addRules([
41-
$prefix . '/<route:[a-zA-Z0-9-/]+>' => '/cms/page/view'
42-
]);
43-
4413
if ($app instanceof WebApplication) {
14+
$prefix = $app->getModule('cms')->urlPrefix;
15+
$app->urlManager->addRules([
16+
$prefix . '/<route:[a-zA-Z0-9-/]+>' => '/cms/page/view'
17+
]);
4518
$app->controllerMap['elfinder-backend'] = [
4619
'class' => 'mihaildev\elfinder\Controller',
4720
'user' => 'admin',
@@ -58,7 +31,3 @@ public function bootstrap($app)
5831
}
5932

6033
}
61-
62-
}
63-
64-
}

src/actions/PageView.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace nullref\cms\actions;
4+
5+
use nullref\cms\models\Page;
6+
use Yii;
7+
use yii\base\Action;
8+
use yii\web\NotFoundHttpException;
9+
10+
11+
/**
12+
* @author Dmytro Karpovych
13+
* @copyright 2015 NRE
14+
*/
15+
class PageView extends Action
16+
{
17+
public $view = 'view';
18+
19+
public function run()
20+
{
21+
if (($route = Yii::$app->request->getQueryParam('route')) == null) {
22+
throw new NotFoundHttpException(Yii::t('cms','Page not found.'));
23+
}
24+
$page = Page::find()->byRoute($route)->one();
25+
if (!isset($page)) {
26+
throw new NotFoundHttpException(Yii::t('cms','Page not found.'));
27+
}
28+
if ($page->layout){
29+
$this->controller->layout = $page->layout;
30+
}
31+
return $this->controller->render($this->view, [
32+
'page' => $page,
33+
]);
34+
}
35+
}

src/components/RelatedBehavior.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class RelatedBehavior extends Behavior
2828
protected $_editedValues = [];
2929
/** @var ActiveRecord[][] */
3030
protected $_removedValues = [];
31+
/** @var ActiveRecord[][] */
32+
protected $_formValues = [];
3133

3234

3335
/**
@@ -50,20 +52,21 @@ public function beforeValidate()
5052
foreach ($this->fields as $name => $class) {
5153
$originalName = $name . $this->filedSuffix;
5254
foreach ($this->_newValues[$originalName] as $model) {
53-
if (!$model->validate()){
54-
$owner->addErrors(['items'=>$model->getErrors()]);
55+
if (!$model->validate()) {
56+
$owner->addErrors(['items' => $model->getErrors()]);
5557
return false;
5658
}
5759
}
5860
foreach ($this->_editedValues[$originalName] as $model) {
59-
if (!$model->validate()){
60-
$owner->addErrors(['items'=>$model->getErrors()]);
61+
if (!$model->validate()) {
62+
$owner->addErrors(['items' => $model->getErrors()]);
6163
return false;
6264
}
6365
}
6466
}
6567
return true;
6668
}
69+
6770
/**
6871
* @param $data
6972
* @param null $formName
@@ -81,6 +84,7 @@ public function loadWithRelations($data, $formName = null)
8184
foreach ($data[$relatedFormName] as $key => $item) {
8285
$models[$key] = new $class(array_merge($item, ['id' => $key]));
8386
}
87+
$this->_formValues[$name . $this->filedSuffix] = $models;
8488
$owner->{$name . $this->filedSuffix} = ($models);
8589
}
8690
}
@@ -149,6 +153,9 @@ public function __get($name)
149153
if ($owner->isNewRecord) {
150154
return $this->_newValues[$name];
151155
}
156+
if (!(empty($this->_newValues[$name]) && empty($this->_editedValues[$name]) && empty($this->_removedValues[$name]))) {
157+
return $this->_formValues[$name];
158+
}
152159
return $owner->{$originalName};
153160
}
154161
return parent::__get($name);

src/controllers/PageController.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
<?php
22

33
namespace nullref\cms\controllers;
4-
use nullref\cms\models\Page;
4+
5+
use nullref\cms\actions\PageView;
56
use yii\web\Controller;
6-
use yii\web\NotFoundHttpException;
77

88

99
/**
1010
* Class PageController
1111
*/
1212
class PageController extends Controller
1313
{
14-
public function actionView($route)
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function actions()
1518
{
16-
$page = Page::find()->byRoute($route)->one();
17-
if (!isset($page)){
18-
throw new NotFoundHttpException();
19-
}
20-
return $this->render('view',[
21-
'page'=>$page,
22-
]);
19+
return [
20+
'view' => [
21+
'class' => PageView::className(),
22+
]
23+
];
2324
}
25+
2426
}

src/models/Page.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use nullref\cms\components\RelatedBehavior;
66
use Yii;
7+
use yii\base\Exception;
78
use yii\behaviors\TimestampBehavior;
89
use yii\db\ActiveRecord;
910

@@ -39,8 +40,8 @@ public static function tableName()
3940
public function behaviors()
4041
{
4142
return [
42-
'timestamp'=>[
43-
'class'=>TimestampBehavior::className(),
43+
'timestamp' => [
44+
'class' => TimestampBehavior::className(),
4445
'createdAtAttribute' => 'createdAt',
4546
'updatedAtAttribute' => 'updatedAt',
4647
],
@@ -53,6 +54,16 @@ public function behaviors()
5354
];
5455
}
5556

57+
/**
58+
* @inheritdoc
59+
*/
60+
public function attributeHints()
61+
{
62+
return [
63+
'layout' => Yii::t('cms', 'e.g. @app/views/layouts/main'),
64+
];
65+
}
66+
5667

5768
/**
5869
* @inheritdoc
@@ -63,9 +74,26 @@ public function rules()
6374
[['route', 'title', 'layout'], 'required'],
6475
[['createdAt', 'updatedAt'], 'integer'],
6576
[['route', 'title', 'layout'], 'string', 'max' => 255],
77+
[['layout'], 'validateAlias'],
6678
];
6779
}
6880

81+
/**
82+
* @param $attribute
83+
*/
84+
public function validateAlias($attribute)
85+
{
86+
try {
87+
88+
$path = Yii::getAlias($this->{$attribute});
89+
if (!(file_exists($path) || file_exists($path . '.php'))) {
90+
$this->addError($attribute, Yii::t('cms', 'Layout file must exist'));
91+
}
92+
} catch (\Exception $e) {
93+
$this->addError($attribute, $e->getMessage());
94+
}
95+
}
96+
6997
/**
7098
* @inheritdoc
7199
*/
@@ -95,6 +123,6 @@ public static function find()
95123
*/
96124
public function getItems()
97125
{
98-
return $this->hasMany(PageHasBlock::className(),['page_id'=>'id'])->orderBy(['order'=>SORT_ASC]);
126+
return $this->hasMany(PageHasBlock::className(), ['page_id' => 'id'])->orderBy(['order' => SORT_ASC]);
99127
}
100128
}

src/views/admin/page/_form.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ function addBlock(data) {
6464
}
6565
JS
6666
);
67+
68+
$this->registerCss(<<<CSS
69+
.hint-block {
70+
font-size: 12px;
71+
padding: 2px;
72+
opacity: 0.7;
73+
}
74+
75+
.hint-block:hover {
76+
opacity: 1;
77+
}
78+
CSS
79+
);
6780
?>
6881
<div class="hide">
6982
<li class="list-group-item" id="pageItemTmpl">

src/views/layouts/clear.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
use yii\helpers\Html;
3+
4+
/** @var $this \yii\web\View */
5+
/** @var $content string */
6+
?>
7+
<?php $this->beginPage() ?>
8+
<!DOCTYPE html>
9+
<html lang="<?= Yii::$app->language ?>">
10+
<head>
11+
<meta charset="<?= Yii::$app->charset ?>">
12+
<meta name="viewport" content="width=device-width, initial-scale=1">
13+
<?= Html::csrfMetaTags() ?>
14+
<title><?= Html::encode($this->title) ?></title>
15+
<?php $this->head() ?>
16+
</head>
17+
<body>
18+
19+
<?php $this->beginBody() ?>
20+
21+
<?= $content ?>
22+
23+
<?php $this->endBody() ?>
24+
</body>
25+
</html>
26+
<?php $this->endPage() ?>
27+

0 commit comments

Comments
 (0)