Commit 1d14679c authored by Nicolas Widart's avatar Nicolas Widart

Make the menu dynammicaly

parent a7548229
......@@ -9,6 +9,7 @@ use Modules\Menu\Http\Requests\CreateMenuRequest;
use Modules\Menu\Http\Requests\UpdateMenuRequest;
use Modules\Menu\Repositories\MenuItemRepository;
use Modules\Menu\Repositories\MenuRepository;
use Modules\Menu\Services\MenuRenderer;
class MenuController extends AdminBaseController
{
......@@ -24,13 +25,22 @@ class MenuController extends AdminBaseController
* @var MenuItemRepository
*/
private $menuItem;
/**
* @var MenuRenderer
*/
private $menuRenderer;
public function __construct(MenuRepository $menu, MenuItemRepository $menuItem, Redirector $redirector)
{
public function __construct(
MenuRepository $menu,
MenuItemRepository $menuItem,
Redirector $redirector,
MenuRenderer $menuRenderer
) {
parent::__construct();
$this->menu = $menu;
$this->redirector = $redirector;
$this->menuItem = $menuItem;
$this->menuRenderer = $menuRenderer;
}
public function index()
......@@ -57,7 +67,9 @@ class MenuController extends AdminBaseController
{
$menuItems = $this->menuItem->roots();
return View::make('menu::admin.menus.edit', compact('menu', 'menuItems'));
$menuStructure = $this->menuRenderer->renderForMenu($menu->id, $menuItems);
return View::make('menu::admin.menus.edit', compact('menu', 'menuStructure'));
}
public function update(Menu $menu, UpdateMenuRequest $request)
......
......@@ -27,36 +27,11 @@
</a>
</div>
</div>
<?php if (!$menuItems->isEmpty()): ?>
<div class="box box-info" style="overflow: hidden;">
<div class="box-body">
<div class="dd">
<ol class="dd-list">
<?php foreach($menuItems as $menuItem): ?>
<li class="dd-item" data-id="{{ $menuItem->id }}">
<a href="{{ URL::route('dashboard.menuitem.edit', [$menu->id, $menuItem->id]) }}" class="btn btn-sm btn-info" style="float:left; margin-right: 15px;">Edit</a>
<div class="dd-handle">
{{ $menuItem->title }}
</div>
<?php if (!$menuItem->children()->get()->isEmpty()): ?>
<ol class="dd-list">
<?php foreach($menuItem->children()->get() as $leaf): ?>
<li class="dd-item" data-id="{{ $leaf->id }}">
<a href="{{ URL::route('dashboard.menuitem.edit', [$menu->id, $leaf->id]) }}" class="btn btn-sm btn-info" style="float:left; margin-right: 15px;">Edit</a>
<div class="dd-handle">
{{ $leaf->title }}
</div>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ol>
</div>
</div>
<div class="box box-info" style="overflow: hidden;">
<div class="box-body">
{!! $menuStructure !!}
</div>
<?php endif; ?>
</div>
</div>
<div class="col-md-6">
<div class="box box-info">
......
<?php namespace Modules\Menu\Services;
use Illuminate\Support\Facades\URL;
class MenuRenderer
{
private $startTag = '<div class="dd">';
private $endTag = '</div>';
private $menu = '';
public function renderForMenu($menuId, $menuItems)
{
$this->menu .= $this->startTag;
$this->renderItemsForMenu($menuId, $menuItems);
$this->menu .= $this->endTag;
return $this->menu;
}
private function renderItemsForMenu($menuId, $items)
{
$this->menu .= '<ol class="dd-list">';
foreach ($items as $item) {
$this->menu .= "<li class=\"dd-item\" data-id=\"{$item->id}\">";
$this->menu .= '<a class="btn btn-sm btn-info"
style="float:left; margin-right: 15px;"
href="' . URL::route('dashboard.menuitem.edit', [$menuId, $item->id]) . '">Edit</a>';
$this->menu .= "<div class=\"dd-handle\">{$item->title}</div>";
if (!$item->children()->get()->isEmpty()) {
$this->renderItemsForMenu($menuId, $item->children()->get());
}
$this->menu .= '</li>';
}
$this->menu .= '</ol>';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment