route('login'); } $userLogged = User::where('id', '=', Auth::id())->get()->first(); $allNotifications = DB::table('notifications')->orderBy('id', 'desc')->limit('3')->get(); $siteData = SiteContext::current(); if(!$siteData->pagelink){ return redirect()->route('dashboard.r'); } $myPage = DB::connection('mysql_site_' . session('serverID'))->table('pagelink_settings')->where('site_id', '=', session('siteID'))->first(); if($siteData->pagelink && !$myPage){ DB::connection('mysql_site_' . session('serverID'))->table('pagelink_settings')->insert([ 'site_id' => session('siteID'), 'status' => '1', 'name' => $siteData->name, 'description' => 'O melhor site', 'updated_at' => NOW() ]); return redirect()->to(url()->current()); } $allLinks = DB::connection('mysql_site_' . session('serverID'))->table('pagelink_items')->where('page_id', '=', $myPage->id)->get(); $links = $allLinks->map(function($l) { return [ 'id' => $l->id, 'titulo' => $l->name, 'url' => $l->url, 'ativo' => $l->status === '1', ]; })->values(); $arrC = array('none'=> '','red' => '#e53935','green' => '#43a047','orange' => '#fb8c00','blue' => '#1e88e5','ciano' => '#00897b','purple' => '#8e24aa','pink' => '#e91e8c', 'yellow' => '#bdad27'); $hexColor = $arrC[$myPage->color]; $socials = DB::connection('mysql_site_' . session('serverID'))->table('social')->where('site_id', session('siteID'))->first(); $socialActive = json_decode($myPage->social ?? '{}', true) ?? []; // Monta apenas as redes preenchidas $redes = collect([ 'facebook' => 'fa-brands fa-facebook-f', 'instagram' => 'fa-brands fa-instagram', 'whatsapp' => 'fa-brands fa-whatsapp', 'youtube' => 'fa-brands fa-youtube', 'tiktok' => 'fa-brands fa-tiktok', 'twitter' => 'fa-brands fa-x-twitter', 'telegram' => 'fa-brands fa-telegram', 'spotify' => 'fa-brands fa-spotify', ])->filter(function($icon, $slug) use ($socials) { $field = 'social_' . $slug; return !empty($socials->$field ?? null); })->map(function($icon, $slug) use ($socials, $socialActive) { $field = 'social_' . $slug; return [ 'slug' => $slug, 'icon' => $icon, 'value' => $socials->$field, 'active' => isset($socialActive[$slug]) ? (bool) $socialActive[$slug] : false, ]; })->values(); return view('site.pagelink', ['userLogged' => $userLogged , 'allNotifications' => $allNotifications, 'topHref' => 'site', 'siteData' => $siteData, 'curPage' => $myPage, 'allLinks'=>$links, 'redes' => $redes, 'hexColor' => $hexColor]); } public function update(Request $request){ $db = DB::connection('mysql_site_' . session('serverID')); $validated = $request->validate([ 'name' => 'required|string|max:150', 'description' => 'nullable|string|max:255', 'theme' => 'required|in:light,dark', 'color' => 'nullable|string|max:7', 'show_player' => 'required|boolean', 'links' => 'nullable|array', 'links.*.id' => 'nullable|integer', 'links.*.title' => 'required_with:links|string|max:255', 'links.*.url' => 'required_with:links|url|max:2048', 'links.*.active' => 'required_with:links|boolean', 'social_networks' => 'nullable|array', 'social_networks.*.id' => 'required|integer', 'social_networks.*.slug' => 'required|string', 'social_networks.*.active' => 'required|boolean', ]); $pageId = session('siteID'); $page = $db->table('pagelink_settings')->where('site_id', $pageId)->first(); $arrC = array(''=> 'none','#e53935' => 'red','#43a047' => 'green','#fb8c00' => 'orange', '#1e88e5' => 'blue', '#00897b' => 'ciano', '#8e24aa' => 'purple', '#e91e8c' => 'pink', 'yellow' => '#bdad27'); $socialJson = collect($validated['social_networks'] ?? []) ->mapWithKeys(function($item) { return [$item['slug'] => $item['active']]; })->toJson(); $db->table('pagelink_settings')->where('site_id', $pageId)->update([ 'name' => $validated['name'], 'description' => $validated['description'] ?? null, 'theme' => $validated['theme'], 'color' => $arrC[$validated['color']] ?? 'none', 'player' => $validated['show_player'] ? '1' : '0', 'social' => $socialJson, 'updated_at' => NOW() ]); // 1. Remove primeiro os deletados $idsRecebidos = collect($validated['links'] ?? []) ->pluck('id') ->filter() ->toArray(); $db->table('pagelink_items') ->where('page_id', $page->id) ->when(!empty($idsRecebidos), fn($q) => $q->whereNotIn('id', $idsRecebidos)) ->when(empty($idsRecebidos), fn($q) => $q) ->delete(); // 2. Depois insere/atualiza if (!empty($validated['links'])) { foreach ($validated['links'] as $link) { if (!empty($link['id'])) { $db->table('pagelink_items') ->where('id', $link['id']) ->where('page_id', $page->id) ->update([ 'name' => $link['title'], 'url' => $link['url'], 'status' => $link['active'] ? '1' : '0', ]); } else { $db->table('pagelink_items')->insert([ 'page_id' => $page->id, 'name' => $link['title'], 'url' => $link['url'], 'status' => $link['active'] ? '1' : '0', ]); } } } return response()->json(['message' => 'Configurações salvas com sucesso.']); } public function updateAsset(Request $request): JsonResponse { $siteData = SiteContext::current(); $request->validate([ 'logo' => [ 'required', 'file', 'mimes:jpg,jpeg,png', 'max:5120', ], ]); if(file_exists(str_replace('\\','/',public_path()).'/assets-page/'.$siteData->stream_port.'.png')){ unlink(str_replace('\\','/',public_path()).'/assets-page/'.$siteData->stream_port.'.png'); } $fileLogo = $request->file('logo'); $logoImg = Image::make($fileLogo); $logoImg->encode('png', 90)->fit(500, 500)->save(public_path("assets-page/".$siteData->stream_port.".png")); DB::connection('mysql_site_' . session('serverID'))->table('pagelink_settings')->where('site_id', '=', session('siteID'))->update([ 'updated_at' => NOW() ]); return response()->json([ 'url' => '/assets-page/'.$siteData->stream_port.'.png', 'filename' => $siteData->stream_port.'.png', 'format' => 'png', ], 201); } public function publish(Request $request): JsonResponse { $request->validate(['published' => 'required|boolean']); // Radio::where('id', session('siteID'))->update(['published' => ]); DB::connection('mysql_site_' . session('serverID'))->table('pagelink_settings')->where('site_id', '=', session('siteID'))->update([ 'status' => ($request->published ? '1' : '0') ]); return response()->json(['message' => 'ok']); } public function destroy(): JsonResponse { $siteData = SiteContext::current(); $filename = $siteData->stream_port.'.png'; $path = public_path('assets-page/' . $filename); if (!file_exists($path)) { return response()->json(['message' => 'Arquivo não encontrado: '.$path], 404); } unlink($path); return response()->json(['message' => 'Logo removida com sucesso.']); } }