120 lines
5.1 KiB
PHP
120 lines
5.1 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
|
header('location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$start_date = $end_date = $reason = '';
|
|
$start_date_err = $end_date_err = $reason_err = '';
|
|
$success_msg = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
// Validate start date
|
|
if (empty(trim($_POST['start_date']))) {
|
|
$start_date_err = 'Please enter a start date.';
|
|
} else {
|
|
$start_date = trim($_POST['start_date']);
|
|
}
|
|
|
|
// Validate end date
|
|
if (empty(trim($_POST['end_date']))) {
|
|
$end_date_err = 'Please enter an end date.';
|
|
} else {
|
|
$end_date = trim($_POST['end_date']);
|
|
}
|
|
|
|
// Validate reason
|
|
if (empty(trim($_POST['reason']))) {
|
|
$reason_err = 'Please provide a reason.';
|
|
} else {
|
|
$reason = trim($_POST['reason']);
|
|
}
|
|
|
|
if (empty($start_date_err) && empty($end_date_err) && empty($reason_err)) {
|
|
$sql = 'INSERT INTO leave_requests (employee_id, start_date, end_date, reason) VALUES (:employee_id, :start_date, :end_date, :reason)';
|
|
if ($stmt = db()->prepare($sql)) {
|
|
$stmt->bindParam(':employee_id', $_SESSION['id'], PDO::PARAM_INT);
|
|
$stmt->bindParam(':start_date', $start_date, PDO::PARAM_STR);
|
|
$stmt->bindParam(':end_date', $end_date, PDO::PARAM_STR);
|
|
$stmt->bindParam(':reason', $reason, PDO::PARAM_STR);
|
|
|
|
if ($stmt->execute()) {
|
|
$success_msg = 'Leave request submitted successfully!';
|
|
|
|
// Send email notification to Admins and HR
|
|
require_once 'mail/MailService.php';
|
|
$sql_users = "SELECT username FROM users WHERE role = 'Admin' OR role = 'HR'";
|
|
$stmt_users = db()->query($sql_users);
|
|
$recipients = $stmt_users->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
if (!empty($recipients)) {
|
|
$subject = "New Leave Request Submitted";
|
|
$body = "A new leave request has been submitted by {$_SESSION['username']}.<br><br>"
|
|
. "<b>Start Date:</b> {$start_date}<br>"
|
|
. "<b>End Date:</b> {$end_date}<br>"
|
|
. "<b>Reason:</b> {$reason}<br><br>"
|
|
. "Please log in to the system to approve or reject this request.";
|
|
MailService::sendMail($recipients, $subject, $body, strip_tags($body));
|
|
}
|
|
|
|
$start_date = $end_date = $reason = '';
|
|
} 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>Submit Leave Request - 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">
|
|
</head>
|
|
<body>
|
|
<?php include 'sidebar.php'; ?>
|
|
<div class="main-content">
|
|
<div class="container-fluid">
|
|
<h1 class="mt-4">Submit Leave Request</h1>
|
|
<p class="lead">Fill out the form to request time off.</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="start_date" class="form-label">Start Date</label>
|
|
<input type="date" name="start_date" id="start_date" class="form-control <?php echo (!empty($start_date_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $start_date; ?>">
|
|
<span class="invalid-feedback"><?php echo $start_date_err; ?></span>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="end_date" class="form-label">End Date</label>
|
|
<input type="date" name="end_date" id="end_date" class="form-control <?php echo (!empty($end_date_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $end_date; ?>">
|
|
<span class="invalid-feedback"><?php echo $end_date_err; ?></span>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="reason" class="form-label">Reason for Leave</label>
|
|
<textarea name="reason" id="reason" class="form-control <?php echo (!empty($reason_err)) ? 'is-invalid' : ''; ?>" rows="3"><?php echo $reason; ?></textarea>
|
|
<span class="invalid-feedback"><?php echo $reason_err; ?></span>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary">Submit Request</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>
|