« Back to History
functional_roles_manage.php
|
20260921_175631.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; require_once __DIR__ . '/modules/activity/activity_logger.php'; $ctx = page_require_access('functional_roles_manage'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; require_once __DIR__ . '/partials/header.php'; if (!$pdo) { require_once __DIR__ . '/core/db.php'; } function h($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); } $success = ''; $error = ''; /* ========================= SAVE ROLE ========================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $role_name = trim($_POST['role_name'] ?? ''); $role_code = trim($_POST['role_code'] ?? ''); $description = trim($_POST['description'] ?? ''); $is_active = isset($_POST['is_active']) ? 1 : 0; $edit_id = (int)($_POST['edit_id'] ?? 0); if ($role_name === '') { $error = 'Role name required.'; } else { try { if ($edit_id > 0) { $sql = " UPDATE functional_roles SET role_name = :role_name, role_code = :role_code, description = :description, is_active = :is_active WHERE id = :id AND company_id = :cid LIMIT 1 "; $stmt = $pdo->prepare($sql); $stmt->execute([ ':role_name' => $role_name, ':role_code' => $role_code, ':description'=> $description, ':is_active' => $is_active, ':id' => $edit_id, ':cid' => $company_id, ]); activity_log([ 'company_id' => $company_id, 'user_id' => (int)($u['id'] ?? 0), 'module' => 'functional_roles', 'action_name' => 'edit', 'entity_type' => 'functional_role', 'entity_id' => $edit_id, 'remarks' => 'Updated functional role' ]); $success = 'Functional Role updated successfully.'; } else { $sql = " INSERT INTO functional_roles ( company_id, role_name, role_code, description, is_active, created_by ) VALUES ( :cid, :role_name, :role_code, :description, :is_active, :created_by ) "; $stmt = $pdo->prepare($sql); $stmt->execute([ ':cid' => $company_id, ':role_name' => $role_name, ':role_code' => $role_code, ':description' => $description, ':is_active' => $is_active, ':created_by' => (int)($u['id'] ?? 0), ]); activity_log([ 'company_id' => $company_id, 'user_id' => (int)($u['id'] ?? 0), 'module' => 'functional_roles', 'action_name' => 'create', 'entity_type' => 'functional_role', 'entity_id' => (int)$pdo->lastInsertId(), 'remarks' => 'Created functional role' ]); $success = 'Functional Role added successfully.'; } } catch (Throwable $e) { $error = $e->getMessage(); } } } /* ========================= EDIT DATA ========================= */ $edit = null; if (isset($_GET['edit'])) { $edit_id = (int)$_GET['edit']; $stmt = $pdo->prepare(" SELECT * FROM functional_roles WHERE id = ? AND company_id = ? LIMIT 1 "); $stmt->execute([$edit_id, $company_id]); $edit = $stmt->fetch(PDO::FETCH_ASSOC); } /* ========================= ROLE LIST ========================= */ $roles = []; try { $stmt = $pdo->prepare(" SELECT * FROM functional_roles WHERE company_id = ? ORDER BY role_name ASC "); $stmt->execute([$company_id]); $roles = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (Throwable $e) { $roles = []; } ?> <div class="container py-4"> <div class="card shadow-sm border-0 rounded-4"> <div class="card-header bg-primary text-white"> <h4 class="mb-0"> Functional Role Management </h4> </div> <div class="card-body"> <?php if ($success): ?> <div class="alert alert-success"> <?= h($success) ?> </div> <?php endif; ?> <?php if ($error): ?> <div class="alert alert-danger"> <?= h($error) ?> </div> <?php endif; ?> <form method="post"> <input type="hidden" name="edit_id" value="<?= (int)($edit['id'] ?? 0) ?>" > <div class="row g-3"> <div class="col-md-4"> <label class="form-label"> Role Name </label> <input type="text" name="role_name" class="form-control" required value="<?= h($edit['role_name'] ?? '') ?>" > </div> <div class="col-md-4"> <label class="form-label"> Role Code </label> <input type="text" name="role_code" class="form-control" value="<?= h($edit['role_code'] ?? '') ?>" > </div> <div class="col-md-4"> <label class="form-label"> Status </label> <div class="form-check mt-2"> <input class="form-check-input" type="checkbox" name="is_active" id="is_active" <?= !isset($edit['is_active']) || $edit['is_active'] ? 'checked' : '' ?> > <label class="form-check-label" for="is_active" > Active </label> </div> </div> <div class="col-12"> <label class="form-label"> Description </label> <textarea name="description" rows="3" class="form-control" ><?= h($edit['description'] ?? '') ?></textarea> </div> </div> <div class="mt-4"> <button type="submit" class="btn btn-primary" > <?= $edit ? 'Update Role' : 'Add Role' ?> </button> <a href="functional_roles_manage.php" class="btn btn-secondary" > Reset </a> </div> </form> </div> </div> <div class="card shadow-sm border-0 rounded-4 mt-4"> <div class="card-header bg-dark text-white"> <h5 class="mb-0"> Functional Role List </h5> </div> <div class="card-body p-0"> <div class="table-responsive"> <table class="table table-bordered table-hover mb-0 align-middle"> <thead class="table-light"> <tr> <th width="60">#</th> <th>Role Name</th> <th>Role Code</th> <th>Status</th> <th width="100">Action</th> </tr> </thead> <tbody> <?php if ($roles): ?> <?php foreach ($roles as $k => $r): ?> <tr> <td> <?= $k + 1 ?> </td> <td> <?= h($r['role_name']) ?> </td> <td> <?= h($r['role_code']) ?> </td> <td> <?php if ($r['is_active']): ?> <span class="badge bg-success"> Active </span> <?php else: ?> <span class="badge bg-danger"> Inactive </span> <?php endif; ?> </td> <td> <a href="?edit=<?= (int)$r['id'] ?>" class="btn btn-sm btn-primary" > Edit </a> </td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="5" class="text-center py-4"> No roles found. </td> </tr> <?php endif; ?> </tbody> </table> </div> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>