Commit 712f07ca2dd7650eb571af031468d54eb6eb9f15

Authored by Сергей П
1 parent 99bbf6bf62
Exists in master

часть задачи по рефакторингу сообщений

Showing 8 changed files with 227 additions and 177 deletions Side-by-side Diff

app/Http/Controllers/EmployerController.php
... ... @@ -14,6 +14,7 @@ use App\Mail\SendAllMessages;
14 14 use App\Models\Ad_employer;
15 15 use App\Models\ad_response;
16 16 use App\Models\Category;
  17 +use App\Models\Chat;
17 18 use App\Models\Employer;
18 19 use App\Models\Flot;
19 20 use App\Models\Job_title;
... ... @@ -337,7 +338,19 @@ class EmployerController extends Controller
337 338 public function messages($type_message) {
338 339 $user_id = Auth()->user()->id;
339 340  
340   - $messages_input = Message::query()->with('vacancies')->with('user_from')->
  341 + $chats = Chat::query()
  342 + ->with('user')
  343 + ->with('worker')
  344 + ->with('employer')
  345 + ->with('last_message')
  346 + ->where('user_id', '=', $user_id)
  347 + ->orderByDesc('is_fixed')
  348 + ->orderByDesc('last_message_date')
  349 + ->paginate(5)
  350 + ;
  351 + //dd($chats);
  352 +
  353 + /*$messages_input = Message::query()->with('vacancies')->with('user_from')->
341 354 Where('to_user_id', $user_id)->OrderByDesc('created_at');
342 355  
343 356 $messages_output = Message::query()->with('vacancies')->
... ... @@ -353,12 +366,12 @@ class EmployerController extends Controller
353 366  
354 367 if ($type_message == 'output') {
355 368 $messages = $messages_output->paginate(5);
356   - }
  369 + }*/
357 370  
358 371 //dd($user_id, $messages[2]->vacancies);
359 372 //jobs);
360 373  
361   - return view('employers.messages', compact('messages', 'count_input', 'count_output', 'type_message', 'user_id'));
  374 + return view('employers.messages', compact('chats','user_id'));
362 375 }
363 376  
364 377 // Диалог между пользователями
... ... @@ -535,10 +548,8 @@ class EmployerController extends Controller
535 548 return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2])
536 549 ->withErrors($validator);
537 550 } else {
538   - if ($request->has('file')) {
539   - $params['file'] = $request->file('file')->store("messages", 'public');
540   - }
541   - Message::create($params);
  551 + Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages');
  552 +
542 553 //return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2]);
543 554 return redirect()->route('employer.dialog',
544 555 ['user1' => $user1, 'user2' => $user2, 'ad_employer' => $id_vacancy, 'ad_name' => $ad_name]);
... ... @@ -756,25 +767,23 @@ class EmployerController extends Controller
756 767 //////////////////////////////////////////////////////////////////
757 768 public function new_message(Request $request) {
758 769 $params = $request->all();
  770 +
759 771 $id = $params['_user_id'];
760   - $message = new Message();
761   - $message->user_id = $params['_user_id'];
762   - $message->to_user_id = $params['_to_user_id'];
763   - $message->title = $params['title'];
764   - $message->text = $params['text'];
765   - if ($request->has('_file')) {
766   - $message->file = $request->file('_file')->store("worker/$id", 'public');
767   - }
768   - $message->ad_employer_id = $params['_vacancy'];
769   - $message->flag_new = 1;
770   - $id_message = $message->save();
  772 + $message_params = [
  773 + 'title' => $params['title'],
  774 + 'text' => $params['text'],
  775 + 'ad_employer_id' => $params['_vacancy'],
  776 + 'flag_new' => 1
  777 + ];
771 778  
772   - //$data['message_id'] = $id_message;
773   - //$data['ad_employer_id'] = $params['_vacancy'];
774   - //$data['job_title_id'] = 0;
  779 + Message::add_message(
  780 + $request,
  781 + $params['_user_id'],
  782 + $params['_to_user_id'],
  783 + $message_params,
  784 + file_store_path: "worker/$id"
  785 + );
775 786  
776   - $data['flag'] = 1;
777   - //$ad_responce = ad_response::create($data);
778 787 return redirect()->route('employer.messages', ['type_message' => 'output']);
779 788 }
780 789  
app/Http/Controllers/WorkerController.php
... ... @@ -1046,17 +1046,20 @@ class WorkerController extends Controller
1046 1046 $params = $request->all();
1047 1047  
1048 1048 $id = $params['send_user_id'];
1049   - $message = new Message();
1050   - $message->user_id = $params['send_user_id'];
1051   - $message->to_user_id = $params['send_to_user_id'];
1052   - $message->title = $params['send_title'];
1053   - $message->text = $params['send_text'];
1054   - $message->ad_employer_id = $params['send_vacancy'];
1055   - if ($request->has('send_file')) {
1056   - $message->file = $request->file('send_file')->store("worker/$id", 'public');
1057   - }
1058   - $message->flag_new = 1;
1059   - $id_message = $message->save();
  1049 + $message_params = [
  1050 + 'title' => $params['send_title'],
  1051 + 'text' => $params['send_text'],
  1052 + 'ad_employer_id' => $params['send_vacancy'],
  1053 + 'flag_new' => 1
  1054 + ];
  1055 +
  1056 + $id_message = Message::add_message(
  1057 + $request,
  1058 + $params['send_user_id'],
  1059 + $params['send_to_user_id'],
  1060 + $message_params,
  1061 + file_store_path: "worker/$id"
  1062 + );
1060 1063  
1061 1064 $data['message_id'] = $id_message;
1062 1065 $data['ad_employer_id'] = $params['send_vacancy'];
... ... @@ -1098,11 +1101,8 @@ class WorkerController extends Controller
1098 1101 return redirect()->route('worker.dialog', ['user1' => $user1, 'user2' => $user2, 'ad_employer' => $id_vacancy, 'ad_name' => $ad_name])
1099 1102 ->withErrors($validator);
1100 1103 } else {
1101   - if ($request->has('file')) {
1102   - $params['file'] = $request->file('file')->store("messages", 'public');
1103   - }
1104   - Message::create($params);
1105   - //return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2]);
  1104 + Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages');
  1105 +
1106 1106 return redirect()->route('worker.dialog',
1107 1107 ['user1' => $user1, 'user2' => $user2, 'ad_employer' => $id_vacancy, 'ad_name' => $ad_name]);
1108 1108  
... ... @@ -0,0 +1,38 @@
  1 +<?php
  2 +
  3 +namespace App\Models;
  4 +
  5 +use Illuminate\Database\Eloquent\Factories\HasFactory;
  6 +use Illuminate\Database\Eloquent\Model;
  7 +
  8 +class Chat extends Model
  9 +{
  10 + use HasFactory;
  11 +
  12 + protected $fillable = [
  13 + 'user_id',
  14 + 'to_user_id',
  15 + 'is_removed',
  16 + 'is_fixed',
  17 + 'last_message_date',
  18 + 'last_message_id',
  19 + 'fixed_time'
  20 + ];
  21 +
  22 + public function user() {
  23 + return $this->belongsTo(User::class, 'to_user_id');
  24 + }
  25 +
  26 + public function worker() {
  27 + return $this->belongsTo(Worker::class, 'to_user_id', 'user_id');
  28 + }
  29 +
  30 + public function employer() {
  31 + return $this->belongsTo(Employer::class, 'to_user_id', 'user_id');
  32 + }
  33 +
  34 + public function last_message() {
  35 + return $this->belongsTo(Message::class, 'last_message_id');
  36 + }
  37 +
  38 +}
app/Models/Message.php
... ... @@ -4,6 +4,8 @@ namespace App\Models;
4 4  
5 5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 6 use Illuminate\Database\Eloquent\Model;
  7 +use Illuminate\Http\Request;
  8 +use Illuminate\Support\Facades\Validator;
7 9  
8 10 class Message extends Model
9 11 {
... ... @@ -12,6 +14,7 @@ class Message extends Model
12 14 protected $fillable = [
13 15 'user_id',
14 16 'to_user_id',
  17 + 'title',
15 18 'text',
16 19 'file',
17 20 'flag_new',
... ... @@ -38,4 +41,36 @@ class Message extends Model
38 41 public function vacancies() {
39 42 return $this->belongsTo(Ad_employer::class, 'ad_employer_id', 'id');
40 43 }
  44 +
  45 + public static function add_message(
  46 + Request $request,
  47 + int $user_id,
  48 + int $to_user_id,
  49 + array $message_params,
  50 + string $file_store_path = '/'
  51 + ) {
  52 + $message_params['user_id'] = $user_id;
  53 + $message_params['to_user_id'] = $to_user_id;
  54 + if ($request->has('file')) {
  55 + $message_params['file'] = $request->file('file')->store($file_store_path, 'public');
  56 + }
  57 +
  58 + $new_message = Message::create($message_params);
  59 +
  60 + if (!empty($new_message->id)) {
  61 + Chat::updateOrCreate(
  62 + ['user_id' => $user_id, 'to_user_id' => $to_user_id],
  63 + ['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]
  64 + );
  65 +
  66 + Chat::updateOrCreate(
  67 + ['user_id' => $to_user_id, 'to_user_id' => $user_id],
  68 + ['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]
  69 + );
  70 + }
  71 +
  72 + return $new_message->id ?? 0;
  73 +
  74 + }
  75 +
41 76 }
database/migrations/2024_07_19_063615_create_chat_table.php
... ... @@ -0,0 +1,38 @@
  1 +<?php
  2 +
  3 +use Illuminate\Database\Migrations\Migration;
  4 +use Illuminate\Database\Schema\Blueprint;
  5 +use Illuminate\Support\Facades\Schema;
  6 +
  7 +return new class extends Migration
  8 +{
  9 + /**
  10 + * Run the migrations.
  11 + *
  12 + * @return void
  13 + */
  14 + public function up()
  15 + {
  16 + Schema::create('chats', function (Blueprint $table) {
  17 + $table->id();
  18 + $table->integer('user_id')->nullable(false);
  19 + $table->integer('to_user_id');
  20 + $table->boolean('is_removed')->default(false);
  21 + $table->boolean('is_fixed')->default(false);
  22 + $table->dateTime('last_message_date')->nullable(false);
  23 + $table->integer('last_message_id')->nullable(false);
  24 + $table->dateTime('fixed_time')->nullable(true);
  25 + $table->timestamps();
  26 + });
  27 + }
  28 +
  29 + /**
  30 + * Reverse the migrations.
  31 + *
  32 + * @return void
  33 + */
  34 + public function down()
  35 + {
  36 + Schema::dropIfExists('chats');
  37 + }
  38 +};
public/css/helpers.css
... ... @@ -896,4 +896,10 @@
896 896 border: 0;
897 897 }
898 898  
  899 +.hover-shadow:hover{
  900 + -webkit-box-shadow: 0px 0px 8px 1px rgba(66, 68, 90, 1);
  901 + -moz-box-shadow: 0px 0px 8px 1px rgba(66, 68, 90, 1);
  902 + box-shadow: 0px 0px 8px 1px rgba(66, 68, 90, 1);
  903 +}
  904 +
899 905 /*# sourceMappingURL=helpers.css.map */
public/css/style_may2024.css
... ... @@ -6777,13 +6777,14 @@ main + .news {
6777 6777 @media (min-width: 768px) {
6778 6778 .messages__item {
6779 6779 padding: 20px;
6780   - font-size: 16px;
  6780 + font-size: 18px;
6781 6781 }
6782 6782 }
6783 6783 .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) {
6784 6784 display: -webkit-box;
6785 6785 display: -ms-flexbox;
6786 6786 display: flex;
  6787 + justify-content: space-between;
6787 6788 }
6788 6789 .messages__item-info {
6789 6790 display: -webkit-box;
... ... @@ -6860,16 +6861,33 @@ main + .news {
6860 6861 .messages__item-text span {
6861 6862 color: #000;
6862 6863 }
6863   -.messages__item-date {
6864   - color: #000;
6865   - width: 90px;
6866   - text-align: right;
  6864 +.messages__item-actions{
  6865 +
6867 6866 }
6868   -@media (min-width: 768px) {
6869   - .messages__item-date {
6870   - width: 150px;
6871   - }
  6867 +.messages__item-buttons{
  6868 + float: right;
  6869 +}
  6870 +.messages__item-buttons button{
  6871 + padding: 0;
  6872 + background: unset;
  6873 + border: unset;
  6874 +}
  6875 +.messages__item-buttons button svg{
  6876 + width: 25px;
  6877 + height: 25px;
  6878 + color: gray;
  6879 +}
  6880 +.messages__item-buttons button svg:hover{
  6881 + color: black;
6872 6882 }
  6883 +.messages__item-date {
  6884 + color: #00000070;
  6885 + width: 90px;
  6886 + text-align: right;
  6887 + font-size: 14px;
  6888 + margin-bottom: 8px;
  6889 +}
  6890 +
6873 6891 .messages.active .messages__item {
6874 6892 display: -webkit-box;
6875 6893 display: -ms-flexbox;
resources/views/employers/messages.blade.php
... ... @@ -69,149 +69,55 @@
69 69 <h2 class="title cabinet__title">Сообщения</h2>
70 70 </div>
71 71 <div class="cabinet__body-item">
72   - <div class="cabinet__filters">
73   - <div class="cabinet__filters-item">
74   - <div class="cabinet__filters-buttons">
75   - <a href="{{ route('employer.messages', ['type_message' => 'input']) }}" class="button button_light @if ($type_message == 'input') active @endif">Входящие ({{ $count_input }})</a>
76   - <a href="{{ route('employer.messages', ['type_message' => 'output']) }}" class="button button_light @if ($type_message == 'output') active @endif">Исходящие ({{ $count_output }})</a>
77   - </div>
78   - </div>
79   - <!--<div class="cabinet__filters-item">
80   - <div class="select">
81   - <select class="js-select2" id="sort_ajax" name="sort_ajax">
82   - <option value="default">Сортировка (по умолчанию)</option>
83   - <option value="name (asc)">По имени (возрастание)</option>
84   - <option value="name (desc)">По имени (убывание)</option>
85   - <option value="created_at (asc)">По дате (возрастание)</option>
86   - <option value="created_at (desc)">По дате (убывание)</option>
87   - </select>
88   - </div>
89   - </div>-->
90   - </div>
91 72 <div class="messages">
92   - <!--<button type="button" class="messages__button js-toggle js-parent-toggle button button_light button_more">
93   - <span>Показать ещё</span>
94   - <span>Скрыть</span>
95   - </button>-->
96 73 <div class="messages__body">
97   - @if ($messages->count())
98   - @foreach($messages as $it)
99   - <div class="messages__item">
100   - <div class="messages__item-info">
101   - <div class="messages__item-photo">
102   - <svg>
103   - <use xlink:href="{{ asset('images/sprite.svg#pic') }}"></use>
104   - </svg>
105   - @if ($type_message == 'input')
106   - @if (isset($it->user_from->is_worker))
107   - @if ($it->user_from->is_worker)
108   - @if (isset($it->user_from->workers[0]->photo))
109   - <img src="{{ asset(Storage::url($it->user_from->workers[0]->photo)) }}" alt="">
110   - @else
111   - <img src="{{ asset('images/default_man.jpg') }}" alt="">
112   - @endif
113   - @else
114   - @if (isset($it->user_from->employers[0]->logo))
115   - <img src="{{ asset(Storage::url($it->user_from->employers[0]->logo)) }}" alt="">
116   - @else
117   - <img src="{{ asset('images/default_man.jpg') }}" alt="">
118   - @endif
119   - @endif
120   - @else
121   - <img src="{{ asset('images/default_man.jpg') }}" alt="">
122   - @endif
123   - @endif
124   -
125   - @if ($type_message == 'output')
126   - @if (isset($it->user_from->is_worker))
127   - @if ($it->user_from->is_worker)
128   - @if (isset($it->user_from->workers[0]->photo))
129   - <img src="{{ asset(Storage::url($it->user_from->workers[0]->photo)) }}" alt="">
130   - @else
131   - <img src="{{ asset('images/default_man.jpg') }}" alt="">
132   - @endif
133   - @else
134   - @if (isset($it->user_from->employers[0]->logo))
135   - <img src="{{ asset(Storage::url($it->user_from->employers[0]->logo)) }}" alt="">
136   - @else
137   - <img src="{{ asset('images/default_man.jpg') }}" alt="">
138   - @endif
139   - @endif
  74 + @if ($chats->count())
  75 + @foreach($chats as $chat)
  76 + <div class="">
  77 + <a class="messages__item hover-shadow" href="{{ route('worker.dialog', ['user1' => $chat->user_id, 'user2' => $chat->to_user_id]) }}">
  78 + <div class="messages__item-info">
  79 + <div class="messages__item-photo">
  80 + @if (isset($chat->worker->photo))
  81 + <img src="{{ asset(Storage::url($chat->worker->photo)) }}" alt="">
140 82 @else
141 83 <img src="{{ asset('images/default_man.jpg') }}" alt="">
142 84 @endif
143   - @endif
144   - </div>
145   - <div class="messages__item-text">
146   - <div>
147   - От: @if (isset($it->user_from))
148   - @if ($it->user_from->id !== $user_id)
149   - <!--<a href=" route('employer.dialog', ['user1' => $user_id, 'user2' => $it->user_from->id]) }}" style="text-decoration: underline">-->
150   - {{ $it->user_from->surname." ".$it->user_from->name_man." ".$it->user_from->surname2." (".$it->user_from->id.")" }}
151   - <!--</a>-->
152   - @else
153   - {{ $it->user_from->surname." ".$it->user_from->name_man." ".$it->user_from->surname2." (".$it->user_from->id.")" }}
154   - @endif
155   - @else
156   - Удаленный пользователь
157   - @endif
158   - <br>
159   - К: @if (isset($it->user_to))
160   - @if ($it->user_to->id !== $user_id)
161   - <!--<a href=" route('employer.dialog', ['user1' => $user_id, 'user2' => $it->user_to->id]) }}" style="text-decoration: underline">-->
162   - {{ $it->user_to->surname." ".$it->user_to->name_man." ".$it->user_to->surname2." (".$it->user_to->id.")" }}
163   - <!--</a>-->
164   - @else
165   - {{ $it->user_to->surname." ".$it->user_to->name_man." ".$it->user_to->surname2." (".$it->user_to->id.")" }}
166   - @endif
167   - @else
168   - Удаленный пользователь
169   - @endif
170 85 </div>
171   - <div>
172   - <span>Вакансия: </span>
173   - @if (isset($it->user_from))
174   - @if ($it->user_from->id !== $user_id)
175   - @if (isset($it->vacancies))
176   - <a href="{{ route('employer.dialog', ['user1' => $user_id, 'user2' => $it->user_from->id, 'ad_employer' => $it->vacancies->id, 'ad_name' => $it->vacancies->name ]) }}" style="text-decoration: underline">
177   - {{ $it->vacancies->name." (".($it->vacancies->id).")" }}
178   - </a>
179   - @else
180   - <a href="{{ route('employer.dialog', ['user1' => $user_id, 'user2' => $it->user_from->id, 'ad_employer' => 0, 'ad_name' => 'Не указана вакансия' ]) }}" style="text-decoration: underline">
181   - Вакансия не указана
182   - </a>
183   - @endif
184   - @else
185   -
186   - @endif
187   - @endif
188   -
189   - @if (isset($it->user_to))
190   - @if ($it->user_to->id !== $user_id)
191   - @if (isset($it->vacancies))
192   - <a href="{{ route('employer.dialog', ['user1' => $user_id, 'user2' => $it->user_to->id, 'ad_employer' => $it->vacancies->id, 'ad_name' => $it->vacancies->name ]) }}" style="text-decoration: underline">
193   - {{ $it->vacancies->name." (".($it->vacancies->id).")" }}
194   - </a>
195   - @else
196   - <a href="{{ route('employer.dialog', ['user1' => $user_id, 'user2' => $it->user_to->id, 'ad_employer' => 0, 'ad_name' => 'Не указана вакансия' ]) }}" style="text-decoration: underline">
197   - Вакансия не указана
198   - </a>
199   - @endif
  86 + <div class="messages__item-text">
  87 + <div>
  88 + <b>
  89 + @if ($chat->employer && $chat->employer->name_company)
  90 + {{ $chat->employer->name_company }}
200 91 @else
201   -
  92 + {{ $chat->user->surname . ' ' . $chat->user->name_man . ' ' . $chat->user->surname2 }}
202 93 @endif
203   - @endif
204   -
  94 + </b>
  95 + </div>
  96 + <div>
  97 + {{ $chat->last_message->text }}
  98 + </div>
205 99 </div>
206   - <div><span>Текст:</span>{{ $it->text }}</div>
207 100 </div>
208   - </div>
209 101  
210   - <div class="messages__item-date">{{ $it->created_at }}</div>
  102 + <div class="messages__item-actions">
  103 + <div class="messages__item-date max-content">{{ date(' H:i, d.m.Y', strtotime($chat->created_at)) }}</div>
  104 + <div class="messages__item-buttons">
  105 + <button class="mr-10">
  106 + <img src="{{ asset("images/icon-pin-empty.png") }}"/>
  107 + </button>
  108 + <button class="">
  109 + <svg>
  110 + <use xlink:href="{{ asset('images/sprite.svg#del') }}"></use>
  111 + </svg>
  112 + </button>
  113 + </div>
  114 + <div class="clear"></div>
  115 + </div>
  116 + </a>
211 117 </div>
212 118 @endforeach
213 119 <div style="margin-top: 20px">
214   - {{ $messages->onEachSide(0)->appends($_GET)->links('paginate') }}
  120 + {{ $chats->onEachSide(0)->appends($_GET)->links('paginate') }}
215 121 </div><!-- конец -->
216 122 @else
217 123 <div class="notify">