Files
database-kesiswaan/app/Http/Controllers/BrandingSettingController.php
T

73 lines
2.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\AppSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use RealRashid\SweetAlert\Facades\Alert;
class BrandingSettingController extends Controller
{
public function index(): View
{
return view('settings.branding', [
'branding' => AppSetting::branding(),
]);
}
public function update(Request $request): RedirectResponse
{
$data = $request->validate([
'app_name' => ['required', 'string', 'max:255'],
'app_short_name' => ['required', 'string', 'max:20'],
'app_logo' => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp,svg', 'max:2048'],
'app_favicon' => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp,svg,ico', 'max:1024'],
'public_portal_logo' => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp,svg', 'max:2048'],
'remove_app_logo' => ['nullable', 'boolean'],
'remove_app_favicon' => ['nullable', 'boolean'],
'remove_public_portal_logo' => ['nullable', 'boolean'],
'public_show_student_documents' => ['nullable', 'boolean'],
]);
AppSetting::setValue('app_name', $data['app_name']);
AppSetting::setValue('app_short_name', $data['app_short_name']);
AppSetting::setValue('public_show_student_documents', $request->boolean('public_show_student_documents') ? '1' : '0');
$this->handleAssetUpdate($request, 'app_logo', 'branding/app-logo', (bool) ($data['remove_app_logo'] ?? false));
$this->handleAssetUpdate($request, 'app_favicon', 'branding/app-favicon', (bool) ($data['remove_app_favicon'] ?? false));
$this->handleAssetUpdate($request, 'public_portal_logo', 'branding/public-portal-logo', (bool) ($data['remove_public_portal_logo'] ?? false));
Alert::success('Berhasil', 'Pengaturan branding aplikasi berhasil diperbarui.');
return redirect()->route('settings.branding.index')->with('success', 'Pengaturan branding aplikasi berhasil diperbarui.');
}
protected function handleAssetUpdate(Request $request, string $field, string $directory, bool $remove = false): void
{
$currentPath = AppSetting::getValue($field);
if ($remove) {
if ($currentPath && Storage::disk('public')->exists($currentPath)) {
Storage::disk('public')->delete($currentPath);
}
AppSetting::setValue($field, null);
return;
}
if (! $request->hasFile($field)) {
return;
}
if ($currentPath && Storage::disk('public')->exists($currentPath)) {
Storage::disk('public')->delete($currentPath);
}
$path = $request->file($field)->store($directory, 'public');
AppSetting::setValue($field, $path);
}
}