diff --git a/frontend/controllers/ChatController.php b/frontend/controllers/ChatController.php index 8241b1f..1dce51f 100755 --- a/frontend/controllers/ChatController.php +++ b/frontend/controllers/ChatController.php @@ -1,150 +1,182 @@ [ - 'class' => AccessControl::className(), - 'rules' => [ - [ - 'actions' => ['list', 'message', 'message-save'], - 'allow' => true, - 'roles' => ['@'], + public $defaultAction = 'list'; + + public function behaviors() + { + return [ + 'access' => [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'actions' => [ + 'list', + 'message', + 'message-save', + ], + 'allow' => true, + 'roles' => [ '@' ], + ], ], ], - ], - ]; - } - - - public function actionList() - { - $user = \Yii::$app->user->identity; - $chat = Chat::find() - ->where([ - 'or', - ['from_user' => $user->id,], - ['to_user' => $user->id,], - ]) - - ->with('messages.user'); - - $chat = new ActiveDataProvider([ - 'query' => $chat, - 'pagination' => [ - 'pageSize' => 5, - ], - ]); - - return $this->render('list',[ - 'chat' => $chat - ]); - } - - public function actionMessage($user_id) - { - $user = \Yii::$app->user->identity; - - $chat = Chat::find() - ->where([ - 'or', - ['from_user' => $user_id,], - ['to_user' => $user_id,], - ]) - ->andWhere([ - 'or', - ['from_user'=> $user->id,], - ['to_user'=> $user->id,], - ]) - ->with('messages.user') - ->one(); - if(!$chat instanceof Chat){ - $chat = new Chat(); - $chat->from_user = $user->id; - $chat->to_user = $user_id; - $chat->save(); + ]; + } + + public function actionList() + { + $user = \Yii::$app->user->identity; + $chat = Chat::find() + ->where( + [ + 'or', + [ 'from_user' => $user->id, ], + [ 'to_user' => $user->id, ], + ] + ) + ->with('messages.user'); + + $chat = new ActiveDataProvider( + [ + 'query' => $chat, + 'pagination' => [ + 'pageSize' => 5, + ], + ] + ); + + return $this->render( + 'list', + [ + 'chat' => $chat, + ] + ); } - - $phones = Fields::getData($chat->interlocutor->id, User::className(), 'phone'); - $sites = Fields::getData($chat->interlocutor->id, User::className(), 'site'); - - - $post = \Yii::$app->request->post(); - if(isset($post)){ - - $message = new Message(); - - if($message->load($post, 'Message')){ - - $message->chat_id = $chat->chat_id; - $message->user_id = $user->id; - - $message->file = UploadedFile::getInstances($message, 'file'); - - if(!empty($message->file)) { - - if(is_array($message->file)){ - - foreach($message->file as $file){ - - if($file instanceof UploadedFile){ - + + public function actionMessage($user_id) + { + $user = \Yii::$app->user->identity; + if ($user->getId() == $user_id) { + return $this->redirect([ 'chat/list' ]); + } + + $chat = Chat::find() + ->where( + [ + 'or', + [ 'from_user' => $user_id, ], + [ 'to_user' => $user_id, ], + ] + ) + ->andWhere( + [ + 'or', + [ 'from_user' => $user->id, ], + [ 'to_user' => $user->id, ], + ] + ) + ->with( + [ + 'messages' => function ($query) { + /** + * @var ActiveQuery $query + */ + $query->orderBy([ 'date' => SORT_DESC ]) + ->with('user'); + }, + ] + ) + ->one(); + if (!$chat instanceof Chat) { + $chat = new Chat(); + $chat->from_user = $user->id; + $chat->to_user = $user_id; + $chat->save(); + } + + $phones = Fields::getData($chat->interlocutor->id, User::className(), 'phone'); + $sites = Fields::getData($chat->interlocutor->id, User::className(), 'site'); + + $post = \Yii::$app->request->post(); + if (isset( $post )) { + + $message = new Message(); + + if ($message->load($post, 'Message')) { + + $message->chat_id = $chat->chat_id; + $message->user_id = $user->id; + + $message->file = UploadedFile::getInstances($message, 'file'); + + if (!empty( $message->file )) { + + if (is_array($message->file)) { + + foreach ($message->file as $file) { + + if ($file instanceof UploadedFile) { + + $file_model = new File(); + $file_id[] = $file_model->saveFile($file); + + } + + } + + } else { + + if ($message->file instanceof UploadedFile) { + $file_model = new File(); - $file_id[] = $file_model->saveFile($file); - + $file_id[] = $file_model->saveFile($message->file); } - - } - - } else { - - if($message->file instanceof UploadedFile){ - - $file_model = new File(); - $file_id[] = $file_model->saveFile($message->file); + } - + + $message->files = json_encode($file_id); } - - $message->files = json_encode($file_id); + + $message->save(); + + return $this->redirect( + [ + 'chat/message', + 'user_id' => $user_id, + ] + ); + } - - - $message->save(); - - return $this->redirect(['chat/message', 'user_id'=>$user_id]); - } + + return $this->render( + 'message', + [ + 'chat' => $chat, + 'user_id' => $user_id, + 'phones' => $phones, + 'sites' => $sites, + ] + ); } - - return $this->render('message',[ - 'chat' => $chat, - 'user_id' => $user_id, - 'phones' => $phones, - 'sites' => $sites, - ]); + } - -} diff --git a/frontend/views/chat/message.php b/frontend/views/chat/message.php index 4be23df..f8dbfe0 100755 --- a/frontend/views/chat/message.php +++ b/frontend/views/chat/message.php @@ -41,14 +41,20 @@