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()
        else if(isset($_POST['submitComment']) && $comment->save())

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()
        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;
    $pages=new CPagination(Comment::model()->count($criteria));

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(
    )); ?> |
<?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.