|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/.trash/survey_system/ | |
|
Path: /home2/outerorb/.trash/survey_system/user_edit.php
Size: 12.67 KB
Permissions: 0666
<?php
require_once 'config.php';
//  Check if user is logged in
if (!is_logged_in()) {
header("Location: index.php");
exit;
}
//  Allow only admin
$currentUser = current_user();
if ($currentUser['role'] !== 'admin') {
header("Location: dashboard.php");
exit;
}
//  Validate and fetch user by ID
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!isset($_POST['id']) || !is_numeric($_POST['id'])) {
die("Invalid User ID.");
}
$id = (int)$_POST['id'];
} else {
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid User ID.");
}
$id = (int)$_GET['id'];
}
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
$user = $stmt->fetch();
if (!$user) {
die("User not found.");
}
//  Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$role = trim($_POST['role']);
$full_name = trim($_POST['full_name']);
$team = trim($_POST['team']);
$active = isset($_POST['active']) ? (int)$_POST['active'] : $user['active'];
// Prevent saving blank team names
if ($team === '') {
$team = $user['team']; // fallback to previous value
}
$changes = [];
if ($user['username'] !== $username) $changes['username'] = ['old' => $user['username'], 'new' => $username];
if ($user['role'] !== $role) $changes['role'] = ['old' => $user['role'], 'new' => $role];
if ($user['full_name'] !== $full_name) $changes['full_name'] = ['old' => $user['full_name'], 'new' => $full_name];
if ($user['team'] !== $team) $changes['team'] = ['old' => $user['team'], 'new' => $team];
if ($user['active'] != $active) $changes['active'] = ['old' => $user['active'], 'new' => $active];
if (!empty($password)) $changes['password_changed'] = true;
if (!empty($password)) {
$stmt = $pdo->prepare("UPDATE users SET username=?, password=?, role=?, full_name=?, team=?, active=? WHERE id=?");
$stmt->execute([$username, $password, $role, $full_name, $team, $active, $id]);
} else {
$stmt = $pdo->prepare("UPDATE users SET username=?, role=?, full_name=?, team=?, active=? WHERE id=?");
$stmt->execute([$username, $role, $full_name, $team, $active, $id]);
}
require_once 'inc/eventlog.php';
log_event('user_updated', ['user_id' => $id, 'username' => $username, 'changes' => $changes]);
header("Location: users.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<?php include __DIR__ . '/inc/header.php'; ?>
<body class="bg-light">
<style>
/* Light mode specific styles */
body:not(.dark-mode) .user-edit-card {
background: #eaf4fb;
}
body:not(.dark-mode) .user-edit-header {
color: #343a40;
}
body:not(.dark-mode) .user-edit-header i {
color: #343a40;
}
body:not(.dark-mode) .user-edit-body {
color: #6c757d;
}
body:not(.dark-mode) .btn-cancel {
background: #e3eaf3;
color: #2563eb;
}
body:not(.dark-mode) .btn-update {
background: #2563eb;
color: #fff;
}
/* Dark mode styles */
body.dark-mode .btn-update {
background: #2563eb;
color: #fff;
}
</style>
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-lg-7 col-md-10">
<div class="card shadow-sm user-edit-card" style="border-radius: 18px;">
<div class="card-header d-flex align-items-center user-edit-header" style="background: transparent; border-top-left-radius: 1rem; border-top-right-radius: 1rem; border-bottom: none;">
<i class="bi bi-pencil-square me-2 fs-4"></i>
<span style="font-size: 1.25rem; font-weight: 600;">Edit User</span>
</div>
<div class="card-body p-4 user-edit-body" style="border-bottom-left-radius: 1rem; border-bottom-right-radius: 1rem; font-size: 0.93rem;">
<form method="post" action="user_edit.php">
<input type="hidden" name="id" value="<?= htmlspecialchars($user['id']); ?>">
<div class="row g-3">
<div class="col-md-6">
<label for="username" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Username</label>
<div class="input-group input-group-sm">
<span class="input-group-text" style="font-size: 0.93rem;"><i class="bi bi-person-fill"></i></span>
<input type="text" class="form-control" style="font-size: 0.93rem;" name="username" value="<?php echo $user['username']; ?>" required>
</div>
</div>
<div class="col-md-6">
<label for="full_name" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Full Name</label>
<div class="input-group input-group-sm">
<span class="input-group-text" style="font-size: 0.93rem;"><i class="bi bi-card-text"></i></span>
<input type="text" class="form-control" style="font-size: 0.93rem;" name="full_name" value="<?php echo $user['full_name']; ?>" required>
</div>
</div>
<div class="col-md-6">
<label for="password" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Password</label>
<div class="input-group input-group-sm">
<span class="input-group-text" style="font-size: 0.93rem;"><i class="bi bi-key-fill"></i></span>
<input type="password" class="form-control" style="font-size: 0.93rem;" name="password" placeholder="Enter new password if you want to change it">
</div>
</div>
<div class="col-md-6">
<label for="role" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Role</label>
<select class="form-select form-select-sm" style="font-size: 0.93rem;" name="role">
<option value="admin" <?= $user['role'] === 'admin' ? 'selected' : ''; ?>>Admin</option>
<option value="agent" <?= $user['role'] === 'agent' ? 'selected' : ''; ?>>Agent</option>
<option value="manager" <?= $user['role'] === 'manager' ? 'selected' : ''; ?>>Manager</option>
</select>
</div>
<div class="col-md-6">
<label for="team" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Team</label>
<?php
// Fetch all unique team names from teams and users tables
$teamOptions = [];
$teams_stmt = $pdo->query('SELECT name FROM teams UNION SELECT DISTINCT team FROM users WHERE team IS NOT NULL AND team != "" ORDER BY name ASC');
if ($teams_stmt) {
$teamOptions = $teams_stmt->fetchAll(PDO::FETCH_COLUMN);
}
?>
<select class="form-select form-select-sm" style="font-size: 0.93rem;" name="team">
<?php foreach ($teamOptions as $t): ?>
<option value="<?= htmlspecialchars($t) ?>" <?= $user['team'] === $t ? 'selected' : ''; ?>><?= htmlspecialchars($t) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label for="active" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Status</label>
<select class="form-select form-select-sm" style="font-size: 0.93rem;" name="active">
<option value="1" <?= $user['active'] == 1 ? 'selected' : '' ?>>Active</option>
<option value="0" <?= $user['active'] == 0 ? 'selected' : '' ?>>Inactive</option>
</select>
</div>
<div class="col-12 text-center mt-3">
<button type="submit" class="btn btn-update" style="font-size: 0.97rem; border-radius: 8px; font-weight: 500; padding: 0.45rem 1.1rem;"><i class="bi bi-save me-2"></i>Update</button>
<a href="users.php" class="btn btn-secondary ms-2 btn-cancel" style="font-size: 0.97rem; border-radius: 8px; font-weight: 500; padding: 0.45rem 1.1rem; border: none;">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
<?php include __DIR__ . '/inc/footer.php'; ?>
</html>