Penambahan Menu Service Provider

This commit is contained in:
Daeng Deni Mardaeni 2024-10-29 11:57:32 +07:00
parent 9ec32469ce
commit 58606ec9be
3 changed files with 163 additions and 99 deletions

View File

@ -0,0 +1,72 @@
<?php
namespace App\Providers;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
class MenuServiceProvider extends ServiceProvider
{
protected $menus = [];
public function boot()
{
$this->loadMenus();
$this->shareMenusWithViews();
}
protected function loadMenus()
{
$modulesPath = base_path('Modules');
$moduleDirs = File::directories($modulesPath);
foreach ($moduleDirs as $moduleDir) {
$moduleJsonPath = $moduleDir . '/module.json';
if (File::exists($moduleJsonPath)) {
$moduleConfig = json_decode(File::get($moduleJsonPath), true);
if (isset($moduleConfig['menu'])) {
$this->mergeMenus($moduleConfig['menu']);
}
}
}
}
protected function mergeMenus(array $menu)
{
foreach ($menu as $key => $items) {
if (!isset($this->menus[$key])) {
$this->menus[$key] = [];
}
foreach ($items as $newItem) {
$existingItemKey = $this->findExistingMenuItem($this->menus[$key], $newItem);
if ($existingItemKey !== null) {
// Merge submenus if the item already exists
$this->menus[$key][$existingItemKey]['sub'] = array_merge(
$this->menus[$key][$existingItemKey]['sub'] ?? [],
$newItem['sub'] ?? []
);
} else {
// Add new item if it doesn't exist
$this->menus[$key][] = $newItem;
}
}
}
}
protected function findExistingMenuItem(array $menuItems, array $newItem)
{
foreach ($menuItems as $index => $item) {
if ($item['title'] === $newItem['title'] || $item['path'] === $newItem['path']) {
return $index;
}
}
return null;
}
protected function shareMenusWithViews()
{
view()->share('menus', $this->menus);
}
}

View File

@ -2,4 +2,5 @@
return [ return [
App\Providers\AppServiceProvider::class, App\Providers\AppServiceProvider::class,
App\Providers\MenuServiceProvider::class,
]; ];

View File

