130 lines
5.3 KiB
PHP
130 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Presenters\Admin;
|
|
|
|
use App\Presenters\Html\HtmlPresenter;
|
|
|
|
class MainMenuItemPresenter
|
|
{
|
|
private $htmlPresenter;
|
|
|
|
public function __construct(HtmlPresenter $htmlPresenter)
|
|
{
|
|
$this->htmlPresenter = $htmlPresenter;
|
|
}
|
|
|
|
/**
|
|
* 檢查選單項目權限
|
|
*
|
|
* @param array $item
|
|
* @return mixed
|
|
*/
|
|
private function checkMenuItemPermission($item)
|
|
{
|
|
return !empty($item['userHasPermission']) && $item['userHasPermission'];
|
|
}
|
|
|
|
/**
|
|
* Check if any child has permission
|
|
*
|
|
* @param $item
|
|
* @return bool
|
|
*/
|
|
private function checkAnyChildPermission($item)
|
|
{
|
|
if(isset($item['children'])) {
|
|
$anyChildHasPermission = false;
|
|
foreach ($item['children'] as $childMenu) {
|
|
$anyChildHasPermission = $anyChildHasPermission || $childMenu['userHasPermission'];
|
|
}
|
|
return $anyChildHasPermission;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 回傳選單項目Html
|
|
*
|
|
* return string
|
|
*/
|
|
public function render()
|
|
{
|
|
$menu = app('AdminMenu')->getMenu();
|
|
|
|
$html = '';
|
|
$htmlPresenter = $this->htmlPresenter;
|
|
|
|
foreach ($menu as $parentKey => $item) {
|
|
switch($item['type']) {
|
|
case 'title':
|
|
$html .= $htmlPresenter->li([
|
|
'class' => 'nav-title',
|
|
'html' => trans('adminMenu.titles.' . $item['name'])
|
|
]);
|
|
break;
|
|
case 'item':
|
|
if($this->checkMenuItemPermission($item) && $this->checkAnyChildPermission($item)) {
|
|
$hasChildren = !empty($item['children']);
|
|
$html .= $htmlPresenter->li([
|
|
'class' => [
|
|
'nav-item',
|
|
$hasChildren ? 'nav-dropdown has-children' : '',
|
|
"slug-{$item['slug']}"
|
|
],
|
|
'html' => [
|
|
$htmlPresenter->a([
|
|
'class' => [
|
|
'nav-link',
|
|
$hasChildren ? 'nav-dropdown-toggle' : '',
|
|
],
|
|
'href' => $item['link'],
|
|
'html' => [
|
|
!empty($item['iconClass']) ? $htmlPresenter->i(['class' => $item['iconClass']]) : '',
|
|
$item['name'],
|
|
!empty($item['badge']) ? $htmlPresenter->bs()->badge($item['badge']['label'], $item['badge']['type'], $item['badge']['isPill']) : ''
|
|
],
|
|
]),
|
|
$hasChildren ? $htmlPresenter->ul([
|
|
'class' => 'nav-dropdown-items submenu',
|
|
'html' => function() use ($hasChildren, $item, $htmlPresenter) {
|
|
$html = '';
|
|
if($hasChildren) {
|
|
foreach ($item['children'] as $childItem) {
|
|
if($this->checkMenuItemPermission($childItem)) {
|
|
$html .= $htmlPresenter->li([
|
|
'class' => [
|
|
'nav-item',
|
|
"slug-{$childItem['slug']}",
|
|
],
|
|
'html' => $htmlPresenter->a([
|
|
'class' => 'nav-link',
|
|
'href' => $childItem['link'],
|
|
'html' => [
|
|
!empty($childItem['iconClass']) ? $htmlPresenter->i(['class' => $childItem['iconClass']]) : '',
|
|
$childItem['name'],
|
|
!empty($childItem['badge']) ? $htmlPresenter->bs()->badge($childItem['badge']['label'], $childItem['badge']['type'], $childItem['badge']['isPill']) : ''
|
|
]
|
|
])
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
return $html;
|
|
}
|
|
]) : ''
|
|
]
|
|
]);
|
|
}
|
|
break;
|
|
case 'divider':
|
|
$html .= $htmlPresenter->li([
|
|
'class' => 'nav-divider'
|
|
]);
|
|
break;
|
|
}
|
|
}
|
|
echo $html;
|
|
}
|
|
}
|