b2in/app/Livewire/Web/Components/UI/Header.php
2026-04-10 17:18:17 +02:00

86 lines
2.2 KiB
PHP

<?php
namespace App\Livewire\Web\Components\Ui;
use Livewire\Component;
class Header extends Component
{
public $isMobileMenuOpen = false;
public $domainName;
public $domainUrl;
public $content = [];
public $currentLocale;
public $availableLocales = [
'de' => 'DE',
'en' => 'EN',
];
public function mount(): void
{
$this->domainName = \App\Helpers\ThemeHelper::getDomainName();
$this->domainUrl = \App\Helpers\ThemeHelper::getDomainUrl();
$this->currentLocale = app()->getLocale();
$this->content = cms_theme_section('header');
// Ensure required keys exist
if (!isset($this->content['portal_login'])) {
$this->content['portal_login'] = 'Portal Login';
}
if (!isset($this->content['navigation']) || !is_array($this->content['navigation'])) {
$this->content['navigation'] = [];
}
}
public function switchLanguage(string $locale): mixed
{
if (array_key_exists($locale, $this->availableLocales)) {
app()->setLocale($locale);
session(['locale' => $locale]);
$this->currentLocale = $locale;
$referer = request()->header('Referer') ?? '/';
return redirect()->to($referer);
}
return null;
}
public function toggleMobileMenu(): void
{
$this->isMobileMenuOpen = ! $this->isMobileMenuOpen;
}
public function closeMobileMenu(): void
{
$this->isMobileMenuOpen = false;
}
public function isActiveRoute($url)
{
$currentPath = request()->path();
$currentPath = '/' . ltrim($currentPath, '/');
// Exact match
if ($currentPath === $url) {
return true;
}
// Special handling for home route
if ($url === '/' && ($currentPath === '/' || $currentPath === '')) {
return true;
}
// Check if current path starts with the navigation URL (for sub-pages)
if ($url !== '/' && str_starts_with($currentPath, rtrim($url, '/'))) {
return true;
}
return false;
}
public function render()
{
return view('livewire.web.components.ui.header');
}
}