157 lines
6.4 KiB
PHP
157 lines
6.4 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$pdo = db();
|
|
$message = '';
|
|
$error = '';
|
|
$edit_user = null;
|
|
|
|
// Handle form submissions for adding/editing a user
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim($_POST['name']);
|
|
$email = trim($_POST['email']);
|
|
$id = $_POST['id'] ?? null;
|
|
|
|
if (empty($name) || empty($email)) {
|
|
$error = 'User name and email are required.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$error = 'Invalid email format.';
|
|
} else {
|
|
try {
|
|
if ($id) {
|
|
// Update existing user
|
|
$stmt = $pdo->prepare('UPDATE users SET name = ?, email = ? WHERE id = ?');
|
|
$stmt->execute([$name, $email, $id]);
|
|
$message = 'User updated successfully!';
|
|
} else {
|
|
// Insert new user
|
|
$stmt = $pdo->prepare('INSERT INTO users (name, email) VALUES (?, ?)');
|
|
$stmt->execute([$name, $email]);
|
|
$message = 'User added successfully!';
|
|
}
|
|
} catch (PDOException $e) {
|
|
if ($e->errorInfo[1] == 1062) { // Duplicate entry for email
|
|
$error = 'Email address already exists.';
|
|
} else {
|
|
$error = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle deleting a user
|
|
if (isset($_GET['delete'])) {
|
|
$id = $_GET['delete'];
|
|
try {
|
|
$stmt = $pdo->prepare('DELETE FROM users WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$message = 'User deleted successfully!';
|
|
} catch (PDOException $e) {
|
|
$error = 'Error deleting user. They might be associated with expenses.';
|
|
}
|
|
}
|
|
|
|
// Handle fetching a user for editing
|
|
if (isset($_GET['edit'])) {
|
|
$id = $_GET['edit'];
|
|
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$edit_user = $stmt->fetch();
|
|
}
|
|
|
|
// Fetch all users to display
|
|
$stmt = $pdo->query('SELECT * FROM users ORDER BY name');
|
|
$users = $stmt->fetchAll();
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Manage Users</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<header class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1>Manage Users</h1>
|
|
<a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
|
|
</header>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Add/Edit User Form -->
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<h5 class="mb-0"><?php echo $edit_user ? 'Edit User' : 'Add New User'; ?></h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="users.php" method="POST">
|
|
<input type="hidden" name="id" value="<?php echo htmlspecialchars($edit_user['id'] ?? ''); ?>">
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="name" class="form-label">User Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($edit_user['name'] ?? ''); ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="email" class="form-label">Email</label>
|
|
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($edit_user['email'] ?? ''); ?>" required>
|
|
</div>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary"><?php echo $edit_user ? 'Update User' : 'Add User'; ?></button>
|
|
<?php if ($edit_user): ?>
|
|
<a href="users.php" class="btn btn-secondary">Cancel Edit</a>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Users List -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="mb-0">Existing Users</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Email</th>
|
|
<th class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($users)): ?>
|
|
<tr>
|
|
<td colspan="3" class="text-center">No users found.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($users as $user): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($user['name']); ?></td>
|
|
<td><?php echo htmlspecialchars($user['email']); ?></td>
|
|
<td class="text-end">
|
|
<a href="users.php?edit=<?php echo $user['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i> Edit</a>
|
|
<a href="users.php?delete=<?php echo $user['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this user?');"><i class="bi bi-trash"></i> Delete</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|