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