137 lines
3.8 KiB
PHP
137 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Master;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Sector;
|
|
|
|
class SectorController extends Controller
|
|
{
|
|
protected $title = 'Sector';
|
|
protected $template = 'modules.master.sector';
|
|
protected $route = 'modules.master.sector';
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.index',$data);
|
|
}
|
|
|
|
public function grid()
|
|
{
|
|
$data = Sector::all();
|
|
$_data = [];
|
|
|
|
foreach ($data as $key => $row) {
|
|
|
|
$btn = '<a href="' . url('master/sector/').'/'.$row->code. '/subsector" class="btn btn-sm w-100 mb-1 btn-success">Sub Sector</a>';
|
|
$btn .= '<a href="' . url('master/sector/update/').'/'.encode_id($row->id). '" class="btn btn-sm w-100 mb-1 btn-primary">Edit</a>';
|
|
$btn .= ' <form action="' . route($this->route.'.destroy', $row->id) . '" method="POST" style="display: inline;" class="delete-form">';
|
|
$btn .= csrf_field();
|
|
$btn .= method_field('DELETE');
|
|
$btn .= '<button type="button" class="btn btn-sm btn-danger delete-button remove_data">Hapus</button>';
|
|
$btn .= '</form>';
|
|
|
|
|
|
$_data[] = [
|
|
'no' => $key+1,
|
|
'name' => $row->name,
|
|
'code' => $row->code,
|
|
'action' => @$btn,
|
|
];
|
|
|
|
}
|
|
|
|
return response()->json($_data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.form',$data);
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
$keyId = decode_id($id);
|
|
$data['title'] = $this->title;
|
|
$data['route'] = $this->route;
|
|
$data['keyId'] = $id;
|
|
$data['item'] = Sector::where('id',$keyId)->first();
|
|
return view($this->template.'.form',$data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'code' => 'nullable|string',
|
|
]);
|
|
|
|
try {
|
|
if(@$request->secure_id){
|
|
$data = Sector::find(decode_id(@$request->secure_id));
|
|
$data->name = $request->name;
|
|
$data->code = $request->code;
|
|
$data->save();
|
|
}else{
|
|
$data = new Sector;
|
|
$data->name = $request->name;
|
|
$data->code = $request->code;
|
|
$data->row_num = $request->row_num;
|
|
$data->row_status = 1;
|
|
$data->save();
|
|
}
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'Sector berhasil ditambahkan.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'Sector gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
$data['ar'] = Sector::find($id);
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
return view($this->template.'.form', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|