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