---
Yii: Creating Demo Blog 3
Comments Management -
======================================================================================
======================================================================================
A) CUSTOMIZING COMMENT MODEL
1) Customizing rules() Method
1.1) Modify {webroot}/protected/models/Comment.php
Edit rules()
Public function rules()
{
return array(
array('content, author, email', 'required'),
array('author, email, url', 'length', 'max'=>128),
array('email','email'),
array('url','url'),
);
}
|
2) Customizing AttributeLabels() Method
Modify {webroot}/protected/models/Comment.php
Edit
public function attributeLabels()
{
return array(
'id' => 'Id',
'content' => 'Comment',
'status' => 'Status',
'create_time' => 'Create Time',
'author' => 'Name',
'email' => 'Email',
'url' => 'Website',
'post_id' => 'Post',
);
}
|
3) Customizing Saving Process
Modify {webroot}/protected/models/Comment.php
Add
protected function beforeSave()
{
if(parent::beforeSave())
{
if($this->isNewRecord)
$this->create_time=time();
return true;
}
else
return false;
}
|
4) Add additional methods for Comment
Modify {webroot}/protected/models/Comment.php
getURL()
getAuthorLink()
getPendingCommentCount()
findRecentComments()
/**
* @param Post the post that this comment belongs to. If null, the method
* will query for the post.
* @return string the permalink URL for this comment
*/
public function getUrl($post=null)
{
if($post===null)
$post=$this->post;
return $post->url.'#c'.$this->id;
}
/**
* @return string the hyperlink display for the current comment's author
*/
public function getAuthorLink()
{
if(!empty($this->url))
return CHtml::link(CHtml::encode($this->author),$this->url);
else
return CHtml::encode($this->author);
}
/**
* @return integer the number of comments that are pending approval
*/
public function getPendingCommentCount()
{
return $this->count('status='.self::STATUS_PENDING);
}
/**
* @param integer the maximum number of comments that should be returned
* @return array the most recently added comments
*/
public function findRecentComments($limit=10)
{
return $this->with('post')->findAll(array(
'condition'=>'t.status='.self::STATUS_APPROVED,
'order'=>'t.create_time DESC',
'limit'=>$limit,
));
}
|
5) Add additional file into {webroot}/protected/views/post/ folder
filename: _comments.php
<?php foreach($comments as $comment): ?>
<div class="comment" id="c<?php echo $comment->id; ?>">
<?php echo CHtml::link("#{$comment->id}", $comment->getUrl($post), array(
'class'=>'cid',
'title'=>'Permalink to this comment',
)); ?>
<div class="author">
<?php echo $comment->authorLink; ?> says:
</div>
<div class="time">
<?php echo date('F j, Y \a\t h:i a',$comment->create_time); ?>
</div>
<div class="content">
<?php echo nl2br(CHtml::encode($comment->content)); ?>
</div>
</div><!-- comment -->
<?php endforeach; ?>
<?php foreach($comments as $comment): ?>
<div class="comment" id="c<?php echo $comment->id; ?>">
<?php echo CHtml::link("#{$comment->id}", $comment->getUrl($post), array(
'class'=>'cid',
'title'=>'Permalink to this comment',
)); ?>
<div class="author">
<?php echo $comment->authorLink; ?> says:
</div>
<div class="time">
<?php echo date('F j, Y \a\t h:i a',$comment->create_time); ?>
</div>
<div class="content">
<?php echo nl2br(CHtml::encode($comment->content)); ?>
</div>
</div><!-- comment -->
<?php endforeach; ?>
|
B) CREATING AND DISPLAYING COMMENTS
1) Displaying Comments.
1.1) Modify {webroot}/protected/views/post/view.php
<?php
$this->breadcrumbs=array(
$model->title,
);
$this->pageTitle=$model->title;
?>
<?php $this->renderPartial('_view', array(
'data'=>$model,
)); ?>
<div id="comments">
<?php if($model->commentCount>=1): ?>
<h3>
<?php echo $model->commentCount>1 ? $model->commentCount . ' comments' : 'One comment'; ?>
</h3>
<?php $this->renderPartial('_comments',array(
'post'=>$model,
'comments'=>$model->comments,
)); ?>
<?php endif; ?>
<h3>Leave a Comment</h3>
<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
</div>
<?php else: ?>
<?php $this->renderPartial('/comment/_form',array(
'model'=>$comment,
)); ?>
<?php endif; ?>
</div><!-- comments -->
|
1.2) Modify {webroot}/protected/controllers/PostController.php
Edit actionView().
public function actionView()
{
$post=$this->loadModel();
$comment=$this->newComment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
}
|
Add newComment()
protected function newComment($post)
{
$comment=new Comment;
if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
echo CActiveForm::validate($comment);
Yii::app()->end();
}
if(isset($_POST['Comment']))
{
$comment->attributes=$_POST['Comment'];
if($post->addComment($comment))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
$this->refresh();
}
}
return $comment;
}
|
1.3) Modify {webroot}/protected/model/Post.php
public function addComment($comment)
{
if(Yii::app()->params['commentNeedApproval'])
$comment->status=Comment::STATUS_PENDING;
else
$comment->status=Comment::STATUS_APPROVED;
$comment->post_id=$this->id;
return $comment->save();
}
|
1.4) Modify {webroot}/protected/views/comment/_form.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'author'); ?>
<?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'author'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'url'); ?>
<?php echo $form->textField($model,'url',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'url'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'content'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
|
C) MANAGING COMMENTS
1) Approving Comments
public function actionApprove()
{
if(Yii::app()->request->isPostRequest)
{
$comment=$this->loadModel();
$comment->approve();
$this->redirect(array('index'));
}
else
throw new CHttpException(400,'Invalid request...');
}
|
{webroot}/protected/models/Comment.php
public function approve()
{
$this->status=Comment::STATUS_APPROVED;
$this->update(array('status'));
}
|
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Comment', array(
'criteria'=>array(
'with'=>'post',
'order'=>'t.status, t.create_time DESC',
),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
|
2) Modify {webroot}/protected/controllers/CommentController.php
Edit AccessRule Line.no 40
Replace 'admin' with @.
Broken image at 1.1, Google Drive: "You need permission"
ReplyDeleteReally awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
ReplyDeleteOutsource Yii Application Development in India