Server IP : 66.29.132.124 / Your IP : 3.143.237.203 Web Server : LiteSpeed System : Linux business141.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : wavevlvu ( 1524) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/wavevlvu/book24.ng/modules/Core/Walkers/ |
Upload File : |
<?php namespace Modules\Core\Walkers; class MenuWalker { protected static $currentMenuItem; protected $menu; protected $activeItems = []; public function __construct($menu) { $this->menu = $menu; } public function generate() { $items = json_decode($this->menu->items, true); if (!empty($items)) { echo '<ul class="main-menu menu-generated">'; $this->generateTree($items); echo '</ul>'; } } public function generateTree($items = [],$depth = 0,$parentKey = '') { foreach ($items as $k=>$item) { $class = e($item['class'] ?? ''); $url = $item['url'] ?? ''; $item['target'] = $item['target'] ?? ''; if (!isset($item['item_model'])) continue; if (class_exists($item['item_model'])) { $itemClass = $item['item_model']; $itemObj = $itemClass::find($item['id']); if (empty($itemObj)) { continue; } $url = $itemObj->getDetailUrl(); } if ($this->checkCurrentMenu($item, $url)) { $class .= ' active'; $this->activeItems[] = $parentKey; } if (!empty($item['children'])) { ob_start(); $this->generateTree($item['children'],$depth + 1,$parentKey.'_'.$k); $html = ob_get_clean(); if(in_array($parentKey.'_'.$k,$this->activeItems)){ $class.=' active '; } } $class.=' depth-'.($depth); printf('<li class="%s">', $class); if (!empty($item['children'])) { $item['name'] .= ' <i class="caret fa fa-angle-down"></i>'; } printf('<a target="%s" href="%s" >%s</a>', e($item['target']), e($url), clean($item['name'])); if (!empty($item['children'])) { echo '<ul class="children-menu menu-dropdown">'; echo $html; echo "</ul>"; } echo '</li>'; } } protected function checkCurrentMenu($item, $url = '') { if(trim($url,'/') == request()->path()){ return true; } if (!static::$currentMenuItem) return false; if (empty($item['item_model'])) return false; if (is_string(static::$currentMenuItem) and ($url == static::$currentMenuItem or $url == url(static::$currentMenuItem))) { return true; } if (is_object(static::$currentMenuItem) and get_class(static::$currentMenuItem) == $item['item_model'] && static::$currentMenuItem->id == $item['id']) { return true; } return false; } public static function setCurrentMenuItem($item) { static::$currentMenuItem = $item; } public static function getActiveMenu() { return static::$currentMenuItem; } }