220 lines
7.8 KiB
PHP
220 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Pengaturan;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\File;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\SumberDataEF;
|
|
use App\Models\FileDataAktivitas;
|
|
use App\Models\Master\Sektor;
|
|
|
|
class UploadAktifitasController extends Controller
|
|
{
|
|
protected $title = 'Data Aktifitas';
|
|
protected $template = 'modules.pengaturan.upload-aktifitas';
|
|
protected $route = 'modules.pengaturan.upload-aktifitas';
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
permission('is_read', $this->route, 'module',true);
|
|
|
|
$data['breadcrumbs'] = [
|
|
['name' => 'Dashboard'],
|
|
['name' => 'Pengaturan'],
|
|
['name' => 'Data Aktifitas','active' => true],
|
|
];
|
|
$data['title'] = $this->title;
|
|
$data['route'] = $this->route;
|
|
$data['sektor'] = Sektor::all();
|
|
|
|
return view($this->template.'.index',$data);
|
|
}
|
|
|
|
public function grid(Request $request)
|
|
{
|
|
|
|
$data = FileDataAktivitas::with(['sektor'])->get();
|
|
$_data = [];
|
|
|
|
|
|
foreach ($data as $key => $row) {
|
|
|
|
|
|
$action = '';
|
|
$status = '';
|
|
if($row->status == 0){
|
|
$status = '<span class="btn btn-sm btn-block btn-danger"> Tidak Aktif </span>';
|
|
}else{
|
|
$status = '<span class="btn btn-sm btn-block btn-success"> Aktif </span>';
|
|
}
|
|
$file = '<a download href="'.asset($row->file).'" class="btn btn-sm w-100 btn-success"><i class="mdi mdi-download text-white"></i></a>';
|
|
$action .= '<div class="d-flex gap-1">';
|
|
if((permission('is_create', $this->route.'.*','module',false)) || (permission('is_update', $this->route.'.*','module',false))){
|
|
$action .= '<a data-id="'.encode_id($row->FileDataAktivitasId).'" href="'.url('pengaturan/upload-aktifitas/update/'.encode_id($row->FileDataAktivitasId)).'" data-toggle="tooltip" title="Edit Data" class="btn btn-sm btn-block btn-primary"><i class="mdi mdi-pencil text-white"></i></a>';
|
|
if(session('group_id') == 1){
|
|
$action .= '<a href="#" data-href="'.url('pengaturan/upload-aktifitas/delete/'.encode_id($row->FileDataAktivitasId)).'" data-toggle="tooltip" title="Edit Data" class="remove_data btn btn-sm btn-block btn-danger"><i class="mdi mdi-delete text-white"></i></a>';
|
|
}
|
|
}
|
|
$action .= '</div>';
|
|
|
|
$_data[] = [
|
|
'no' => $key+1,
|
|
'id' => encode_id($row->FileDataAktivitasId),
|
|
'sektor' => @$row->sektor->nama,
|
|
'tahun' => @$row->tahun,
|
|
'nama' => @$row->nama,
|
|
'file' => @$file,
|
|
'created_at' => date('d-m-Y H:i:s',strtotime(@$row->created_at)),
|
|
'status' => @$status,
|
|
'action' => @$action,
|
|
];
|
|
|
|
}
|
|
|
|
// return response()->json($_data); // Return the data as a JSON response
|
|
return response()->json($_data);
|
|
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// dd(request()->all());
|
|
$request->validate([
|
|
'sektor' => 'required',
|
|
'tahun' => 'required',
|
|
'nama' => 'required',
|
|
'file' => 'required|file|mimes:xls,xlsx,pdf,png,jpg,jpeg,docx|max:2000',
|
|
]);
|
|
|
|
try {
|
|
|
|
if (@$request->file) {
|
|
$file = $request->file;
|
|
$destinationPath = public_path('uploads/data_aktifitas');
|
|
$current = Carbon::now()->format('Y/m/d');
|
|
$path = $destinationPath . '/' . $current;
|
|
$fileName = $file->getClientOriginalName();
|
|
$fileMime = $file->getClientMimeType();
|
|
$fileExtension = $file->getClientOriginalExtension();
|
|
$fileSize = $file->getSize();
|
|
if(($fileExtension != 'xls') && ($fileExtension != 'xlsx') && ($fileExtension != 'pdf') && ($fileExtension != 'docx') && ($fileExtension != 'png') && ($fileExtension != 'jpg') && ($fileExtension != 'jpeg') ){
|
|
return redirect()->back()->with([
|
|
'message' => 'Maaf File Harus Berupa xls,xlsx,pdf,png,jpg,jpeg,docx!',
|
|
'type' => "error"
|
|
]);
|
|
}
|
|
$newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension;
|
|
|
|
if (!File::exists($path)) {
|
|
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
|
|
}
|
|
|
|
$filePath = 'data_aktifitas/' . $current . '/' . $newFilename;
|
|
$uploaded = $file->move($path, $newFilename);
|
|
}
|
|
|
|
|
|
if(@request()->secure_id){
|
|
$keyId = decode_id(@request()->secure_id);
|
|
$data = FileDataAktivitas::find($keyId);
|
|
$data->ms_sektor_id = decode_id($request->sektor);
|
|
$data->ms_subsektor_id = @$request->subsektor ? decode_id($request->subsektor) : NULL;
|
|
$data->tahun = $request->tahun;
|
|
$data->nama = $request->nama;
|
|
$data->file = $filePath;
|
|
$data->deskripsi = $request->deskripsi;
|
|
$data->save();
|
|
}else{
|
|
$data = new FileDataAktivitas;
|
|
$data->ms_sektor_id = decode_id($request->sektor);
|
|
$data->ms_subsektor_id = @$request->subsektor ? decode_id($request->subsektor) : NULL;
|
|
$data->tahun = $request->tahun;
|
|
$data->nama = $request->nama;
|
|
$data->file = $filePath;
|
|
$data->deskripsi = $request->deskripsi;
|
|
$data->save();
|
|
}
|
|
|
|
|
|
return redirect()->back()->with([
|
|
'message' => 'Berhasil update data',
|
|
'type' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->with([
|
|
'message' => 'Gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage(),
|
|
'type' => 'error',
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update($id = null)
|
|
{
|
|
$data['breadcrumbs'] = [
|
|
['name' => 'Dashboard'],
|
|
['name' => 'Pengaturan'],
|
|
['name' => 'Data Aktivitas','active' => true],
|
|
];
|
|
$keyId = decode_id($id);
|
|
$data['title'] = $this->title;
|
|
$data['route'] = $this->route;
|
|
$data['keyId'] = $id;
|
|
$data['item'] = FileDataAktivitas::where('FileDataAktivitasId',$keyId)->first();
|
|
$data['sektor'] = Sektor::all();
|
|
|
|
return view($this->template.'.form',$data);
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
$keyId = decode_id($id);
|
|
|
|
$data = FileDataAktivitas::where('FileDataAktivitasId',$keyId)->delete();
|
|
|
|
return response()->json(['success' => true,'message' => 'Berhasil update data','type' => 'success']);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|