diff --git a/app/Http/Controllers/EmployerController.php b/app/Http/Controllers/EmployerController.php index 04f550f..eb5e685 100644 --- a/app/Http/Controllers/EmployerController.php +++ b/app/Http/Controllers/EmployerController.php @@ -14,6 +14,7 @@ use App\Mail\SendAllMessages; use App\Models\Ad_employer; use App\Models\ad_response; use App\Models\Category; +use App\Models\Chat; use App\Models\Employer; use App\Models\Flot; use App\Models\Job_title; @@ -337,7 +338,19 @@ class EmployerController extends Controller public function messages($type_message) { $user_id = Auth()->user()->id; - $messages_input = Message::query()->with('vacancies')->with('user_from')-> + $chats = Chat::query() + ->with('user') + ->with('worker') + ->with('employer') + ->with('last_message') + ->where('user_id', '=', $user_id) + ->orderByDesc('is_fixed') + ->orderByDesc('last_message_date') + ->paginate(5) + ; + //dd($chats); + + /*$messages_input = Message::query()->with('vacancies')->with('user_from')-> Where('to_user_id', $user_id)->OrderByDesc('created_at'); $messages_output = Message::query()->with('vacancies')-> @@ -353,12 +366,12 @@ class EmployerController extends Controller if ($type_message == 'output') { $messages = $messages_output->paginate(5); - } + }*/ //dd($user_id, $messages[2]->vacancies); //jobs); - return view('employers.messages', compact('messages', 'count_input', 'count_output', 'type_message', 'user_id')); + return view('employers.messages', compact('chats','user_id')); } // Диалог между пользователями @@ -535,10 +548,8 @@ class EmployerController extends Controller return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2]) ->withErrors($validator); } else { - if ($request->has('file')) { - $params['file'] = $request->file('file')->store("messages", 'public'); - } - Message::create($params); + Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages'); + //return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2]); return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2, 'ad_employer' => $id_vacancy, 'ad_name' => $ad_name]); @@ -756,25 +767,23 @@ class EmployerController extends Controller ////////////////////////////////////////////////////////////////// public function new_message(Request $request) { $params = $request->all(); + $id = $params['_user_id']; - $message = new Message(); - $message->user_id = $params['_user_id']; - $message->to_user_id = $params['_to_user_id']; - $message->title = $params['title']; - $message->text = $params['text']; - if ($request->has('_file')) { - $message->file = $request->file('_file')->store("worker/$id", 'public'); - } - $message->ad_employer_id = $params['_vacancy']; - $message->flag_new = 1; - $id_message = $message->save(); + $message_params = [ + 'title' => $params['title'], + 'text' => $params['text'], + 'ad_employer_id' => $params['_vacancy'], + 'flag_new' => 1 + ]; - //$data['message_id'] = $id_message; - //$data['ad_employer_id'] = $params['_vacancy']; - //$data['job_title_id'] = 0; + Message::add_message( + $request, + $params['_user_id'], + $params['_to_user_id'], + $message_params, + file_store_path: "worker/$id" + ); - $data['flag'] = 1; - //$ad_responce = ad_response::create($data); return redirect()->route('employer.messages', ['type_message' => 'output']); } diff --git a/app/Http/Controllers/WorkerController.php b/app/Http/Controllers/WorkerController.php index 7de9bc9..b7227b3 100644 --- a/app/Http/Controllers/WorkerController.php +++ b/app/Http/Controllers/WorkerController.php @@ -1046,17 +1046,20 @@ class WorkerController extends Controller $params = $request->all(); $id = $params['send_user_id']; - $message = new Message(); - $message->user_id = $params['send_user_id']; - $message->to_user_id = $params['send_to_user_id']; - $message->title = $params['send_title']; - $message->text = $params['send_text']; - $message->ad_employer_id = $params['send_vacancy']; - if ($request->has('send_file')) { - $message->file = $request->file('send_file')->store("worker/$id", 'public'); - } - $message->flag_new = 1; - $id_message = $message->save(); + $message_params = [ + 'title' => $params['send_title'], + 'text' => $params['send_text'], + 'ad_employer_id' => $params['send_vacancy'], + 'flag_new' => 1 + ]; + + $id_message = Message::add_message( + $request, + $params['send_user_id'], + $params['send_to_user_id'], + $message_params, + file_store_path: "worker/$id" + ); $data['message_id'] = $id_message; $data['ad_employer_id'] = $params['send_vacancy']; @@ -1098,11 +1101,8 @@ class WorkerController extends Controller return redirect()->route('worker.dialog', ['user1' => $user1, 'user2' => $user2, 'ad_employer' => $id_vacancy, 'ad_name' => $ad_name]) ->withErrors($validator); } else { - if ($request->has('file')) { - $params['file'] = $request->file('file')->store("messages", 'public'); - } - Message::create($params); - //return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2]); + 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]); diff --git a/app/Models/Chat.php b/app/Models/Chat.php new file mode 100644 index 0000000..d178f43 --- /dev/null +++ b/app/Models/Chat.php @@ -0,0 +1,38 @@ +belongsTo(User::class, 'to_user_id'); + } + + public function worker() { + return $this->belongsTo(Worker::class, 'to_user_id', 'user_id'); + } + + public function employer() { + return $this->belongsTo(Employer::class, 'to_user_id', 'user_id'); + } + + public function last_message() { + return $this->belongsTo(Message::class, 'last_message_id'); + } + +} diff --git a/app/Models/Message.php b/app/Models/Message.php index 2c68d53..11acc3e 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -4,6 +4,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Validator; class Message extends Model { @@ -12,6 +14,7 @@ class Message extends Model protected $fillable = [ 'user_id', 'to_user_id', + 'title', 'text', 'file', 'flag_new', @@ -38,4 +41,36 @@ class Message extends Model public function vacancies() { return $this->belongsTo(Ad_employer::class, 'ad_employer_id', 'id'); } + + public static function add_message( + Request $request, + int $user_id, + int $to_user_id, + array $message_params, + string $file_store_path = '/' + ) { + $message_params['user_id'] = $user_id; + $message_params['to_user_id'] = $to_user_id; + if ($request->has('file')) { + $message_params['file'] = $request->file('file')->store($file_store_path, 'public'); + } + + $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] + ); + } + + return $new_message->id ?? 0; + + } + } diff --git a/database/migrations/2024_07_19_063615_create_chat_table.php b/database/migrations/2024_07_19_063615_create_chat_table.php new file mode 100644 index 0000000..be747c0 --- /dev/null +++ b/database/migrations/2024_07_19_063615_create_chat_table.php @@ -0,0 +1,38 @@ +id(); + $table->integer('user_id')->nullable(false); + $table->integer('to_user_id'); + $table->boolean('is_removed')->default(false); + $table->boolean('is_fixed')->default(false); + $table->dateTime('last_message_date')->nullable(false); + $table->integer('last_message_id')->nullable(false); + $table->dateTime('fixed_time')->nullable(true); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('chats'); + } +}; diff --git a/public/css/helpers.css b/public/css/helpers.css index b6cc5c2..ad8d8d0 100644 --- a/public/css/helpers.css +++ b/public/css/helpers.css @@ -896,4 +896,10 @@ border: 0; } +.hover-shadow:hover{ + -webkit-box-shadow: 0px 0px 8px 1px rgba(66, 68, 90, 1); + -moz-box-shadow: 0px 0px 8px 1px rgba(66, 68, 90, 1); + box-shadow: 0px 0px 8px 1px rgba(66, 68, 90, 1); +} + /*# sourceMappingURL=helpers.css.map */ diff --git a/public/css/style_may2024.css b/public/css/style_may2024.css index 43e1b8c..109cc0f 100644 --- a/public/css/style_may2024.css +++ b/public/css/style_may2024.css @@ -6777,13 +6777,14 @@ main + .news { @media (min-width: 768px) { .messages__item { padding: 20px; - font-size: 16px; + font-size: 18px; } } .messages__item:nth-of-type(1), .messages__item:nth-of-type(2), .messages__item:nth-of-type(3), .messages__item:nth-of-type(4), .messages__item:nth-of-type(5), .messages__item:nth-of-type(6) { display: -webkit-box; display: -ms-flexbox; display: flex; + justify-content: space-between; } .messages__item-info { display: -webkit-box; @@ -6860,16 +6861,33 @@ main + .news { .messages__item-text span { color: #000; } -.messages__item-date { - color: #000; - width: 90px; - text-align: right; +.messages__item-actions{ + } -@media (min-width: 768px) { - .messages__item-date { - width: 150px; - } +.messages__item-buttons{ + float: right; +} +.messages__item-buttons button{ + padding: 0; + background: unset; + border: unset; +} +.messages__item-buttons button svg{ + width: 25px; + height: 25px; + color: gray; +} +.messages__item-buttons button svg:hover{ + color: black; } +.messages__item-date { + color: #00000070; + width: 90px; + text-align: right; + font-size: 14px; + margin-bottom: 8px; +} + .messages.active .messages__item { display: -webkit-box; display: -ms-flexbox; diff --git a/resources/views/employers/messages.blade.php b/resources/views/employers/messages.blade.php index 1bfb81b..9cb56d7 100644 --- a/resources/views/employers/messages.blade.php +++ b/resources/views/employers/messages.blade.php @@ -69,149 +69,55 @@