Beispiel #1
0
 public function actionPostMessages($id)
 {
     $sender = User::findIdentityByAccessToken(Yii::$app->request->get('access-token'));
     $type = $sender->user_type == "trainer" || $sender->user_type == "studio" ? "student" : "trainer";
     $receiver = User::findOne(['user_id' => $id]);
     if (!$receiver || !$sender) {
         return ApiHelper::errorResponse("User not found", 404);
     }
     $trainer_ = $type === "trainer" ? $receiver->id : $sender->id;
     $student_ = $type !== "trainer" ? $receiver->id : $sender->id;
     $result = Classes::canUserSendMessage($trainer_, $student_);
     if (!$result) {
         return ApiHelper::errorResponse("You cannot chat with this user", 403);
     }
     $message = new Chat();
     $chatData = ['sender_id' => $sender->id, 'receiver_id' => $receiver->id, 'message_text' => Yii::$app->request->post('message_text')];
     $message->load($chatData, '');
     if (!$message->save()) {
         return ApiHelper::errorResponse($message->getErrors(), 422);
     }
     $info = $sender->getUserInfo($id)['payment'];
     $body = "You have a new message from {$info->payment_first_name} {$info->payment_last_name} " . Yii::$app->params['email_new_message_link'];
     $return = Yii::$app->mailer->compose()->setTo($receiver->user_email)->setFrom(Yii::$app->params['adminEmail'])->setSubject('New message from ' . $info->payment_first_name . " " . $info->payment_last_name)->setTextBody($body)->send();
     return ApiHelper::successResponse(["sended" => $return]);
 }
Beispiel #2
0
 public function getUserMsgs($user, $sender)
 {
     return Chat::find()->select('sender_id, receiver_id, message_text, created_at')->where(['sender_id' => $user, 'receiver_id' => $sender])->orWhere(['receiver_id' => $user, 'sender_id' => $sender])->all();
 }