Skip to content

Commit 0fa4e31

Browse files
committed
small enhancements
1 parent ef6cd29 commit 0fa4e31

File tree

6 files changed

+71
-27
lines changed

6 files changed

+71
-27
lines changed

src/controllers/admin/BlockController.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace nullref\cms\controllers\admin;
44

5-
use Yii;
5+
use nullref\admin\components\AdminController;
66
use nullref\cms\models\Block;
7+
use Yii;
78
use yii\data\ActiveDataProvider;
8-
use nullref\admin\components\AdminController;
9-
use yii\web\NotFoundHttpException;
109
use yii\filters\VerbFilter;
10+
use yii\web\NotFoundHttpException;
1111

1212
/**
1313
* BlockController implements the CRUD actions for Block model.
@@ -26,32 +26,36 @@ public function behaviors()
2626
];
2727
}
2828

29-
public function actionConfig()
29+
public function actionConfig($id = null)
3030
{
3131
/** @var Block $model */
32-
$model = Yii::$app->session->get('new-block');
33-
if (!$model) {
34-
$this->redirect('create');
32+
if ($id === null) {
33+
$model = Yii::$app->session->get('new-block');
34+
if (!$model) {
35+
$this->redirect('create');
36+
}
37+
} else {
38+
$model = $this->findModel($id);
3539
}
3640
/** @var \nullref\cms\components\BlockManager $blockManager */
3741
$blockManager = Yii::$app->getModule('cms')->get('blockManager');
3842

3943
/** @var \nullref\cms\components\Block $block */
4044
$block = $blockManager->getBlock($model->class_name);
4145

42-
if (!$model->isNewRecord){
46+
if (!$model->isNewRecord) {
4347
$block->setAttributes($model->getData());
4448
}
4549

46-
if ($block->load(Yii::$app->request->post()) && ($block->validate())){
50+
if ($block->load(Yii::$app->request->post()) && ($block->validate())) {
4751
$model->setData($block);
4852
$model->save();
4953
Yii::$app->session->remove('new-block');
50-
return $this->redirect(['view','id'=>$model->id]);
54+
return $this->redirect(['view', 'id' => $model->id]);
5155
}
5256

53-
return $this->render('config',[
54-
'block'=>$block,
57+
return $this->render('config', [
58+
'block' => $block,
5559
]);
5660
}
5761

src/models/Block.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* @property string $config
1717
* @property integer $createdAt
1818
* @property integer $updatedAt
19+
*
20+
* @property Page[] $pages
1921
*/
2022
class Block extends ActiveRecord
2123
{
@@ -49,6 +51,7 @@ public function rules()
4951
return [
5052
[['id', 'class_name'], 'required'],
5153
[['config'], 'string'],
54+
[['id'], 'unique'],
5255
[['createdAt', 'updatedAt'], 'integer'],
5356
[['id', 'class_name'], 'string', 'max' => 255],
5457
];
@@ -63,6 +66,7 @@ public function attributeLabels()
6366
'id' => Yii::t('cms', 'ID'),
6467
'class_name' => Yii::t('cms', 'Block Type'),
6568
'config' => Yii::t('cms', 'Config'),
69+
'pages' => Yii::t('cms', 'Pages'),
6670
'createdAt' => Yii::t('cms', 'Created At'),
6771
'updatedAt' => Yii::t('cms', 'Updated At'),
6872
];
@@ -104,4 +108,12 @@ public function getWidget()
104108
{
105109
return CMSBlock::getManager()->getWidget($this->id);
106110
}
111+
112+
/**
113+
* @return $this
114+
*/
115+
public function getPages()
116+
{
117+
return $this->hasMany(Page::className(),['id'=>'page_id'])->viaTable(PageHasBlock::tableName(),['block_id'=>'id']);
118+
}
107119
}

src/models/Page.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use nullref\cms\components\RelatedBehavior;
66
use Yii;
7-
use yii\base\Exception;
87
use yii\behaviors\TimestampBehavior;
98
use yii\db\ActiveRecord;
109

src/views/admin/block/index.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3-
use yii\helpers\Html;
3+
use nullref\cms\models\Block;
4+
use nullref\cms\models\Page;
45
use yii\grid\GridView;
6+
use yii\helpers\Html;
57

68
/* @var $this yii\web\View */
79
/* @var $dataProvider yii\data\ActiveDataProvider */
@@ -11,13 +13,13 @@
1113
?>
1214
<div class="block-index">
1315

14-
<div class="row">
15-
<div class="col-lg-12">
16-
<h1 class="page-header">
17-
<?= Html::encode($this->title) ?>
18-
</h1>
16+
<div class="row">
17+
<div class="col-lg-12">
18+
<h1 class="page-header">
19+
<?= Html::encode($this->title) ?>
20+
</h1>
21+
</div>
1922
</div>
20-
</div>
2123

2224

2325
<p>
@@ -32,13 +34,35 @@
3234
'id',
3335
[
3436
//'filter'=>\nullref\cms\components\Block::getManager()->getDropDownArray(),
35-
'attribute'=>'class_name',
36-
'value'=>'typeName'
37+
'attribute' => 'class_name',
38+
'value' => 'typeName'
3739
],
3840
'createdAt:datetime',
3941
'updatedAt:datetime',
40-
41-
['class' => 'yii\grid\ActionColumn'],
42+
[
43+
'attribute'=>'pages',
44+
'format'=>'html',
45+
'value'=>function(Block $model){
46+
$names = \yii\helpers\ArrayHelper::getColumn($model->pages,function(Page $page){
47+
return Html::a($page->title,['/cms/admin/page/update','id'=>$page->id],['class'=>'label label-primary']);
48+
});
49+
return implode(' ', $names);
50+
}
51+
],
52+
[
53+
'class' => 'yii\grid\ActionColumn',
54+
'template' => '{view} {update} {config} {delete}',
55+
'buttons' => [
56+
'config' => function ($url, Block $model, $key) {
57+
$options = [
58+
'title' => Yii::t('cms', 'Config'),
59+
'aria-label' => Yii::t('cms', 'Config'),
60+
'data-pjax' => '0',
61+
];
62+
return Html::a('<span class="glyphicon glyphicon-cog"></span>', $url, $options);
63+
}
64+
]
65+
],
4266
],
4367
]); ?>
4468

