1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Common; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use App\Thread; |
9
|
|
|
use App\ThreadTopic; |
10
|
|
|
|
11
|
|
|
class ThreadAskController extends Controller |
12
|
|
|
{ |
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->middleware('auth', ['except' => [ |
16
|
|
|
'index', 'show' |
17
|
|
|
]]); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Display a listing of the resource. |
23
|
|
|
* |
24
|
|
|
* @return \Illuminate\Http\Response |
25
|
|
|
*/ |
26
|
|
|
public function index() |
27
|
|
|
{ |
28
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
29
|
|
|
$data = [ |
30
|
|
|
'threads' => $threads |
31
|
|
|
]; |
32
|
|
|
return view('ask-index')->with('data', $data); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Show the form for creating a new resource. |
37
|
|
|
* |
38
|
|
|
* @return \Illuminate\Http\Response |
39
|
|
|
*/ |
40
|
|
|
public function create() |
41
|
|
|
{ |
42
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
43
|
|
|
$data = [ |
44
|
|
|
'threads' => $threads |
45
|
|
|
]; |
46
|
|
|
return view('ask-form')->with('data', $data); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Store a newly created resource in storage. |
51
|
|
|
* |
52
|
|
|
* @param Request $request |
53
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
54
|
|
|
* @throws \Illuminate\Validation\ValidationException |
55
|
|
|
*/ |
56
|
|
|
public function store(Request $request) |
57
|
|
|
{ |
58
|
|
|
$this->validate($request, [ |
59
|
|
|
'topic' => 'required|min:10', |
60
|
|
|
'question' => 'required|min:100' |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$topic = $this->addTopic($request->input('topic')); |
64
|
|
|
|
65
|
|
|
if($topic != null) { |
66
|
|
|
$thread = new Thread; |
67
|
|
|
$thread->user_id = $this->currentUser()->id; |
68
|
|
|
$thread->id_topic = $topic->id; |
69
|
|
|
$thread->question = $request->input('question'); |
70
|
|
|
|
71
|
|
|
$log = null; |
72
|
|
|
if($thread->save()) { |
73
|
|
|
$log = Common::registerLog([ |
74
|
|
|
'action' => "membuat diskusi baru.", |
75
|
|
|
'target' => 'thread', |
76
|
|
|
'prefix' => 't-create', |
77
|
|
|
'target_id' => $thread->id, |
78
|
|
|
'actor' => 'user', |
79
|
|
|
'actor_id' => Common::currentUser('web')->id |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if($log != null && $log == true) { |
|
|
|
|
84
|
|
|
return redirect(route('user.thread.index'))->with('success', 'Pertanyaan dikirim !'); |
85
|
|
|
} |
86
|
|
|
return redirect()->back()->with('failed', 'Gagal mengirim pertanyaan, silahkan coba lagi nanti.'); |
87
|
|
|
} |
88
|
|
|
return redirect()->back()->with('failed', 'Gagal mengirim pertanyaan, silahkan coba lagi nanti.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Display the specified resource. |
93
|
|
|
* |
94
|
|
|
* @param int $id |
95
|
|
|
* @return \Illuminate\Http\Response |
96
|
|
|
*/ |
97
|
|
|
public function show($id) |
98
|
|
|
{ |
99
|
|
|
$thread = Thread::find($id); |
100
|
|
|
if(!$thread) { |
101
|
|
|
abort(404); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
105
|
|
|
$data = [ |
106
|
|
|
'thread' => $thread, |
107
|
|
|
'threads' => $threads |
108
|
|
|
]; |
109
|
|
|
return view('ask-view')->with('data', $data); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Show the form for editing the specified resource. |
114
|
|
|
* |
115
|
|
|
* @param int $id |
116
|
|
|
* @return \Illuminate\Http\Response |
117
|
|
|
*/ |
118
|
|
|
public function edit($id) |
119
|
|
|
{ |
120
|
|
|
$user = $this->currentUser(); |
121
|
|
|
$thread = Thread::find($id); |
122
|
|
|
if($thread->user_id != $user->id) { |
123
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.'); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if($thread->doctor_id != null) { |
127
|
|
|
return redirect()->back()->with('warning', 'Tidak dapat mengubah pertanyaan karena sudah terjawab, silahkan tanyakan pertanyaan baru.'); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
130
|
|
|
$data = [ |
131
|
|
|
'thread' => $thread, |
132
|
|
|
'threads' => $threads |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
return view('ask-edit')->with('data', $data); |
|
|
|
|
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Update the specified resource in storage. |
141
|
|
|
* |
142
|
|
|
* @param Request $request |
143
|
|
|
* @param $id |
144
|
|
|
* @return \Illuminate\Http\RedirectResponse |
145
|
|
|
* @throws \Illuminate\Validation\ValidationException |
146
|
|
|
*/ |
147
|
|
|
public function update(Request $request, $id) |
148
|
|
|
{ |
149
|
|
|
$user = $this->currentUser(); |
150
|
|
|
$thread = Thread::find($id); |
151
|
|
|
if($thread->user_id != $user->id) { |
152
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak mengubah ulasan tersebut.'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->validate($request, [ |
156
|
|
|
'topic' => 'required|min:10', |
157
|
|
|
'question' => 'required|min:100' |
158
|
|
|
]); |
159
|
|
|
|
160
|
|
|
$thread->topic = $request->input('topic'); |
161
|
|
|
$thread->question = $request->input('question'); |
162
|
|
|
if($thread->save()) { |
163
|
|
|
return redirect(route('user.thread.show', $thread->id))->with('success', 'Berhasil mengubah ulasan !'); |
164
|
|
|
} |
165
|
|
|
return redirect(route('user.thread.show', $thread->id))->with('success', 'Berhasil mengubah ulasan !'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Remove the specified resource from storage. |
170
|
|
|
* |
171
|
|
|
* @param int $id |
172
|
|
|
* @return \Illuminate\Http\Response |
173
|
|
|
*/ |
174
|
|
|
public function destroy($id) |
175
|
|
|
{ |
176
|
|
|
$user = $this->currentUser(); |
177
|
|
|
$thread = Thread::find($id); |
178
|
|
|
if($thread->user_id != $user->id) { |
179
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak menghapus ulasan tersebut.'); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$unreg = null; |
183
|
|
|
if($thread->delete() && $this->deleteTopic($thread->id_topic)) { |
184
|
|
|
$unreg = Common::unregisterLog([ |
185
|
|
|
'target' => 'thread', |
186
|
|
|
'target_id' => $id |
187
|
|
|
]); |
188
|
|
|
} |
189
|
|
|
if($unreg != null && $unreg) { |
190
|
|
|
return redirect(route('user.profile'))->with('success', 'Ulasan dihapus !'); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
return redirect()->back()->with('failed', 'Gagal menghapus ulasan.'); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get currently logged in user |
197
|
|
|
* |
198
|
|
|
* @return mixed |
199
|
|
|
*/ |
200
|
|
|
private function currentUser() |
201
|
|
|
{ |
202
|
|
|
return Auth::guard('web')->user(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $topic |
207
|
|
|
* @return ThreadTopic|null |
208
|
|
|
*/ |
209
|
|
|
private function addTopic(string $topic) |
210
|
|
|
{ |
211
|
|
|
$new = new ThreadTopic; |
212
|
|
|
$new->topic_name = $topic; |
213
|
|
|
if($new->save()) { |
214
|
|
|
return $new; |
215
|
|
|
} |
216
|
|
|
return null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Delete topic with given id |
221
|
|
|
* |
222
|
|
|
* @param int $id |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
private function deleteTopic($id) |
226
|
|
|
{ |
227
|
|
|
$topic = ThreadTopic::find($id); |
228
|
|
|
if($topic->delete()) { |
229
|
|
|
return true; |
230
|
|
|
} |
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|