/**
  * After save operations
  */
 public function afterSave()
 {
     // Add message to the reply table
     $reply = new PersonalMessageReply();
     $reply->topic_id = $this->id;
     $reply->message = $this->message;
     $reply->save();
     // Update Topic
     PersonalMessageTopic::model()->updateByPk($this->id, array('first_post' => $reply->id, 'last_reply_created_at' => $reply->created_at, 'last_reply_author_id' => $reply->user_id));
     // Add current user
     $participant = new PersonalMessageParticipant();
     $participant->topic_id = $this->id;
     $participant->user_id = Yii::app()->user->id;
     $participant->save();
     // Add recipients to the particiapnts table
     foreach ($this->to as $userid) {
         $participant = new PersonalMessageParticipant();
         $participant->topic_id = $this->id;
         $participant->user_id = $userid;
         $participant->save();
     }
     // Add the notifications for all the participants
     $this->sendNotifications(null, $reply->id);
     return parent::afterSave();
 }
 /**
  * view help topic action
  */
 public function actionView()
 {
     // Check Access
     checkAccessThrowException('op_personalmessages_view');
     if (isset($_GET['id']) && ($model = PersonalMessageTopic::model()->with(array('author', 'participants', 'replies'))->findByPk($_GET['id']))) {
         // Make sure we are a participant
         if ($model->author_id != Yii::app()->user->id) {
             $participantExists = PersonalMessageParticipant::model()->exists('topic_id=:topic AND user_id=:user', array(':user' => Yii::app()->user->id, ':topic' => $model->id));
             if (!$participantExists) {
                 ferror(at('Sorry, You are not allowed to view this personal message as you are not a participant.'));
                 alog(at("Tried Accessing a Personal Message '{name}' When he is not a participant.", array('{name}' => $model->title)));
                 $this->redirect(getReferrer('personalmessages/index'));
             }
         }
         alog(at("Viewed Personal Message '{name}'.", array('{name}' => $model->title)));
         // Add Breadcrumb
         $this->addBreadCrumb(at('Viewing Personal Message'));
         $this->title[] = at('Viewing Personal Message "{name}"', array('{name}' => $model->title));
         $reply = new PersonalMessageReply();
         if (isset($_POST['PersonalMessageReply'])) {
             checkAccessThrowException('op_personalmessages_reply');
             $reply->attributes = $_POST['PersonalMessageReply'];
             $reply->topic_id = $model->id;
             if ($reply->save()) {
                 fok(at('Reply Sent.'));
                 alog(at("Replied To Personal Message '{name}'.", array('{name}' => $model->title)));
                 $this->redirect(getReferrer('personalmessages/index'));
             }
         }
         // Delete all the notifications we have for this topic
         PersonalMessageNotification::model()->deleteAll('user_id=:user AND topic_id=:topic', array(':user' => Yii::app()->user->id, ':topic' => $model->id));
         // Display form
         $this->render('view', array('model' => $model, 'reply' => $reply));
     } else {
         ferror(at('Could not find that ID.'));
         $this->redirect(array('personalmessages/index'));
     }
 }