61 lines
2.1 KiB
PHP
61 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Setting;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\Setting\UnitConversionService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
|
|
class UnitConversionController implements HasMiddleware
|
|
{
|
|
protected $title = 'Unit Conversion';
|
|
protected $template = 'modules.setting.unit-conversion';
|
|
protected $route = 'modules.pengaturan.unit-conversion';
|
|
protected $unitConversionService;
|
|
|
|
public function __construct(UnitConversionService $unitConversionService)
|
|
{
|
|
$this->unitConversionService = $unitConversionService;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/setting/unit_conversion'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
// For combobox category
|
|
$data['categories'] = $this->unitConversionService->getCategories();
|
|
$data['selectedCategory'] = $request->get('category', $data['categories']->first());
|
|
|
|
// For unit conversion matrix
|
|
$data['units'] = $this->unitConversionService->getUnitsByCategory($data['selectedCategory']);
|
|
$data['conversions'] = $this->unitConversionService->getConversionsByCategory($data['selectedCategory']);
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.index', $data);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'category' => 'required',
|
|
'conversions' => 'required|array',
|
|
]);
|
|
|
|
try {
|
|
$this->unitConversionService->save($request->category, $request->get('conversions', []));
|
|
|
|
return redirect()->route($this->route.'.index', ['category' => $request->category])->with('success', 'Unit Conversion berhasil disimpan.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'Unit Conversion gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
}
|