158 lines
6.6 KiB
PHP
158 lines
6.6 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Check if user is logged in and is an Admin
|
|
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true || $_SESSION['role'] !== 'Admin') {
|
|
header('location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$username = $password = $role = '';
|
|
$username_err = $password_err = $role_err = '';
|
|
$success_msg = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
// Validate username
|
|
if (empty(trim($_POST['username']))) {
|
|
$username_err = 'Please enter a username.';
|
|
} else {
|
|
// Check if username already exists
|
|
$sql = 'SELECT id FROM users WHERE username = :username';
|
|
if ($stmt = db()->prepare($sql)) {
|
|
$stmt->bindParam(':username', trim($_POST['username']), PDO::PARAM_STR);
|
|
if ($stmt->execute()) {
|
|
if ($stmt->rowCount() == 1) {
|
|
$username_err = 'This username is already taken.';
|
|
} else {
|
|
$username = trim($_POST['username']);
|
|
}
|
|
} else {
|
|
echo 'Oops! Something went wrong. Please try again later.';
|
|
}
|
|
unset($stmt);
|
|
}
|
|
}
|
|
|
|
// Validate password
|
|
if (empty(trim($_POST['password']))) {
|
|
$password_err = 'Please enter a password.';
|
|
} elseif (strlen(trim($_POST['password'])) < 6) {
|
|
$password_err = 'Password must have at least 6 characters.';
|
|
} else {
|
|
$password = trim($_POST['password']);
|
|
}
|
|
|
|
// Validate role
|
|
if (empty($_POST['role'])) {
|
|
$role_err = 'Please select a role.';
|
|
} else {
|
|
$role = $_POST['role'];
|
|
}
|
|
|
|
// Check input errors before inserting in database
|
|
if (empty($username_err) && empty($password_err) && empty($role_err)) {
|
|
$sql = 'INSERT INTO users (username, password, role) VALUES (:username, :password, :role)';
|
|
|
|
if ($stmt = db()->prepare($sql)) {
|
|
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
|
|
$stmt->bindParam(':password', $hashed_password, PDO::PARAM_STR);
|
|
$stmt->bindParam(':role', $role, PDO::PARAM_STR);
|
|
|
|
// Hash password
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
if ($stmt->execute()) {
|
|
$success_msg = 'Employee added successfully!';
|
|
// Clear form fields
|
|
$username = $password = $role = '';
|
|
} else {
|
|
echo 'Oops! Something went wrong. Please try again later.';
|
|
}
|
|
unset($stmt);
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Add Employee - Employee Attendance System</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">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
|
</head>
|
|
<body>
|
|
|
|
<div class="sidebar">
|
|
<h4 class="mb-4 fw-bold">Attendance System</h4>
|
|
<ul class="nav flex-column">
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="index.php"><i class="bi bi-grid-fill me-2"></i> Dashboard</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link active" href="#"><i class="bi bi-person-plus-fill me-2"></i> Add Employee</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="#"><i class="bi bi-people-fill me-2"></i> Employees</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="#"><i class="bi bi-calendar-check-fill me-2"></i> Attendance</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="#"><i class="bi bi-file-earmark-bar-graph-fill me-2"></i> Reports</a>
|
|
</li>
|
|
<li class="nav-item mt-auto">
|
|
<a class="nav-link" href="logout.php"><i class="bi bi-box-arrow-left me-2"></i> Logout</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="main-content">
|
|
<div class="container-fluid">
|
|
<h1 class="mt-4">Add New Employee</h1>
|
|
<p class="lead">Fill out the form to add a new employee to the system.</p>
|
|
|
|
<?php if(!empty($success_msg)): ?>
|
|
<div class="alert alert-success"><?php echo $success_msg; ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="mt-4 card p-4 bg-white border-0 shadow-sm">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" name="username" id="username" class="form-control <?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $username; ?>">
|
|
<span class="invalid-feedback"><?php echo $username_err; ?></span>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" name="password" id="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>">
|
|
<span class="invalid-feedback"><?php echo $password_err; ?></span>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="role" class="form-label">Role</label>
|
|
<select name="role" id="role" class="form-select <?php echo (!empty($role_err)) ? 'is-invalid' : ''; ?>">
|
|
<option value="">Select a role...</option>
|
|
<option value="Admin" <?php if($role == 'Admin') echo 'selected'; ?>>Admin</option>
|
|
<option value="HR" <?php if($role == 'HR') echo 'selected'; ?>>HR</option>
|
|
<option value="Employee" <?php if($role == 'Employee') echo 'selected'; ?>>Employee</option>
|
|
<option value="Supervisor" <?php if($role == 'Supervisor') echo 'selected'; ?>>Supervisor</option>
|
|
</select>
|
|
<span class="invalid-feedback"><?php echo $role_err; ?></span>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary" style="background-color: #3B82F6;">Add Employee</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|