34148-vm/apply.php
2025-09-17 15:46:15 +00:00

179 lines
8.6 KiB
PHP

<?php
// Disable caching
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: 0");
require_once __DIR__ . '/mail/MailService.php';
require_once __DIR__ . '/db/config.php';
$job_id = $_GET['id'] ?? null;
$job = null;
if ($job_id) {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM jobs WHERE id = ?");
$stmt->execute([$job_id]);
$job = $stmt->fetch();
}
// Redirect if job not found
if (!$job) {
header("Location: careers.php");
exit;
}
$success_message = '';
$error_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$resume = $_FILES['resume'] ?? null;
if (empty($name) || empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($resume) || $resume['error'] !== UPLOAD_ERR_OK) {
$error_message = 'Please fill in all fields and upload a valid resume.';
} else {
// Handle file upload
$upload_dir = __DIR__ . '/uploads/resumes/';
$file_extension = pathinfo($resume['name'], PATHINFO_EXTENSION);
$safe_filename = uniqid('resume_', true) . '.' . $file_extension;
$upload_path = $upload_dir . $safe_filename;
if (move_uploaded_file($resume['tmp_name'], $upload_path)) {
try {
// Save application to database
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO applications (job_id, name, email, resume_path) VALUES (?, ?, ?, ?)");
$stmt->execute([$job_id, $name, $email, $upload_path]);
// Send email notification
$to = getenv('MAIL_TO') ?: 'your-email@example.com'; // Fallback email
$subject = "New Application for " . $job['title'];
$html_content = "<p>A new application has been submitted for the position of <strong>" . htmlspecialchars($job['title']) . "</strong>.</p>"
. "<p><strong>Applicant Name:</strong> " . htmlspecialchars($name) . "</p>"
. "<p><strong>Applicant Email:</strong> " . htmlspecialchars($email) . "</p>"
. "<p>The resume has been saved to the server.</p>";
$text_content = "New Application for " . $job['title'] . "\n"
. "Applicant Name: " . $name . "\n"
. "Applicant Email: " . $email;
$result = MailService::sendMail($to, $subject, $html_content, $text_content);
if (!empty($result['success'])) {
// Send confirmation email to applicant
$applicant_subject = "Your Application for " . $job['title'];
$applicant_html_content = "<p>Dear " . htmlspecialchars($name) . ",</p>"
. "<p>Thank you for applying for the position of <strong>" . htmlspecialchars($job['title']) . "</strong> at CosmicHire.</p>"
. "<p>We have received your application and will be in touch shortly if your qualifications match our requirements.</p>"
. "<p>Best regards,<br>The CosmicHire Team</p>";
$applicant_text_content = "Dear " . $name . ",\n\nThank you for applying for the position of " . $job['title'] . " at CosmicHire.\n\nWe have received your application and will be in touch shortly if your qualifications match our requirements.\n\nBest regards,\nThe CosmicHire Team";
MailService::sendMail($email, $applicant_subject, $applicant_html_content, $applicant_text_content);
$success_message = 'Your application has been submitted successfully! A confirmation email has been sent to you.';
} else {
$error_message = 'Your application was saved, but there was an error sending the notification email.';
// Optional: Log the detailed error: error_log($result['error']);
}
} catch (PDOException $e) {
$error_message = 'There was a database error. Please try again later.';
// Optional: Log the detailed error: error_log($e->getMessage());
}
} else {
$error_message = 'There was an error uploading your resume. Please try again.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Apply for <?php echo htmlspecialchars($job['title']); ?> - CosmicHire</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<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">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<meta name="robots" content="noindex, nofollow">
</head>
<body>
<!-- Header -->
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container">
<a class="navbar-brand" href="index.php">CosmicHire</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="careers.php">Open Positions</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Application Form -->
<main class="container my-5">
<div class="row justify-content-center">
<div class="col-lg-8">
<h1 class="mb-4">Apply for <?php echo htmlspecialchars($job['title']); ?></h1>
<?php if ($success_message): ?>
<div class="alert alert-success">
<?php echo $success_message; ?>
</div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger">
<?php echo $error_message; ?>
</div>
<?php endif; ?>
<?php if (!$success_message): ?>
<form action="apply.php?id=<?php echo htmlspecialchars($job['id']); ?>" method="post" enctype="multipart/form-data">
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="resume" class="form-label">Resume (PDF, DOC, DOCX)</label>
<input type="file" class="form-control" id="resume" name="resume" accept=".pdf,.doc,.docx" required>
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="consent" required>
<label class="form-check-label" for="consent">I consent to my data being processed for this application.</label>
</div>
<button type="submit" class="btn btn-primary">Submit Application</button>
<a href="job-details.php?id=<?php echo htmlspecialchars($job['id']); ?>" class="btn btn-secondary">Cancel</a>
</form>
<?php endif; ?>
</div>
</div>
</main>
<!-- Footer -->
<footer class="footer">
<div class="container">
<p>&copy; <?php echo date('Y'); ?> CosmicHire. All Rights Reserved.</p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>