|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Articles; |
|
6
|
|
|
use App\Doctor; |
|
7
|
|
|
use App\Log; |
|
8
|
|
|
use App\Thread; |
|
9
|
|
|
use App\User; |
|
10
|
|
|
use Illuminate\Support\Facades\Auth; |
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
use Illuminate\Support\Facades\Hash; |
|
14
|
|
|
use App\Admin; |
|
15
|
|
|
use Illuminate\Support\Facades\Storage; |
|
16
|
|
|
|
|
17
|
|
|
class AdminController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* AdminController constructor. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->middleware('auth:admin'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
30
|
|
|
* @throws \Exception |
|
31
|
|
|
*/ |
|
32
|
|
|
public function dashboard() |
|
33
|
|
|
{ |
|
34
|
|
|
$data = [ |
|
35
|
|
|
'role' => session('role'), |
|
36
|
|
|
'articles' => count(Articles::all()), |
|
37
|
|
|
'doctors' => count(Doctor::all()), |
|
38
|
|
|
'members' => count(User::all()), |
|
39
|
|
|
'threads' => count(Thread::all()) |
|
40
|
|
|
]; |
|
41
|
|
|
return view('pages.dashboard')->with('data', $data); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* START OF ADMIN CRUD |
|
47
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
48
|
|
|
*/ |
|
49
|
|
|
public function index() |
|
50
|
|
|
{ |
|
51
|
|
|
$admin = Admin::all(); |
|
52
|
|
|
$data = [ |
|
53
|
|
|
'role' => session('role'), |
|
54
|
|
|
'admin' => $admin, |
|
55
|
|
|
]; |
|
56
|
|
|
return view('pages.admin')->with('data',$data); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
62
|
|
|
*/ |
|
63
|
|
|
public function create() |
|
64
|
|
|
{ |
|
65
|
|
|
$data = [ |
|
66
|
|
|
'role' => session('role') |
|
67
|
|
|
]; |
|
68
|
|
|
return view ('pages.ext.add-admin')->with('data',$data); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Request $request |
|
74
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
75
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function store(Request $request) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->validate($request,[ |
|
80
|
|
|
'name' => 'required|min:3|max:50', |
|
81
|
|
|
'email' => 'required|email', |
|
82
|
|
|
'password' => 'required_with:password_confirmation|same:password_confirmation|min:6', |
|
83
|
|
|
'password_confirmation' => 'min:6' |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$admin = new Admin; |
|
87
|
|
|
$admin->name = $request->input('name'); |
|
88
|
|
|
$admin->email = $request->input('email'); |
|
89
|
|
|
$admin->password = Hash::make($request->input('password')); |
|
90
|
|
|
|
|
91
|
|
|
if($admin->save()) { |
|
92
|
|
|
return redirect (route('admin.index'))->with('success', 'Admin berhasil di tambahkan !'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return redirect (route('admin.index'))->with('failed', 'Gagal menambah admin !'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $id |
|
101
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
102
|
|
|
*/ |
|
103
|
|
|
public function profile($id) |
|
104
|
|
|
{ |
|
105
|
|
|
$admin = $this->currentUser(); |
|
106
|
|
|
if ($admin->id == $id) { |
|
107
|
|
|
$data = [ |
|
108
|
|
|
'admin' => $admin |
|
109
|
|
|
]; |
|
110
|
|
|
return view('pages.profile')->with('data', $data); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $id |
|
119
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
120
|
|
|
*/ |
|
121
|
|
|
public function editProfile($id) |
|
122
|
|
|
{ |
|
123
|
|
|
$admin = $this->currentUser(); |
|
124
|
|
|
if ($admin->id == $id) { |
|
125
|
|
|
$data = [ |
|
126
|
|
|
'admin' => $admin |
|
127
|
|
|
]; |
|
128
|
|
|
return view('pages.profile-edit')->with('data', $data); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param Request $request |
|
137
|
|
|
* @param $id |
|
138
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
139
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
140
|
|
|
*/ |
|
141
|
|
|
public function updateProfile(Request $request, $id) |
|
142
|
|
|
{ |
|
143
|
|
|
$admin = $this->currentUser(); |
|
144
|
|
|
if ($admin->id == $id) { |
|
145
|
|
|
$this->validate($request, [ |
|
146
|
|
|
'profile_picture' => 'image|nullable|max:3999', |
|
147
|
|
|
'email' => 'required|email', |
|
148
|
|
|
'name' => 'required|min:3' |
|
149
|
|
|
]); |
|
150
|
|
|
|
|
151
|
|
|
$img = null; |
|
152
|
|
|
|
|
153
|
|
|
if($request->hasFile('profile_picture')) { |
|
154
|
|
|
|
|
155
|
|
|
if( $admin->profile_picture != "user-default.jpg") { |
|
156
|
|
|
Storage::delete('public/user_images/'.$admin->profile_picture); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// Get Filename.ext |
|
160
|
|
|
$fileNameWExt = $request->file('profile_picture')->getClientOriginalName(); |
|
161
|
|
|
// Get Filename |
|
162
|
|
|
$fileName = pathinfo($fileNameWExt, PATHINFO_FILENAME); |
|
163
|
|
|
// Get ext |
|
164
|
|
|
$ext = $request->file('profile_picture')->getClientOriginalExtension(); |
|
165
|
|
|
// Filename to Store |
|
166
|
|
|
$img = $fileName.'_'.time().'.'.$ext; |
|
167
|
|
|
// Upload Image |
|
168
|
|
|
$path = $request->file('profile_picture')->storeAs('public/user_images', $img); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$admin->name = $request->input('name'); |
|
172
|
|
|
$admin->email = $request->input('email'); |
|
173
|
|
|
if($request->hasFile('profile_picture')) { |
|
174
|
|
|
$admin->profile_picture = $img; |
|
175
|
|
|
} |
|
176
|
|
|
$admin->save(); |
|
177
|
|
|
|
|
178
|
|
|
return redirect(route('admin.profile', $admin->id))->with('success', 'Profil berhasil diubah !'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak melakukan transaksi tersebut.'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param $id |
|
187
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
188
|
|
|
*/ |
|
189
|
|
|
public function editPass($id) |
|
190
|
|
|
{ |
|
191
|
|
|
$admin = $this->currentUser(); |
|
192
|
|
|
if($admin->id == $id) { |
|
193
|
|
|
$data = [ |
|
194
|
|
|
'admin' => $admin |
|
195
|
|
|
]; |
|
196
|
|
|
return view('pages.profile-password')->with('data', $data); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param Request $request |
|
205
|
|
|
* @param $id |
|
206
|
|
|
* @return bool|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
207
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
208
|
|
|
*/ |
|
209
|
|
|
public function updatePass(Request $request, $id) |
|
210
|
|
|
{ |
|
211
|
|
|
$admin = $this->currentUser(); |
|
212
|
|
|
if($admin->id == $id) { |
|
213
|
|
|
|
|
214
|
|
|
$this->validate($request, [ |
|
215
|
|
|
'old_password' => 'required|min:6', |
|
216
|
|
|
'new_password' => 'required_with:password_confirmation|same:password_confirmation|min:6', |
|
217
|
|
|
'password_confirmation' => 'required|min:6' |
|
218
|
|
|
]); |
|
219
|
|
|
|
|
220
|
|
|
if($this->validatePass($request->input('old_password'))) { |
|
221
|
|
|
$admin->password = Hash::make($request->input('new_password')); |
|
222
|
|
|
$admin->save(); |
|
223
|
|
|
|
|
224
|
|
|
return redirect(route('admin.profile', $admin->id))->with('success', 'Password berhasil diubah !'); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return redirect(route('admin.password.edit', $admin->id))->with('failed', 'Password lama tidak cocok.'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return false; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function log($id) |
|
234
|
|
|
{ |
|
235
|
|
|
$admin = $this->currentUser(); |
|
236
|
|
|
if($admin->id == $id) { |
|
237
|
|
|
$data = [ |
|
238
|
|
|
'admin' => $admin, |
|
239
|
|
|
'logs' => Log::orderBy('created_at', 'desc')->paginate(10) |
|
240
|
|
|
]; |
|
241
|
|
|
return view('pages.log')->with('data', $data); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.'); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
250
|
|
|
*/ |
|
251
|
|
|
public function removeImage() |
|
252
|
|
|
{ |
|
253
|
|
|
$admin = $this->currentUser(); |
|
254
|
|
|
if($admin->profile_picture != "user-default.jpg") { |
|
255
|
|
|
Storage::delete('public/user_images/'.$admin->profile_picture); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$admin->profile_picture = "user-default.jpg"; |
|
259
|
|
|
if($admin->save()) { |
|
260
|
|
|
return redirect(route('admin.profile.edit', $admin->id))->with('success', 'Foto profil berhasil dihapus !'); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
267
|
|
|
*/ |
|
268
|
|
|
public function destroy() { |
|
269
|
|
|
$admin = $this->currentUser(); |
|
270
|
|
|
|
|
271
|
|
|
if($admin->delete()) { |
|
272
|
|
|
|
|
273
|
|
|
session()->flush(); |
|
274
|
|
|
return redirect(route('admin.login', $admin->id))->with('success', 'Akun berhasil dihapus !'); |
|
275
|
|
|
} |
|
276
|
|
|
return redirect(route('admin.profile', $admin->id))->with('failed', 'Gagal menghapus akun !'); |
|
277
|
|
|
|
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @return mixed |
|
283
|
|
|
*/ |
|
284
|
|
|
private function currentUser() |
|
285
|
|
|
{ |
|
286
|
|
|
return Auth::guard('admin')->user(); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param string $oldPassword |
|
292
|
|
|
* @return bool |
|
293
|
|
|
*/ |
|
294
|
|
|
private function validatePass(string $oldPassword) |
|
295
|
|
|
{ |
|
296
|
|
|
$admin = $this->currentUser(); |
|
297
|
|
|
if(Hash::check($oldPassword, $admin->password)) { |
|
298
|
|
|
return true; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
return false; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
|