« Back to History
menu_builder.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php header('X-Frame-Options: SAMEORIGIN'); error_reporting(E_ALL); ini_set('display_errors',1); /* ================= AUTH ================= */ require __DIR__ . '/../erp/modules/auth/auth.php'; require_login(); $u = auth_user(); /** * STRICT DEVELOPER-ONLY ACCESS * strtolower() use kiya hai taaki 'developer' small ho ya capital, match ho jaye. * ID wali condition yahan se hata di gayi hai. */ if (strtolower($u['role'] ?? '') !== 'developer') { http_response_code(403); exit('403 — Forbidden: Developer access only.'); } $user_id = (int)$u['id']; $pdo = $GLOBALS['pdo'] ?? null; if(!$pdo){ require __DIR__ . '/../erp/core/db.php'; $pdo = $GLOBALS['pdo']; } /* ================= HELPERS ================= */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function gv($k,$d=null){ return isset($_GET[$k])? trim((string)$_GET[$k]) : $d; } function pv($k,$d=null){ return isset($_POST[$k])? trim((string)$_POST[$k]) : $d; } function i($v){ return (int)$v; } function redirect_keep(){ $sec = gv('section_id') ?? pv('section_id'); header("Location: menu_builder.php".($sec?"?section_id=$sec":'')); exit; } $act = gv('act',''); /* ================= ACTIONS ================= */ // Add Section if ($act==='add_section' && $_SERVER['REQUEST_METHOD']==='POST'){ $name=pv('name'); if($name){ $max=$pdo->query("SELECT COALESCE(MAX(sort_order),0) FROM nav_menu_sections WHERE company_id=0")->fetchColumn(); $next=((int)$max)+10; $pdo->prepare("INSERT INTO nav_menu_sections(company_id,name,sort_order,is_enabled,created_by) VALUES(0,?,?,1,?)")->execute([$name,$next,$user_id]); } redirect_keep(); } // Edit Section if ($act==='edit_section' && $_SERVER['REQUEST_METHOD']==='POST'){ $pdo->prepare("UPDATE nav_menu_sections SET name=?, is_enabled=? WHERE id=? AND company_id=0") ->execute([pv('name'), pv('is_enabled')?1:0, i(pv('id'))]); redirect_keep(); } // Delete Section if ($act==='del_section'){ $pdo->prepare("DELETE FROM nav_menu_sections WHERE id=? AND company_id=0") ->execute([i(gv('id'))]); redirect_keep(); } // Add Item if ($act==='add_item' && $_SERVER['REQUEST_METHOD']==='POST'){ $sec=i(pv('section_id')); $parent_id = pv('parent_id') ? i(pv('parent_id')) : null; if($sec && pv('title') && pv('url')){ $st_max = $pdo->prepare("SELECT COALESCE(MAX(sort_order),0) FROM nav_menu_items WHERE section_id=? AND company_id=0"); $st_max->execute([$sec]); $next=((int)$st_max->fetchColumn())+10; $pdo->prepare("INSERT INTO nav_menu_items(company_id,section_id,parent_id,title,url,slug,sort_order,is_enabled,created_by) VALUES(0,?,?,?,?,?,?,1,?)") ->execute([$sec,$parent_id,pv('title'),pv('url'),pv('slug'),$next,$user_id]); } redirect_keep(); } // Edit Item if ($act==='edit_item' && $_SERVER['REQUEST_METHOD']==='POST'){ $parent_id = pv('parent_id') ? i(pv('parent_id')) : null; $pdo->prepare("UPDATE nav_menu_items SET title=?, url=?, slug=?, is_enabled=?, parent_id=? WHERE id=? AND company_id=0") ->execute([ pv('title'), pv('url'), pv('slug'), pv('is_enabled')?1:0, $parent_id, i(pv('id')) ]); redirect_keep(); } // Move Item if ($act==='move_item'){ $id=i(gv('id')); $dir=gv('dir'); $cur=$pdo->prepare("SELECT id,section_id,sort_order FROM nav_menu_items WHERE id=? AND company_id=0"); $cur->execute([$id]); $cur=$cur->fetch(PDO::FETCH_ASSOC); if($cur){ if($dir==='up'){ $sib=$pdo->prepare("SELECT id,sort_order FROM nav_menu_items WHERE section_id=? AND company_id=0 AND sort_order < ? ORDER BY sort_order DESC LIMIT 1"); }else{ $sib=$pdo->prepare("SELECT id,sort_order FROM nav_menu_items WHERE section_id=? AND company_id=0 AND sort_order > ? ORDER BY sort_order ASC LIMIT 1"); } $sib->execute([$cur['section_id'],$cur['sort_order']]); if($sib=$sib->fetch(PDO::FETCH_ASSOC)){ $pdo->beginTransaction(); $pdo->prepare("UPDATE nav_menu_items SET sort_order=? WHERE id=?")->execute([$sib['sort_order'],$cur['id']]); $pdo->prepare("UPDATE nav_menu_items SET sort_order=? WHERE id=?")->execute([$cur['sort_order'],$sib['id']]); $pdo->commit(); } } redirect_keep(); } // Delete Item if ($act==='del_item'){ $pdo->prepare("DELETE FROM nav_menu_items WHERE id=? AND company_id=0") ->execute([i(gv('id'))]); redirect_keep(); } /* ================= LOAD ================= */ $sections=$pdo->query("SELECT * FROM nav_menu_sections WHERE company_id=0 ORDER BY sort_order,id")->fetchAll(PDO::FETCH_ASSOC); $items=[]; foreach($sections as $s){ $st=$pdo->prepare("SELECT * FROM nav_menu_items WHERE section_id=? AND company_id=0 ORDER BY sort_order,id"); $st->execute([$s['id']]); $items[$s['id']]=$st->fetchAll(PDO::FETCH_ASSOC); } $selected=i(gv('section_id',0)); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Global Menu Builder</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"> <style> .table-submenu { background-color: #f8f9fa; } .is-child { padding-left: 20px !important; color: #666; font-style: italic; } </style> </head> <body class="bg-light"> <div class="container-fluid mt-4" style="max-width: 1200px;"> <div class="card shadow-sm mb-4 border-0"> <div class="card-body bg-dark text-white rounded"> <div class="d-flex justify-content-between align-items-center gap-3 flex-wrap"> <div> <h4 class="mb-0">Developer Portal: Global Menu Builder</h4> <small class="opacity-75">Logged in: <?= h($u['name']) ?> (ID: 1)</small> </div> <div> <a href="dashboard.php" class="btn btn-sm btn-light">Back to Dashboard</a> </div> </div> </div> </div> <!-- Section Filter & Add Section Form --> <div class="row g-3 mb-4"> <div class="col-md-6"> <div class="card shadow-sm h-100 border-0"> <div class="card-body"> <label class="form-label fw-bold">Jump to Section</label> <select name="section_id" class="form-select" onchange="location.href='?section_id='+this.value"> <option value="0" <?=$selected===0?'selected':''?>>Show All Sections</option> <?php foreach($sections as $s): ?> <option value="<?=$s['id']?>" <?=$selected===$s['id']?'selected':''?>><?=h($s['name'])?></option> <?php endforeach; ?> </select> </div> </div> </div> <div class="col-md-6"> <div class="card shadow-sm h-100 border-0"> <div class="card-body"> <form method="post" action="?act=add_section"> <label class="form-label fw-bold">Add New Global Section</label> <div class="input-group"> <input type="text" name="name" class="form-control" placeholder="e.g., Inventory, Reports" required> <button class="btn btn-primary">Create</button> </div> </form> </div> </div> </div> </div> <?php foreach($sections as $sec): if($selected && $selected!=$sec['id']) continue; $sec_items = $items[$sec['id']]; $parents = array_filter($sec_items, fn($m) => !$m['parent_id']); ?> <div class="card shadow-sm mb-5 border-0"> <div class="card-header bg-white py-3 border-bottom-0"> <form method="post" action="?act=edit_section" class="row g-2 align-items-center"> <input type="hidden" name="id" value="<?=$sec['id']?>"> <input type="hidden" name="section_id" value="<?=$sec['id']?>"> <div class="col-auto"> <input type="text" name="name" value="<?=h($sec['name'])?>" class="form-control fw-bold border-0 shadow-none bg-light" style="font-size:1.2rem;" required> </div> <div class="col-auto ms-3"> <div class="form-check form-switch"> <input type="checkbox" name="is_enabled" value="1" class="form-check-input" <?=$sec['is_enabled']?'checked':''?>> <label class="form-check-label small text-muted">Enabled</label> </div> </div> <div class="col-auto ms-auto"> <button class="btn btn-sm btn-success px-3">Update Title</button> <a href="?act=del_section&id=<?=$sec['id']?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete whole section?')">Delete Section</a> </div> </form> </div> <div class="table-responsive"> <table class="table table-hover align-middle mb-0"> <thead class="table-light small text-uppercase text-muted"> <tr> <th style="width:60px">ID</th> <th>Title & Hierarchy</th> <th>URL Path</th> <th>Slug</th> <th style="width:80px">Status</th> <th style="width:220px" class="text-end pe-3">Actions</th> </tr> </thead> <tbody> <?php foreach($parents as $p): $children = array_filter($sec_items, fn($m) => (int)$m['parent_id'] === (int)$p['id']); $ordered = array_merge([$p], $children); foreach($ordered as $it): $is_child = !empty($it['parent_id']); ?> <tr class="<?=$is_child?'table-submenu':''?>"> <form method="post" action="?act=edit_item"> <td class="text-muted small"><?=$it['id']?></td> <td> <div class="input-group input-group-sm"> <?php if($is_child): ?><span class="input-group-text bg-white border-0 text-muted">↳</span><?php endif; ?> <input type="text" name="title" value="<?=h($it['title'])?>" class="form-control <?=$is_child?'is-child':''?>" required> <select name="parent_id" class="form-select bg-light" style="max-width: 140px;"> <option value="">Main Item</option> <?php foreach($parents as $opt): if($opt['id'] == $it['id']) continue; ?> <option value="<?=$opt['id']?>" <?=$it['parent_id']==$opt['id']?'selected':''?>>Sub of: <?=h($opt['title'])?></option> <?php endforeach; ?> </select> </div> </td> <td><input type="text" name="url" value="<?=h($it['url'])?>" class="form-control form-control-sm border-0 bg-light" required></td> <td><input type="text" name="slug" value="<?=h($it['slug'])?>" class="form-control form-control-sm border-0 bg-light"></td> <td class="text-center"> <input type="checkbox" name="is_enabled" value="1" class="form-check-input" <?=$it['is_enabled']?'checked':''?>> </td> <td class="text-end pe-3"> <input type="hidden" name="id" value="<?=$it['id']?>"> <input type="hidden" name="section_id" value="<?=$sec['id']?>"> <div class="btn-group btn-group-sm shadow-sm"> <button class="btn btn-primary px-3">Save</button> <a href="?act=move_item&id=<?=$it['id']?>&dir=up§ion_id=<?=$sec['id']?>" class="btn btn-outline-secondary">↑</a> <a href="?act=move_item&id=<?=$it['id']?>&dir=down§ion_id=<?=$sec['id']?>" class="btn btn-outline-secondary">↓</a> <a href="?act=del_item&id=<?=$it['id']?>§ion_id=<?=$sec['id']?>" class="btn btn-outline-danger" onclick="return confirm('Delete this menu item?')">×</a> </div> </td> </form> </tr> <?php endforeach; endforeach; ?> <tr class="table-primary border-top"> <form method="post" action="?act=add_item"> <td class="text-center fw-bold">+</td> <td> <div class="input-group input-group-sm"> <input type="text" name="title" class="form-control" placeholder="New Item Title" required> <select name="parent_id" class="form-select bg-white" style="max-width: 140px;"> <option value="">Main Menu</option> <?php foreach($parents as $opt): ?> <option value="<?=$opt['id']?>">Sub of: <?=h($opt['title'])?></option> <?php endforeach; ?> </select> </div> </td> <td><input type="text" name="url" class="form-control form-control-sm" placeholder="/modules/your_page.php" required></td> <td><input type="text" name="slug" class="form-control form-control-sm" placeholder="permission_slug"></td> <td class="text-center small text-muted">Auto</td> <td class="text-end pe-3"> <input type="hidden" name="section_id" value="<?=$sec['id']?>"> <button class="btn btn-sm btn-dark w-100 fw-bold py-1">ADD ITEM</button> </td> </form> </tr> </tbody> </table> </div> </div> <?php endforeach; ?> </div> </body> </html>