GroupsController.php
4.88 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
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Group_user;
use App\Models\Group_works;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class GroupsController extends Controller
{
public $limit = 15;
// индексная страница
public function index() {
$groups = Group_user::with('user')->with('ingroup')->active()->paginate(15);
return view('admin.groups.index', compact('groups'));
}
// форма добавления группы
public function add(Request $request) {
/*$editor = Auth::user()->id;
$users = User::query()->active()->Offset(0)->limit($this->limit)->get();
return view('admin.groups.add', compact('editor', 'users'));*/
$hide = false;
$total = User::query()->active()->count();
$amt = ceil($total / $this->limit);
if ($this->limit >= $total) $hide = true;
if ($request->ajax()) {
$page = intval($request->page);
$page = (empty($page)) ? 1 : $page;
$start = ($page != 1) ? $page * $this->limit - $this->limit : 0;
$users = User::query()->active()->Offset($start)->limit($this->limit)->get();
return view('admin.groups.ajax_add', compact('users', 'amt'));
} else {
$editor = Auth::user()->id;
$users = User::query()->active()->Offset(0)->limit($this->limit)->get();
return view('admin.groups.add', compact('editor', 'users', 'amt', 'hide'));
}
}
// форма сохранения добавленной группы
public function store(Request $request) {
$rules = [
'name_group' => 'required|min:3',
];
$messages = [
'required' => 'Укажите обязательное поле',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return redirect()->route('admin.add-group')
->withErrors($validator);
} else {
$id_group = Group_user::create($request->all())->id;
foreach ($request->usergroup as $user) {
$people = new Group_works();
$people->group_user_id = $id_group;
$people->user_id = $user;
$people->save();
}
return redirect()->route('admin.groups')
->with('success', 'Данные были успешно сохранены');
}
return redirect()->route('admin.groups');
}
// форма редактирования группы
public function edit(Group_user $group, Request $request) {
//https://snipp.ru/php/loading-scrolling
$hide = false;
$total = User::query()->active()->count();
$amt = ceil($total / $this->limit);
if ($this->limit >= $total) $hide = true;
if ($request->ajax()) {
$page = intval($request->page);
$page = (empty($page)) ? 1 : $page;
$start = ($page != 1) ? $page * $this->limit - $this->limit : 0;
$users = User::query()->active()->Offset($start)->limit($this->limit)->get();
return view('admin.groups.ajax', compact('users', 'group', 'amt'));
} else {
$editor = Auth::user()->id;
$users = User::query()->active()->Offset(0)->limit($this->limit)->get();
return view('admin.groups.edit', compact('editor', 'group', 'users', 'amt', 'hide'));
}
}
// форма сохранения редактированной группы
public function update(Group_user $group, Request $request) {
$params = $request->all();
unset($params['usergroup']);
$rules = [
'name_group' => 'required|min:3',
];
$messages = [
'required' => 'Укажите обязательное поле',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return redirect()->route('admin.edit-group', ['group' => $group->id])
->withErrors($validator);
} else {
$group->update($request->all());
$group->ingroup()->sync($request->usergroup);
/*if ($request->usergroup->count()) {
foreach ($request->usergroup as $us) {
Group_works
}
}*/
return redirect()->route('admin.groups')
->with('success', 'Данные были успешно сохранены');
}
return redirect()->route('admin.groups');
}
public function destroy(Group_user $group) {
$group->update(['is_remove' => 1]);
return redirect()->route('admin.groups');
}
}