1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Thread; |
6
|
|
|
use App\ThreadTopic; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
|
9
|
|
|
class ThreadController extends Controller |
10
|
|
|
{ |
11
|
|
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
$this->middleware('auth:admin', ['except' => [ |
14
|
|
|
'index', 'show' |
15
|
|
|
]]); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Display a listing of the resource. |
20
|
|
|
* |
21
|
|
|
* @param $query |
22
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
23
|
|
|
*/ |
24
|
|
|
public function index($query) |
25
|
|
|
{ |
26
|
|
|
$threads = null; |
27
|
|
|
$count = null; |
|
|
|
|
28
|
|
|
if($query == "all") { |
29
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(15); |
30
|
|
|
} elseif($query == "answered") { |
31
|
|
|
$threads = Thread::where('status', true) |
32
|
|
|
->orderBy('created_at', 'desc') |
33
|
|
|
->paginate(15); |
34
|
|
|
} else { |
35
|
|
|
$threads = Thread::where('status', false) |
36
|
|
|
->orderBy('created_at', 'desc') |
37
|
|
|
->paginate(15); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$data = [ |
41
|
|
|
'threads' => $threads, |
42
|
|
|
'query' => $query, |
43
|
|
|
'count' => [ |
44
|
|
|
'all' => count(Thread::all()), |
45
|
|
|
'answered' => count(Thread::where('status', true)->get()), |
46
|
|
|
'unanswered' => count(Thread::where('status', false)->get()) |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
return view('pages.thread')->with('data', $data); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Display the specified resource. |
56
|
|
|
* |
57
|
|
|
* @param int $id |
58
|
|
|
* @return \Illuminate\Http\Response |
59
|
|
|
*/ |
60
|
|
|
public function show($id) |
61
|
|
|
{ |
62
|
|
|
$thread = Thread::find($id); |
63
|
|
|
if(!$thread) { |
64
|
|
|
abort(404); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$doctor = Auth::guard('doctor')->user(); |
68
|
|
|
$data = [ |
69
|
|
|
'thread' => $thread, |
70
|
|
|
'doctor' => $doctor |
71
|
|
|
]; |
72
|
|
|
return view('pages.ext.view-thread')->with('data', $data); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Remove the specified resource from storage. |
78
|
|
|
* |
79
|
|
|
* @param int $id |
80
|
|
|
* @return \Illuminate\Http\Response |
81
|
|
|
*/ |
82
|
|
|
public function destroy($id) |
83
|
|
|
{ |
84
|
|
|
$thread = Thread::find($id); |
85
|
|
|
if($thread->delete() && $this->deleteTopic($thread->id_topic)) { |
86
|
|
|
return redirect(route('admin.thread.index', ['query' => "all"]))->with('success', 'Diskusi dihapus !'); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
return redirect()->back()->with('failed', 'Gagal menghapus diskusi.'); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Delete topic with given id |
94
|
|
|
* |
95
|
|
|
* @param $id |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function deleteTopic($id) |
99
|
|
|
{ |
100
|
|
|
$topic = ThreadTopic::find($id); |
101
|
|
|
if($topic->delete()) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|