src/views/admin/block/view.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<p>
2424
<?= Html::a(Yii::t('cms', 'List'), ['index'], ['class' => 'btn btn-success']) ?>
2525
<?= Html::a(Yii::t('cms', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
26+
<?= Html::a(Yii::t('cms', 'Config'), ['config', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
2627
<?= Html::a(Yii::t('cms', 'Delete'), ['delete', 'id' => $model->id], [
2728
'class' => 'btn btn-danger',
2829
'data' => [

src/views/admin/page/_form.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ function addBlock(data) {
7575
.hint-block:hover {
7676
opacity: 1;
7777
}
78+
79+
.page-items-list .list-group-item {
80+
cursor: pointer;
81+
}
7882
CSS
7983
);
8084
?>
@@ -109,8 +113,8 @@ class="btn btn-danger btn-xs"
109113
</div>
110114
<div class="col-md-4">
111115
<div class="panel panel-default">
112-
<div class="panel-heading">Page Content</div>
113-
<ul class="list-group" id="pageItemsList">
116+
<div class="panel-heading"><?= Yii::t('cms','Page Content') ?></div>
117+
<ul class="list-group page-items-list" id="pageItemsList">
114118
<?php foreach ($model->items_list as $item): ?>
115119

116120

@@ -133,7 +137,7 @@ class="btn btn-danger btn-xs"
133137
</div>
134138
<div class="col-md-4">
135139
<div class="panel panel-default">
136-
<div class="panel-heading">Available Blocks</div>
140+
<div class="panel-heading"><?= Yii::t('cms','Available Blocks') ?></div>
137141
<ul class="list-group" id="blocksList">
138142
<?php foreach ($blocks as $id => $block): ?>
139143
<li class="list-group-item">

0 commit comments

Comments
 (0)