diff --git a/app/Http/Controllers/EmployerController.php b/app/Http/Controllers/EmployerController.php index d3020ff..5cb2972 100644 --- a/app/Http/Controllers/EmployerController.php +++ b/app/Http/Controllers/EmployerController.php @@ -342,12 +342,13 @@ class EmployerController extends Controller $chats = Chat::get_user_chats($user_id); $user_type = 'employer'; + $admin_chat = false; - return view('employers.messages', compact('chats','user_id', 'user_type')); + return view('employers.messages', compact('chats', 'admin_chat', 'user_id', 'user_type')); } // Диалог между пользователями - public function dialog(Request $request, User_Model $user1, User_Model $user2) { + public function dialog(Chat $chat, Request $request) { // Получение параметров. if ($request->has('ad_employer')){ $ad_employer = $request->get('ad_employer'); @@ -355,29 +356,12 @@ class EmployerController extends Controller $ad_employer = 0; } - if (isset($user2->id)) { - $companion = User_Model::query()->with('workers')-> - with('employers')-> - where('id', $user2->id)->first(); - } + $sender = User_Model::query()->with('workers')->with('employers')->where('id', $chat->user_id)->first(); + $companion = User_Model::query()->with('workers')->with('employers')->where('id', $chat->to_user_id)->first(); - $Messages = Message::query() - ->where(function($query) use ($user1, $user2) { - $query->where('user_id', $user1->id)->where('to_user_id', $user2->id); - }) - ->orWhere(function($query) use ($user1, $user2) { - $query->where('user_id', $user2->id)->where('to_user_id', $user1->id); - }) - ->orderBy('created_at') - ->get() - ; + $Messages = Chat::get_chat_messages($chat); - $sender = $user1; - - Message::where('user_id', '=', $user2->id) - ->where('to_user_id', '=', $user1->id) - ->update(['flag_new' => 0]) - ; + Message::where('user_id', '=', $chat->to_user_id)->where('to_user_id', '=', $chat->user_id)->update(['flag_new' => 0]); return view('employers.dialog', compact('companion', 'sender', 'ad_employer', 'Messages')); } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 7cbc2c3..330ab5e 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Message; use Illuminate\Http\Request; class HomeController extends Controller @@ -25,4 +26,28 @@ class HomeController extends Controller { return view('home'); } + + public function send_message(Request $request) + { + $user_id = Auth()->user()->id; + + $message_params = [ + 'text' => $request->input('text'), + 'reply_message_id' => $request->input('reply_message_id', null), + ]; + + $new_message = Message::add_message( + $request, + $user_id, + $request->input('to_user_id'), + $message_params + ); + + $user_type = Auth()->user()->is_worker ? 'worker' : 'employer'; + + return response()->json([ + 'success' => true, + 'url_redirect' => route($user_type . '.dialog', ['chat' => $new_message->chat_id_to]) + ]); + } } diff --git a/app/Http/Controllers/WorkerController.php b/app/Http/Controllers/WorkerController.php index 0c5ec64..48028bc 100644 --- a/app/Http/Controllers/WorkerController.php +++ b/app/Http/Controllers/WorkerController.php @@ -496,9 +496,10 @@ class WorkerController extends Controller $user_id = Auth()->user()->id; $chats = Chat::get_user_chats($user_id); + $admin_chat = Chat::get_user_admin_chat($user_id); $user_type = 'worker'; - return view('workers.messages', compact('chats','user_id', 'user_type')); + return view('workers.messages', compact('chats', 'admin_chat','user_id', 'user_type')); } // Избранный @@ -803,7 +804,7 @@ class WorkerController extends Controller } //Переписка - public function dialog(User_Model $user1, User_Model $user2, Request $request) { + public function dialog(Chat $chat, Request $request) { // Получение параметров. if ($request->has('ad_employer')){ $ad_employer = $request->get('ad_employer'); @@ -811,35 +812,16 @@ class WorkerController extends Controller $ad_employer = 0; } - if (isset($user1->id)) { - $sender = User_Model::query()->with('workers')-> - with('employers')-> - where('id', $user1->id)->first(); - } + $sender = User_Model::query()->with('workers')->with('employers')->where('id', $chat->user_id)->first(); + $companion = User_Model::query()->with('workers')->with('employers')->where('id', $chat->to_user_id)->first(); - if (isset($user2->id)) { - $companion = User_Model::query() - ->with('workers') - ->with('employers') - ->where('id', $user2->id) - ->first() - ; - } - - $Messages = Message::query()-> - where(function($query) use ($user1, $user2) { - $query->where('user_id', $user1->id)->where('to_user_id', $user2->id); - })->orWhere(function($query) use ($user1, $user2) { - $query->where('user_id', $user2->id)->where('to_user_id', $user1->id); - })->OrderBy('created_at') - ->get() - ; + $Messages = Chat::get_chat_messages($chat); - Message::where('user_id', '=', $user2->id) - ->where('to_user_id', '=', $user1->id) + Message::where('user_id', '=', $chat->to_user_id) + ->where('to_user_id', '=', $chat->user_id) ->update(['flag_new' => 0]); - return view('workers.dialog', compact('companion', 'sender', 'Messages', 'ad_employer')); + return view('workers.dialog', compact('companion', 'sender', 'chat', 'Messages', 'ad_employer')); } // Даунылоады @@ -1078,10 +1060,10 @@ class WorkerController extends Controller return redirect()->route('worker.dialog', ['user1' => $user1, 'user2' => $user2, 'ad_employer' => $id_vacancy, 'ad_name' => $ad_name]) ->withErrors($validator); } else { - Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages'); + $new_message = Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages'); return redirect()->route('worker.dialog', - ['user1' => $user1, 'user2' => $user2, 'ad_employer' => $id_vacancy, 'ad_name' => $ad_name]); + ['user1' => $user1, 'user2' => $user2, 'chat' => $new_message->chat_id_from]); } } diff --git a/app/Models/Chat.php b/app/Models/Chat.php index eb930ba..b0f6c35 100644 --- a/app/Models/Chat.php +++ b/app/Models/Chat.php @@ -17,7 +17,8 @@ class Chat extends Model 'is_fixed', 'last_message_date', 'last_message_id', - 'fixed_time' + 'fixed_time', + 'is_admin_chat' ]; public function user() { @@ -74,4 +75,46 @@ class Chat extends Model ; } + public static function get_user_admin_chat(int $user_id) + { + return Chat::query() + ->with('last_message') + ->withCount(['unread_messages' => function ($query) use($user_id) { + $query->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); + }]) + ->where('to_user_id', '=', $user_id) + ->where('is_admin_chat', 1) + ->first() + ; + } + + public static function get_chat_messages(Chat $chat){ + if ($chat->is_admin_chat){ + return Message::query() + ->where('chat_id_to', $chat->id) + ->where('to_user_id', $chat->to_user_id) + ->orderBy('created_at') + ->get() + ; + } else { + return Message::query() + ->where(function ($query) use ($chat) { + $query->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id); + }) + ->where(function($query) use ($chat) { + $query + ->where(function($query) use ($chat) { + $query->where('user_id', $chat->user_id)->where('to_user_id', $chat->to_user_id); + }) + ->orWhere(function($query) use ($chat) { + $query->where('user_id', $chat->to_user_id)->where('to_user_id', $chat->user_id); + }) + ; + }) + ->OrderBy('created_at') + ->get() + ; + } + } + } diff --git a/app/Models/Message.php b/app/Models/Message.php index 11acc3e..bdbc333 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; +use mysql_xdevapi\Collection; class Message extends Model { @@ -19,7 +20,10 @@ class Message extends Model 'file', 'flag_new', 'ad_employer_id', - 'job_title_id' + 'job_title_id', + 'chat_id_from', + 'chat_id_to', + 'reply_message_id', ]; @@ -43,34 +47,57 @@ class Message extends Model } public static function add_message( - Request $request, + ?Request $request, int $user_id, int $to_user_id, array $message_params, - string $file_store_path = '/' + string $file_store_path = '/', + bool $is_admin_chat = false ) { $message_params['user_id'] = $user_id; $message_params['to_user_id'] = $to_user_id; - if ($request->has('file')) { + if ($request && $request->has('file')) { $message_params['file'] = $request->file('file')->store($file_store_path, 'public'); } + $chat_form = Chat::firstOrCreate([ + 'user_id' => $is_admin_chat ? 0 : $user_id, + 'to_user_id' => $to_user_id, + 'is_removed' => 0, + 'is_admin_chat' => $is_admin_chat ? 1 : 0, + ]); + $message_params[$is_admin_chat ? 'chat_id_to' : 'chat_id_from'] = $chat_form->id; + + if (!$is_admin_chat) { + $chat_to = Chat::firstOrCreate([ + 'user_id' => $to_user_id, + 'to_user_id' => $user_id, + 'is_removed' => 0 + ]); + $message_params['chat_id_to'] = $chat_to->id; + } + $new_message = Message::create($message_params); if (!empty($new_message->id)) { - Chat::updateOrCreate( - ['user_id' => $user_id, 'to_user_id' => $to_user_id], - ['user_id' => $user_id, 'to_user_id' => $to_user_id, 'last_message_date' => date("Y-m-d H:i:s"), 'last_message_id' => $new_message->id] - ); - - Chat::updateOrCreate( - ['user_id' => $to_user_id, 'to_user_id' => $user_id], - ['user_id' => $to_user_id, 'to_user_id' => $user_id, 'last_message_date' => date("Y-m-d H:i:s"), 'last_message_id' => $new_message->id] - ); + $chat_form->update(['last_message_date' => date("Y-m-d H:i:s"), 'last_message_id' => $new_message->id]); + + if (!$is_admin_chat) { + $chat_to->update(['last_message_date' => date("Y-m-d H:i:s"), 'last_message_id' => $new_message->id]); + } } - return $new_message->id ?? 0; + return $new_message ?? false; + } + + public function getReplyMessageAttribute() + { + $reply_message = false; + if ($this->attributes['reply_message_id']){ + $reply_message = self::find($this->attributes['reply_message_id']); + } + return $reply_message; } } diff --git a/app/Models/MessagesRequests.php b/app/Models/MessagesRequests.php index 4cf8c55..b7dcb76 100644 --- a/app/Models/MessagesRequests.php +++ b/app/Models/MessagesRequests.php @@ -30,6 +30,25 @@ class MessagesRequests extends Model public static function send_message($message_request_id) { $message_request = MessagesRequests::find($message_request_id); + $job_ids = json_decode($message_request->job_titles); + + if (!empty($job_ids)){ + $workers = Title_worker::select('worker_id') + ->whereIN('job_title_id', $job_ids) + ->groupBy('worker_id') + ->get() + ; + + if ($workers->count()){ + $message_params = [ + 'text' => $message_request->text + ]; + foreach ($workers as $worker){ + Message::add_message(null, $message_request->user_id, $worker->worker_id, $message_params, file_store_path : '/', is_admin_chat: true); + } + } + } + $message_request->update(['is_sent' => now()]); return true; diff --git a/database/migrations/2024_08_09_071214_alter_table_messages.php b/database/migrations/2024_08_09_071214_alter_table_messages.php new file mode 100644 index 0000000..10f165d --- /dev/null +++ b/database/migrations/2024_08_09_071214_alter_table_messages.php @@ -0,0 +1,36 @@ +integer('chat_id_from')->nullable(true)->after('to_user_id'); + $table->integer('chat_id_to')->nullable(true)->after('chat_id_from'); + $table->integer('reply_message_id')->nullable(true); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('messages', function (Blueprint $table) { + $table->dropColumn('chat_id_from'); + $table->dropColumn('chat_id_to'); + $table->dropColumn('reply_message_id'); + }); + } +}; diff --git a/database/migrations/2024_08_09_072423_alter_table_chats.php b/database/migrations/2024_08_09_072423_alter_table_chats.php new file mode 100644 index 0000000..e90e1a2 --- /dev/null +++ b/database/migrations/2024_08_09_072423_alter_table_chats.php @@ -0,0 +1,34 @@ +dateTime('last_message_date')->nullable(true)->change(); + $table->integer('last_message_id')->nullable(true)->change(); + $table->boolean('is_admin_chat')->default(false)->after('is_fixed'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('chats', function (Blueprint $table) { + $table->dropColumn('is_admin_chat'); + }); + } +}; diff --git a/public/css/style_may2024.css b/public/css/style_may2024.css index 8dfc0b4..544fe03 100644 --- a/public/css/style_may2024.css +++ b/public/css/style_may2024.css @@ -7214,6 +7214,19 @@ main + .news { padding: 10px; line-height: 1.6; } +.chatbox__item-text .admin-chat-answer{ + padding: 2px 5px; + height: auto; + float: right; + margin-left: 10px; +} +.chatbox__item-text .reply-message{ + border-left: 1px grey solid; + padding-left: 11px; + font-size: 12px; + font-style: italic; + margin-top: 10px; +} .chatbox__item-time { width: 100%; padding-left: 54px; diff --git a/resources/views/chats/chats_list.blade.php b/resources/views/chats/chats_list.blade.php index 2cd9f00..f56e1a5 100644 --- a/resources/views/chats/chats_list.blade.php +++ b/resources/views/chats/chats_list.blade.php @@ -1,8 +1,29 @@ @if ($chats->count()) @csrf + + @if($admin_chat) +
+ + @include('svg.logo_icon') +
+
+ Администратор сайта +
+
+ {{ $admin_chat->last_message->text }} +
+
+
+ +
+
{{ date(' H:i, d.m.Y', strtotime($admin_chat->created_at)) }}
+
+
+ @endif + @foreach($chats as $chat)
- +
@if (isset($chat->employer->logo)) diff --git a/resources/views/employers/dialog.blade.php b/resources/views/employers/dialog.blade.php index eabdcc5..c9345a6 100644 --- a/resources/views/employers/dialog.blade.php +++ b/resources/views/employers/dialog.blade.php @@ -163,7 +163,14 @@
-
{{ $it->text }}
+
+ {{ $it->text }} + @if($it->reply_message_id) +
+ {{ $it->reply_message->text }} +
+ @endif +
{{ $it->created_at }}
diff --git a/resources/views/modals/chats/answer_from_admin_chat.blade.php b/resources/views/modals/chats/answer_from_admin_chat.blade.php new file mode 100644 index 0000000..1f54fa9 --- /dev/null +++ b/resources/views/modals/chats/answer_from_admin_chat.blade.php @@ -0,0 +1,54 @@ +
+ +
+ + diff --git a/resources/views/svg/logo_icon.blade.php b/resources/views/svg/logo_icon.blade.php new file mode 100644 index 0000000..9f98370 --- /dev/null +++ b/resources/views/svg/logo_icon.blade.php @@ -0,0 +1,5 @@ +
+ + + +
diff --git a/resources/views/workers/dialog.blade.php b/resources/views/workers/dialog.blade.php index 2dee58a..35919ae 100644 --- a/resources/views/workers/dialog.blade.php +++ b/resources/views/workers/dialog.blade.php @@ -5,6 +5,13 @@ $(function(){ var chatbox_div = $('.chatbox__list'); chatbox_div.scrollTop(chatbox_div.prop("scrollHeight")); + + $('.admin-chat-answer').click(function(){ + var modal = $('#answer_from_admin_chat_modal'); + + modal.data('to-user-id', $(this).data('to-user-id')); + modal.data('message-id', $(this).data('message-id')); + }); }); $(document).on('change', '#send_btn', function() { @@ -81,7 +88,14 @@
- @if ($companion->is_worker) + @if($chat->is_admin_chat) +
+ @include('svg.logo_icon') +
+ Администратор сайта +
+
+ @elseif ($companion->is_worker)
@@ -104,7 +118,7 @@ @endif @else -
+
@@ -151,7 +165,7 @@ - @if ($companion->is_worker) + @if($companion->is_worker) @if ((isset($companion->workers[0]->photo)) && (!empty($companion->workers[0]->photo))) @@ -174,29 +188,49 @@
@else
-
- - - - @if ($sender->is_worker) - @if ((isset($sender->workers[0]->photo)) && - (!empty($sender->workers[0]->photo))) - - @else - - @endif +
+ @if($chat->is_admin_chat) + @include('svg.logo_icon') @else - @if ((isset($sender->employers[0]->logo)) && - (!empty($sender->employers[0]->logo))) - + @if ($sender->is_worker) + @if ((isset($sender->workers[0]->photo)) && + (!empty($sender->workers[0]->photo))) + + @else + + @endif @else - + + + + + @if ((isset($sender->employers[0]->logo)) && + (!empty($sender->employers[0]->logo))) + + @else + + @endif @endif @endif -
-
{{ $it->text }}
+
+ @if($chat->is_admin_chat) + + @endif + + {{ $it->text }} + + @if($it->reply_message_id) +
+ {{ $it->reply_message->text }} +
+ @endif +
@if ((isset($it->file)) && (!empty($it->file))) @@ -212,6 +246,7 @@ @endforeach @endif
+ @if(!$chat->is_admin_chat)
@csrf
@@ -239,4 +275,6 @@
+ + @include('modals.chats.answer_from_admin_chat') @endsection diff --git a/routes/web.php b/routes/web.php index ace863c..8df3177 100644 --- a/routes/web.php +++ b/routes/web.php @@ -514,7 +514,7 @@ Route::group([ // 2 страница - Сообщения Route::get('cabinet/messages/{type_message}', [WorkerController::class, 'messages'])->name('messages'); - Route::get('cabinet/dialog/{user1}/{user2}', [WorkerController::class, 'dialog'])->name('dialog'); + Route::get('cabinet/dialog/{chat}', [WorkerController::class, 'dialog'])->name('dialog'); // 3 страница - Избранные вакансии Route::get('cabinet/favorite', [WorkerController::class, 'favorite'])->name('favorite'); // Продолжение борьбы против колорадов - избранные вакансии @@ -617,7 +617,7 @@ Route::group([ // 5 страница - Сообщения Route::get('cabinet/messages/{type_message}', [EmployerController::class, 'messages'])->name('messages'); - Route::get('cabinet/dialog/{user1}/{user2}', [EmployerController::class, 'dialog'])->name('dialog'); + Route::get('cabinet/dialog/{chat}', [EmployerController::class, 'dialog'])->name('dialog'); Route::post('cabinet/send-message', [EmployerController::class, 'send_message'])->name('send_message'); Route::post('test123', [EmployerController::class, 'test123'])->name('test123'); @@ -659,3 +659,5 @@ Route::group([ Route::get('TestWorker', [WorkerController::class, 'TestWorker'])->name('TestWorker'); +Route::post('send_message', [HomeController::class, 'send_message'])->name('send_message'); +