Files
Virtnet.bond/extensions/Others/NavbarManager/View/Components/NavigationLinks.php
2025-12-03 14:23:33 +01:00

140 lines
4.3 KiB
PHP

<?php
namespace Paymenter\Extensions\Others\NavbarManager\View\Components;
use Illuminate\View\Component;
use Paymenter\Extensions\Others\NavbarManager\Models\NavbarLink;
class NavigationLinks extends Component
{
public $links;
public $type;
public function __construct($type = 'primary')
{
$this->type = $type;
$this->links = $this->getNavigationLinks($type);
}
public function render()
{
return view('navbar-manager::components.navigation-links');
}
private function getNavigationLinks($type)
{
try {
$links = NavbarLink::where('type', $type)
->where('is_active', true)
->whereNull('parent_id')
->orderBy('sort_order')
->with('children')
->get();
$navLinks = [];
foreach ($links as $link) {
if ($this->shouldShowLink($link)) {
$formattedLink = $this->formatLink($link);
if ($formattedLink) {
$navLinks[] = $formattedLink;
}
}
}
return $navLinks;
} catch (\Exception $e) {
\Log::error('NavbarManager: Error loading navigation links', [
'type' => $type,
'error' => $e->getMessage()
]);
return [];
}
}
private function shouldShowLink($link)
{
try {
switch ($link->condition) {
case 'always_visible':
return true;
case 'always_hidden':
return false;
case 'clients_only':
return auth()->check() && auth()->user()->role_id === null;
case 'admin_only':
return auth()->check() && auth()->user()->role_id !== null;
case 'guests_only':
return !auth()->check();
default:
return true;
}
} catch (\Exception $e) {
return true;
}
}
private function formatLink($link)
{
try {
$formatted = [
'name' => $link->name,
'priority' => $link->sort_order,
];
// Add icon
if ($link->icon) {
$formatted['icon'] = $link->icon;
}
// Handle different link types
switch ($link->link_type) {
case 'route':
if ($link->route_name) {
$formatted['route'] = $link->route_name;
if ($link->route_params) {
$params = is_string($link->route_params)
? json_decode($link->route_params, true)
: $link->route_params;
if ($params && is_array($params)) {
$formatted['params'] = $params;
}
}
} else {
return null;
}
break;
case 'path':
$formatted['direct_url'] = $link->custom_path;
break;
case 'url':
$formatted['direct_url'] = $link->custom_url;
break;
}
// Handle children (dropdown items)
if ($link->children && $link->children->count() > 0) {
$children = [];
foreach ($link->children->where('is_active', true) as $child) {
if ($this->shouldShowLink($child)) {
$childFormatted = $this->formatLink($child);
if ($childFormatted) {
$children[] = $childFormatted;
}
}
}
if (!empty($children)) {
$formatted['children'] = $children;
}
}
return $formatted;
} catch (\Exception $e) {
\Log::error('NavbarManager: Error formatting link', [
'link_id' => $link->id ?? 'unknown',
'error' => $e->getMessage()
]);
return null;
}
}
}