0 follower

Managing Comments

Comment management includes updating, deleting and approving comments. These operations are implemented as actions in the CommentController class.

1. Updating and Deleting Comments

The code generated by yiic for updating and deleting comments remains largely unchanged. Because we support comment preview when updating a comment, we only need to change the actionUpdate() method of CommentController as follows,

public function actionUpdate()
{
    $comment=$this->loadComment();
 
    if(isset($_POST['Comment']))
    {
        $comment->attributes=$_POST['Comment'];
        if(isset($_POST['previewComment']))
            $comment->validate('update');
        else if(isset($_POST['submitComment']) && $comment->save())
            $this->redirect(array('post/show',
                'id'=>$comment->postId,
                '#'=>'c'.$comment->id));
    }
 
    $this->render('update',array('comment'=>$comment));
}

It is very similar to that in PostController.

2. Approving Comments

When comments are newly created, they are in pending approval status and need to be approved in order to be visible to guest users. Approving a comment is mainly about changing the status column of the comment.

We create an actionApprove() method in CommentController as follows,

public function actionApprove()
{
    if(Yii::app()->request->isPostRequest)
    {
        $comment=$this->loadComment();
        $comment->approve();
        $this->redirect(array('post/show',
            'id'=>$comment->postId,
            '#'=>'c'.$comment->id));
    }
    else
        throw new CHttpException(400,'Invalid request...');
}

In the above, when the approve action is invoked via a POST request, we call the approve() method defined in the Comment model to change the status. We then redirect the user browser to the page displaying the post that this comment belongs to.

We also modify the actionList() method of Comment to show a list of comments pending approval.

public function actionList()
{
    $criteria=new CDbCriteria;
    $criteria->condition='Comment.status='.Comment::STATUS_PENDING;
 
    $pages=new CPagination(Comment::model()->count($criteria));
    $pages->pageSize=self::PAGE_SIZE;
    $pages->applyLimit($criteria);
 
    $comments=Comment::model()->with('post')->findAll($criteria);
 
    $this->render('list',array(
        'comments'=>$comments,
        'pages'=>$pages,
    ));
}

In the list view, we display the detail of every comment that is pending approval. In particular, we show an approve link button as follows,

<?php if($comment->status==Comment::STATUS_PENDING): ?>
    <span class="pending">Pending approval</span> |
    <?php echo CHtml::linkButton('Approve', array(
        'submit'=>array('comment/approve','id'=>$comment->id),
    )); ?> |
<?php endif; ?>

We use CHtml::linkButton() instead of CHtml::link() because the former would trigger a POST request while the latter a GET request. It is recommended that a GET request should not alter the data on the server. Otherwise, we face the danger that a user may inadvertently change the server-side data several times if he refreshes the page.