@ -5,12 +5,12 @@
<div class="sidebar-header hidden lg:flex items-center relative justify-between px-3 lg:px-6 shrink-0" <div class="sidebar-header hidden lg:flex items-center relative justify-between px-3 lg:px-6 shrink-0"
id="sidebar_header"> id="sidebar_header">
<a class="dark:hidden" href="{{ url('/') }}"> <a class="dark:hidden" href="{{ url('/') }}">
<img class="default-logo min-h-[22px] max-w-none" style="height: 53.55px;" src="assets/media/app/logo-agi-croped.png"/> <img class="default-logo min-h-[22px] max-w-none" style="height: 53.55px;" src="assets/media/app/logo-agi-croped.png" />
<img class="small-logo min-h-[22px] max-w-none" style="height: 50px" src="assets/media/app/logo-agi-mini.png"/> <img class="small-logo min-h-[22px] max-w-none" style="height: 50px" src="assets/media/app/logo-agi-mini.png" />
</a> </a>
<a class="light:hidden dark:flex" href="{{ url('/') }}"> <a class="light:hidden dark:flex" href="{{ url('/') }}">
<img class="default-logo min-h-[22px] max-w-none" style="height: 53.55px;" src="assets/media/app/logo-agi-croped.png"/> <img class="default-logo min-h-[22px] max-w-none" style="height: 53.55px;" src="assets/media/app/logo-agi-croped.png" />
<img class="small-logo min-h-[22px] max-w-none" style="height: 50px" src="assets/media/app/logo-agi-mini.png"/> <img class="small-logo min-h-[22px] max-w-none" style="height: 50px" src="assets/media/app/logo-agi-mini.png" />
</a> </a>
<button <button
class="btn btn-icon btn-icon-md size-[30px] rounded-lg border border-gray-200 dark:border-gray-300 bg-light text-gray-500 hover:text-gray-700 toggle absolute left-full top-2/4 -translate-x-2/4 -translate-y-2/4" class="btn btn-icon btn-icon-md size-[30px] rounded-lg border border-gray-200 dark:border-gray-300 bg-light text-gray-500 hover:text-gray-700 toggle absolute left-full top-2/4 -translate-x-2/4 -translate-y-2/4"
@ -29,13 +29,13 @@
<div <div
class="menu-link flex items-center grow cursor-pointer border border-transparent gap-[10px] pl-[10px] pr-[10px] py-[6px]" class="menu-link flex items-center grow cursor-pointer border border-transparent gap-[10px] pl-[10px] pr-[10px] py-[6px]"
tabindex="0"> tabindex="0">
<span class="menu-icon items-start text-gray-500 dark:text-gray-400 w-[20px]"> <span class="menu-icon items-start text-gray-500 dark:text-gray-400 w-[20px]">
<i class="ki-filled ki-element-11 text-lg"></i> <i class="ki-filled ki-element-11 text-lg"></i>
</span> </span>
<span <span
class="menu-title text-sm font-semibold text-gray-700 menu-item-active:text-primary menu-link-hover:!text-primary"> class="menu-title text-sm font-semibold text-gray-700 menu-item-active:text-primary menu-link-hover:!text-primary">
Dashboards Dashboards
</span> </span>
</div> </div>
</div> </div>
@php @php
@ -43,100 +43,91 @@
$headingMain = 0; $headingMain = 0;
$headingMaster = 0; $headingMaster = 0;
@endphp @endphp
@foreach($module as $row => $key) @php $menus = json_decode(json_encode($menus)); @endphp
@if($key) @foreach($menus as $key => $value)
@if(file_exists(dirname(__FILE__, 4) . '/Modules/'.$row.'/module.json')) @foreach($value as $menu)
@php @if($key=='main')
$module = json_decode(file_get_contents(dirname(__FILE__, 4) . '/Modules/'.$row.'/module.json')); @if($headingMain == 0)
$menus = $module->menu; <div class="menu-item pt-2.25 pb-px">
@endphp <span class="menu-heading uppercase text-2sm font-semibold text-gray-500 pl-[10px] pr-[10px]">
@foreach($menus as $key => $value) Apps
@foreach($value as $menu) </span>
@if($key=='main') </div>
@if($headingMain == 0) @php $headingMain = 1; @endphp
<div class="menu-item pt-2.25 pb-px"> @endif
<span class="menu-heading uppercase text-2sm font-semibold text-gray-500 pl-[10px] pr-[10px]"> @elseif($key=='master')
Apps @if($headingMaster == 0)
</span> <div class="menu-item pt-2.25 pb-px">
</div> <span class="menu-heading uppercase text-2sm font-semibold text-gray-500 pl-[10px] pr-[10px]">
@php $headingMain = 1; @endphp Master Data
@endif </span>
@elseif($key=='master') </div>
@if($headingMaster == 0) @php $headingMaster = 1; @endphp
<div class="menu-item pt-2.25 pb-px"> @endif
<span class="menu-heading uppercase text-2sm font-semibold text-gray-500 pl-[10px] pr-[10px]"> @elseif($key=='system')
Master Data @if($headingSystem == 0)
</span> <div class="menu-item pt-2.25 pb-px">
</div> <span class="menu-heading uppercase text-2sm font-semibold text-gray-500 pl-[10px] pr-[10px]">
@php $headingMaster = 1; @endphp Systems
@endif </span>
@elseif($key=='system') </div>
@if($headingSystem == 0) @php $headingSystem = 1; @endphp
<div class="menu-item pt-2.25 pb-px"> @endif
<span class="menu-heading uppercase text-2sm font-semibold text-gray-500 pl-[10px] pr-[10px]"> @endif
Systems @if(auth()->user()->hasRole($menu->roles))
</span> @if(isset($menu->sub))
</div> <div class="menu-item {{ request()->routeIs($menu->path) || request()->routeIs($menu->path.'.*') ? 'show' : '' }}" data-menu-item-toggle="accordion" data-menu-item-trigger="click">
@php $headingSystem = 1; @endphp <div
@endif class="menu-link flex items-center grow cursor-pointer border border-transparent gap-[10px] pl-[10px] pr-[10px] py-[6px]"
@endif tabindex="0">
@if(auth()->user()->hasRole($menu->roles)) <span class="menu-icon items-start text-gray-500 dark:text-gray-400 w-[20px]">
@if(isset($menu->sub)) <i class="{{ $menu->icon ?? 'ki-filled ki-element-11 text-lg' }}"></i>
<div class="menu-item {{ request()->routeIs($menu->path) || request()->routeIs($menu->path.'.*') ? 'show' : '' }}" data-menu-item-toggle="accordion" data-menu-item-trigger="click"> </span>
<div <span class="menu-title text-sm font-semibold text-gray-700 menu-item-active:text-primary menu-link-hover:!text-primary">
class="menu-link flex items-center grow cursor-pointer border border-transparent gap-[10px] pl-[10px] pr-[10px] py-[6px]" {{ $menu->title }}
tabindex="0"> </span>
<span class="menu-icon items-start text-gray-500 dark:text-gray-400 w-[20px]"> <span class="menu-arrow text-gray-400 w-[20px] shrink-0 justify-end ml-1 mr-[-10px]">
<i class="{{ $menu->icon ?? 'ki-filled ki-element-11 text-lg' }}"></i> <i class="ki-filled ki-plus text-2xs menu-item-show:hidden">
</span> </i>
<span class="menu-title text-sm font-semibold text-gray-700 menu-item-active:text-primary menu-link-hover:!text-primary"> <i class="ki-filled ki-minus text-2xs hidden menu-item-show:inline-flex">
{{ $menu->title }} </i>
</span> </span>
<span class="menu-arrow text-gray-400 w-[20px] shrink-0 justify-end ml-1 mr-[-10px]"> </div>
<i class="ki-filled ki-plus text-2xs menu-item-show:hidden"> @if(is_array($menu->sub))
</i> <div class="menu-accordion gap-0.5 pl-[10px] relative before:absolute before:left-[20px] before:top-0 before:bottom-0 before:border-l before:border-gray-200">
<i class="ki-filled ki-minus text-2xs hidden menu-item-show:inline-flex"> @foreach($menu->sub as $sub)
</i> @if(auth()->user()->hasRole($sub->roles))
</span> <div class="menu-item {{ request()->routeIs($sub->path.'.*') && in_array(request()->route()->getName(), [$sub->path.'.index', $sub->path.'.create', $sub->path.'.edit']) ? 'active' : '' }}">
</div> <a class="menu-link gap-[14px] pl-[10px] pr-[10px] py-[8px] border border-transparent items-center grow menu-item-active:bg-secondary-active dark:menu-item-active:bg-coal-300 dark:menu-item-active:border-gray-100 menu-item-active:rounded-lg hover:bg-secondary-active dark:hover:bg-coal-300 dark:hover:border-gray-100 hover:rounded-lg"
@if(is_array($menu->sub)) href="{{ $sub->path ? route($sub->path.'.index') : '' }}" tabindex="0">
<div class="menu-accordion gap-0.5 pl-[10px] relative before:absolute before:left-[20px] before:top-0 before:bottom-0 before:border-l before:border-gray-200"> <span
@foreach($menu->sub as $sub) class="menu-bullet flex w-[6px] relative before:absolute before:top-0 before:size-[6px] before:rounded-full before:-translate-x-1/2 before:-translate-y-1/2 menu-item-active:before:bg-primary menu-item-hover:before:bg-primary">
@if(auth()->user()->hasRole($sub->roles)) </span>
<div class="menu-item {{ request()->routeIs($sub->path.'.*') && in_array(request()->route()->getName(), [$sub->path.'.index', $sub->path.'.create', $sub->path.'.edit']) ? 'active' : '' }}"> <span
<a class="menu-link gap-[14px] pl-[10px] pr-[10px] py-[8px] border border-transparent items-center grow menu-item-active:bg-secondary-active dark:menu-item-active:bg-coal-300 dark:menu-item-active:border-gray-100 menu-item-active:rounded-lg hover:bg-secondary-active dark:hover:bg-coal-300 dark:hover:border-gray-100 hover:rounded-lg" class="menu-title text-2sm font-medium text-gray-700 menu-item-active:text-primary menu-item-active:font-semibold menu-link-hover:!text-primary">
href="{{ $sub->path ? route($sub->path.'.index') : '' }}" tabindex="0"> {{ $sub->title }}
<span </span>
class="menu-bullet flex w-[6px] relative before:absolute before:top-0 before:size-[6px] before:rounded-full before:-translate-x-1/2 before:-translate-y-1/2 menu-item-active:before:bg-primary menu-item-hover:before:bg-primary"> </a>
</span>
<span
class="menu-title text-2sm font-medium text-gray-700 menu-item-active:text-primary menu-item-active:font-semibold menu-link-hover:!text-primary">
{{ $sub->title }}
</span>
</a>
</div>
@endif
@endforeach
</div> </div>
@endif @endif
</div> @endforeach
@else </div>
<div class="menu-item {{ request()->routeIs($menu->path.'.*') ? 'active' : '' }}">
<a class="menu-link flex items-center grow cursor-pointer border border-transparent gap-[10px] pl-[10px] pr-[10px] py-[6px]" href="{{ $menu->path ? route($menu->path.'.index') : '' }}">
<span class="menu-icon items-start text-gray-500 dark:text-gray-400 w-[20px]">
<i class="{{ $menu->icon ?? 'ki-filled ki-element-11 text-lg' }}"></i>
</span>
<span class="menu-title text-sm font-semibold text-gray-700 menu-item-active:text-primary menu-link-hover:!text-primary">
{{ $menu->title }}
</span>
</a>
</div>
@endif
@endif @endif
@endforeach </div>
@endforeach @else
<div class="menu-item {{ request()->routeIs($menu->path.'.*') ? 'active' : '' }}">
<a class="menu-link flex items-center grow cursor-pointer border border-transparent gap-[10px] pl-[10px] pr-[10px] py-[6px]" href="{{ $menu->path ? route($menu->path.'.index') : '' }}">
<span class="menu-icon items-start text-gray-500 dark:text-gray-400 w-[20px]">
<i class="{{ $menu->icon ?? 'ki-filled ki-element-11 text-lg' }}"></i>
</span>
<span class="menu-title text-sm font-semibold text-gray-700 menu-item-active:text-primary menu-link-hover:!text-primary">
{{ $menu->title }}
</span>
</a>
</div>
@endif
@endif @endif
@endif @endforeach
@endforeach @endforeach
</div> </div>
</div> </div>