|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use App\Exports\AdatExport; |
| 6 | +use App\Http\Controllers\Controller; |
| 7 | +use App\Http\Requests\AdatRequest; |
| 8 | +use App\Imports\SukuImport; |
| 9 | +use App\Models\WilayahAdat; |
| 10 | +use App\Models\Region; |
| 11 | +use Illuminate\Http\Request; |
| 12 | +use Illuminate\Support\Facades\Storage; |
| 13 | +use Maatwebsite\Excel\Facades\Excel; |
| 14 | +use Yajra\DataTables\DataTables; |
| 15 | + |
| 16 | +class AdatController extends Controller |
| 17 | +{ |
| 18 | + public function index(Request $request) |
| 19 | + { |
| 20 | + if ($request->excel) { |
| 21 | + $paramDatatable = json_decode($request->get('params'), 1); |
| 22 | + $request->merge($paramDatatable); |
| 23 | + } |
| 24 | + |
| 25 | + if ($request->ajax() || $request->excel) { |
| 26 | + $query = DataTables::of(WilayahAdat::with('region')); |
| 27 | + if ($request->excel) { |
| 28 | + $query->filtering(); |
| 29 | + |
| 30 | + return Excel::download(new AdatExport($query->results()), 'Wilayah-WilayahAdat.xlsx'); |
| 31 | + } |
| 32 | + |
| 33 | + return $query->addIndexColumn() |
| 34 | + ->addColumn('action', function ($data) { |
| 35 | + $edit = '<a href="' . route('adat.edit', $data->id) . '" class="btn btn-sm btn-warning btn-sm"><i class="fas fa-pencil-alt"></i></a>'; |
| 36 | + $delete = '<button data-href="' . route('adat.destroy', $data->id) . '" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete"><i class="fas fa-trash"></i></button>'; |
| 37 | + |
| 38 | + return '<div class="btn btn-group">' . $edit . $delete . '</div>'; |
| 39 | + }) |
| 40 | + ->rawColumns(['action']) |
| 41 | + ->make(true); |
| 42 | + } |
| 43 | + |
| 44 | + return view('admin.adat.index'); |
| 45 | + } |
| 46 | + |
| 47 | + public function create() |
| 48 | + { |
| 49 | + return view('admin.adat.create'); |
| 50 | + } |
| 51 | + |
| 52 | + public function store(AdatRequest $request) |
| 53 | + { |
| 54 | + $input = $request->all(); |
| 55 | + $input['tbl_region_id'] = Region::where('region_code', $input['tbl_region_id'])->where('parent_code', 0)->first()->id; |
| 56 | + if (WilayahAdat::create($input)) { |
| 57 | + return redirect('adat')->with('success', 'Data berhasil disimpan'); |
| 58 | + } |
| 59 | + |
| 60 | + return back()->with('error', 'Data gagal disimpan'); |
| 61 | + } |
| 62 | + |
| 63 | + public function edit($id) |
| 64 | + { |
| 65 | + return view('admin.adat.edit', [ |
| 66 | + 'adat' => WilayahAdat::with('region')->findOrFail($id), |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + public function update(AdatRequest $request, $id) |
| 71 | + { |
| 72 | + $input = $request->all(); |
| 73 | + $input['tbl_region_id'] = Region::where('region_code', $input['tbl_region_id'])->where('parent_code', 0)->first()->id; |
| 74 | + $adat = WilayahAdat::findOrFail($id); |
| 75 | + |
| 76 | + if ($adat->update($input)) { |
| 77 | + return redirect('adat')->with('success', 'Data berhasil diubah'); |
| 78 | + } |
| 79 | + |
| 80 | + return back()->with('error', 'Data gagal diubah'); |
| 81 | + } |
| 82 | + |
| 83 | + public function import() |
| 84 | + { |
| 85 | + return view('admin.adat.import'); |
| 86 | + } |
| 87 | + |
| 88 | + public function prosesImport(Request $request) |
| 89 | + { |
| 90 | + try { |
| 91 | + Excel::import(new SukuImport, $request->file('file')->store('temp')); |
| 92 | + } catch (\Exception $e) { |
| 93 | + report($e); |
| 94 | + |
| 95 | + return back()->with('error', 'Data gagal diimport'); |
| 96 | + } |
| 97 | + |
| 98 | + // Hapus folder temp ketika sudah selesai |
| 99 | + Storage::deleteDirectory('temp'); |
| 100 | + |
| 101 | + return redirect('adat')->with('success', 'Data berhasil diimport'); |
| 102 | + } |
| 103 | + |
| 104 | + public function contohImport() |
| 105 | + { |
| 106 | + $file = public_path('/assets/import/data_adat.xlsx'); |
| 107 | + |
| 108 | + return response()->download($file); |
| 109 | + } |
| 110 | + |
| 111 | + public function destroy($id) |
| 112 | + { |
| 113 | + if (WilayahAdat::destroy($id)) { |
| 114 | + return redirect('adat')->with('success', 'Data berhasil dihapus'); |
| 115 | + } |
| 116 | + |
| 117 | + return redirect('adat')->with('error', 'Data gagal dihapus'); |
| 118 | + } |
| 119 | +} |
0 commit comments