Hi! I am reading this cookbook. I am at page 14:
added class NewCommentEvent
<?php
class NewCommentEvent extends CModelEvent {
public $comment;
public $post;
}
changed Post model adding some code:
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;
$event = new NewCommentEvent($this);
$event->post = $this;
$event->comment = $comment;
$this->onNewComment($event);
return $comment->save();
}
public function onNewComment($event) {
$this->raiseEvent('onNewComment', $event);
}
added Notifier
<?php
class Notifier {
function comment($event) {
$text = "There was new comment from {$event->author} on post {$this->post->title}";
mail('sensorario@gmail.com', 'New Comment', $text);
}
}
changed PostController:
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.');
Yii::app()->user->setFlash('commentSubmitted', Yii::t('post.newcomment','Grazie per il tuo commento. Tutti i commenti sono moderati. Sarà visibile solo una volta che sarà approvato.'));
}
$this->refresh();
}
}
$notifier = new Notifier();
$post->onNewComment = array($notifier, 'comment');
return $comment;
}
Nothing happen.