Skip to content

Commit 43da329

Browse files
committed
V1.6
完成评论管理功能
1 parent 74ba078 commit 43da329

File tree

5 files changed

+163
-22
lines changed

5 files changed

+163
-22
lines changed

backend/controllers/CommentController.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,23 @@ protected function findModel($id)
121121
throw new NotFoundHttpException('The requested page does not exist.');
122122
}
123123
}
124+
125+
public function actionApprove($id)
126+
{
127+
$model = $this->findModel($id);
128+
if($model->approve()) //审核
129+
{
130+
return $this->redirect(['index']);
131+
}
132+
}
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
124143
}

backend/views/comment/index.php

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,99 @@
22

33
use yii\helpers\Html;
44
use yii\grid\GridView;
5+
use common\models\Commentstatus;
6+
use yii\grid\Column;
57

68
/* @var $this yii\web\View */
79
/* @var $searchModel common\models\CommentSearch */
810
/* @var $dataProvider yii\data\ActiveDataProvider */
911

10-
$this->title = 'Comments';
12+
$this->title = '评论管理';
1113
$this->params['breadcrumbs'][] = $this->title;
1214
?>
1315
<div class="comment-index">
1416

1517
<h1><?= Html::encode($this->title) ?></h1>
1618
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
1719

18-
<p>
19-
<?= Html::a('Create Comment', ['create'], ['class' => 'btn btn-success']) ?>
20-
</p>
20+
2121
<?= GridView::widget([
2222
'dataProvider' => $dataProvider,
2323
'filterModel' => $searchModel,
2424
'columns' => [
25-
['class' => 'yii\grid\SerialColumn'],
25+
// ['class' => 'yii\grid\SerialColumn'],
2626

27-
'id',
28-
'content:ntext',
29-
'status',
30-
'create_time:datetime',
31-
'userid',
27+
//'id',
28+
[
29+
'attribute'=>'id',
30+
'contentOptions'=>['width'=>'30px'],
31+
],
32+
//'content:ntext',
33+
[
34+
'attribute'=>'content',
35+
'value'=>'beginning',
36+
// 'value'=>function($model)
37+
// {
38+
// $tmpStr=strip_tags($model->content);
39+
// $tmpLen=mb_strlen($tmpStr);
40+
41+
// return mb_substr($tmpStr,0,20,'utf-8').(($tmpLen>20)?'...':'');
42+
// }
43+
],
44+
//'userid',
45+
[
46+
'attribute'=>'user.username',
47+
'label'=>'作者',
48+
'value'=>'user.username',
49+
],
50+
//'status',
51+
[
52+
'attribute'=>'status',
53+
'value'=>'status0.name',
54+
'filter'=>Commentstatus::find()
55+
->select(['name','id'])
56+
->orderBy('position')
57+
->indexBy('id')
58+
->column(),
59+
'contentOptions'=>
60+
function($model)
61+
{
62+
return ($model->status==1)?['class'=>'bg-danger']:[];
63+
}
64+
],
65+
// 'create_time:datetime',
66+
[
67+
'attribute'=>'create_time',
68+
'format'=>['date','php:m-d H:i'],
69+
],
70+
3271
// 'email:email',
3372
// 'url:url',
3473
// 'post_id',
74+
'post.title',
75+
76+
[
77+
'class' => 'yii\grid\ActionColumn',
78+
'template'=>'{view} {update} {delete} {approve}',
79+
'buttons'=>
80+
[
81+
'approve'=>function($url,$model,$key)
82+
{
83+
$options=[
84+
'title'=>Yii::t('yii', '审核'),
85+
'aria-label'=>Yii::t('yii','审核'),
86+
'data-confirm'=>Yii::t('yii','你确定通过这条评论吗?'),
87+
'data-method'=>'post',
88+
'data-pjax'=>'0',
89+
];
90+
return Html::a('<span class="glyphicon glyphicon-check"></span>',$url,$options);
91+
92+
},
93+
],
94+
3595

36-
['class' => 'yii\grid\ActionColumn'],
96+
97+
],
3798
],
3899
]); ?>
39100
</div>

