/** * Creates a new Message model. * If creation is successful, the browser will be redirected to the 'view' page. * @param $receiver : to some body send a msg * @return mixed * @throws BadRequestHttpException */ public function actionCreate($receiver) { $this->layout = '//i'; if ($receiver == Yii::$app->user->id) { throw new BadRequestHttpException('给自己发消息有意思不', 403); } $receiverUser = User::findOne($receiver); $model = new Message(); $model->receiver = $receiver; $model->sender = Yii::$app->user->id; $model->user_id = $receiver; $model->friend_id = Yii::$app->user->id; if ($model->load(Yii::$app->request->post()) && $model->save()) { $msg = new Message(); $msg->receiver = $receiver; $msg->sender = Yii::$app->user->id; $msg->user_id = $model->sender; $msg->friend_id = $model->receiver; $msg->content = $model->content; $msg->save(false); return $this->redirect(['index']); } else { var_dump($model->errors); return $this->render('create', ['model' => $model, 'receiverUser' => $receiverUser]); } }
/** * Creates a new Message model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Message(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { if (Yii::$app->request->post('save_type') == 'apply') { return $this->redirect(['update', 'id' => (string) $model->primaryKey]); } return $this->redirect(['view', 'id' => (string) $model->primaryKey]); } } else { Yii::$app->view->title = Yii::t('yii', 'Create'); Yii::$app->view->params['breadcrumbs'][] = ['label' => Yii::t('message', 'Message'), 'url' => ['index']]; Yii::$app->view->params['breadcrumbs'][] = Yii::$app->view->title; return $this->render('form', ['model' => $model]); } }
/** * Creates a new Message model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Message(['scenario' => 'new']); if ($model->load(Yii::$app->request->post())) { // Danh sach cac user(s) tuong tac voi tin nhan nay $users = $model->users; $users[] = Yii::$app->user->id; $users = array_unique($users); $model->users = $users; if ($model->save()) { // Cap nhat thon tin nguoi nhan. Nen chuyen sang batch chay cho nhanh foreach ($model->users as $userId) { $modelMessageUser = new MessageUser(); $modelMessageUser->user_id = $userId; $modelMessageUser->message_id = $model->_id; $modelMessageUser->category_id = $model->category_id; $modelMessageUser->is_delete = 0; $modelMessageUser->type_id = Message::TYPE_INBOX; $modelMessageUser->is_read = 0; if ($userId == $model->created_by) { $modelMessageUser->type_id = Message::TYPE_SENT; $modelMessageUser->is_read = 1; } if ($modelMessageUser->save()) { if ($userId != $model->created_by && !empty($modelMessageUser->user->email)) { // Send email $mailer = \Yii::$app->mailer; $mailer->viewPath = '@app/modules/message/mail'; $mailer->compose(['html' => 'messageNew-html', 'text' => 'messageNew-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 $this->redirect(['index']); } } Yii::$app->view->title = Yii::t('yii', 'Create'); Yii::$app->view->params['breadcrumbs'][] = ['label' => Yii::t('message', 'Message'), 'url' => ['index']]; Yii::$app->view->params['breadcrumbs'][] = Yii::$app->view->title; return $this->render('form', ['model' => $model]); }