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