all(); // Tambahkan default jika version belum ada if (!isset($settings['app_version'])) { $settings['app_version'] = '1.0.0'; } $waStatus = $this->checkWhatsAppService(); $emailStatus = $this->checkEmailService(); $logs = ActivityLog::with('user') ->where('action', 'LIKE', '%SETTING%') ->latest() ->take(5) ->get(); $roles = Role::orderBy('name')->get(['id','name']); return view('pages.admin.settings', compact('settings', 'waStatus', 'emailStatus', 'logs', 'roles')); } public function update(Request $request) { Gate::authorize('check-access', ['settings', 'update']); $request->validate([ 'app_logo' => 'nullable|image|mimes:png,jpg,jpeg|max:2048', 'app_favicon' => 'nullable|image|mimes:png,jpg,ico|max:512', 'app_name' => 'required|string|max:100', 'app_slogan' => 'nullable|string|max:255', 'app_version' => 'required|string|max:20', // Validasi versi ]); // 1. Proses Logo & Favicon if ($request->hasFile('app_logo')) { $this->uploadAndProcessImage($request->file('app_logo'), 'app_logo', 512); } if ($request->hasFile('app_favicon')) { $this->uploadAndProcessImage($request->file('app_favicon'), 'app_favicon', 128); } // 2. Update Pengaturan Teks (Hanya yang ada di whitelist) foreach ($this->allowedSettings as $key) { if ($request->has($key)) { Setting::updateOrCreate(['key' => $key], ['value' => $request->get($key)]); } } foreach (['notif_hpl_wa','notif_inventory_expiry_wa','notif_weight_stale_wa','notif_wa_status'] as $notificationKey) { Setting::updateOrCreate( ['key' => $notificationKey], ['value' => $request->boolean($notificationKey) ? 'active' : 'inactive', 'group' => 'notification'] ); } foreach (['notif_hpl_role_id','notif_inventory_role_id','notif_weight_role_id'] as $roleKey) { if ($request->filled($roleKey)) { $request->validate([$roleKey => 'exists:roles,id']); Setting::updateOrCreate(['key' => $roleKey], ['value' => $request->input($roleKey), 'group' => 'notification']); } } $this->logAction('UPDATED_SETTINGS', null, "Memperbarui identitas dan notifikasi sistem ke versi " . $request->app_version); return back()->with('success', 'Konfigurasi ' . $request->app_name . ' berhasil diperbarui!'); } /** * Helper Image Processing (Intervention Image v3) */ private function uploadAndProcessImage($file, $key, $size) { $oldFile = Setting::where('key', $key)->value('value'); if ($oldFile && Storage::disk('public')->exists($oldFile)) { Storage::disk('public')->delete($oldFile); } $filename = $key . '_' . time() . '.png'; $path = 'settings/' . $filename; // Intervention Image v3 Logic $image = Image::read($file); $image->cover($size, $size); Storage::disk('public')->put($path, (string) $image->toPng()); Setting::updateOrCreate(['key' => $key], ['value' => $path]); } private function checkWhatsAppService() { $provider = strtoupper((string) config('services.whatsapp.provider', 'fonnte')); return [ 'active' => WhatsappHelper::configured(), 'quota' => null, 'message' => WhatsappHelper::configured() ? "{$provider} dikonfigurasi" : "{$provider} belum dikonfigurasi", ]; } private function checkEmailService() { try { $transport = Mail::getSymfonyTransport(); // 1. Cek jika menggunakan driver 'log' (biasanya di localhost) if ($transport instanceof \Illuminate\Mail\Transport\LogTransport) { return [ 'active' => true, 'message' => 'Mail Mode: LOG (Local Development)' ]; } // 2. Jika menggunakan SMTP, coba lakukan jabat tangan (handshake) if (method_exists($transport, 'start')) { $transport->stop(); $transport->start(); } return ['active' => true, 'message' => 'SMTP Terhubung']; } catch (\Exception $e) { // Jika gagal konek ke Mailtrap/Gmail, kembalikan status false return [ 'active' => false, 'message' => 'SMTP Error: ' . $e->getMessage() ]; } } }