Files
aso/Modules/Internal/Http/Controllers/Api/NavigationController.php
Server D3 Linksehat 9634b12f11 update
2025-08-19 09:39:37 +07:00

128 lines
3.6 KiB
PHP
Executable File

<?php
namespace Modules\Internal\Http\Controllers\Api;
use App\Models\Navigations;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class NavigationController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
// Ambil semua navigasi dari tabel dan ubah menjadi array
// $navigations = Navigations::all()->toArray();
$navigations = Navigations::orderBy('urutan', 'asc')->get()->toArray();
$navigationMaster = [];
if ($navigations) {
// Buat array untuk menyimpan menu utama
foreach ($navigations as $navigation) {
if ($navigation['parent_id'] == 0) {
// Tambahkan menu utama ke $navigationMaster
$navigation['children'] = []; // Siapkan array untuk children
$navigationMaster[$navigation['id']] = $navigation;
}
}
// Tambahkan submenu ke menu utama yang sesuai
foreach ($navigations as $navigation) {
if ($navigation['parent_id'] != 0 && isset($navigationMaster[$navigation['parent_id']])) {
$navigationMaster[$navigation['parent_id']]['children'][] = $navigation;
}
}
}
// Ubah array menjadi list tanpa indeks id
$navigationMaster = array_values($navigationMaster);
// Transformasi data untuk sesuai dengan format yang diinginkan
$formattedNavigation = [
'items' => array_map(function ($navItem) {
return [
'title' => $navItem['title'],
'path' => $navItem['path'],
'children' => array_map(function ($child) {
return [
'title' => $child['title'],
'path' => $child['path'],
'icon' => $child['icon'], // Asumsikan Anda memiliki field 'icon' di tabel navigasi
'permission' => $child['permission'],
];
}, $navItem['children']),
'icon' => $navItem['icon'],
'permission' => $navItem['permission'],
];
}, $navigationMaster)
];
return response()->json($formattedNavigation);
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
return view('internal::create');
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
{
//
}
/**
* Show the specified resource.
* @param int $id
* @return Renderable
*/
public function show($id)
{
return view('internal::show');
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
return view('internal::edit');
}
/**
* Update the specified resource in storage.
* @param Request $request
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return Renderable
*/
public function destroy($id)
{
//
}
}