其实日志评论功能和日志本身很类似,不过评论功能是基于日志的,所以,评论的增删改查行为基本都基于日志发生的,由gii生成的curd改动不是很大。需要做的反而是在日志的增删改查或者列表页面上的入口的添加。
首先是日志评论的显示。我并没有在数据库建立实际的外键关系,首先还是要在comment类里建立与日志的关联关系并定义评论的验证规则。
1 public function rules() {
2 return array(
3 array('content,author,email', 'required'),
4 array('content', 'length', 'max' => 255),
5 array('author, email, url', 'length', 'max' => 50),
6 array('email', 'email'),
7 array('url', 'url'),
8 );
9 }
10 public function relations() {
11 return array(
12 'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
13 );
14 }
2 return array(
3 array('content,author,email', 'required'),
4 array('content', 'length', 'max' => 255),
5 array('author, email, url', 'length', 'max' => 50),
6 array('email', 'email'),
7 array('url', 'url'),
8 );
9 }
10 public function relations() {
11 return array(
12 'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
13 );
14 }
1 public function beforeSave() {
2 if (parent::beforeSave()) {
3 if ($this->isNewRecord)
4 $this->create_time = date("Y-m-d H:i:s", time());
5 return true;
6 }else {
7 return false;
8 }
9 }
2 if (parent::beforeSave()) {
3 if ($this->isNewRecord)
4 $this->create_time = date("Y-m-d H:i:s", time());
5 return true;
6 }else {
7 return false;
8 }
9 }
评论的显示是随着日志展现的。所以是在日志的视图里房子改日志的评论,同时加入评论的表单供用户可以实时创建评论。对日志的试图进行如下修改,添加如下内容。同时view/post/下需要行政评论的视图文件。
1 <div id="comments">
2 <?php if($model->commentCount >= 1):?>
3 <h3>
4 <?php echo $model->commentCount . "comment(s)";?>
5 </h3>
6 <?php $this->renderPartial('_comments', array(
7 'post' => $model,
8 'comments' => $model->comments,
9 ));?>
10 <?php endif;?>
11 </div>
2 <?php if($model->commentCount >= 1):?>
3 <h3>
4 <?php echo $model->commentCount . "comment(s)";?>
5 </h3>
6 <?php $this->renderPartial('_comments', array(
7 'post' => $model,
8 'comments' => $model->comments,
9 ));?>
10 <?php endif;?>
11 </div>
1 <?php foreach ($comments as $comment):?>
2 <div class="comment" id="c<?php echo $comment->id; ?>">
3 <?php echo CHtml::link("#{$comment->id}", $comment->getUrl($post), array(
4 'class' => 'cid',
5 'title' => 'Permalink to this comment',
6 ));?>
7 <div class="author">
8 <?php echo $comment->authorLink;?>says:
9 </div>
10 <div class="time">
11 <?php echo $comment->create_time;?>
12 </div>
13 <div class="content">
14 <?php echo nl2br(CHtml::encode($comment->content));?>
15 </div>
16 </div>
17 <?php endforeach;?>
2 <div class="comment" id="c<?php echo $comment->id; ?>">
3 <?php echo CHtml::link("#{$comment->id}", $comment->getUrl($post), array(
4 'class' => 'cid',
5 'title' => 'Permalink to this comment',
6 ));?>
7 <div class="author">
8 <?php echo $comment->authorLink;?>says:
9 </div>
10 <div class="time">
11 <?php echo $comment->create_time;?>
12 </div>
13 <div class="content">
14 <?php echo nl2br(CHtml::encode($comment->content));?>
15 </div>
16 </div>
17 <?php endforeach;?>
1 public function getUrl($post = null) {
2 if ($post === null)
3 $post = $this->post;
4 return $post->url . '#c' . $this->id;
5 }
6 public function getAuthorLink() {
7 if (!empty($this->url))
8 return CHtml::link(CHtml::encode($this->author), $this->url);
9 else
10 return CHtml::encode($this->author);
11 }
2 if ($post === null)
3 $post = $this->post;
4 return $post->url . '#c' . $this->id;
5 }
6 public function getAuthorLink() {
7 if (!empty($this->url))
8 return CHtml::link(CHtml::encode($this->author), $this->url);
9 else
10 return CHtml::encode($this->author);
11 }
然后就是要在查询日志记录时顺便查询日志的相关评论信息,对post控制器做如下修改
1 postcontroller
2 public function actionView() {
3 $post = $this->loadModel();
4 $comment = $this->newComment($post);
5 $this->render('view', array(
6 'model' => $post,
7 'comment' => $comment,
8 ));
9 }
2 public function actionView() {
3 $post = $this->loadModel();
4 $comment = $this->newComment($post);
5 $this->render('view', array(
6 'model' => $post,
7 'comment' => $comment,
8 ));
9 }
1 protected function newComment($post){
2 $comment = new Comment;
3 if(isset($_POST['ajax']) && $_POST['ajax'] == 'comment-form'){
4 echo CActiveForm::validate($comment);
5 Yii::app()->end();
6 }
7 if(isset($_POST['Comment'])){
8 $comment->attributes = $_POST['Comment'];
9 if($post->addComment($comment)){
10 if($comment->status == Comment::STATUS_PENDING)
11 Yii::app()->user->setFlash('commentSubmitted', 'thank you...');
12 $this->refresh();
13 }
14 }
15 return $comment;
16 }
2 $comment = new Comment;
3 if(isset($_POST['ajax']) && $_POST['ajax'] == 'comment-form'){
4 echo CActiveForm::validate($comment);
5 Yii::app()->end();
6 }
7 if(isset($_POST['Comment'])){
8 $comment->attributes = $_POST['Comment'];
9 if($post->addComment($comment)){
10 if($comment->status == Comment::STATUS_PENDING)
11 Yii::app()->user->setFlash('commentSubmitted', 'thank you...');
12 $this->refresh();
13 }
14 }
15 return $comment;
16 }
评论的添加是在日志相关评论的下方提供表单
1 post.php
2 public function addComment($comment){
3 if(Yii::app()->params['commentNeedApproval'])
4 $comment->status = Comment::STATUS_PENDING;
5 else
6 $comment->status = Comment::STATUS_APPROVED;
7 $comment->post_id = $this->id;
8 return $comment->save();
9 }
2 public function addComment($comment){
3 if(Yii::app()->params['commentNeedApproval'])
4 $comment->status = Comment::STATUS_PENDING;
5 else
6 $comment->status = Comment::STATUS_APPROVED;
7 $comment->post_id = $this->id;
8 return $comment->save();
9 }
然后再次对comment的视图文件进行修改,加上ajax提交的效果
1 <h3>发表新留言</h3>
2 <?php if(Yii::app()->user->hasFlash('commentSubmitted')):?>
3 <div class="flash-success">
4 <?php echo Yii::app()->user->getFlash('commentSubmitted');?>
5 </div>
6 <?php else:?>
7 <?php $this->renderPartial('/comment/_form', array(
8 'model' => $comment,
9 ));?>
10 <?php endif;?>
2 <?php if(Yii::app()->user->hasFlash('commentSubmitted')):?>
3 <div class="flash-success">
4 <?php echo Yii::app()->user->getFlash('commentSubmitted');?>
5 </div>
6 <?php else:?>
7 <?php $this->renderPartial('/comment/_form', array(
8 'model' => $comment,
9 ));?>
10 <?php endif;?>
这样,基于日志的评论的增改和现实就完成了。
日志的评论应该有审核的功能,这样就需要开设评论的审核页面。并且不应该任何人都可以审核评论,我设计了只有admin账号可以审核评论的权限控制访问。评论的管理页就是按创建时间降序列出所有评论,同时未审核的评论放在前面,并提供审核的按钮,同时还有删除,修改的操作按钮。
1 public function actionIndex() {
2 $dataProvider = new CActiveDataProvider('Comment', array(
3 'criteria' => array(
4 'with' => 'post',
5 'order' => 't.status,t.create_time DESC',
6 ),
7 ));
8 $this->render('index', array(
9 'dataProvider' => $dataProvider,
10 ));
11 }
2 $dataProvider = new CActiveDataProvider('Comment', array(
3 'criteria' => array(
4 'with' => 'post',
5 'order' => 't.status,t.create_time DESC',
6 ),
7 ));
8 $this->render('index', array(
9 'dataProvider' => $dataProvider,
10 ));
11 }
在关系查询中,我们使用t作为主表的别名。审核的动作如下。
1 public function actionApprove() {
2 if (Yii::app()->request->isPostRequest) {
3 $comment = $this->loadModel($_GET['id']);
4 $comment->approve();
5 $this->redirect(array('index'));
6 } else {
7 throw new CHttpException(400, '非法访问');
8 }
9 }
2 if (Yii::app()->request->isPostRequest) {
3 $comment = $this->loadModel($_GET['id']);
4 $comment->approve();
5 $this->redirect(array('index'));
6 } else {
7 throw new CHttpException(400, '非法访问');
8 }
9 }
审核功能其实就是把状态重置一下。
1 public function approve() {
2 $this->status = Comment::STATUS_APPROVED;
3 $this->update(array('status'));
4 }
2 $this->status = Comment::STATUS_APPROVED;
3 $this->update(array('status'));
4 }
然后是列表视图
1 <div class="comment" id="c<?php echo $data->id; ?>">
2
3 <?php
4 echo CHtml::link("#link{$data->id}", $data->url, array(
5 'class' => 'cid',
6 'title' => 'Permalink to this comment',
7 ));
8 ?>
9
10 <div class="author">
11 <?php echo $data->authorLink;?>says on
12 <?php echo CHtml::link(CHtml::encode($data->post->title), $data->post->url);?>
13 </div>
14
15 <div class="time">
16 <?php if($data->status == Comment::STATUS_PENDING):?>
17 <span class="pending">Pending approval</span>|
18 <?php echo CHtml::linkButton('Approve', array(
19 'submit' => array('comment/approve', 'id' => $data->id),
20 ))?>|
21 <?php endif;?>
22 <?php echo CHtml::link('Update', array('comment/update', 'id' => $data->id));?>
23 <?php echo CHtml::linkButton('Delete', array(
24 'submit' => array('comment/delete', 'id' => $data->id),
25 'confirm' => "你确定删除此条评论#{$data->id}?",
26 ));?>|
27 <?php echo $data->create_time;?>
28 </div>
29
30 <div class="content">
31 <?php echo nl2br(CHtml::encode($data->content));?>
32 </div>
33
34 </div>
2
3 <?php
4 echo CHtml::link("#link{$data->id}", $data->url, array(
5 'class' => 'cid',
6 'title' => 'Permalink to this comment',
7 ));
8 ?>
9
10 <div class="author">
11 <?php echo $data->authorLink;?>says on
12 <?php echo CHtml::link(CHtml::encode($data->post->title), $data->post->url);?>
13 </div>
14
15 <div class="time">
16 <?php if($data->status == Comment::STATUS_PENDING):?>
17 <span class="pending">Pending approval</span>|
18 <?php echo CHtml::linkButton('Approve', array(
19 'submit' => array('comment/approve', 'id' => $data->id),
20 ))?>|
21 <?php endif;?>
22 <?php echo CHtml::link('Update', array('comment/update', 'id' => $data->id));?>
23 <?php echo CHtml::linkButton('Delete', array(
24 'submit' => array('comment/delete', 'id' => $data->id),
25 'confirm' => "你确定删除此条评论#{$data->id}?",
26 ));?>|
27 <?php echo $data->create_time;?>
28 </div>
29
30 <div class="content">
31 <?php echo nl2br(CHtml::encode($data->content));?>
32 </div>
33
34 </div>
至此,blog系统的架子也基本完成了。