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