/**
  * 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();
 }
 /**
  * Add participant to a topic
  *
  */
 public function actionAddParticiapnt($userId, $topicId)
 {
     if (!checkAccess('op_personalmessages_add_participants')) {
         echoJson(array('error' => at('Sorry, You are not allowed to perform this action.')));
     }
     // Load the topic id
     $topic = PersonalMessageTopic::model()->findByPk($topicId);
     if (!$topic) {
         echoJson(array('error' => at('Sorry, We could not find that topic.')));
     }
     // Load the user id
     $user = User::model()->findByPk($userId);
     if (!$user) {
         echoJson(array('error' => at('Sorry, We could not find that user.')));
     }
     // Make sure the user is not the author
     if ($topic->author_id == $userId) {
         echoJson(array('error' => at('Sorry, That user is the topic author. He can not be added as a participant.')));
     }
     // Make sure the user is not part of the topic already
     $participantExists = PersonalMessageParticipant::model()->exists('topic_id=:topic AND user_id=:user', array(':user' => $userId, ':topic' => $topic->id));
     if ($participantExists) {
         echoJson(array('error' => at('Sorry, That user is already a participant.')));
     }
     // Add him
     $newParticipant = new PersonalMessageParticipant();
     $newParticipant->topic_id = $topic->id;
     $newParticipant->user_id = $userId;
     $newParticipant->save();
     // Load the new participants list
     $participantsList = $this->renderPartial('_participants', array('model' => $topic), true);
     // Load the new add participant form
     $addParticipantsForm = $this->renderPartial('_add_participant', array('model' => $topic), true);
     // Success message
     $html = at("{name} added to the topic.", array('{name}' => $user->name));
     echoJson(array('html' => $html, 'participantsList' => $participantsList, 'addParticipantsForm' => $addParticipantsForm));
 }