Commit d3fc274b19b550db8993a1a530c3fec9c22dfab9

Authored by Hayk Nazaryan
1 parent b57da04555
Exists in master

users fixes

Showing 3 changed files with 53 additions and 2 deletions Inline Diff

app/Console/Commands/OptimizeUsers.php
File was created 1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Models\Employer;
6 use App\Models\Worker;
7 use Illuminate\Console\Command;
8
9 class OptimizeUsers extends Command
10 {
11 /**
12 * The name and signature of the console command.
13 *
14 * @var string
15 */
16 protected $signature = 'optimize:users';
17
18 /**
19 * The console command description.
20 *
21 * @var string
22 */
23 protected $description = 'Command description';
24
25 /**
26 * Execute the console command.
27 *
28 * @return int
29 */
30 public function handle()
31 {
32 Employer::doesntHave('users')->delete();
33 Worker::doesntHave('users')->delete();
34
35 }
36 }
37
app/Http/Controllers/Admin/UsersController.php
1 <?php 1 <?php
2 2
3 namespace App\Http\Controllers\Admin; 3 namespace App\Http\Controllers\Admin;
4 4
5 use App\Http\Controllers\Controller; 5 use App\Http\Controllers\Controller;
6 use App\Http\Requests\BaseUserRequest; 6 use App\Http\Requests\BaseUserRequest;
7 use App\Models\ContentRoles; 7 use App\Models\ContentRoles;
8 use App\Models\Job_title; 8 use App\Models\Job_title;
9 use App\Models\User; 9 use App\Models\User;
10 use App\Models\Worker; 10 use App\Models\Worker;
11 use Illuminate\Http\Request; 11 use Illuminate\Http\Request;
12 use Illuminate\Support\Facades\Auth; 12 use Illuminate\Support\Facades\Auth;
13 use Illuminate\Support\Facades\Storage; 13 use Illuminate\Support\Facades\Storage;
14 use PhpOffice\PhpSpreadsheet\Spreadsheet; 14 use PhpOffice\PhpSpreadsheet\Spreadsheet;
15 use PhpOffice\PhpSpreadsheet\Style\Alignment; 15 use PhpOffice\PhpSpreadsheet\Style\Alignment;
16 use PhpOffice\PhpSpreadsheet\Style\Border; 16 use PhpOffice\PhpSpreadsheet\Style\Border;
17 use PhpOffice\PhpSpreadsheet\Style\Font; 17 use PhpOffice\PhpSpreadsheet\Style\Font;
18 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; 18 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
19 19
20 class UsersController extends Controller 20 class UsersController extends Controller
21 { 21 {
22 public function index(Request $request) { 22 public function index(Request $request) {
23 $title = 'Админка - Пользователи системы'; 23 $title = 'Админка - Пользователи системы';
24 $id_admin = Auth::user()->id; 24 $id_admin = Auth::user()->id;
25 if ($request->ajax()) { 25 if ($request->ajax()) {
26 $user = User::find($request->id); 26 $user = User::find($request->id);
27 $request->offsetUnset('id'); 27 $request->offsetUnset('id');
28 $user->update($request->all()); 28 $user->update($request->all());
29 } 29 }
30 30
31 $find_key = ""; 31 $find_key = "";
32 $users = User::query(); 32 $users = User::query();
33 if (isset($request->find)) { 33 if (isset($request->find)) {
34 $find_key = $request->find; 34 $find_key = $request->find;
35 $users = $users->where('name', 'LIKE', "%$find_key%") 35 $users = $users->where('name', 'LIKE', "%$find_key%")
36 ->orWhere('email', 'LIKE', "%$find_key%"); 36 ->orWhere('email', 'LIKE', "%$find_key%");
37 } 37 }
38 38
39 $users = $users->paginate(15); 39 $users = $users->paginate(15);
40 40
41 if ($request->ajax()) { 41 if ($request->ajax()) {
42 return view('admin.users.index_ajax', compact('users', 'id_admin')); 42 return view('admin.users.index_ajax', compact('users', 'id_admin'));
43 } else { 43 } else {
44 return view('admin.users.index', compact('users', 'title', 'id_admin', 'find_key')); 44 return view('admin.users.index', compact('users', 'title', 'id_admin', 'find_key'));
45 } 45 }
46 } 46 }
47 47
48 public function user_delete(User $user) { 48 public function user_delete(User $user) {
49 $id = $user->delete(); 49
50 $worker = $user->workers()->first();
51 $employer = $user->employers()->first();
52
53 if ($worker) {
54
55 $worker->delete();
56
57 } elseif ($employer) {
58
59 $employer->delete();
60
61 }
62
63 $user->delete();
50 64
51 return redirect()->route('admin.users')->with('Пользователь был удален из системы'); 65 return redirect()->route('admin.users')->with('Пользователь был удален из системы');
52 } 66 }
53 67
54 public function index_bd(Request $request) { 68 public function index_bd(Request $request) {
55 $title = 'Админка - Пользователи базы данных'; 69 $title = 'Админка - Пользователи базы данных';
56 70
57 $find_key = ""; 71 $find_key = "";
58 $users = User::query(); 72 $users = User::query();
59 if (isset($request->find)) { 73 if (isset($request->find)) {
60 $find_key = $request->find; 74 $find_key = $request->find;
61 $users = $users->where('name', 'LIKE', "%$find_key%") 75 $users = $users->where('name', 'LIKE', "%$find_key%")
62 ->orWhere('email', 'LIKE', "%$find_key%") 76 ->orWhere('email', 'LIKE', "%$find_key%")
63 ->orWhere('telephone', 'LIKE', "%$find_key%"); 77 ->orWhere('telephone', 'LIKE', "%$find_key%");
64 } 78 }
65 79
66 $users = $users->Baseuser() 80 $users = $users->Baseuser()
67 ->orderByDesc(Worker::select('created_at')->whereColumn('workers.user_id', 'users.id')) 81 ->orderByDesc(Worker::select('created_at')->whereColumn('workers.user_id', 'users.id'))
68 ->paginate(15); 82 ->paginate(15);
69 83
70 if ($request->ajax()) { 84 if ($request->ajax()) {
71 return view('admin.users.index_bd_ajax', compact('users')); 85 return view('admin.users.index_bd_ajax', compact('users'));
72 } else { 86 } else {
73 return view('admin.users.index_bd', compact('users', 'title', 'find_key')); 87 return view('admin.users.index_bd', compact('users', 'title', 'find_key'));
74 } 88 }
75 } 89 }
76 90
77 public function add_bd() { 91 public function add_bd() {
78 $list_job_titles = Job_title::query()->active()->where('is_bd', '=' , '2')->orderBy('name', 'asc')->get(); 92 $list_job_titles = Job_title::query()->active()->where('is_bd', '=' , '2')->orderBy('name', 'asc')->get();
79 return view('admin.users.add', compact('list_job_titles')); 93 return view('admin.users.add', compact('list_job_titles'));
80 } 94 }
81 95
82 public function add_store_bd(BaseUserRequest $request) { 96 public function add_store_bd(BaseUserRequest $request) {
83 $params = $request->all(); 97 $params = $request->all();
84 98
85 if ($request->has('file')) { 99 if ($request->has('file')) {
86 $params['file'] = $request->file('file')->store('basedata', 'public'); 100 $params['file'] = $request->file('file')->store('basedata', 'public');
87 } 101 }
88 102
89 if (isset($request->name)) { 103 if (isset($request->name)) {
90 $params['name'] = $request->surname." ".$request->name_man." ".$request->surname2; 104 $params['name'] = $request->surname." ".$request->name_man." ".$request->surname2;
91 } 105 }
92 106
93 $user = User::create($params); 107 $user = User::create($params);
94 $worker = new Worker(); 108 $worker = new Worker();
95 $worker->positions_work = isset($params['positions_work']) ? json_encode($params['positions_work']) : []; 109 $worker->positions_work = isset($params['positions_work']) ? json_encode($params['positions_work']) : [];
96 $worker->user_id = $user->id; 110 $worker->user_id = $user->id;
97 $worker->comment = isset($params['comment']) ? $params['comment'] : null; 111 $worker->comment = isset($params['comment']) ? $params['comment'] : null;
98 $worker->save(); 112 $worker->save();
99 113
100 return redirect()->route('admin.basedata'); 114 return redirect()->route('admin.basedata');
101 } 115 }
102 116
103 public function edit_bd(User $user) { 117 public function edit_bd(User $user) {
104 $list_job_titles = Job_title::query() 118 $list_job_titles = Job_title::query()
105 ->active() 119 ->active()
106 ->where('is_bd', '=' , '2') 120 ->where('is_bd', '=' , '2')
107 ->orderByDesc('sort') 121 ->orderByDesc('sort')
108 ->orderBy('name', 'asc') 122 ->orderBy('name', 'asc')
109 ->get(); 123 ->get();
110 return view('admin.users.edit', compact('user', 'list_job_titles')); 124 return view('admin.users.edit', compact('user', 'list_job_titles'));
111 } 125 }
112 126
113 public function update_bd(BaseUserRequest $request, User $user) { 127 public function update_bd(BaseUserRequest $request, User $user) {
114 $params = $request->all(); 128 $params = $request->all();
115 $positions_work = $request->input('positions_work', []); 129 $positions_work = $request->input('positions_work', []);
116 130
117 if ($request->has('file')) { 131 if ($request->has('file')) {
118 if (!empty($user->file)) Storage::delete($user->file); 132 if (!empty($user->file)) Storage::delete($user->file);
119 $params['file'] = $request->file('file')->store('basedata', 'public'); 133 $params['file'] = $request->file('file')->store('basedata', 'public');
120 } else { 134 } else {
121 if (!empty($user->image)) $params['file'] = $user->file; 135 if (!empty($user->image)) $params['file'] = $user->file;
122 } 136 }
123 137
124 if (isset($request->name)) { 138 if (isset($request->name)) {
125 $params['name'] = $request->surname." ".$request->name_man." ".$request->surname2; 139 $params['name'] = $request->surname." ".$request->name_man." ".$request->surname2;
126 } 140 }
127 141
128 $user->update($params); 142 $user->update($params);
129 if (isset($user->workers[0]->id)) { 143 if (isset($user->workers[0]->id)) {
130 $worker = Worker::find($user->workers[0]->id); 144 $worker = Worker::find($user->workers[0]->id);
131 $worker->positions_work = $positions_work; 145 $worker->positions_work = $positions_work;
132 $worker->comment = isset($params['comment']) ? $params['comment'] : null; 146 $worker->comment = isset($params['comment']) ? $params['comment'] : null;
133 $worker->save(); 147 $worker->save();
134 } else { 148 } else {
135 $worker = new Worker(); 149 $worker = new Worker();
136 $worker->user_id = $user->id; 150 $worker->user_id = $user->id;
137 $worker->positions_work = $positions_work; 151 $worker->positions_work = $positions_work;
138 $worker->comment = isset($params['comment']) ? $params['comment'] : null; 152 $worker->comment = isset($params['comment']) ? $params['comment'] : null;
139 $worker->save(); 153 $worker->save();
140 } 154 }
141 155
142 return redirect()->route('admin.basedata'); 156 return redirect()->route('admin.basedata');
143 } 157 }
144 158
145 public function destroy_bd(User $user) { 159 public function destroy_bd(User $user) {
146 $user->delete(); 160 $user->delete();
147 return redirect()->route('admin.basedata'); 161 return redirect()->route('admin.basedata');
148 } 162 }
149 163
150 public function roles(Request $request) { 164 public function roles(Request $request) {
151 if ($request->ajax()) { 165 if ($request->ajax()) {
152 $user = User::find($request->id); 166 $user = User::find($request->id);
153 $request->offsetUnset('id'); 167 $request->offsetUnset('id');
154 $user->update($request->all()); 168 $user->update($request->all());
155 } 169 }
156 170
157 $users = User::query()->Realuser()->paginate(15); 171 $users = User::query()->Realuser()->paginate(15);
158 172
159 if ($request->ajax()) { 173 if ($request->ajax()) {
160 return view('admin.users.roles.index_ajax', compact('users')); 174 return view('admin.users.roles.index_ajax', compact('users'));
161 } else { 175 } else {
162 return view('admin.users.roles.index', compact('users')); 176 return view('admin.users.roles.index', compact('users'));
163 } 177 }
164 } 178 }
165 179
166 public function admin_roles(Request $request) { 180 public function admin_roles(Request $request) {
167 181
168 if ($request->ajax()) { 182 if ($request->ajax()) {
169 $id = $request->id; 183 $id = $request->id;
170 $request->offsetUnset('id'); 184 $request->offsetUnset('id');
171 ContentRoles::where('id', '=', $id)->update($request->all()); 185 ContentRoles::where('id', '=', $id)->update($request->all());
172 } 186 }
173 $roles = ContentRoles::query()->OrderBy('id')->paginate(25); 187 $roles = ContentRoles::query()->OrderBy('id')->paginate(25);
174 188
175 189
176 if ($request->ajax()) { 190 if ($request->ajax()) {
177 return view('admin.content.roles_index_ajax', compact('roles')); 191 return view('admin.content.roles_index_ajax', compact('roles'));
178 } else { 192 } else {
179 return view('admin.content.roles_index', compact('roles')); 193 return view('admin.content.roles_index', compact('roles'));
180 } 194 }
181 } 195 }
182 196
183 public function doc_bd(User $user) { 197 public function doc_bd(User $user) {
184 $id = $user->id; 198 $id = $user->id;
185 $spreadsheet = new Spreadsheet(); 199 $spreadsheet = new Spreadsheet();
186 $activeWorksheet = $spreadsheet->getActiveSheet(); 200 $activeWorksheet = $spreadsheet->getActiveSheet();
187 $activeWorksheet->setCellValue('A1', 'Отчет по соискателю'); 201 $activeWorksheet->setCellValue('A1', 'Отчет по соискателю');
188 $activeWorksheet->getStyle('A1')->applyFromArray([ 202 $activeWorksheet->getStyle('A1')->applyFromArray([
189 'font' => [ 203 'font' => [
190 'name' => 'Arial', 204 'name' => 'Arial',
191 'bold' => true, 205 'bold' => true,
192 'italic' => false, 206 'italic' => false,
193 'underline' => Font::UNDERLINE_DOUBLE, 207 'underline' => Font::UNDERLINE_DOUBLE,
194 'strikethrough' => false, 208 'strikethrough' => false,
195 'color' => [ 209 'color' => [
196 'rgb' => '808080' 210 'rgb' => '808080'
197 ] 211 ]
198 ], 212 ],
199 'borders' => [ 213 'borders' => [
200 'allBorders' => [ 214 'allBorders' => [
201 'borderStyle' => Border::BORDER_THIN, 215 'borderStyle' => Border::BORDER_THIN,
202 'color' => [ 216 'color' => [
203 'rgb' => '808080' 217 'rgb' => '808080'
204 ] 218 ]
205 ], 219 ],
206 'outline' => array( 220 'outline' => array(
207 'style' => Border::BORDER_THIN, 221 'style' => Border::BORDER_THIN,
208 'color' => array('rgb' => '000000') 222 'color' => array('rgb' => '000000')
209 ), 223 ),
210 ], 224 ],
211 225
212 'alignment' => [ 226 'alignment' => [
213 'horizontal' => Alignment::HORIZONTAL_CENTER, 227 'horizontal' => Alignment::HORIZONTAL_CENTER,
214 'vertical' => Alignment::VERTICAL_CENTER, 228 'vertical' => Alignment::VERTICAL_CENTER,
215 'wrapText' => true, 229 'wrapText' => true,
216 ] 230 ]
217 ]); 231 ]);
218 232
219 $activeWorksheet->setCellValue('A2', "Псевдоним/имя: ".$user->name); 233 $activeWorksheet->setCellValue('A2', "Псевдоним/имя: ".$user->name);
220 $activeWorksheet->setCellValue('A3', "Фамилия: ".$user->surname); 234 $activeWorksheet->setCellValue('A3', "Фамилия: ".$user->surname);
221 $activeWorksheet->setCellValue('A4', "Имя: ".$user->name_man); 235 $activeWorksheet->setCellValue('A4', "Имя: ".$user->name_man);
222 $activeWorksheet->setCellValue('A5', "Отчество: ".$user->surname2); 236 $activeWorksheet->setCellValue('A5', "Отчество: ".$user->surname2);
223 $activeWorksheet->setCellValue('A6', "Телефон: ".$user->telephone); 237 $activeWorksheet->setCellValue('A6', "Телефон: ".$user->telephone);
224 $activeWorksheet->setCellValue('A7', "Емайл: ".$user->email); 238 $activeWorksheet->setCellValue('A7', "Емайл: ".$user->email);
225 239
226 if (isset($user->workers[0]->id)) { 240 if (isset($user->workers[0]->id)) {
227 $activeWorksheet->setCellValue('A9', "Анкета: "); 241 $activeWorksheet->setCellValue('A9', "Анкета: ");
228 $activeWorksheet->setCellValue('A10', "Телефон: " . $user->workers[0]->telephone); 242 $activeWorksheet->setCellValue('A10', "Телефон: " . $user->workers[0]->telephone);
229 $activeWorksheet->setCellValue('A11', "Емайл: " . $user->workers[0]->email); 243 $activeWorksheet->setCellValue('A11', "Емайл: " . $user->workers[0]->email);
230 } 244 }
231 245
232 if (isset($user->jobtitles[0]->id)) { 246 if (isset($user->jobtitles[0]->id)) {
233 $activeWorksheet->setCellValue('A12', "Должность: " . $user->jobtitles[0]->name); 247 $activeWorksheet->setCellValue('A12', "Должность: " . $user->jobtitles[0]->name);
234 } 248 }
235 $activeWorksheet->getColumnDimension("A")->setWidth(100); 249 $activeWorksheet->getColumnDimension("A")->setWidth(100);
236 $writer = new Xlsx($spreadsheet); 250 $writer = new Xlsx($spreadsheet);
237 251
238 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 252 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
239 header('Content-Disposition: attachment; filename="user'.$id.'.xlsx"'); 253 header('Content-Disposition: attachment; filename="user'.$id.'.xlsx"');
240 header('Cache-Control: no-cache'); 254 header('Cache-Control: no-cache');
241 255
242 $writer->save('php://output'); 256 $writer->save('php://output');
243 $writer->save(storage_path("app/public/export/user$id.xlsx")); 257 $writer->save(storage_path("app/public/export/user$id.xlsx"));
244 258
245 //$spreadsheet->disconnectWorksheets(); 259 //$spreadsheet->disconnectWorksheets();
246 return redirect()->route('admin.basedata'); 260 return redirect()->route('admin.basedata');
247 261
248 } 262 }
249 } 263 }
250 264
app/Http/Controllers/CompanyController.php
1 <?php 1 <?php
2 2
3 namespace App\Http\Controllers; 3 namespace App\Http\Controllers;
4 4
5 use App\Models\Ad_employer; 5 use App\Models\Ad_employer;
6 use App\Models\Employer; 6 use App\Models\Employer;
7 use App\Models\User;
7 use Illuminate\Http\Request; 8 use Illuminate\Http\Request;
8 9
9 class CompanyController extends Controller 10 class CompanyController extends Controller
10 { 11 {
11 public function shipping_companies(Request $request) { 12 public function shipping_companies(Request $request) {
12 $emps = Employer::query()->with('ads')->where('status_hidden', '=', '0'); 13 $emps = Employer::query()->with('ads')->where('status_hidden', '=', '0')->whereHas('users');
13 if (($request->has('search')) && (!empty($request->get('search')))) { 14 if (($request->has('search')) && (!empty($request->get('search')))) {
14 $search = $request->get('search'); 15 $search = $request->get('search');
15 $emps = $emps->where('name_company', 'LIKE', "%$search%"); 16 $emps = $emps->where('name_company', 'LIKE', "%$search%");
16 } 17 }
17 18
18 $count_emps = $emps->count(); 19 $count_emps = $emps->count();
19 20
20 if ($request->get('sort')) { 21 if ($request->get('sort')) {
21 $sort = $request->get('sort'); 22 $sort = $request->get('sort');
22 switch ($sort) { 23 switch ($sort) {
23 case 'name_up': $emps = $emps->orderBy('name_company')->orderBy('id'); break; 24 case 'name_up': $emps = $emps->orderBy('name_company')->orderBy('id'); break;
24 case 'name_down': $emps = $emps->orderByDesc('name_company')->orderby('id'); break; 25 case 'name_down': $emps = $emps->orderByDesc('name_company')->orderby('id'); break;
25 case 'created_at_up': $emps = $emps->OrderBy('created_at')->orderBy('id'); break; 26 case 'created_at_up': $emps = $emps->OrderBy('created_at')->orderBy('id'); break;
26 case 'created_at_down': $emps = $emps->orderByDesc('created_at')->orderBy('id'); break; 27 case 'created_at_down': $emps = $emps->orderByDesc('created_at')->orderBy('id'); break;
27 case 'default': $emps = $emps->orderBy('id')->orderby('updated_at'); break; 28 case 'default': $emps = $emps->orderBy('id')->orderby('updated_at'); break;
28 default: $emps = $emps->orderBy('id')->orderby('updated_at'); break; 29 default: $emps = $emps->orderBy('id')->orderby('updated_at'); break;
29 } 30 }
30 } 31 }
31 if (is_null($request->get('sort'))){ 32 if (is_null($request->get('sort'))){
32 33
33 $emps = $emps->orderByDesc('created_at')->orderBy('id'); 34 $emps = $emps->orderByDesc('created_at')->orderBy('id');
34 35
35 } 36 }
36 37
37 $emps = $emps->paginate(4); 38 $emps = $emps->paginate(4);
38 39
39 if ($request->ajax()) { 40 if ($request->ajax()) {
40 if ($request->get('block') == '1') 41 if ($request->get('block') == '1')
41 return view('ajax.companies', compact('emps', 'count_emps')); 42 return view('ajax.companies', compact('emps', 'count_emps'));
42 else 43 else
43 return view('ajax.companies2', compact('emps', 'count_emps')); 44 return view('ajax.companies2', compact('emps', 'count_emps'));
44 } else { 45 } else {
45 return view('companies', compact('emps', 'count_emps')); 46 return view('companies', compact('emps', 'count_emps'));
46 } 47 }
47 } 48 }
48 49
49 public function info_company(Employer $company) { 50 public function info_company(Employer $company) {
50 if (isset(Auth()->user()->id)) { 51 if (isset(Auth()->user()->id)) {
51 $user_id = Auth()->user()->id; 52 $user_id = Auth()->user()->id;
52 } else { 53 } else {
53 $user_id = 0; 54 $user_id = 0;
54 } 55 }
55 56
56 $company = Employer::with('ads')->with('flots')->with('users') 57 $company = Employer::with('ads')->with('flots')->with('users')
57 ->where('id', '=', $company->id)->get(); 58 ->where('id', '=', $company->id)->get();
58 59
59 $title = $company[0]->name_company; 60 $title = $company[0]->name_company;
60 61
61 $ads = Ad_employer::query()->with('jobs')->with('jobs_code') 62 $ads = Ad_employer::query()->with('jobs')->with('jobs_code')
62 ->where('employer_id', '=', $company[0]->id) 63 ->where('employer_id', '=', $company[0]->id)
63 ->where('is_remove', '=', '0') 64 ->where('is_remove', '=', '0')
64 ->where('active_is', 1) 65 ->where('active_is', 1)
65 ->OrderByDesc('id') 66 ->OrderByDesc('id')
66 ->paginate(50) 67 ->paginate(50)
67 ; 68 ;
68 69
69 return view('info_company_new', compact('company', 'user_id', 'title', 'ads')); 70 return view('info_company_new', compact('company', 'user_id', 'title', 'ads'));
70 } 71 }
71 } 72 }
72 73