Beispiel #1
0
 /**
  * Finds the Message model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $_id
  * @return Message the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Message::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Beispiel #2
0
 /**
  * Displays a single Message model.
  *
  * @param $id
  * @return string|\yii\web\Response
  * @throws NotFoundHttpException
  */
 public function actionView($id)
 {
     /** @var $model \app\modules\message\models\Message */
     /** @var $modelUser \app\modules\message\models\MessageUser */
     if (($model = Message::findOne($id)) === null) {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
     // Cap nhat trang thai tin nhan da doc
     MessageUser::updateAll(['is_read' => 1], ['message_id' => $model->primaryKey, 'user_id' => Yii::$app->user->id]);
     // Khoi tao form tra loi tin nhan
     $modelReply = new Message(['scenario' => 'reply']);
     $modelReply->message_id = $model->primaryKey;
     if ($modelReply->load(Yii::$app->request->post())) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         if (!$modelReply->validate()) {
             return ['status' => 0, 'errors' => $modelReply->errors];
         }
         if ($modelReply->save()) {
             // Cap nhat trang thai chua doc cho tat ca cac user
             $users = $model->users;
             if (($key = array_search(Yii::$app->user->id, $users)) !== false) {
                 unset($users[$key]);
             }
             $users = array_values($users);
             // Reset key
             MessageUser::updateAll(['is_delete' => 0, 'is_read' => 0, 'type_id' => Message::TYPE_INBOX, 'updated_at' => new \MongoDate()], ['message_id' => $model->primaryKey, 'user_id' => $users]);
             foreach ($users as $userId) {
                 $modelMessageUser = MessageUser::find()->where(['message_id' => $model->primaryKey, 'user_id' => $userId])->one();
                 if ($modelMessageUser != null) {
                     // Send email
                     $mailer = \Yii::$app->mailer;
                     $mailer->viewPath = '@app/modules/message/mail';
                     $mailer->compose(['html' => 'messageReply-html', 'text' => 'messageReply-text'], ['model' => $model, 'modelMessageUser' => $modelMessageUser])->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name])->setTo($modelMessageUser->user->email)->setSubject(Yii::t('message', 'Message notifications'))->send();
                 }
             }
             return ['status' => 1, 'content' => $this->renderAjax('viewItem', ['model' => $modelReply])];
         }
     }
     $query = Message::find()->with('user');
     $query->orWhere(['_id' => $model->primaryKey]);
     $query->orWhere(['message_id' => $model->primaryKey]);
     $query->orderBy(['created_at' => SORT_ASC]);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 20]]);
     Yii::$app->view->title = $model->subject;
     Yii::$app->view->params['breadcrumbs'][] = ['label' => Yii::t('message', 'Message'), 'url' => ['index']];
     return $this->render('view', ['model' => $model, 'modelReply' => $modelReply, 'dataProvider' => $dataProvider]);
 }