From 3c6557d9b8538a8580b11bb18970d27cbca56555 Mon Sep 17 00:00:00 2001 From: saparatayev Date: Tue, 29 Jun 2021 17:03:18 +0500 Subject: [PATCH] sending messages --- config/app.php | 2 +- plugins/rainlab/user/models/User.php | 3 +- plugins/tps/birzha/Plugin.php | 3 +- plugins/tps/birzha/components/Messages.php | 62 ++++++++++ .../birzha/components/messages/default.htm | 112 ++++++++++++++++++ plugins/tps/birzha/models/Chatroom.php | 37 ++++++ plugins/tps/birzha/models/Message.php | 4 + ...lder_table_create_tps_birzha_chatrooms.php | 22 ++++ ...able_create_tps_birzha_chatrooms_users.php | 24 ++++ ...lder_table_update_tps_birzha_chatrooms.php | 27 +++++ ...able_update_tps_birzha_chatrooms_users.php | 23 ++++ ...ilder_table_update_tps_birzha_messages.php | 23 ++++ ...der_table_update_tps_birzha_messages_2.php | 23 ++++ plugins/tps/birzha/updates/version.yaml | 18 +++ themes/birzha/pages/kabinet/messages.htm | 16 ++- themes/birzha/partials/header.htm | 2 +- 16 files changed, 396 insertions(+), 5 deletions(-) create mode 100644 plugins/tps/birzha/components/Messages.php create mode 100644 plugins/tps/birzha/components/messages/default.htm create mode 100644 plugins/tps/birzha/models/Chatroom.php create mode 100644 plugins/tps/birzha/updates/builder_table_create_tps_birzha_chatrooms.php create mode 100644 plugins/tps/birzha/updates/builder_table_create_tps_birzha_chatrooms_users.php create mode 100644 plugins/tps/birzha/updates/builder_table_update_tps_birzha_chatrooms.php create mode 100644 plugins/tps/birzha/updates/builder_table_update_tps_birzha_chatrooms_users.php create mode 100644 plugins/tps/birzha/updates/builder_table_update_tps_birzha_messages.php create mode 100644 plugins/tps/birzha/updates/builder_table_update_tps_birzha_messages_2.php diff --git a/config/app.php b/config/app.php index df6d7921d..1a1f69772 100644 --- a/config/app.php +++ b/config/app.php @@ -92,7 +92,7 @@ return [ | */ - 'timezone' => 'UTC', + 'timezone' => 'Asia/Ashgabat', /* |-------------------------------------------------------------------------- diff --git a/plugins/rainlab/user/models/User.php b/plugins/rainlab/user/models/User.php index bfdc55283..979a44590 100644 --- a/plugins/rainlab/user/models/User.php +++ b/plugins/rainlab/user/models/User.php @@ -41,7 +41,8 @@ class User extends UserBase * @var array Relations */ public $belongsToMany = [ - 'groups' => [UserGroup::class, 'table' => 'users_groups'] + 'groups' => [UserGroup::class, 'table' => 'users_groups'], + 'chatrooms' => ['TPS\Birzha\Models\Chatroom', 'table'=>'tps_birzha_chatrooms_users'] ]; public $attachOne = [ diff --git a/plugins/tps/birzha/Plugin.php b/plugins/tps/birzha/Plugin.php index 16735b21f..b38235e33 100644 --- a/plugins/tps/birzha/Plugin.php +++ b/plugins/tps/birzha/Plugin.php @@ -169,7 +169,8 @@ class Plugin extends PluginBase 'TPS\Birzha\Components\Sliders' => 'sliders', 'TPS\Birzha\Components\Offers' => 'offers', 'TPS\Birzha\Components\Singleoffer' => 'singleoffer', - 'TPS\Birzha\Components\OfferForm' => 'offerform' + 'TPS\Birzha\Components\OfferForm' => 'offerform', + 'TPS\Birzha\Components\Messages' => 'messages' ]; } diff --git a/plugins/tps/birzha/components/Messages.php b/plugins/tps/birzha/components/Messages.php new file mode 100644 index 000000000..a46c00283 --- /dev/null +++ b/plugins/tps/birzha/components/Messages.php @@ -0,0 +1,62 @@ + 'Messages', + 'description' => 'Messages' + ]; + } + + protected function loadMessages() { + $this->chatrooms = Auth::user()->chatrooms; + foreach($this->chatrooms as $room) { + $room->last_message = $room->messages()->latest('send_at')->first(); + $room->message_partner = $room->users()->where('users.id','!=',Auth::user()->id)->first(); + $room->count_unread_messages = $room->messages()->where('read_at',null)->where('reciver_id',Auth::user()->id)->count(); + } + } + + public $chatrooms; + + public function onRun() { + $this->loadMessages(); + } + + public function onChatroom() { + $chatRoomId = Input::get('chatroom_id'); + + // Read unread messages + Chatroom::find($chatRoomId)->messages()->where('reciver_id',Auth::user()->id)->where('read_at',null)->update(['read_at'=>Carbon::now()]); + + + + return [ + 'messages' => Chatroom::find($chatRoomId)->messages, + 'currentUserId' => Auth::user()->id, + 'chatRoomId' => $chatRoomId, + 'chatRoomPartnerId' => Chatroom::find($chatRoomId)->users()->where('users.id','!=',Auth::user()->id)->first()->id + ]; + } + + public function onMessageSend() { + $newMsg = new Message; + $newMsg->sender_id = Auth::user()->id; + $newMsg->reciver_id = Input::get('reciver_id'); + $newMsg->send_at = Carbon::now(); + $newMsg->message = Input::get('msg'); + $newMsg->chatroom_id = Input::get('chatroom_id'); + $newMsg->save(); + + return $this->onChatroom(); + } +} \ No newline at end of file diff --git a/plugins/tps/birzha/components/messages/default.htm b/plugins/tps/birzha/components/messages/default.htm new file mode 100644 index 000000000..003d67c0c --- /dev/null +++ b/plugins/tps/birzha/components/messages/default.htm @@ -0,0 +1,112 @@ +{% set chatrooms = __SELF__.chatrooms %} + + +
+ +
+ + +{% put scripts %} + +{% endput %} diff --git a/plugins/tps/birzha/models/Chatroom.php b/plugins/tps/birzha/models/Chatroom.php new file mode 100644 index 000000000..028f3aa91 --- /dev/null +++ b/plugins/tps/birzha/models/Chatroom.php @@ -0,0 +1,37 @@ + ['RainLab\User\Models\User','table'=>'tps_birzha_chatrooms_users'] + ]; + + public $hasMany = [ + 'messages' => ['TPS\Birzha\Models\Message'], + ]; +} diff --git a/plugins/tps/birzha/models/Message.php b/plugins/tps/birzha/models/Message.php index 03fbf3b9b..5ccb0ad3c 100644 --- a/plugins/tps/birzha/models/Message.php +++ b/plugins/tps/birzha/models/Message.php @@ -30,4 +30,8 @@ class Message extends Model */ public $rules = [ ]; + + public $hasOne = [ + 'chatroom' => ['TPS\Birzha\Models\Chatroom'], + ]; } diff --git a/plugins/tps/birzha/updates/builder_table_create_tps_birzha_chatrooms.php b/plugins/tps/birzha/updates/builder_table_create_tps_birzha_chatrooms.php new file mode 100644 index 000000000..55017c42c --- /dev/null +++ b/plugins/tps/birzha/updates/builder_table_create_tps_birzha_chatrooms.php @@ -0,0 +1,22 @@ +engine = 'InnoDB'; + $table->increments('id'); + $table->integer('user_id'); + }); + } + + public function down() + { + Schema::dropIfExists('tps_birzha_chatrooms'); + } +} diff --git a/plugins/tps/birzha/updates/builder_table_create_tps_birzha_chatrooms_users.php b/plugins/tps/birzha/updates/builder_table_create_tps_birzha_chatrooms_users.php new file mode 100644 index 000000000..9ab3123ae --- /dev/null +++ b/plugins/tps/birzha/updates/builder_table_create_tps_birzha_chatrooms_users.php @@ -0,0 +1,24 @@ +engine = 'InnoDB'; + $table->integer('id'); + $table->integer('user_id'); + $table->integer('chatroom_id'); + $table->primary(['id']); + }); + } + + public function down() + { + Schema::dropIfExists('tps_birzha_chatrooms_users'); + } +} diff --git a/plugins/tps/birzha/updates/builder_table_update_tps_birzha_chatrooms.php b/plugins/tps/birzha/updates/builder_table_update_tps_birzha_chatrooms.php new file mode 100644 index 000000000..752ae2c1d --- /dev/null +++ b/plugins/tps/birzha/updates/builder_table_update_tps_birzha_chatrooms.php @@ -0,0 +1,27 @@ +timestamp('created_at')->nullable(); + $table->timestamp('updated_at')->nullable(); + $table->dropColumn('user_id'); + }); + } + + public function down() + { + Schema::table('tps_birzha_chatrooms', function($table) + { + $table->dropColumn('created_at'); + $table->dropColumn('updated_at'); + $table->integer('user_id'); + }); + } +} diff --git a/plugins/tps/birzha/updates/builder_table_update_tps_birzha_chatrooms_users.php b/plugins/tps/birzha/updates/builder_table_update_tps_birzha_chatrooms_users.php new file mode 100644 index 000000000..afcfb5b90 --- /dev/null +++ b/plugins/tps/birzha/updates/builder_table_update_tps_birzha_chatrooms_users.php @@ -0,0 +1,23 @@ +increments('id')->change(); + }); + } + + public function down() + { + Schema::table('tps_birzha_chatrooms_users', function($table) + { + $table->integer('id')->change(); + }); + } +} diff --git a/plugins/tps/birzha/updates/builder_table_update_tps_birzha_messages.php b/plugins/tps/birzha/updates/builder_table_update_tps_birzha_messages.php new file mode 100644 index 000000000..9f2e52dd9 --- /dev/null +++ b/plugins/tps/birzha/updates/builder_table_update_tps_birzha_messages.php @@ -0,0 +1,23 @@ +integer('chat_id'); + }); + } + + public function down() + { + Schema::table('tps_birzha_messages', function($table) + { + $table->dropColumn('chat_id'); + }); + } +} diff --git a/plugins/tps/birzha/updates/builder_table_update_tps_birzha_messages_2.php b/plugins/tps/birzha/updates/builder_table_update_tps_birzha_messages_2.php new file mode 100644 index 000000000..365598ebf --- /dev/null +++ b/plugins/tps/birzha/updates/builder_table_update_tps_birzha_messages_2.php @@ -0,0 +1,23 @@ +renameColumn('chat_id', 'chatroom_id'); + }); + } + + public function down() + { + Schema::table('tps_birzha_messages', function($table) + { + $table->renameColumn('chatroom_id', 'chat_id'); + }); + } +} diff --git a/plugins/tps/birzha/updates/version.yaml b/plugins/tps/birzha/updates/version.yaml index ef7c63919..75d0d84c6 100644 --- a/plugins/tps/birzha/updates/version.yaml +++ b/plugins/tps/birzha/updates/version.yaml @@ -134,3 +134,21 @@ 1.0.46: - 'Updated table tps_birzha_category' - builder_table_update_tps_birzha_categories.php +1.0.47: + - 'Created table tps_birzha_chatrooms' + - builder_table_create_tps_birzha_chatrooms.php +1.0.48: + - 'Updated table tps_birzha_chatrooms' + - builder_table_update_tps_birzha_chatrooms.php +1.0.49: + - 'Created table tps_birzha_chatrooms_users' + - builder_table_create_tps_birzha_chatrooms_users.php +1.0.50: + - 'Updated table tps_birzha_chatrooms_users' + - builder_table_update_tps_birzha_chatrooms_users.php +1.0.51: + - 'Updated table tps_birzha_messages' + - builder_table_update_tps_birzha_messages.php +1.0.52: + - 'Updated table tps_birzha_messages' + - builder_table_update_tps_birzha_messages_2.php diff --git a/themes/birzha/pages/kabinet/messages.htm b/themes/birzha/pages/kabinet/messages.htm index 10c47eec6..88d685322 100644 --- a/themes/birzha/pages/kabinet/messages.htm +++ b/themes/birzha/pages/kabinet/messages.htm @@ -1,2 +1,16 @@ -title = "messages" +title = "Messages" +url = "/messages" +layout = "default" +is_hidden = 0 + +[messages] + +[session] +security = "user" +redirect = "index" + +[viewBag] +localeTitle[ru] = "Сообщения" +localeUrl[ru] = "/soobshcheniya" == +{% component 'messages' %} \ No newline at end of file diff --git a/themes/birzha/partials/header.htm b/themes/birzha/partials/header.htm index 2b620fbf4..c1e759cae 100644 --- a/themes/birzha/partials/header.htm +++ b/themes/birzha/partials/header.htm @@ -134,7 +134,7 @@ code = "main-top" - +