|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
use App\Articles; |
|
9
|
|
|
use App\Admin; |
|
10
|
|
|
use App\Doctor; |
|
11
|
|
|
|
|
12
|
|
|
class ArticleController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
16
|
|
|
*/ |
|
17
|
|
|
public function index() |
|
18
|
|
|
{ |
|
19
|
|
|
$articles = Articles::orderBy('category','asc')->paginate(5); |
|
20
|
|
|
$data = [ |
|
21
|
|
|
'articles' => $articles |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
return view('pages.article')->with('data', $data); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
29
|
|
|
*/ |
|
30
|
|
|
public function create() |
|
31
|
|
|
{ |
|
32
|
|
|
return view('pages.ext.add-article'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Request $request |
|
37
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
38
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function store(Request $request) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->validate($request,[ |
|
43
|
|
|
'category' => 'required', |
|
44
|
|
|
'title' => 'required', |
|
45
|
|
|
'content' => 'required|min:500', |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
|
|
$article = new Articles; |
|
49
|
|
|
$article->category = $request->input('category'); |
|
50
|
|
|
$article->title = $request->input('title'); |
|
51
|
|
|
$article->content = $request->input('content'); |
|
52
|
|
|
if(session('guard') == 'admin') { |
|
53
|
|
|
$article->admin_id = Auth::guard('admin')->user()->id; |
|
54
|
|
|
} else { |
|
55
|
|
|
$article->doctor_id = Auth::guard('doctor')->user()->id; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if($article->save()) { |
|
59
|
|
|
return redirect (route('article.index'))->with('success', 'Artikel baru berhasil ditambahkan !'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return redirect(route('article.create'))->with('failed', 'Gagal menambahkan artikel.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $id |
|
67
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
68
|
|
|
*/ |
|
69
|
|
|
public function show($id) |
|
70
|
|
|
{ |
|
71
|
|
|
$article = Articles::find($id); |
|
72
|
|
|
return view('pages.ext.view-article')->with('article', $article); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $cat |
|
77
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
78
|
|
|
*/ |
|
79
|
|
|
public function listByCat($cat) |
|
80
|
|
|
{ |
|
81
|
|
|
$category = null; |
|
82
|
|
|
|
|
83
|
|
|
switch ($cat) { |
|
84
|
|
|
case "penyakit": |
|
85
|
|
|
$category = "Penyakit"; |
|
86
|
|
|
break; |
|
87
|
|
|
case "obat": |
|
88
|
|
|
$category = "Obat - obatan"; |
|
89
|
|
|
break; |
|
90
|
|
|
case "hidup-sehat": |
|
91
|
|
|
$category = "Hidup Sehat"; |
|
92
|
|
|
break; |
|
93
|
|
|
case "keluarga": |
|
94
|
|
|
$category = "Keluarga"; |
|
95
|
|
|
break; |
|
96
|
|
|
case "kesehatan": |
|
97
|
|
|
$category = "Kesehatan"; |
|
98
|
|
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$data = [ |
|
102
|
|
|
'articles' => Articles::where('category', $cat)->orderBy('title','asc')->get(), |
|
103
|
|
|
'category' => $category, |
|
104
|
|
|
'cat' => $cat |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
return view('articles')->with('data', $data); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function listByName($cat, $name) |
|
111
|
|
|
{ |
|
112
|
|
|
$category = null; |
|
113
|
|
|
|
|
114
|
|
|
switch ($cat) { |
|
115
|
|
|
case "penyakit": |
|
116
|
|
|
$category = "Penyakit"; |
|
117
|
|
|
break; |
|
118
|
|
|
case "obat": |
|
119
|
|
|
$category = "Obat - obatan"; |
|
120
|
|
|
break; |
|
121
|
|
|
case "hidup-sehat": |
|
122
|
|
|
$category = "Hidup Sehat"; |
|
123
|
|
|
break; |
|
124
|
|
|
case "keluarga": |
|
125
|
|
|
$category = "Keluarga"; |
|
126
|
|
|
break; |
|
127
|
|
|
case "kesehatan": |
|
128
|
|
|
$category = "Kesehatan"; |
|
129
|
|
|
break; |
|
130
|
|
|
} |
|
131
|
|
|
$data = [ |
|
132
|
|
|
'articles' => Articles::where('category', $cat) |
|
133
|
|
|
->where('title','LIKE',$name.'%') |
|
134
|
|
|
->orderBy('title','asc') |
|
135
|
|
|
->get(), |
|
136
|
|
|
'category' => $category, |
|
137
|
|
|
'cat' => $cat |
|
138
|
|
|
]; |
|
139
|
|
|
|
|
140
|
|
|
return view('articles')->with('data', $data); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function search(Request $request, $cat) |
|
144
|
|
|
{ |
|
145
|
|
|
$cari = $request->cari; |
|
146
|
|
|
$articles = Articles::where('category', $cat) |
|
147
|
|
|
->where('content','LIKE','%'.$cari.'%') |
|
148
|
|
|
->orderBy('title','asc') |
|
149
|
|
|
->get(); |
|
150
|
|
|
$category = null; |
|
151
|
|
|
switch ($cat) { |
|
152
|
|
|
case "penyakit": |
|
153
|
|
|
$category = "Penyakit"; |
|
154
|
|
|
break; |
|
155
|
|
|
case "obat": |
|
156
|
|
|
$category = "Obat - obatan"; |
|
157
|
|
|
break; |
|
158
|
|
|
case "hidup-sehat": |
|
159
|
|
|
$category = "Hidup Sehat"; |
|
160
|
|
|
break; |
|
161
|
|
|
case "keluarga": |
|
162
|
|
|
$category = "Keluarga"; |
|
163
|
|
|
break; |
|
164
|
|
|
case "kesehatan": |
|
165
|
|
|
$category = "Kesehatan"; |
|
166
|
|
|
break; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$data = [ |
|
170
|
|
|
'articles' => $articles, |
|
171
|
|
|
'category' => $category, |
|
172
|
|
|
'cat' => $cat |
|
173
|
|
|
]; |
|
174
|
|
|
return view('articles')->with('data', $data); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param $id |
|
179
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
180
|
|
|
*/ |
|
181
|
|
|
public function edit($id) |
|
182
|
|
|
{ |
|
183
|
|
|
$article = Articles::find($id); |
|
184
|
|
|
$data = [ |
|
185
|
|
|
'article' => $article |
|
186
|
|
|
]; |
|
187
|
|
|
return view('pages.ext.edit-article')->with('data', $data); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param Request $request |
|
192
|
|
|
* @param $id |
|
193
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
194
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
195
|
|
|
*/ |
|
196
|
|
|
public function update(Request $request, $id) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->validate($request,[ |
|
199
|
|
|
'category' => 'required', |
|
200
|
|
|
'title' => 'required', |
|
201
|
|
|
'content' => 'required|min:500', |
|
202
|
|
|
]); |
|
203
|
|
|
|
|
204
|
|
|
$article = Articles::find($id); |
|
205
|
|
|
$article->category = $request->input('category'); |
|
206
|
|
|
$article->title = $request->input('title'); |
|
207
|
|
|
$article->content = $request->input('content'); |
|
208
|
|
|
|
|
209
|
|
|
if($article->save()) { |
|
210
|
|
|
return redirect (route('article.index'))->with('success', 'Artikel berhasil diubah !'); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return redirect (route('article.edit', $id))->with('failed', 'Gagal mengubah artikel !'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param $id |
|
218
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
219
|
|
|
*/ |
|
220
|
|
|
public function destroy($id) |
|
221
|
|
|
{ |
|
222
|
|
|
$article = Articles::find($id); |
|
223
|
|
|
|
|
224
|
|
|
if($article->delete()) { |
|
225
|
|
|
return redirect()->back()->with('success', 'Artikel dihapus !'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return redirect()->back()->with('failed', 'Gagal menghapus artikel.'); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|