<?php
 namespace App\Http\Controllers\Store; use App\Http\Controllers\Controller; use App\Models\Language; use App\Models\Page; use App\Services\PageBuilder\PageRenderer; use App\Services\PageBuilder\ReservedSlugs; use App\Services\VisitorService; class StorePageController extends Controller { public function show(string $slug = '', ?string $localeOverride = null) { if ($slug !== '' && ReservedSlugs::isReserved($slug)) { abort(404); } $brandId = (app()->bound('brand') && app('brand')) ? app('brand')->id : null; try { if ($slug !== '' && $localeOverride === null && $this->isLocaleSegment($slug)) { $localeOverride = $slug; $slug = ''; } if (empty($slug)) { if ($localeOverride !== null) { $locale = $localeOverride; $this->switchAppLocale($locale); } else { $locale = app()->getLocale(); } $page = $this->resolveHomePage($locale, $brandId) ?? $this->resolveHomePage( Language::where('site_default', true)->value('a2_iso') ?? config('app.locale'), $brandId ) ?? $this->resolveAnyHomePage($brandId); } else { $locale = app()->getLocale(); $page = $this->resolvePageBySlug($slug, $locale, $brandId); if ($page && $page->locale && $page->locale !== app()->getLocale()) { $this->switchAppLocale($page->locale); } } } catch (\Throwable $e) { \Log::error('Page resolve failed', ['slug' => $slug, 'error' => $e->getMessage()]); abort(500); } if (! $page) { if ($slug === '') { return redirect()->route('store.products.index'); } abort(404); } if ($page->status !== 'published' && ! $this->canPreview()) abort(404); return app(PageRenderer::class)->renderResponse($page); } private function switchAppLocale(string $locale): void { if (app()->getLocale() === $locale) { return; } app()->setLocale($locale); $visitorService = app(VisitorService::class); $visitorId = request()->attributes->get('visitor_id'); if (! $visitorId) { $visitorId = $visitorService->getOrCreateVisitorId(request()); } $visitorService->updateVisitorDetails($visitorId, ['preferredLanguage' => $locale]); } private function isLocaleSegment(string $segment): bool { if (! preg_match('/^[a-z]{2}$/', $segment)) { return false; } return Language::active() ->where('a2_iso', $segment) ->where('site_default', false) ->exists(); } private function resolveHomePage(string $locale, ?int $brandId): ?Page { if ($brandId) { return Page::where('brand_id', $brandId)->where('locale', $locale) ->where('is_home', true)->where('status', 'published')->first(); } return Page::whereNull('brand_id')->where('locale', $locale) ->where('is_home', true)->where('status', 'published')->first(); } private function resolveAnyHomePage(?int $brandId): ?Page { $query = Page::where('is_home', true)->where('status', 'published'); if ($brandId) { $query->where('brand_id', $brandId); } else { $query->whereNull('brand_id'); } return $query->orderByRaw("locale = 'en' DESC")->first(); } private function resolvePageBySlug(string $slug, string $locale, ?int $brandId): ?Page { if ($brandId) { $brandPage = Page::where('brand_id', $brandId)->where('locale', $locale) ->where('slug', $slug)->where('status', 'published')->first(); if ($brandPage) return $brandPage; return Page::where('brand_id', $brandId) ->where('slug', $slug)->where('status', 'published')->first(); } $page = Page::whereNull('brand_id')->where('locale', $locale) ->where('slug', $slug)->where('status', 'published')->first(); if ($page) return $page; return Page::whereNull('brand_id') ->where('slug', $slug)->where('status', 'published')->first(); } private function canPreview(): bool { return auth('admin')->check() && request()->query('preview') === '1'; } } 