0 follower

Managing Posts

Managing posts mainly refers to listing posts in an administrative view and deleting posts. They are accomplished by the admin operation and the delete operation, respectively. The code generated by yiic does not need much modification. Below we mainly explain how these two operations are implemented.

1. Listing Posts in Tabular View

The admin operation shows all posts (including both published and unpublished) in a tabular view. The view supports multi-column sorting and pagination. The following is the actionAdmin() method in PostController:

public function actionAdmin()
{
    $criteria=new CDbCriteria;
 
    $pages=new CPagination(Post::model()->count());
    $pages->applyLimit($criteria);
 
    $sort=new CSort('Post');
    $sort->defaultOrder='status ASC, createTime DESC';
    $sort->applyOrder($criteria);
 
    $posts=Post::model()->findAll($criteria);
 
    $this->render('admin',array(
        'posts'=>$posts,
        'pages'=>$pages,
        'sort'=>$sort,
    ));
}

The above code is very similar to that in actionList(). The main difference is that here we use a CSort object to represent the sorting informatin (e.g. which columns are being sorted in which directions). The CSort object is used by the admin view to generate appropriate hyperlinks in the table head cells. Clicking on a link would cause the current page to be refreshed and the data to be sorted along that column.

Below is the code for the admin view:

<h2>Manage Posts</h2>
 
<table class="dataGrid">
  <tr>
    <th><?php echo $sort->link('status'); ?></th>
    <th><?php echo $sort->link('title'); ?></th>
    <th><?php echo $sort->link('createTime'); ?></th>
    <th><?php echo $sort->link('updateTime'); ?></th>
  </tr>
<?php foreach($posts as $n=>$post): ?>
  <tr class="<?php echo $n%2?'even':'odd';?>">
    <td><?php echo CHtml::encode($post->statusText); ?></td>
    <td><?php echo CHtml::link(CHtml::encode($post->title),
        array('show','id'=>$post->id)); ?></td>
    <td><?php echo date('F j, Y',$post->createTime); ?></td>
    <td><?php echo date('F j, Y',$post->updateTime); ?></td>
  </tr>
<?php endforeach; ?>
</table>
 
<br/>
<?php $this->widget('CLinkPager',array('pages'=>$pages)); ?>

The code is very straight-forward. We iterate through the list of posts and display them in a table. In the head cells of the table, we use the CSort object to generate the hyperlinks for sorting purpose. And at the end, we embed a CLinkPager widget to display pagination buttons if needed.

Tip: When displaying text, we call CHtml::encode() to encode HTML entities in it. This prevents from cross-site scripting attack.

2. Deleting Posts

When a post is displayed using the show operation, we display a delete link if the current user is the system owner. Clicking on this button would cause the deletion of the post. Since the post deletion is causing the change of the server-side data, we use a POST request to trigger the deletion. We thus use the following code to generate the delete button:

<?php echo CHtml::linkButton('Delete',array(
   'submit'=>array('post/delete','id'=>$post->id),
   'confirm'=>"Are you sure to delete this post?",
)); ?>

The CHtml::linkButton() method generates a link button that is like the normal push button. Clicking on the link would cause the submission of the enclosing HTML form in POST method. Here we specify that the form should be submitted to the URL generated according to array('post/delete','id'=>$post->id). In our blog application, the generated URL would be /blog/index.php?r=post/delete&id=1, which refers to the delete action of PostController. We also specify that a confirmation dialog should pop up when clicking on this link. This gives the user a chance to re-consider his deletion request.

The code for the delete operation is self-explanatory. We are not going to explain here.

public function actionDelete()
{
    if(Yii::app()->request->isPostRequest)
    {
        // we only allow deletion via POST request
        $this->loadPost()->delete();
        $this->redirect(array('list'));
    }
    else
        throw new CHttpException(400,'Invalid request...');
}