Yii Framework Forum: Using Yii events - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Using Yii events pagre 14 - 15

#1 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 04 October 2011 - 04:36 AM

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.
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 04 October 2011 - 05:23 AM

Just by looking at your code (did not have the book here)...

In the controller you first call $post->addComment() that in turns raises the event onNewComment()... and only then you set $post->onNewComment to $notifier->comment...
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 04 October 2011 - 06:49 AM

This is correct:

First we have to create the event
<?php

class NewCommentEvent extends CModelEvent {

    public $comment;
    public $post;

}


Secondo, we must tell to Post model to raise event onNewComment when someone add a comment:
    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);
    }


third, in PostController we call the event onNewComment
    protected function newComment($post) {
        $comment = new Comment;
        $notifier = new Notifier();
        $post->onNewComment = array($notifier,'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();
            }
        }
        return $comment;
    }


And finally the Notifier:
<?php

class Notifier {

    function comment($event) {
        $text = "There was new comment from <strong>{$event->comment->content}</strong> on post <strong>{$event->post->title}</strong>";
        mail('sensorario@gmail.com', 'New Comment', $text);
    }

}


This work. It is not very clear, but work. I'll try later to add some other events.
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users