1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers\Admin\User; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\RedirectResponse; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\View\View; |
7
|
|
|
use Ultraware\Roles\Models\Role; |
8
|
|
|
use Xetaravel\Http\Controllers\Admin\Controller; |
9
|
|
|
use Xetaravel\Models\Repositories\UserRepository; |
10
|
|
|
use Xetaravel\Models\Repositories\AccountRepository; |
11
|
|
|
use Xetaravel\Models\User; |
12
|
|
|
use Xetaravel\Models\Validators\UserValidator; |
13
|
|
|
|
14
|
|
|
class UserController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Show the search page. |
18
|
|
|
* |
19
|
|
|
* @return \Illuminate\View\View |
20
|
|
|
*/ |
21
|
|
|
public function index(): View |
22
|
|
|
{ |
23
|
|
|
$latestUsers = User::with(['roles']) |
24
|
|
|
->limit(5) |
25
|
|
|
->latest() |
26
|
|
|
->get(); |
27
|
|
|
|
28
|
|
|
$breadcrumbs = $this->breadcrumbs->addCrumb('Manage Users', route('admin.user.user.index')); |
|
|
|
|
29
|
|
|
|
30
|
|
|
return view('Admin::User.user.index', compact('latestUsers', 'breadcrumbs')); |
31
|
|
|
} |
32
|
|
|
/** |
33
|
|
|
* Search users related to the type. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Http\Request $request |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\View\View |
38
|
|
|
*/ |
39
|
|
|
public function search(Request $request): View |
40
|
|
|
{ |
41
|
|
|
$query = User::with(['roles'])->select(); |
42
|
|
|
$search = str_replace('%', '\\%', trim($request->input('search'))); |
43
|
|
|
$type = trim($request->input('type')); |
44
|
|
|
|
45
|
|
|
switch ($type) { |
46
|
|
|
case 'username': |
47
|
|
|
$query->where('username', 'like', '%' . $search . '%'); |
48
|
|
|
break; |
49
|
|
|
|
50
|
|
|
case 'email': |
51
|
|
|
$query->where('email', 'like', '%' . $search . '%'); |
52
|
|
|
break; |
53
|
|
|
|
54
|
|
|
case 'register_ip': |
55
|
|
|
$query->where('register_ip', 'like', '%' . $search . '%'); |
56
|
|
|
break; |
57
|
|
|
|
58
|
|
|
case 'last_login_ip': |
59
|
|
|
$query->where('last_login_ip', 'like', '%' . $search . '%'); |
60
|
|
|
break; |
61
|
|
|
|
62
|
|
|
default: |
63
|
|
|
$query->where('username', 'like', '%' . $search . '%'); |
64
|
|
|
$type = 'username'; |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
$users = $query |
68
|
|
|
->paginate(10) |
69
|
|
|
->appends($request->except('page')); |
70
|
|
|
|
71
|
|
|
$breadcrumbs = $this->breadcrumbs |
72
|
|
|
->addCrumb('Manage Users', route('admin.user.user.index')) |
73
|
|
|
->addCrumb('Search an user', route('admin.user.user.search')); |
74
|
|
|
|
75
|
|
|
return view('Admin::User.user.search', compact('users', 'breadcrumbs', 'type', 'search')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Show the update form. |
80
|
|
|
* |
81
|
|
|
* @param \Illuminate\Http\Request $request |
82
|
|
|
* @param string $slug The slug of the user. |
83
|
|
|
* @param int $id The id of the user. |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
|
|
|
86
|
|
|
*/ |
87
|
|
|
public function showUpdateForm(Request $request, string $slug, int $id) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$user = User::find($id); |
90
|
|
|
|
91
|
|
|
if (is_null($user)) { |
92
|
|
|
return redirect() |
93
|
|
|
->route('admin.user.user.index') |
94
|
|
|
->with('danger', 'This user doesn\'t exist or has been deleted !'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$roles = Role::pluck('name', 'id'); |
98
|
|
|
$attributes = Role::pluck('id')->toArray(); |
99
|
|
|
|
100
|
|
|
$optionsAttributes = []; |
101
|
|
|
foreach ($attributes as $attribute) { |
102
|
|
|
$optionsAttributes[$attribute] = [ |
103
|
|
|
'style' => Role::where('id', $attribute)->select('css')->first()->css |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$breadcrumbs = $this->breadcrumbs |
108
|
|
|
->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0') |
109
|
|
|
->addCrumb('Manage Users', route('admin.user.user.index')) |
110
|
|
|
->addCrumb( |
111
|
|
|
'Edit ' . e($user->username), |
112
|
|
|
route('admin.user.user.update', $user->slug, $user->id) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return view('Admin::User.user.update', compact('user', 'roles', 'optionsAttributes', 'breadcrumbs')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Handle an user update request for the application. |
120
|
|
|
* |
121
|
|
|
* @param \Illuminate\Http\Request $request |
122
|
|
|
* @param int $id The id of the user to update. |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Http\RedirectResponse |
125
|
|
|
*/ |
126
|
|
|
public function update(Request $request, int $id): RedirectResponse |
127
|
|
|
{ |
128
|
|
|
$user = User::find($id); |
129
|
|
|
|
130
|
|
|
if (is_null($user)) { |
131
|
|
|
return redirect() |
132
|
|
|
->back() |
133
|
|
|
->with('danger', 'This user doesn\'t exist or has been deleted !'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
UserValidator::update($request->all(), $user->id)->validate(); |
137
|
|
|
|
138
|
|
|
UserRepository::update($request->all(), $user); |
139
|
|
|
AccountRepository::update($request->get('account'), $user->id); |
140
|
|
|
|
141
|
|
|
$user->roles()->sync($request->get('roles')); |
142
|
|
|
|
143
|
|
|
return redirect() |
144
|
|
|
->back() |
145
|
|
|
->with('success', 'This user has been updated successfully !'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: