312 lines
11 KiB
PHP
312 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Opendata;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\File;
|
|
use Carbon\Carbon;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use App\Models\Master\Instansi;
|
|
use App\Models\Master\Topik;
|
|
use App\Models\Master\Template;
|
|
use App\Models\Dataset;
|
|
|
|
class DatasetController extends Controller
|
|
{
|
|
protected $title = 'Dataset';
|
|
protected $template = 'modules.opendata.dataset';
|
|
protected $route = 'modules.opendata.dataset';
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
permission('is_read', $this->route, 'module',true);
|
|
|
|
$data['breadcrumbs'] = [
|
|
['name' => 'Dashboard','url' => url('dashboard')],
|
|
['name' => 'Open Data'],
|
|
['name' => 'Dataset','active' => true],
|
|
];
|
|
$data['title'] = $this->title;
|
|
$data['route'] = $this->route;
|
|
return view($this->template.'.index',$data);
|
|
}
|
|
|
|
public function grid(Request $request)
|
|
{
|
|
|
|
// $data = Dataset::all();
|
|
// // $data = User::with(['group'])->orderBy('id','DESC')->get();
|
|
// $_data = [];
|
|
|
|
|
|
// foreach ($data as $key => $row) {
|
|
|
|
|
|
// $action = '';
|
|
|
|
// if((permission('is_create', $this->route.'.*','module',false)) || (permission('is_update', $this->route.'.*','module',false))){
|
|
// $action .= '<div class="flex gap-3 justify-center items-center flex-row">';
|
|
// $action .= '<a href="'.url('opendata/dataset/update/'.encode_id($row->DatasetId)).'" data-toggle="tooltip" title="Ubah Data" class="btn btn-sm btn-block bg-primary"><i class="ri-pencil-line text-white"></i></a>';
|
|
// $action .= '<a href="'.url('opendata/dataset/view/'.encode_id($row->DatasetId)).'" data-toggle="tooltip" title="Lihat Data" class="btn btn-sm btn-block bg-info"><i class="ri-eye-line text-white"></i></a>';
|
|
// if(session('group_id') == 1){
|
|
// // $action .= '<a href="#" data-href="'.url('management/user/forcelogin/'.encode_id($row->id)).'" data-toggle="tooltip" title="Force Login" class="forcelogin btn btn-sm btn-block bg-success"><i class="ri-user-2-line text-white"></i></a>';
|
|
// // $action .= '<a href="#" data-href="'.url('management/user/delete/'.encode_id($row->id)).'" data-toggle="tooltip" title="Hapus Data" class="remove_data btn btn-sm btn-block bg-danger"><i class="ri-delete-bin-line text-white"></i></a>';
|
|
// }
|
|
// $action .= '</div>';
|
|
// }
|
|
|
|
// $_data[] = [
|
|
// 'no' => $key+1,
|
|
// 'id' => encode_id($row->id),
|
|
// 'name' => @$row->name,
|
|
// 'publik' => @$row->publik,
|
|
// 'created_at' => date('d-m-Y H:i:s',strtotime(@$row->created_at)),
|
|
// 'instansi' => @$row->instansi->name,
|
|
// 'action' => @$action,
|
|
// ];
|
|
|
|
// }
|
|
|
|
// // return response()->json($_data); // Return the data as a JSON response
|
|
// return response()->json($_data);
|
|
|
|
// $perPage = $request->query('limit', 10);
|
|
// $page = $request->query('page', 1);
|
|
// // dd($page);
|
|
// $search = $request->query('search');
|
|
$id = $request->query('id');
|
|
$keyId = decode_id($id);
|
|
$item = Dataset::find($keyId); // atau pakai ->pluck(), ->find(), dll
|
|
|
|
$json = json_decode($item->data, true);
|
|
$limit = $request->input('limit', 10);
|
|
$offset = $request->input('offset', 0);
|
|
|
|
$rows = array_slice($json, $offset, $limit);
|
|
return response()->json([
|
|
'total' => count($json),
|
|
'rows' => $rows
|
|
]);
|
|
|
|
// // Ambil JSON dari kolom database
|
|
// $rawData = Dataset::find($keyId)->data; // atau pakai ->pluck(), ->find(), dll
|
|
// $array = json_decode($rawData, true); // array dari JSON
|
|
// $collection = collect($array);
|
|
|
|
// if ($search) {
|
|
// $collection = $collection->filter(function ($item) use ($search) {
|
|
// foreach ($item as $value) {
|
|
// if (stripos($value, $search) !== false) {
|
|
// return true;
|
|
// }
|
|
// }
|
|
// return false;
|
|
// });
|
|
// }
|
|
|
|
// $total = $collection->count();
|
|
|
|
// $paginated = $collection->slice(($page - 1) * $perPage, $perPage)->values();
|
|
|
|
// return response()->json([
|
|
// 'data' => $paginated,
|
|
// 'page' => $page,
|
|
// 'total' => $total
|
|
// ]);
|
|
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
Validator::make($request->all(), [
|
|
'name' => 'required',
|
|
'instansi_id' => 'required',
|
|
'template_id' => 'required',
|
|
'template_default' => 'required',
|
|
'publik' => 'required',
|
|
'tags' => 'required',
|
|
'file' => 'required|file|mimes:xlsx,xls',
|
|
'topik' => 'required',
|
|
])->validate();
|
|
|
|
|
|
|
|
try {
|
|
$keyId = decode_id($request->secure_id);
|
|
|
|
$file = $request->file('file');
|
|
$path = $file->getRealPath();
|
|
$spreadsheet = IOFactory::load($path);
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$rows = $sheet->toArray();
|
|
$header = $rows[0]; // Baris pertama sebagai header
|
|
$data = [];
|
|
|
|
if (@$request->hasFile('file')) {
|
|
$file = $request->file('file');
|
|
$destinationPath = public_path('uploads/dataset');
|
|
$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')){
|
|
return redirect()->back()->with([
|
|
'message' => 'Maaf File Harus Berupa xls,xlsx!',
|
|
'type' => "error"
|
|
]);
|
|
}
|
|
$newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension;
|
|
|
|
if (!File::exists($path)) {
|
|
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
|
|
}
|
|
|
|
$filePath = 'dataset/' . $current . '/' . $newFilename;
|
|
$uploaded = $file->move($path, $newFilename);
|
|
|
|
|
|
|
|
for ($i = 1; $i < count($rows); $i++) {
|
|
$row = $rows[$i];
|
|
|
|
// Skip baris kosong
|
|
if (collect($row)->filter()->isEmpty()) continue;
|
|
|
|
$assoc = [];
|
|
foreach ($header as $j => $columnName) {
|
|
if($columnName != null){
|
|
$assoc[strtolower(str_replace(' ','_',$columnName))] = $row[$j] ?? null;
|
|
}
|
|
}
|
|
|
|
$data[] = $assoc;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if(@$keyId){
|
|
$insert = Dataset::find($keyId);
|
|
$insert->instansi_id = decode_id($request->instansi_id);
|
|
$insert->template_id = decode_id($request->template_id);
|
|
$insert->template_default = $request->template_default;
|
|
$insert->name = $request->name;
|
|
$insert->publik = $request->publik;
|
|
$insert->tags = json_encode($request->tags);
|
|
$insert->data = json_encode($data);
|
|
$insert->file = $filePath;
|
|
$insert->topik = json_encode($request->topik);
|
|
$insert->deskripsi = $request->deskripsi;
|
|
$insert->created_by = auth()->user()->id;
|
|
$insert->save();
|
|
}else{
|
|
$insert = new Dataset;
|
|
$insert->instansi_id = decode_id($request->instansi_id);
|
|
$insert->template_id = decode_id($request->template_id);
|
|
$insert->template_default = $request->template_default;
|
|
$insert->name = $request->name;
|
|
$insert->publik = $request->publik;
|
|
$insert->tags = json_encode($request->tags);
|
|
$insert->data = json_encode($data);
|
|
$insert->file = $filePath;
|
|
$insert->topik = json_encode($request->topik);
|
|
$insert->deskripsi = $request->deskripsi;
|
|
$insert->created_by = auth()->user()->id;
|
|
$insert->save();
|
|
}
|
|
|
|
return redirect()->back()->with([
|
|
'message' => 'Berhasil update data',
|
|
'type' => 'success',
|
|
]);
|
|
} catch (Exception $e) {
|
|
return redirect()->back()->with([
|
|
'message' => $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','url' => url('dashboard')],
|
|
['name' => 'Open Data'],
|
|
['name' => 'Dataset','active' => true],
|
|
];
|
|
$keyId = decode_id($id);
|
|
$data['title'] = $this->title;
|
|
$data['route'] = $this->route;
|
|
$data['keyId'] = $id;
|
|
$data['item'] = null;
|
|
$data['instansi'] = Instansi::all();
|
|
$data['topik'] = Topik::all();
|
|
$data['template'] = Template::all();
|
|
return view($this->template.'.form',$data);
|
|
}
|
|
|
|
public function view($id = null)
|
|
{
|
|
$data['breadcrumbs'] = [
|
|
['name' => 'Dashboard','url' => url('dashboard')],
|
|
['name' => 'Open Data'],
|
|
['name' => 'Dataset','active' => true],
|
|
];
|
|
$keyId = decode_id($id);
|
|
$data['title'] = $this->title;
|
|
$data['route'] = $this->route;
|
|
$data['keyId'] = $id;
|
|
$data['item'] = Dataset::find($keyId);
|
|
$data['instansi'] = Instansi::all();
|
|
$data['topik'] = Topik::all();
|
|
$data['template'] = Template::all();
|
|
|
|
return view($this->template.'.view',$data);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|