backend/views/layouts/main.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use yii\bootstrap\NavBar;
1010
use yii\widgets\Breadcrumbs;
1111
use common\widgets\Alert;
12+
use common\models\Comment;
1213

1314
AppAsset::register($this);
1415
?>
@@ -37,6 +38,7 @@
3738
$menuItems = [
3839
['label' => '文章管理', 'url' => ['/post/index']],
3940
['label' => '评论管理', 'url' => ['/comment/index']],
41+
'<li><span class="badge">'.Comment::getPengdingCommentCount().'</span></li>',
4042
['label' => '用户管理', 'url' => ['/user/index']],
4143
['label' => '管理员', 'url' => ['/adminuser/index']],
4244
];

common/models/Comment.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public function attributeLabels()
5353
{
5454
return [
5555
'id' => 'ID',
56-
'content' => 'Content',
57-
'status' => 'Status',
58-
'create_time' => 'Create Time',
59-
'userid' => 'Userid',
56+
'content' => '内容',
57+
'status' => '状态',
58+
'create_time' => '发布时间',
59+
'userid' => '用户',
6060
'email' => 'Email',
6161
'url' => 'Url',
62-
'post_id' => 'Post ID',
62+
'post_id' => '文章',
6363
];
6464
}
6565

@@ -86,4 +86,37 @@ public function getUser()
8686
{
8787
return $this->hasOne(User::className(), ['id' => 'userid']);
8888
}
89+
90+
public function getBeginning()
91+
{
92+
$tmpStr = strip_tags($this->content);
93+
$tmpLen = mb_strlen($tmpStr);
94+
95+
return mb_substr($tmpStr,0,10,'utf-8').(($tmpLen>10)?'...':'');
96+
}
97+
98+
public function approve()
99+
{
100+
$this->status = 2; //设置评论状态为已审核
101+
return ($this->save()?true:false);
102+
}
103+
104+
public static function getPengdingCommentCount()
105+
{
106+
return Comment::find()->where(['status'=>1])->count();
107+
}
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
89122
}

common/models/CommentSearch.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
*/
1313
class CommentSearch extends Comment
1414
{
15+
public function attributes()
16+
{
17+
return array_merge(parent::attributes(),['user.username']);
18+
}
1519
/**
1620
* @inheritdoc
1721
*/
1822
public function rules()
1923
{
2024
return [
2125
[['id', 'status', 'create_time', 'userid', 'post_id'], 'integer'],
22-
[['content', 'email', 'url'], 'safe'],
26+
[['content', 'email', 'url','user.username'], 'safe'],
2327
];
2428
}
2529

@@ -59,17 +63,39 @@ public function search($params)
5963

6064
// grid filtering conditions
6165
$query->andFilterWhere([
62-
'id' => $this->id,
63-
'status' => $this->status,
66+
'comment.id' => $this->id,
67+
'comment.status' => $this->status,
6468
'create_time' => $this->create_time,
6569
'userid' => $this->userid,
6670
'post_id' => $this->post_id,
6771
]);
6872

6973
$query->andFilterWhere(['like', 'content', $this->content])
70-
->andFilterWhere(['like', 'email', $this->email])
71-
->andFilterWhere(['like', 'url', $this->url]);
72-
74+
->andFilterWhere(['like', 'email', $this->email])
75+
->andFilterWhere(['like', 'url', $this->url]);
76+
77+
$query->join('INNER JOIN','user','comment.userid = user.id');
78+
$query->andFilterWhere(['like','user.username',$this->getAttribute('user.username')]);
79+
80+
$dataProvider->sort->attributes['user.username'] =
81+
[
82+
'asc'=>['user.username'=>SORT_ASC],
83+
'desc'=>['user.username'=>SORT_DESC],
84+
];
85+
86+
$dataProvider->sort->defaultOrder =
87+
[
88+
'status'=>SORT_ASC,
89+
'id'=>SORT_DESC,
90+
];
91+
92+
93+
94+
95+
96+
97+
98+
7399
return $dataProvider;
74100
}
75101
}

0 commit comments

Comments
 (0)