diff --git a/app/Http/Controllers/SystemSettingController.php b/app/Http/Controllers/SystemSettingController.php index 12574b2..d25015c 100644 --- a/app/Http/Controllers/SystemSettingController.php +++ b/app/Http/Controllers/SystemSettingController.php @@ -3,8 +3,10 @@ namespace App\Http\Controllers; use App\Models\Setting; +use App\Http\Requests\SettingRequest; use App\Services\System\SystemSettingService; use Illuminate\View\View; +use Illuminate\Http\RedirectResponse; class SystemSettingController extends Controller { @@ -25,4 +27,49 @@ class SystemSettingController extends Controller 'resolved' => $resolved, ]); } + + public function store(SettingRequest $request, SystemSettingService $service): RedirectResponse + { + $data = $request->validated(); + + $service->set( + $data['key'], + $request->castedValue(), + $data['type'] ?? null, + $data['description'] ?? null, + $data['group'] ?? null, + $data['is_public'] ?? false + ); + + return redirect()->route('settings.index')->with('success', 'Setting berhasil ditambahkan.'); + } + + public function update(SettingRequest $request, Setting $setting, SystemSettingService $service): RedirectResponse + { + $data = $request->validated(); + + $service->set( + $data['key'], + $request->castedValue(), + $data['type'] ?? null, + $data['description'] ?? null, + $data['group'] ?? null, + $data['is_public'] ?? false + ); + + // Pastikan key ikut terbarui jika diubah. + if ($setting->key !== $data['key']) { + $setting->key = $data['key']; + } + + return redirect()->route('settings.index')->with('success', 'Setting berhasil diperbarui.'); + } + + public function destroy(Setting $setting, SystemSettingService $service): RedirectResponse + { + $setting->delete(); + $service->refresh(); + + return redirect()->route('settings.index')->with('success', 'Setting berhasil dihapus.'); + } } diff --git a/app/Http/Requests/SettingRequest.php b/app/Http/Requests/SettingRequest.php new file mode 100644 index 0000000..9a19031 --- /dev/null +++ b/app/Http/Requests/SettingRequest.php @@ -0,0 +1,62 @@ +route('setting')?->id; + + return [ + 'key' => [ + 'required', + 'string', + 'max:255', + Rule::unique('settings', 'key')->ignore($settingId), + ], + 'group' => ['nullable', 'string', 'max:100'], + 'type' => ['required', 'string', Rule::in(['string', 'integer', 'boolean', 'float', 'array'])], + 'value' => ['nullable'], + 'description' => ['nullable', 'string', 'max:255'], + 'is_public' => ['sometimes', 'boolean'], + ]; + } + + /** + * Konversi value sesuai tipe agar konsisten saat disimpan. + */ + public function castedValue(): mixed + { + $type = $this->input('type'); + $value = $this->input('value'); + + return match ($type) { + 'boolean' => filter_var($value, FILTER_VALIDATE_BOOL), + 'integer' => (int) $value, + 'float' => (float) $value, + 'array' => $this->toArrayValue($value), + default => (string) $value, + }; + } + + private function toArrayValue($value): array + { + if (is_array($value)) { + return $value; + } + + // Izinkan input JSON untuk array. + $decoded = json_decode($value ?? '', true); + + return is_array($decoded) ? $decoded : []; + } +} diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php index e612817..552b490 100644 --- a/resources/views/settings/index.blade.php +++ b/resources/views/settings/index.blade.php @@ -16,8 +16,30 @@
Daftar Setting
Nilai yang ditampilkan sudah menerapkan override dari database.
+
+ +
+ @if(session('success')) + + @endif + + @if($errors->any()) +
+ +
+ @endif + @forelse($settingsByGroup as $group => $items)
@@ -31,6 +53,7 @@ Nilai Aktif Tipe Keterangan + Aksi @@ -48,6 +71,27 @@ {{ $setting->type }} {{ $setting->description ?? '-' }} + +
+ +
+ @csrf + @method('DELETE') + +
+
+ @endforeach @@ -63,4 +107,72 @@
+ + {{-- Modal Tambah --}} + + + {{-- Modal Edit --}} + @endsection + +@push('scripts') + +@endpush diff --git a/resources/views/settings/partials/form.blade.php b/resources/views/settings/partials/form.blade.php new file mode 100644 index 0000000..cb38dd6 --- /dev/null +++ b/resources/views/settings/partials/form.blade.php @@ -0,0 +1,40 @@ +
+
+ + + Gunakan format dot notation untuk pengelompokan. +
+
+ + +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+
+ + +
+
+
diff --git a/routes/web.php b/routes/web.php index c1c12ba..55cddbe 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,4 +18,4 @@ Route::get('/', function () { // Route::get('/', [DashboardsController::class, 'index']); Route::get('index', [DashboardsController::class, 'index']); -Route::get('settings', [SystemSettingController::class, 'index'])->name('settings.index'); +Route::resource('settings', SystemSettingController::class)->only(['index', 'store', 'update', 'destroy']);