<?php
namespace Extension;
 use TCMS\ControlExtension;
 use TCMS\TException;
 class MenuDefault extends ControlExtension {
 public $option = [ 'template' => null ];
 public function render($menuList, $option = null) {
 copy_option($this->option, $option);
 if($this->option['template']) {
 $template = $this->option['template'];
 foreach($menuList as $menu) {
 $html = str_replace("{url}", $menu->url, $template);
 $html = str_replace("{text}", $menu->text, $html);
 $html = str_replace("{icon}", $menu->icon, $html);
 $html = str_replace("{id}", $menu->id, $html);
 echo $html;
 }
 }
 else {
 foreach($menuList as $menu) {
 if(count($menu->subMenu)) {
 if($menu->icon) echo '<li id="menu-' . $menu->id . '" class="dropdownmenu'. ($menu->class ? (' ' . $menu->class) : '') . ($menu->isActive ? ' active' : '').'"><a href="'. $menu->url .'"><i class="'.$menu->icon.'"></i> <span>' .$menu->text. '</span><small class="fa fa-chevron-down"></small></a>';
 else echo '<li id="menu-' . $menu->id . '" class="dropdownmenu'. ($menu->class ? (' ' . $menu->class) : '') . ($menu->isActive ? ' active' : '').'"><a href="'. $menu->url .'">' .$menu->text. '<small class="fa fa-chevron-down"></small></a>';
 echo '<ul><li class="submenu-item"><a href="'.$menu->url.'">';
 if($menu->subMenuTitle == null) {
 $tt = mb_strtolower($menu->text);
 $startIndex = strpos($tt, "quản lý ");
 if($startIndex === 0) {
 $up = strtoupper(substr($menu->text, 11, 1));
 $menu->text = $up . substr($menu->text, 12);
 }
 echo $menu->text;
 }
 else {
 echo $menu->subMenuTitle;
 }
 echo '</a></li>';
 foreach($menu->subMenu as $subMenuItem) {
 echo '<li class="submenu-item'.($subMenuItem->class ? (' ' . $subMenuItem->class) : '') .($subMenuItem->isActive ? ' active' : '').'" data-parent-id="' . $subMenuItem->parentMenuId . '"><a href="'.$subMenuItem->url.'">'.$subMenuItem->text.'</a></li>';
 }
 echo '</ul></li>';
 }
 else {
 if($menu->icon) echo '<li id="menu-' . $menu->id . '" '.($menu->isActive ? 'class="active"' : '').'><a href="'. $menu->url .'"><i class="'.$menu->icon.'"></i> <span>' .$menu->text. '</span></a></li>';
 else echo '<li id="menu-' . $menu->id . '" '.($menu->isActive ? 'class="active"' : '').'><a href="'. $menu->url .'">' .$menu->text. '</a></li>';
 }
 }
 }
 }
 }
 ?>