File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/app/KommaApp/Shop/Menus/SidebarMenu.php
<?php
namespace KommaApp\Shop\Menus;
use Illuminate\Routing\Router;
class SidebarMenu
{
/**
* @var Router
*/
private $router;
private $level = 0;
/**
* @param Router $router
*/
public function __construct(Router $router)
{
$this->router = $router;
}
public function create($rootNode)
{
$output = '';
if ($rootNode->hasChildren()) {
$output .= '<ul>';
$output .= '<li';
if ($this->router->currentRouteName() == '/' . $rootNode->route) $output .= ' class="active"';
$output .= '>';
$output .= '<a href="/' . $rootNode->route . '">' . $rootNode->name . '</a>';
$output .= '</li>';
foreach ($rootNode->children as $child) {
$output .= $this->addChildMenu($child);
}
$output .= '</ul>';
}
return $output;
}
protected function addChildMenu($node)
{
$hasChildren = $node->hasChildren();
$output = '';
$output .= $this->openListItem($node, $hasChildren);
if ($hasChildren) {
$this->level++;
$output .= '<ul>';
//$output .= $this->openListItem($node);
foreach ($node->children as $child) {
$output .= $this->addChildMenu($child);
}
//$this->closeListItem();
$output .= '</ul>';
$this->level--;
}
$output .= $this->closeListItem();
return $output;
}
protected function openListItem($node, $hasChildren = false)
{
$output = '<li';
//Check if the current route starts with the node route an ends after this Or a next url part (/)
if(preg_match('#^/'.$node->route.'($|/)#i', $this->router->currentRouteName()))$output .= ' class="active"';
$output .= '>';
$output .= '<a href="/' . $node->route . '">';
$output .= $node->name;
if ($hasChildren)
$output .= '<span class="plus"></span>';
$output .= '</a>';
return $output;
}
protected function closeListItem()
{
return '</li>';
}
}