« Back to History
role_redirect_manage.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php declare(strict_types=1); require __DIR__ . '/modules/auth/auth.php'; require __DIR__ . '/modules/auth/role_redirect.php'; require_once __DIR__ . '/modules/activity/activity_logger.php'; require_login(['owner', 'admin', 'developer']); $u = auth_user(); $company_id = (int)($u['company_id'] ?? 0); $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo instanceof PDO) { require __DIR__ . '/core/db.php'; $pdo = $GLOBALS['pdo'] ?? null; } if (!$pdo instanceof PDO) { http_response_code(500); exit('Database connection not available.'); } function h($value): string { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } function pv(string $key, $default = '') { return isset($_POST[$key]) ? trim((string)$_POST[$key]) : $default; } function gv(string $key, $default = '') { return isset($_GET[$key]) ? trim((string)$_GET[$key]) : $default; } function normalize_role(string $role): string { return strtolower(trim($role)); } function normalize_redirect_path(string $path): string { $path = trim($path); if ($path === '') { return ''; } if ($path[0] !== '/') { $path = '/' . $path; } return preg_replace('#/+#', '/', $path) ?? $path; } $flash = ''; $err = ''; $role_options = []; try { $stmt = $pdo->prepare("SELECT DISTINCT role_name AS role_label FROM user_roles WHERE (company_id = :cid OR company_id = 0) AND status = 1 ORDER BY role_name ASC"); $stmt->execute([':cid' => $company_id]); $role_options = array_merge($role_options, array_column($stmt->fetchAll(PDO::FETCH_ASSOC), 'role_label')); } catch (Throwable $e) { } try { $stmt = $pdo->prepare("SELECT DISTINCT name AS role_label FROM company_roles WHERE company_id = :cid AND is_active = 1 ORDER BY name ASC"); $stmt->execute([':cid' => $company_id]); $role_options = array_merge($role_options, array_column($stmt->fetchAll(PDO::FETCH_ASSOC), 'role_label')); } catch (Throwable $e) { } try { $stmt = $pdo->prepare("SELECT DISTINCT role AS role_label FROM users WHERE company_id = :cid AND role IS NOT NULL AND role <> '' ORDER BY role ASC"); $stmt->execute([':cid' => $company_id]); $role_options = array_merge($role_options, array_column($stmt->fetchAll(PDO::FETCH_ASSOC), 'role_label')); } catch (Throwable $e) { } try { $stmt = $pdo->prepare("SELECT DISTINCT role AS role_label FROM role_redirects WHERE company_id = :cid ORDER BY role ASC"); $stmt->execute([':cid' => $company_id]); $role_options = array_merge($role_options, array_column($stmt->fetchAll(PDO::FETCH_ASSOC), 'role_label')); } catch (Throwable $e) { } $role_options = array_values(array_unique(array_filter(array_map('normalize_role', $role_options)))); sort($role_options, SORT_NATURAL | SORT_FLAG_CASE); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $form = pv('form'); try { if ($form === 'save') { $id = (int)pv('id', '0'); $role = normalize_role(pv('role')); $redirect_path = normalize_redirect_path(pv('redirect_path')); $is_active = (int)pv('is_active', '1') === 1 ? 1 : 0; if ($role === '') { throw new RuntimeException('Role required hai.'); } if ($redirect_path === '') { throw new RuntimeException('Redirect path required hai.'); } $dup = $pdo->prepare('SELECT id FROM role_redirects WHERE company_id = :cid AND role = :role AND id <> :id LIMIT 1'); $dup->execute([ ':cid' => $company_id, ':role' => $role, ':id' => $id, ]); if ($dup->fetchColumn()) { throw new RuntimeException('Is role ke liye redirect pehle se bana hua hai.'); } if ($id > 0) { $stmt = $pdo->prepare('UPDATE role_redirects SET role = :role, redirect_path = :redirect_path, is_active = :is_active, updated_at = NOW() WHERE id = :id AND company_id = :cid'); $stmt->execute([ ':role' => $role, ':redirect_path' => $redirect_path, ':is_active' => $is_active, ':id' => $id, ':cid' => $company_id, ]); activity_log([ 'company_id' => $company_id, 'user_id' => (int)($u['id'] ?? 0), 'module' => 'role_redirect', 'action_name' => 'edit', 'entity_type' => 'role_redirect', 'entity_id' => $id, 'remarks' => 'Updated role redirect' ]); } else { $stmt = $pdo->prepare('INSERT INTO role_redirects (company_id, role, redirect_path, is_active, updated_at) VALUES (:cid, :role, :redirect_path, :is_active, NOW())'); $stmt->execute([ ':cid' => $company_id, ':role' => $role, ':redirect_path' => $redirect_path, ':is_active' => $is_active, ]); activity_log([ 'company_id' => $company_id, 'user_id' => (int)($u['id'] ?? 0), 'module' => 'role_redirect', 'action_name' => 'create', 'entity_type' => 'role_redirect', 'entity_id' => (int)$pdo->lastInsertId(), 'remarks' => 'Created role redirect' ]); } role_redirect_rebuild_cache($pdo, $company_id); header('Location: ' . $_SERVER['PHP_SELF'] . '?ok=1'); exit; } if ($form === 'toggle') { $id = (int)pv('id', '0'); $to = (int)pv('to', '0') === 1 ? 1 : 0; $stmt = $pdo->prepare('UPDATE role_redirects SET is_active = :is_active, updated_at = NOW() WHERE id = :id AND company_id = :cid'); $stmt->execute([ ':is_active' => $to, ':id' => $id, ':cid' => $company_id, ]); activity_log([ 'company_id' => $company_id, 'user_id' => (int)($u['id'] ?? 0), 'module' => 'role_redirect', 'action_name' => 'edit', 'entity_type' => 'role_redirect', 'entity_id' => $id, 'remarks' => $to ? 'Enabled role redirect' : 'Disabled role redirect' ]); role_redirect_rebuild_cache($pdo, $company_id); header('Location: ' . $_SERVER['PHP_SELF'] . '?ok=1'); exit; } if ($form === 'delete') { $id = (int)pv('id', '0'); $stmt = $pdo->prepare('DELETE FROM role_redirects WHERE id = :id AND company_id = :cid'); $stmt->execute([ ':id' => $id, ':cid' => $company_id, ]); activity_log([ 'company_id' => $company_id, 'user_id' => (int)($u['id'] ?? 0), 'module' => 'role_redirect', 'action_name' => 'delete', 'entity_type' => 'role_redirect', 'entity_id' => $id, 'remarks' => 'Deleted role redirect' ]); role_redirect_rebuild_cache($pdo, $company_id); header('Location: ' . $_SERVER['PHP_SELF'] . '?ok=1'); exit; } if ($form === 'rebuild_cache') { role_redirect_rebuild_cache($pdo, $company_id); activity_log([ 'company_id' => $company_id, 'user_id' => (int)($u['id'] ?? 0), 'module' => 'role_redirect', 'action_name' => 'edit', 'entity_type' => 'role_redirect_cache', 'entity_id' => 0, 'remarks' => 'Rebuilt role redirect cache' ]); header('Location: ' . $_SERVER['PHP_SELF'] . '?ok=1&cache=1'); exit; } } catch (Throwable $e) { $err = $e->getMessage(); } } $edit_id = (int)gv('edit', '0'); $edit = null; if ($edit_id > 0) { $stmt = $pdo->prepare('SELECT * FROM role_redirects WHERE id = :id AND company_id = :cid LIMIT 1'); $stmt->execute([ ':id' => $edit_id, ':cid' => $company_id, ]); $edit = $stmt->fetch(PDO::FETCH_ASSOC) ?: null; } $stmt = $pdo->prepare('SELECT id, company_id, role, redirect_path, is_active, updated_at FROM role_redirects WHERE company_id = :cid ORDER BY role ASC, id DESC'); $stmt->execute([':cid' => $company_id]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $page_title = 'Role Redirect Manage'; require __DIR__ . '/partials/header.php'; ?> <div class="container-fluid mt-4" style="max-width: 1200px;"> <div class="card shadow-sm border-0 mb-4"> <div class="card-header bg-primary text-white py-3 d-flex justify-content-between align-items-center"> <h5 class="mb-0 fw-bold">Role Redirect Management</h5> <form method="post" class="m-0"> <input type="hidden" name="form" value="rebuild_cache"> <button type="submit" class="btn btn-light btn-sm">Rebuild Cache</button> </form> </div> <div class="card-body bg-light"> <?php if (gv('ok')): ?> <div class="alert alert-success border-0 shadow-sm mb-3"> <?= gv('cache') ? 'Cache rebuild ho gaya.' : 'Operation successful.' ?> </div> <?php endif; ?> <?php if ($err !== ''): ?> <div class="alert alert-danger border-0 shadow-sm mb-3"><?= h($err) ?></div> <?php endif; ?> <form method="post" class="row g-3" autocomplete="off"> <input type="hidden" name="form" value="save"> <input type="hidden" name="id" value="<?= (int)($edit['id'] ?? 0) ?>"> <div class="col-md-3"> <label class="form-label fw-bold small">Role</label> <input name="role" list="role-list" class="form-control" required placeholder="e.g. admin" value="<?= h((string)($edit['role'] ?? '')) ?>"> <datalist id="role-list"> <?php foreach ($role_options as $role_option): ?> <option value="<?= h($role_option) ?>"></option> <?php endforeach; ?> </datalist> </div> <div class="col-md-5"> <label class="form-label fw-bold small">Redirect Path</label> <input name="redirect_path" class="form-control" required placeholder="/erp/index.php" value="<?= h((string)($edit['redirect_path'] ?? '')) ?>"> <div class="form-text">Absolute ERP path do, jaise `/erp/public/employee_portal.php`.</div> </div> <div class="col-md-2"> <label class="form-label fw-bold small">Status</label> <select name="is_active" class="form-select"> <option value="1" <?= (!isset($edit['is_active']) || (int)$edit['is_active'] === 1) ? 'selected' : '' ?>>Active</option> <option value="0" <?= (isset($edit['is_active']) && (int)$edit['is_active'] === 0) ? 'selected' : '' ?>>Inactive</option> </select> </div> <div class="col-md-2 d-flex align-items-end gap-2"> <button type="submit" class="btn btn-primary w-100"><?= $edit ? 'Update' : 'Add' ?></button> <?php if ($edit): ?> <a href="<?= h($_SERVER['PHP_SELF']) ?>" class="btn btn-outline-secondary">Cancel</a> <?php endif; ?> </div> </form> </div> </div> <div class="card shadow-sm border-0"> <div class="card-header bg-white py-3"> <h6 class="mb-0 fw-bold text-dark">Configured Redirects</h6> </div> <div class="table-responsive"> <table class="table table-hover align-middle mb-0"> <thead class="table-light"> <tr> <th class="ps-3">ID</th> <th>Role</th> <th>Redirect Path</th> <th>Status</th> <th>Updated</th> <th class="text-end pe-3">Actions</th> </tr> </thead> <tbody> <?php if (!$rows): ?> <tr> <td colspan="6" class="text-center py-4 text-muted">No redirect rules found.</td> </tr> <?php else: ?> <?php foreach ($rows as $row): ?> <tr> <td class="ps-3 text-muted"><?= (int)$row['id'] ?></td> <td><span class="badge bg-info text-dark px-3 py-2"><?= h((string)$row['role']) ?></span></td> <td><code><?= h((string)$row['redirect_path']) ?></code></td> <td> <?php if ((int)$row['is_active'] === 1): ?> <span class="badge bg-success-subtle text-success border border-success-subtle px-3 py-2">Active</span> <?php else: ?> <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle px-3 py-2">Inactive</span> <?php endif; ?> </td> <td class="text-muted"><?= h((string)($row['updated_at'] ?? '')) ?></td> <td class="text-end pe-3"> <div class="d-flex justify-content-end gap-2"> <a href="?edit=<?= (int)$row['id'] ?>" class="btn btn-sm btn-outline-primary">Edit</a> <form method="post" class="m-0" onsubmit="return confirm('Status change karein?');"> <input type="hidden" name="form" value="toggle"> <input type="hidden" name="id" value="<?= (int)$row['id'] ?>"> <input type="hidden" name="to" value="<?= (int)$row['is_active'] === 1 ? 0 : 1 ?>"> <button type="submit" class="btn btn-sm <?= (int)$row['is_active'] === 1 ? 'btn-outline-warning' : 'btn-outline-success' ?>"> <?= (int)$row['is_active'] === 1 ? 'Disable' : 'Enable' ?> </button> </form> <form method="post" class="m-0" onsubmit="return confirm('Ye redirect delete karna hai?');"> <input type="hidden" name="form" value="delete"> <input type="hidden" name="id" value="<?= (int)$row['id'] ?>"> <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button> </form> </div> </td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </div> </div> </div> <?php require __DIR__ . '/partials/footer.php'; ?>