/** * View thread * @param int $id userid */ public function actionView($id) { $dataProvider = new CActiveDataProvider('PersonalMessage', array('criteria' => array('scopes' => array('byInterlocutorId' => $id), 'order' => 'id DESC'))); $message = new PersonalMessage(); $message->recipient_id = $id; if (isset($_POST['PersonalMessage'])) { $message->attributes = $_POST['PersonalMessage']; $message->sender_id = $this->_userId; $message->recipient_id = $id; $lastMessage = PersonalMessage::model()->byInterlocutorId($id)->find(); if ($lastMessage !== null && $lastMessage->thread_id != 0) { $message->thread_id = $lastMessage->thread_id; } if ($message->save()) { if ($message->thread_id == 0) { $message->thread_id = $message->id; $message->save(false, array('thread_id')); } $this->refresh(); } } $this->render('view', array('dataProvider' => $dataProvider, 'message' => $message)); }
/** * Reply to message * * @param int $id message ID */ public function actionReply($id) { $model = PersonalMessage::model()->with('sender')->haveAccess($this->_userId)->findByPk($id); if ($model === null) { throw new CHttpException(404, PmModule::t("Message not found")); } if ($model->sender_id == $this->_userId) { throw new CHttpException(403, "You can't answer to yourself"); } $modelNew = new PersonalMessage(); $modelNew->sender_id = $this->_userId; $modelNew->recipient_id = $model->sender_id == $this->_userId ? $model->recipient_id : $model->sender_id; if (isset($_POST['PersonalMessage'])) { $modelNew->attributes = $_POST['PersonalMessage']; if ($modelNew->save()) { Yii::app()->user->setFlash('success', PmModule::t('Message has been sent')); $this->redirect(array('/pm/default')); } } else { $modelNew->subject = $model->addReplyPrefix($model->subject); } $this->render('reply', array('modelNew' => $modelNew, 'model' => $model)); }