|
@@ -0,0 +1,305 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace App\Http\Controllers\Admin;
|
|
4
|
+
|
|
5
|
+use App\Http\Controllers\Controller;
|
|
6
|
+use Illuminate\Support\Facades\DB;
|
|
7
|
+use Illuminate\Http\Request;
|
|
8
|
+use App\Helpers\Helper;
|
|
9
|
+use App\Model\Timeline;
|
|
10
|
+use Yajra\Datatables\Datatables;
|
|
11
|
+use Auth;
|
|
12
|
+use Illuminate\Support\Facades\Storage;
|
|
13
|
+use Validator;
|
|
14
|
+use Hashids;
|
|
15
|
+
|
|
16
|
+//load modelmu
|
|
17
|
+class TimelineController extends Controller
|
|
18
|
+{
|
|
19
|
+ /**
|
|
20
|
+ * Title untuk judul di web
|
|
21
|
+ * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
|
|
22
|
+ */
|
|
23
|
+ private $title = 'Timeline'; /**jangan lupa diganti*/
|
|
24
|
+ private $route = 'admin.timeline.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
|
|
25
|
+ private $header = 'Timeline';
|
|
26
|
+ private $sub_header = 'Timeline';
|
|
27
|
+ private $url = 'admin.timeline.';
|
|
28
|
+
|
|
29
|
+ public function __construct()
|
|
30
|
+ {
|
|
31
|
+ DB::enableQueryLog();
|
|
32
|
+ /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
|
|
33
|
+ // $this->middleware('permission:searching-list|searching-create|searching-update|searching-delete', ['only' => ['index', 'create', 'update']]);
|
|
34
|
+ // $this->middleware('permission:searching-create', ['only' => ['create', 'create_action']]);
|
|
35
|
+ // $this->middleware('permission:searching-update', ['only' => ['update', 'update_action']]);
|
|
36
|
+ // $this->middleware('permission:searching-delete', ['only' => ['delete']]);
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ /**
|
|
40
|
+ * Ini contoh crud yang sudah jalan
|
|
41
|
+ * index digunakna untuk tampilan awal dari menu yang akan dibuat
|
|
42
|
+ * Untuk CRUD Biar cepat
|
|
43
|
+ * Silahkan ganti
|
|
44
|
+ * EloquentMu:: => dengan model anda
|
|
45
|
+ * $id_modelmu => ganti dengan id di model anda
|
|
46
|
+ * id_di_routenya => jangan lupa disesuaikan dengan yang dideclare di route
|
|
47
|
+ */
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+ public function index()
|
|
51
|
+ {
|
|
52
|
+ Helper::swal();
|
|
53
|
+
|
|
54
|
+ $data = [
|
|
55
|
+ //bawaan
|
|
56
|
+ 'title' => $this->title,
|
|
57
|
+ 'route' => $this->route,
|
|
58
|
+ 'header' => $this->header,
|
|
59
|
+ 'sub_header' => $this->sub_header,
|
|
60
|
+ ];
|
|
61
|
+ // dd($data);
|
|
62
|
+ return view($this->url . 'index', $data);
|
|
63
|
+ }
|
|
64
|
+ public function getData($udd_id = null)
|
|
65
|
+ {
|
|
66
|
+ $query = Timeline::query();
|
|
67
|
+
|
|
68
|
+ return Datatables::of($query)
|
|
69
|
+ ->addIndexColumn()
|
|
70
|
+ ->addColumn('user', function($query){
|
|
71
|
+ return $query->user->name;
|
|
72
|
+ })
|
|
73
|
+ ->addColumn('aksi', function ($query) {
|
|
74
|
+
|
|
75
|
+ /**cek role */
|
|
76
|
+ $aksi = '';
|
|
77
|
+ $aksi .= "<a href='" . route($this->route . 'approve', ['timeline_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-success btn-edit'>Approve</a>";
|
|
78
|
+ $aksi .= "<a href='" . route($this->route . 'reject', ['timeline_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-warning btn-edit'>Reject</a>";
|
|
79
|
+
|
|
80
|
+ $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['timeline_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
|
|
81
|
+
|
|
82
|
+ return $aksi;
|
|
83
|
+ })
|
|
84
|
+ ->rawColumns(['aksi'])
|
|
85
|
+ ->toJson();
|
|
86
|
+ }
|
|
87
|
+ public function create()
|
|
88
|
+ {
|
|
89
|
+ $data = [
|
|
90
|
+ //bawaan
|
|
91
|
+ 'title' => $this->title,
|
|
92
|
+ 'route' => $this->route,
|
|
93
|
+ 'header' => $this->header,
|
|
94
|
+ 'sub_header' => $this->sub_header,
|
|
95
|
+
|
|
96
|
+ ];
|
|
97
|
+
|
|
98
|
+ Helper::swal();
|
|
99
|
+
|
|
100
|
+ return view($this->url . 'create', $data);
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ public function create_action(Request $request)
|
|
104
|
+ {
|
|
105
|
+ //declare post untuk simpan semua request yang diinputkan oleh form name=""
|
|
106
|
+ $post = $request->all();
|
|
107
|
+ $file = $request->file('gambar');
|
|
108
|
+ //dd($post);
|
|
109
|
+ /**digunakan untuk set rule validator */
|
|
110
|
+ $rules = [
|
|
111
|
+ 'title' => 'required',
|
|
112
|
+ 'gambar' => 'required|file|max:1000',
|
|
113
|
+ 'content' => 'required',
|
|
114
|
+ 'sub_title' => 'required',
|
|
115
|
+ 'date_publish' => 'required',
|
|
116
|
+ ];
|
|
117
|
+ /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
|
|
118
|
+ $alert = [
|
|
119
|
+ 'file' => 'File maksimal 1MB'
|
|
120
|
+ ];
|
|
121
|
+ $validator = Validator::make($post, $rules, $alert);
|
|
122
|
+
|
|
123
|
+ if ($validator->passes()) {
|
|
124
|
+ /**menggunakan transaction */
|
|
125
|
+ DB::beginTransaction();
|
|
126
|
+
|
|
127
|
+ $nama_file = time()."_".$file->getClientOriginalName();
|
|
128
|
+
|
|
129
|
+ $tujuan_upload = 'gambar';
|
|
130
|
+
|
|
131
|
+ //$file->move($tujuan_upload,$nama_file);
|
|
132
|
+ Storage::putFileAs('public/images/news/', $file, $nama_file );
|
|
133
|
+
|
|
134
|
+ $query = new News();
|
|
135
|
+ $query->title = $request->title;
|
|
136
|
+ $query->content = Helper::input_summernote($request->content);
|
|
137
|
+ $query->sub_title = Helper::input_summernote($request->sub_title);
|
|
138
|
+ $query->date_publish = $request->date_publish;
|
|
139
|
+ $query->user_id = Auth::user()->id;
|
|
140
|
+ $query->thumbnail = $nama_file;
|
|
141
|
+ $query->save();
|
|
142
|
+
|
|
143
|
+ if ($query) {
|
|
144
|
+ DB::commit();
|
|
145
|
+ $message = 'Berhasil';
|
|
146
|
+ return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
|
|
147
|
+ } else {
|
|
148
|
+ DB::rollback();
|
|
149
|
+ $message = 'Gagal';
|
|
150
|
+ return redirect()->back()->with('error', Helper::parsing_alert($message));
|
|
151
|
+ }
|
|
152
|
+ }
|
|
153
|
+ /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
|
|
154
|
+ $message = Helper::parsing_alert($validator->errors()->all());
|
|
155
|
+ return redirect()->back()->with('error', $message)->withInput();
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ public function approve($data_id)
|
|
159
|
+ {
|
|
160
|
+
|
|
161
|
+ $data_id = Hashids::decode($data_id);
|
|
162
|
+ if (!empty($data_id)) {
|
|
163
|
+
|
|
164
|
+ $timeline = Timeline::where('id', $data_id[0])->first();
|
|
165
|
+
|
|
166
|
+ $timeline->is_approved = 'approved';
|
|
167
|
+ $timeline->save();
|
|
168
|
+
|
|
169
|
+ return redirect()->back();
|
|
170
|
+ }
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ public function reject($data_id)
|
|
174
|
+ {
|
|
175
|
+
|
|
176
|
+ $data_id = Hashids::decode($data_id);
|
|
177
|
+ if (!empty($data_id)) {
|
|
178
|
+
|
|
179
|
+ $timeline = Timeline::where('id', $data_id[0])->first();
|
|
180
|
+
|
|
181
|
+ $timeline->is_approved = 'rejected';
|
|
182
|
+ $timeline->save();
|
|
183
|
+
|
|
184
|
+ return redirect()->back();
|
|
185
|
+ }
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ public function update_action(Request $request)
|
|
189
|
+ {
|
|
190
|
+ $post = $request->all();
|
|
191
|
+ /**digunakan untuk set rule validator */
|
|
192
|
+ $rules = [
|
|
193
|
+ 'title' => 'required',
|
|
194
|
+ 'content' => 'required',
|
|
195
|
+ 'sub_title' => 'required',
|
|
196
|
+ 'date_publish' => 'required',
|
|
197
|
+ ];
|
|
198
|
+ /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
|
|
199
|
+ $alert = [];
|
|
200
|
+ $validator = Validator::make($post, $rules, $alert);
|
|
201
|
+
|
|
202
|
+ $data_id = Hashids::decode($request['id']);
|
|
203
|
+
|
|
204
|
+ if (!empty($data_id)) {
|
|
205
|
+ /**cek apakah data_idnya ada ? */
|
|
206
|
+ $query = News::find($data_id[0]);
|
|
207
|
+
|
|
208
|
+ if ($query) {
|
|
209
|
+ /**cek apakah id data benar" ada di DB ? */
|
|
210
|
+ if ($validator->passes()) {
|
|
211
|
+ /**validatornya */
|
|
212
|
+ DB::beginTransaction();
|
|
213
|
+ $file = $request->file('gambar');
|
|
214
|
+ if($file){
|
|
215
|
+ $nama_file = time()."_".$file->getClientOriginalName();
|
|
216
|
+ $tujuan_upload = 'gambar';
|
|
217
|
+ //$file->move($tujuan_upload,$nama_file);
|
|
218
|
+ Storage::putFileAs('public/images/news/', $file, $nama_file );
|
|
219
|
+ //File::delete('gambar/'.$query->gambar);
|
|
220
|
+ Storage::delete('public/images/news/'.$query->thumbnail);
|
|
221
|
+ $query->thumbnail = $nama_file;
|
|
222
|
+ }
|
|
223
|
+
|
|
224
|
+ $query->title = $request->title;
|
|
225
|
+ $query->content = Helper::input_summernote($request->content);
|
|
226
|
+ $query->sub_title = Helper::input_summernote($request->sub_title);
|
|
227
|
+ $query->date_publish = $request->date_publish;
|
|
228
|
+ $query->save();
|
|
229
|
+
|
|
230
|
+ if ($query) {
|
|
231
|
+ DB::commit();
|
|
232
|
+ $message = 'Berhasil';
|
|
233
|
+ return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
|
|
234
|
+ } else {
|
|
235
|
+ DB::rollback();
|
|
236
|
+ $message = 'Gagal';
|
|
237
|
+ return redirect()->back()->with('error', Helper::parsing_alert($message));
|
|
238
|
+ }
|
|
239
|
+ }
|
|
240
|
+
|
|
241
|
+ $message = Helper::parsing_alert($validator->errors()->all());
|
|
242
|
+
|
|
243
|
+ return redirect()->back()->with('error', $message);
|
|
244
|
+ } else {
|
|
245
|
+ $message = 'Id tdak ditemukan';
|
|
246
|
+ return redirect()->back()->with('error', $message);
|
|
247
|
+ }
|
|
248
|
+ } else {
|
|
249
|
+ $message = 'Id tidak boleh kosong';
|
|
250
|
+ return redirect()->back()->with('error', $message);
|
|
251
|
+ }
|
|
252
|
+ }
|
|
253
|
+
|
|
254
|
+ public function delete_action($id_modelmu)
|
|
255
|
+ {
|
|
256
|
+ $id = Hashids::decode($id_modelmu);
|
|
257
|
+ /**cek idnya apakah ada ? */
|
|
258
|
+
|
|
259
|
+ if (!empty($id)) {
|
|
260
|
+
|
|
261
|
+ $cek_data = News::find($id[0]);
|
|
262
|
+
|
|
263
|
+ if ($cek_data) {
|
|
264
|
+ DB::beginTransaction();
|
|
265
|
+ try {
|
|
266
|
+ //File::delete('gambar/'.$cek_data->gambar);
|
|
267
|
+ $arr_likes = $cek_data->newsLikes;
|
|
268
|
+ foreach($arr_likes as $like){
|
|
269
|
+ $like->delete();
|
|
270
|
+ }
|
|
271
|
+ Storage::delete('public/images/news/'.$cek_data->thumbnail);
|
|
272
|
+ $delete = $cek_data->delete();
|
|
273
|
+ DB::commit();
|
|
274
|
+ $message = 'Sukses';
|
|
275
|
+ $response = [
|
|
276
|
+ 'message' => $message,
|
|
277
|
+ 'status' => true,
|
|
278
|
+ ];
|
|
279
|
+ return response()->json($response);
|
|
280
|
+ } catch (Exception $e) {
|
|
281
|
+ DB::rollback();
|
|
282
|
+ $message = 'Gagal';
|
|
283
|
+ $response = [
|
|
284
|
+ 'message' => $message,
|
|
285
|
+ 'status' => false,
|
|
286
|
+ ];
|
|
287
|
+ return response()->json($response);
|
|
288
|
+ }
|
|
289
|
+ }
|
|
290
|
+ $message = 'Id tidak ditemukan atau sudah dihapus';
|
|
291
|
+ $response = [
|
|
292
|
+ 'message' => $message,
|
|
293
|
+ 'status' => false,
|
|
294
|
+ ];
|
|
295
|
+ return response()->json($response);
|
|
296
|
+ }
|
|
297
|
+ $message = 'Id tdak ditemukan';
|
|
298
|
+ $response = [
|
|
299
|
+ 'message' => $message,
|
|
300
|
+ 'status' => false,
|
|
301
|
+ ];
|
|
302
|
+ return response()->json($response);
|
|
303
|
+ }
|
|
304
|
+
|
|
305
|
+}
|