/**
  * Return latest message to be notified about
  */
 public function getLastMessage()
 {
     $criteria = new CDbCriteria();
     $criteria->addCondition('t.user_id=:user AND was_shown=:shown');
     $criteria->params = array(':user' => Yii::app()->user->id, ':shown' => 0);
     $criteria->with = array('notificationAuthor', 'notificationTopic', 'notificationReply');
     $criteria->together = true;
     $criteria->order = 'notificationTopic.type DESC, notificationTopic.last_reply_created_at DESC';
     return PersonalMessageNotification::model()->find($criteria);
 }
 /**
  * Send notifications to the topic participants
  */
 public function sendNotifications($id = null, $replyId = null)
 {
     $topicId = $id !== null ? $id : $this->id;
     $participants = PersonalMessageParticipant::model()->findAll('topic_id=:topic', array(':topic' => $topicId));
     // Get current notifications for that topic
     $topicNotifications = PersonalMessageNotification::model()->findAll('topic_id=:topic AND was_shown=:shown', array(':shown' => 0, ':topic' => $topicId));
     $notificationExists = array();
     foreach ($topicNotifications as $topicNotification) {
         $notificationExists[] = $topicNotification->user_id;
     }
     foreach ($participants as $participant) {
         // Skip ourself
         if ($participant->user_id == Yii::app()->user->id) {
             continue;
         }
         // If we have one then dont add it again
         if (in_array($participant->user_id, $notificationExists)) {
             continue;
         }
         // Create the record
         $notification = new PersonalMessageNotification();
         $notification->user_id = $participant->user_id;
         $notification->topic_id = $topicId;
         if ($replyId) {
             $notification->reply_id = $replyId;
         }
         $notification->save();
     }
 }
 /**
  * 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'));
     }
 }