fix redirect back with params menu id
This commit is contained in:
@@ -6,7 +6,6 @@ use App\Http\Requests\MenuRequest;
|
|||||||
use App\Models\Menu;
|
use App\Models\Menu;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Auth;
|
|
||||||
|
|
||||||
class MenusController extends Controller
|
class MenusController extends Controller
|
||||||
{
|
{
|
||||||
@@ -15,36 +14,33 @@ class MenusController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$menuId = $request->query('menu_id');
|
$menuId = (int) $request->query('menu_id', 0);
|
||||||
$user = Auth::user();
|
$permissions = $this->permissions[$menuId] ?? []; // Avoid undefined index error
|
||||||
$userId = $user->id;
|
|
||||||
|
|
||||||
// Ambil role_id yang dimiliki user
|
$creator = $permissions['allow_create'] ?? 0;
|
||||||
$roleIds = DB::table('user_role')
|
$updater = $permissions['allow_update'] ?? 0;
|
||||||
->where('user_id', $userId)
|
$destroyer = $permissions['allow_destroy'] ?? 0;
|
||||||
->pluck('role_id');
|
|
||||||
|
|
||||||
// Ambil data akses berdasarkan role_id dan menu_id
|
return view('menus.index', compact('creator', 'updater', 'destroyer', 'menuId'));
|
||||||
$roleAccess = DB::table('role_menu')
|
|
||||||
->whereIn('role_id', $roleIds)
|
|
||||||
->where('menu_id', $menuId)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
// Pastikan roleAccess tidak null sebelum mengakses properti
|
|
||||||
$creator = $roleAccess->allow_create ?? 0;
|
|
||||||
$updater = $roleAccess->allow_update ?? 0;
|
|
||||||
$destroyer = $roleAccess->allow_destroy ?? 0;
|
|
||||||
|
|
||||||
return view('menus.index', compact('creator', 'updater', 'destroyer'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show the form for creating a new resource.
|
* Show the form for creating a new resource.
|
||||||
*/
|
*/
|
||||||
public function create()
|
public function create(Request $request)
|
||||||
{
|
{
|
||||||
$parent_menus = Menu::whereNull('parent_id')->get();
|
$menuId = $request->query('menu_id'); // Get menu_id from request
|
||||||
return view("menus.create", compact('parent_menus'));
|
$menu = Menu::with('children')->find($menuId); // Find the menu
|
||||||
|
|
||||||
|
// Get IDs of all child menus to exclude
|
||||||
|
$excludedIds = $menu ? $this->getChildMenuIds($menu) : [$menuId];
|
||||||
|
|
||||||
|
// Fetch only menus that have children and are not in the excluded list
|
||||||
|
$parent_menus = Menu::whereHas('children')
|
||||||
|
->whereNotIn('id', $excludedIds)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return view("menus.create", compact('parent_menus', 'menuId'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -77,11 +73,16 @@ class MenusController extends Controller
|
|||||||
/**
|
/**
|
||||||
* Show the form for editing the specified resource.
|
* Show the form for editing the specified resource.
|
||||||
*/
|
*/
|
||||||
public function edit(string $id)
|
public function edit(string $id, Request $request)
|
||||||
{
|
{
|
||||||
$menu = Menu::findOrFail($id);
|
$menuId = $request->query('menu_id');
|
||||||
$parent_menus = Menu::whereNull('parent_id')->where('id','!=',$id)->get();
|
$menu = Menu::with('children')->find($id);
|
||||||
return view("menus.edit", compact('menu','parent_menus'));
|
$excludedIds = $menu ? $this->getChildMenuIds($menu) : [$id];
|
||||||
|
|
||||||
|
$parent_menus = Menu::whereHas('children')
|
||||||
|
->whereNotIn('id', $excludedIds)
|
||||||
|
->get();
|
||||||
|
return view("menus.edit", compact('menu','parent_menus', 'menuId'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -131,4 +132,15 @@ class MenusController extends Controller
|
|||||||
$child->delete();
|
$child->delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getChildMenuIds($menu)
|
||||||
|
{
|
||||||
|
$ids = [$menu->id]; // Start with current menu ID
|
||||||
|
|
||||||
|
foreach ($menu->children as $child) {
|
||||||
|
$ids = array_merge($ids, $this->getChildMenuIds($child)); // Recursively fetch children
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ids;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ class CreateMenu {
|
|||||||
initCreateMenu() {
|
initCreateMenu() {
|
||||||
const toastNotification = document.getElementById("toastNotification");
|
const toastNotification = document.getElementById("toastNotification");
|
||||||
const toast = new bootstrap.Toast(toastNotification);
|
const toast = new bootstrap.Toast(toastNotification);
|
||||||
|
let menuId = document.getElementById("menuId").value;
|
||||||
document
|
document
|
||||||
.getElementById("btnCreateMenus")
|
.getElementById("btnCreateMenus")
|
||||||
.addEventListener("click", async function () {
|
.addEventListener("click", async function () {
|
||||||
@@ -41,7 +42,7 @@ class CreateMenu {
|
|||||||
result.message;
|
result.message;
|
||||||
toast.show();
|
toast.show();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.href = "/menus";
|
window.location.href = `/menus?menu_id=${menuId}`;
|
||||||
}, 2000);
|
}, 2000);
|
||||||
} else {
|
} else {
|
||||||
let error = await response.json();
|
let error = await response.json();
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ class Menus {
|
|||||||
tableContainer.innerHTML = "";
|
tableContainer.innerHTML = "";
|
||||||
let canUpdate = tableContainer.getAttribute("data-updater") === "1";
|
let canUpdate = tableContainer.getAttribute("data-updater") === "1";
|
||||||
let canDelete = tableContainer.getAttribute("data-destroyer") === "1";
|
let canDelete = tableContainer.getAttribute("data-destroyer") === "1";
|
||||||
|
let menuId = tableContainer.getAttribute("data-menuId");
|
||||||
|
|
||||||
this.table = new Grid({
|
this.table = new Grid({
|
||||||
columns: [
|
columns: [
|
||||||
@@ -47,7 +48,7 @@ class Menus {
|
|||||||
|
|
||||||
if (canUpdate) {
|
if (canUpdate) {
|
||||||
buttons += `
|
buttons += `
|
||||||
<a href="/menus/${cell}/edit" class="btn btn-yellow btn-sm d-inline-flex align-items-center justify-content-center">
|
<a href="/menus/${cell}/edit?menu_id=${menuId}" class="btn btn-yellow btn-sm d-inline-flex align-items-center justify-content-center">
|
||||||
<i class='bx bx-edit'></i>
|
<i class='bx bx-edit'></i>
|
||||||
</a>
|
</a>
|
||||||
`;
|
`;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ class UpdateMenu {
|
|||||||
initUpdateMenu() {
|
initUpdateMenu() {
|
||||||
const toastNotification = document.getElementById("toastNotification");
|
const toastNotification = document.getElementById("toastNotification");
|
||||||
const toast = new bootstrap.Toast(toastNotification);
|
const toast = new bootstrap.Toast(toastNotification);
|
||||||
|
let menuId = document.getElementById("menuId").value;
|
||||||
document
|
document
|
||||||
.getElementById("btnUpdateMenus")
|
.getElementById("btnUpdateMenus")
|
||||||
.addEventListener("click", async function () {
|
.addEventListener("click", async function () {
|
||||||
@@ -41,7 +42,7 @@ class UpdateMenu {
|
|||||||
result.message;
|
result.message;
|
||||||
toast.show();
|
toast.show();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.href = "/menus";
|
window.location.href = `/menus?menu_id=${menuId}`;
|
||||||
}, 2000);
|
}, 2000);
|
||||||
} else {
|
} else {
|
||||||
let error = await response.json();
|
let error = await response.json();
|
||||||
|
|||||||
@@ -5,11 +5,12 @@
|
|||||||
@include('layouts.partials/page-title', ['title' => 'Settings', 'subtitle' => 'Menu'])
|
@include('layouts.partials/page-title', ['title' => 'Settings', 'subtitle' => 'Menu'])
|
||||||
|
|
||||||
<x-toast-notification />
|
<x-toast-notification />
|
||||||
|
<input type="hidden" id="menuId" value="{{ $menuId ?? 0 }}">
|
||||||
<div class="row d-flex justify-content-center">
|
<div class="row d-flex justify-content-center">
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header d-flex justify-content-end">
|
<div class="card-header d-flex justify-content-end">
|
||||||
<a href="{{ route('menus.index') }}" class="btn btn-sm btn-secondary">Back</a>
|
<a href="{{ route('menus.index', ['menu_id' => $menuId ?? 0]) }}" class="btn btn-sm btn-secondary">Back</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form id="formCreateMenus" action="{{route("api.menus.store")}}" method="post">
|
<form id="formCreateMenus" action="{{route("api.menus.store")}}" method="post">
|
||||||
|
|||||||
@@ -9,11 +9,12 @@
|
|||||||
@include('layouts.partials/page-title', ['title' => 'Settings', 'subtitle' => 'Menu'])
|
@include('layouts.partials/page-title', ['title' => 'Settings', 'subtitle' => 'Menu'])
|
||||||
|
|
||||||
<x-toast-notification/>
|
<x-toast-notification/>
|
||||||
|
<input type="hidden" id="menuId" value="{{ $menuId ?? 0 }}">
|
||||||
<div class="row d-flex justify-content-center">
|
<div class="row d-flex justify-content-center">
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header d-flex justify-content-end">
|
<div class="card-header d-flex justify-content-end">
|
||||||
<a href="{{ route('menus.index') }}" class="btn btn-sm btn-secondary">Back</a>
|
<a href="{{ route('menus.index', ['menu_id' => $menuId]) }}" class="btn btn-sm btn-secondary">Back</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form id="formUpdateMenus" action="{{route("api.menus.update", $menu->id)}}" method="post">
|
<form id="formUpdateMenus" action="{{route("api.menus.update", $menu->id)}}" method="post">
|
||||||
|
|||||||
@@ -17,13 +17,14 @@
|
|||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<div class="d-flex flex-wrap justify-content-end align-items-center mb-2">
|
<div class="d-flex flex-wrap justify-content-end align-items-center mb-2">
|
||||||
@if ($creator)
|
@if ($creator)
|
||||||
<a href="{{ route('menus.create')}}" class="btn btn-success btn-sm d-block d-sm-inline w-auto">Create</a>
|
<a href="{{ route('menus.create', ['menu_id' => $menuId])}}" class="btn btn-success btn-sm d-block d-sm-inline w-auto">Create</a>
|
||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<div id="table-menus"
|
<div id="table-menus"
|
||||||
data-updater="{{ $updater }}"
|
data-updater="{{ $updater }}"
|
||||||
data-destroyer="{{ $destroyer }}">
|
data-destroyer="{{ $destroyer }}"
|
||||||
|
data-menuId="{{ $menuId }}">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user