1 | <?php |
||||
2 | |||||
3 | namespace App\Http\Controllers; |
||||
4 | |||||
5 | use App\Thread; |
||||
6 | use function GuzzleHttp\Promise\all; |
||||
7 | use Illuminate\Support\Facades\Hash; |
||||
8 | use Illuminate\Support\Facades\Storage; |
||||
9 | use Illuminate\Support\Facades\Auth; |
||||
10 | use Illuminate\Http\Request; |
||||
11 | use App\Articles; |
||||
12 | |||||
13 | |||||
14 | class UserController extends Controller |
||||
15 | { |
||||
16 | /** |
||||
17 | * Create a new controller instance. |
||||
18 | * |
||||
19 | * @return void |
||||
20 | */ |
||||
21 | public function __construct() |
||||
22 | { |
||||
23 | $this->middleware('auth', ['except' => [ |
||||
24 | 'index', |
||||
25 | 'showArticle' |
||||
26 | ]]); |
||||
27 | } |
||||
28 | |||||
29 | |||||
30 | /** |
||||
31 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||||
32 | */ |
||||
33 | public function index() |
||||
34 | { |
||||
35 | $threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
||||
36 | $articles = Articles::where('category','penyakit')->orderBy('created_at','desc')->take(3)->get(); |
||||
37 | $data = [ |
||||
38 | 'threads' => $threads, |
||||
39 | 'articles' => $articles |
||||
40 | ]; |
||||
41 | return view('home')->with('data', $data); |
||||
42 | } |
||||
43 | |||||
44 | |||||
45 | /** |
||||
46 | * Return user profile view with discussion data based on given query |
||||
47 | * |
||||
48 | * @param $query |
||||
49 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||||
50 | */ |
||||
51 | public function profile($query) |
||||
52 | { |
||||
53 | $threads = null; |
||||
54 | if($query == "all") { |
||||
55 | $threads = Thread::where('user_id', $this->currentUser()->id) |
||||
56 | ->orderBy('updated_at', 'desc') |
||||
57 | ->paginate(3); |
||||
58 | } elseif($query == "answered") { |
||||
59 | $threads = Thread::where('user_id', $this->currentUser()->id) |
||||
60 | ->where('status', true) |
||||
61 | ->orderBy('updated_at', 'desc') |
||||
62 | ->paginate(3); |
||||
63 | } |
||||
64 | |||||
65 | $data = [ |
||||
66 | 'user' => $this->currentUser(), |
||||
67 | 'threads' => $threads, |
||||
68 | 'status' => $query |
||||
69 | ]; |
||||
70 | return view('profile')->with('data', $data); |
||||
71 | } |
||||
72 | |||||
73 | |||||
74 | /** |
||||
75 | * @param $id |
||||
76 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View |
||||
77 | */ |
||||
78 | public function edit($id) |
||||
79 | { |
||||
80 | $user = $this->currentUser(); |
||||
81 | if($user->id == $id) { |
||||
82 | $data = [ |
||||
83 | 'user' => $user |
||||
84 | ]; |
||||
85 | return view ('profile-edit')->with('data', $data); |
||||
86 | } |
||||
87 | return redirect()->back()->with('warning', 'Anda tidak berhak untuk mengakses laman tersebut.'); |
||||
88 | } |
||||
89 | |||||
90 | |||||
91 | /** |
||||
92 | * @param $id |
||||
93 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||||
94 | */ |
||||
95 | public function editPass($id) |
||||
96 | { |
||||
97 | $user = $this->currentUser(); |
||||
98 | if($user->id == $id) { |
||||
99 | $data = [ |
||||
100 | 'user' => $user |
||||
101 | ]; |
||||
102 | return view ('profile-password')->with('data', $data); |
||||
103 | } |
||||
104 | return redirect()->back()->with('warning', 'Anda tidak berhak untuk mengakses laman tersebut.'); |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
105 | } |
||||
106 | |||||
107 | |||||
108 | /** |
||||
109 | * @param Request $request |
||||
110 | * @param $id |
||||
111 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||||
112 | * @throws \Illuminate\Validation\ValidationException |
||||
113 | */ |
||||
114 | public function update(Request $request, $id) |
||||
115 | { |
||||
116 | $this->validate($request,[ |
||||
117 | 'name' => 'required|min:3', |
||||
118 | 'email' => 'required|email', |
||||
119 | 'image' => 'image|nullable|max:3999' |
||||
120 | ]); |
||||
121 | |||||
122 | $img = null; |
||||
123 | $user = $this->currentUser(); |
||||
124 | |||||
125 | if($request->hasFile('image')) { |
||||
126 | |||||
127 | if( $user->profile_picture != "user-default.jpg" && |
||||
128 | $user->profile_picture != "user-default-male.png" && |
||||
129 | $user->profile_picture != "user-default-female.png") { |
||||
130 | |||||
131 | Storage::delete('public/user_images/'.$user->profile_picture); |
||||
132 | } |
||||
133 | |||||
134 | // Get Filename.ext |
||||
135 | $fileNameWExt = $request->file('image')->getClientOriginalName(); |
||||
136 | // Get Filename |
||||
137 | $fileName = pathinfo($fileNameWExt, PATHINFO_FILENAME); |
||||
138 | // Get ext |
||||
139 | $ext = $request->file('image')->getClientOriginalExtension(); |
||||
140 | // Filename To Store |
||||
141 | $img = $fileName.'_'.time().'.'.$ext; |
||||
142 | // Upload Image |
||||
143 | $path = $request->file('image')->storeAs('public/user_images', $img); |
||||
0 ignored issues
–
show
|
|||||
144 | |||||
145 | } else { |
||||
146 | if ($request->input('gender') == "Laki - laki") { |
||||
147 | $img = "user-default-male.png"; |
||||
148 | } else { |
||||
149 | $img = "user-default-female.png"; |
||||
150 | } |
||||
151 | } |
||||
152 | |||||
153 | |||||
154 | if($this->currentUser()->id == $id) { |
||||
155 | $user->name = $request->input('name'); |
||||
156 | $user->biography = $request->input('bio'); |
||||
157 | $user->gender = $request->input('gender'); |
||||
158 | $user->profile_picture = $img; |
||||
159 | $user->save(); |
||||
160 | |||||
161 | return redirect (route('user.profile.edit', ['id' => $id]))->with('success', 'Profil berhasil diperbarui !'); |
||||
162 | } |
||||
163 | |||||
164 | return redirect()->back()->with('warning', 'Anda tidak berhak untuk mengakses laman tersebut.'); |
||||
165 | } |
||||
166 | |||||
167 | /** |
||||
168 | * @param Request $request |
||||
169 | * @param $id |
||||
170 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||||
171 | * @throws \Illuminate\Validation\ValidationException |
||||
172 | */ |
||||
173 | public function updatePass(Request $request, $id) |
||||
0 ignored issues
–
show
The parameter
$id is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
174 | { |
||||
175 | $this->validate($request, [ |
||||
176 | 'old_password' => 'required|min:6', |
||||
177 | 'new_password' => 'required_with:new_password_confirmation|same:new_password_confirmation|min:6', |
||||
178 | 'new_password_confirmation' => 'required|min:6' |
||||
179 | ]); |
||||
180 | |||||
181 | $user = $this->currentUser(); |
||||
182 | |||||
183 | if($this->validatePass($request->input('old_password'))) { |
||||
184 | $user->password = Hash::make($request->input('new_password')); |
||||
185 | |||||
186 | if($user->save()) { |
||||
187 | session()->flush(); |
||||
188 | |||||
189 | return redirect(route('login'))->with('success', 'Password berhasil diubah ! Silahkan login kembali.'); |
||||
190 | } |
||||
191 | } |
||||
192 | |||||
193 | return redirect(route('user.password.edit', $user->id))->with('failed', 'Password lama tidak cocok.'); |
||||
194 | } |
||||
195 | |||||
196 | |||||
197 | /** |
||||
198 | * @param $id |
||||
199 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||||
200 | */ |
||||
201 | public function showArticle($id) |
||||
202 | { |
||||
203 | $article = Articles::find($id); |
||||
204 | return view('viewarticle')->with('article',$article); |
||||
205 | } |
||||
206 | |||||
207 | |||||
208 | /** |
||||
209 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||||
210 | */ |
||||
211 | public function removeImage() |
||||
212 | { |
||||
213 | $user = $this->currentUser(); |
||||
214 | $img = null; |
||||
215 | |||||
216 | if( $user->profile_picture != "user-default.jpg" && |
||||
217 | $user->profile_picture != "user-default-male.png" && |
||||
218 | $user->profile_picture != "user-default-female.png") { |
||||
219 | |||||
220 | Storage::delete('public/user_images/'.$user->profile_picture); |
||||
221 | } |
||||
222 | |||||
223 | if( $user->gender != null && $user->gender == "Laki - laki") { |
||||
224 | $img = "user-default-male.png"; |
||||
225 | } elseif($user->gender != null && $user->gender == "Perempuan") { |
||||
226 | $img = "user-default-female.png"; |
||||
227 | } else { |
||||
228 | $img = "user-default.jpg"; |
||||
229 | } |
||||
230 | |||||
231 | $user->profile_picture = $img; |
||||
232 | if($user->save()) { |
||||
233 | return redirect(route('user.profile.edit', $user->id))->with('success', 'Foto profil dihapus !'); |
||||
234 | } |
||||
235 | } |
||||
236 | |||||
237 | |||||
238 | /** |
||||
239 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||||
240 | */ |
||||
241 | public function destroy() |
||||
242 | { |
||||
243 | $user = $this->currentUser(); |
||||
244 | if( $user->profile_picture != "user-default.jpg" && |
||||
245 | $user->profile_picture != "user-default-male.png" && |
||||
246 | $user->profile_picture != "user-default-female.png") { |
||||
247 | |||||
248 | Storage::delete('public/user_images/'.$user->profile_picture); |
||||
249 | } |
||||
250 | |||||
251 | if($user->delete()) { |
||||
252 | session()->flush(); |
||||
253 | return redirect(route('home'))->with('success', 'Akun berhasil dihapus !'); |
||||
254 | } |
||||
255 | } |
||||
256 | |||||
257 | |||||
258 | /** |
||||
259 | * @return mixed |
||||
260 | */ |
||||
261 | private function currentUser() |
||||
262 | { |
||||
263 | return Auth::guard('web')->user(); |
||||
264 | } |
||||
265 | |||||
266 | |||||
267 | /** |
||||
268 | * @param string $oldPassword |
||||
269 | * @return bool |
||||
270 | */ |
||||
271 | private function validatePass(string $oldPassword) |
||||
272 | { |
||||
273 | $user = $this->currentUser(); |
||||
274 | if(Hash::check($oldPassword, $user->password)) { |
||||
275 | return true; |
||||
276 | } |
||||
277 | return false; |
||||
278 | } |
||||
279 | } |
||||
280 |