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