File: /srv/www/rectt-csmcri.res.in/public_html/dashboard.php
<?php
session_start();
include_once 'sites/config/config.php';
/**
* Returns the effective upper age limit for the user for a given post, based on exemption rules.
*
* @param int $base_limit The post's base upper age limit
* @param string $category User's category (e.g. 'General', 'OBC', 'SC', 'ST')
* @param string $gender User's gender ('Male'/'Female'/etc)
* @param string $disability 'Yes'/'No'
* @param string $marital_status (e.g. 'Single', 'Married', 'Widowed', 'Divorced', 'Judicially Separated')
* @return int Effective upper age limit
*/
function getEffectiveAgeLimit($base_limit, $category, $gender, $disability, $marital_status, $ex_servicemen)
{
if (strtolower(trim($ex_servicemen)) === 'yes') {
return INF; // Or 999, or PHP_INT_MAX
}
$cat = trim($category);
$gen = strtolower(trim($gender));
$pwd = strtolower(trim($disability)) === 'yes';
$marital = strtolower(trim($marital_status));
// SC/ST Male: +5
if ($cat === 'SC' || $cat === 'ST') {
$base_limit += 5;
}
// OBC (Non Creamy Layer): +3
elseif ($cat === 'OBC (Non Creamy Layer)') {
$base_limit += 3;
}
// Disability based
if ($pwd) {
if ($cat === 'General') {
$base_limit += 10;
} elseif ($cat === 'OBC (Non Creamy Layer)') {
$base_limit += 13;
} elseif ($cat === 'SC' || $cat === 'ST') {
$base_limit += 15;
}
}
// Marital status: Widowed/Divorced/Judicially Separated + Female + General
$special_marital = in_array($marital, ['widowed', 'divorced', 'judicially separated']);
if ($special_marital && $gen === 'female') {
if ($cat === 'General') {
// General: 35 years if base limit is less than 35
if ($base_limit < 35) $base_limit = 35;
} elseif (in_array($cat, ['OBC (Non Creamy Layer)', 'SC', 'ST'])) {
// OBC/SC/ST: 40 years if base limit is less than 40
if ($base_limit < 40) $base_limit = 40;
}
}
return $base_limit;
}
function getPrimaryDetails($conn, $user_id)
{
$stmt = $conn->prepare("
SELECT
age,
category,
gender,
disability,
marital_status,
ex_servicemen
FROM primary_details
WHERE user_id = ?
LIMIT 1
");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $result;
}
// 🔐 Authentication check
if (!isset($_SESSION['registered']) || $_SESSION['registered'] !== 'registered') {
header("Location: login.php");
exit();
}
if (!isset($_SESSION['primary_saved']) || $_SESSION['primary_saved'] !== 'Yes') {
header("Location: primary.php");
exit();
}
if (!isset($_SESSION['user_id'], $_SESSION['session_token'])) {
session_destroy();
header("Location: logout.php");
exit;
}
$user_id = $_SESSION['user_id'];
$token = $_SESSION['session_token'];
$stmt = $conn->prepare("SELECT session_token FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$stmt->bind_result($db_token);
$stmt->fetch();
$stmt->close();
if ($token !== $db_token) {
session_destroy();
die("You have been logged out because your account was logged in elsewhere.");
}
$ad_id = $_SESSION['ad_id'];
// $user_id = $_SESSION['user_id'];
$user_category = $_SESSION['category'];
$user_age = $_SESSION['age'];
unset($_SESSION['flash']);
// If form submitted, set active post id in session
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['selected_post_id'])) {
$_SESSION['active_post_id'] = intval($_POST['selected_post_id']);
// You can optionally redirect to the relevant form page here
}
// Unset active_post_id when returning to dashboard if submitted
if (isset($_GET['reset_active_post'])) {
unset($_SESSION['active_post_id']);
// Redirect to dashboard.php WITHOUT query params
header("Location: dashboard.php");
exit();
}
// 🔍 Fetch available posts for the user category
$sql = "SELECT * FROM posts WHERE advertisement_id = ? AND category = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("is", $ad_id, $user_category);
$stmt->execute();
$result = $stmt->get_result();
// 🔍 Fetch all submitted applications from master table
$submittedPosts = [];
$appStmt = $conn->prepare("
SELECT post_id, application_type, application_table_id, pdf_path
FROM all_applications
WHERE user_id = ? AND status = 'submitted'
");
$appStmt->bind_param("i", $user_id);
$appStmt->execute();
$appRes = $appStmt->get_result();
while ($row = $appRes->fetch_assoc()) {
$submittedPosts[$row['post_id']] = [
'type' => $row['application_type'],
'app_id' => $row['application_table_id'],
'pdf_path' => $row['pdf_path']
];
}
$appStmt->close();
// Set active post id from session (if any)
$active_post_id = $_SESSION['active_post_id'] ?? null;
?>
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<center>
<h1 class="mb-4">Dashboard</h1>
</center>
<?php if (isset($_GET['submitted'])): ?>
<div class="alert alert-success">Application submitted successfully!</div>
<script>
if (window.history.replaceState) {
// Remove ?submitted from the URL after showing the message
const url = new URL(window.location);
url.searchParams.delete('submitted');
window.history.replaceState({}, document.title, url.pathname + url.search);
}
</script>
<?php endif; ?>
<!-- Single Application Dropdown -->
<?php if (!$active_post_id): ?>
<form method="POST" class="mb-4">
<div class="row g-2 align-items-end">
<div class="col-md-6">
<label for="selected_post_id" class="form-label">Select a Post to Apply:</label>
<select name="selected_post_id" id="selected_post_id" class="form-select" required>
<option value="">-- Select --</option>
<?php
// Reset result pointer to top for dropdown
$result->data_seek(0);
while ($row = $result->fetch_assoc()):
$post_id = $row['id'];
// Skip already submitted posts
if (isset($submittedPosts[$post_id])) continue;
// Skip if not eligible by age
$user_id = $_SESSION['user_id']; // Or however you get the logged-in user
$user_primary = getPrimaryDetails($conn, $user_id);
$user_age = $user_primary['age'];
$user_category = $user_primary['category'];
$user_gender = $user_primary['gender'];
$user_disability = $user_primary['disability'];
$user_marital_status = $user_primary['marital_status'];
$user_ex_servicemen = $user_primary['ex_servicemen'];
$effective_limit = getEffectiveAgeLimit(
$row['age_limit'], // from posts table
$user_category,
$user_gender,
$user_disability,
$user_marital_status,
$user_ex_servicemen
);
if ($user_age > $effective_limit) continue;
$post_label = $row['post_code'] . " - " . $row['post_title'];
?>
<option value="<?= $post_id ?>"><?= htmlspecialchars($post_label) ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-primary">Apply</button>
</div>
</div>
</form>
<?php else: ?>
<!-- Show the post the user is currently applying for -->
<?php
$active_post_id = $_SESSION['active_post_id'] ?? null;
// Extra safety: If application for active_post_id has already been submitted, clear it
if ($active_post_id && isset($submittedPosts[$active_post_id])) {
unset($_SESSION['active_post_id']);
$active_post_id = null;
echo '<div class="alert alert-info mb-3">Your application for the previously selected post has already been submitted.</div>';
}
// Fetch the active post details
$stmt2 = $conn->prepare("SELECT * FROM posts WHERE id = ?");
$stmt2->bind_param("i", $active_post_id);
$stmt2->execute();
$active_post = $stmt2->get_result()->fetch_assoc();
$stmt2->close();
?>
<?php if ($active_post): ?>
<div class="alert alert-info">
<strong>Currently Applying For:</strong>
<?= htmlspecialchars($active_post['post_code'] . " - " . $active_post['post_title']) ?>
<form method="get" class="d-inline ms-3">
<button type="submit" name="reset_active_post" value="1" class="btn btn-warning btn-sm">Cancel Application</button>
</form>
</div>
<?php
$type = strtolower($active_post['type']);
$form_url = match ($type) {
'technical' => "tech_form.php?post_id=$active_post_id",
'non-technical' => "non_tech_form.php?post_id=$active_post_id",
'scientist' => "scientist_form.php?post_id=$active_post_id",
default => "#"
};
?>
<a href="<?= $form_url ?>" class="btn btn-success mb-4">Continue Application</a>
<?php else: ?>
<div class="alert alert-danger">Invalid post selection.</div>
<?php endif; ?>
<?php endif; ?>
<!-- Available Applications Table -->
<h4 class="mt-4">Available Applications</h4>
<table class="table table-bordered table-striped">
<thead class="table-dark">
<tr>
<th>Post Code</th>
<th>Post Title</th>
<th>Type</th>
<th>Qualification Required</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
// Reset result pointer for listing all available applications
$result->data_seek(0);
$any_available = false;
while ($row = $result->fetch_assoc()):
$post_id = $row['id'];
// Skip already submitted posts
if (isset($submittedPosts[$post_id])) continue;
// Skip if not eligible by age
$user_id = $_SESSION['user_id']; // Or however you get the logged-in user
$user_primary = getPrimaryDetails($conn, $user_id);
$user_age = $user_primary['age'];
$user_category = $user_primary['category'];
$user_gender = $user_primary['gender'];
$user_disability = $user_primary['disability'];
$user_marital_status = $user_primary['marital_status'];
$user_ex_servicemen = $user_primary['ex_servicemen'];
$effective_limit = getEffectiveAgeLimit(
$row['age_limit'], // from posts table
$user_category,
$user_gender,
$user_disability,
$user_marital_status,
$user_ex_servicemen
);
if ($user_age > $effective_limit) continue; // or show ineligible
// if ($user_age > $row['age_limit']) continue;
$any_available = true;
?>
<tr>
<td><?= htmlspecialchars($row['post_code']) ?></td>
<td><?= htmlspecialchars($row['post_title']) ?></td>
<td><?= htmlspecialchars($row['type']) ?></td>
<td><?= htmlspecialchars($row['qualification_required']) ?></td>
<td><span class="text-primary">Available</span></td>
</tr>
<?php endwhile; ?>
<?php if (!$any_available): ?>
<tr>
<td colspan="6" class="text-center text-muted">No available applications at the moment.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<h4 class="mt-4">Submitted Applications</h4>
<table class="table table-bordered table-striped">
<thead class="table-dark">
<tr>
<th>Post Code</th>
<th>Post Title</th>
<th>Type</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
// Reset pointer again for listing all posts
$result->data_seek(0);
while ($row = $result->fetch_assoc()):
$post_id = $row['id'];
if (!isset($submittedPosts[$post_id])) continue; // Show only submitted
$app_id = $submittedPosts[$post_id]['app_id'];
$app_type = $submittedPosts[$post_id]['type'];
$pdf_path = $submittedPosts[$post_id]['pdf_path'];
$view_url = !empty($pdf_path) ? "serve_pdf.php?application_id=" . urlencode($app_id) : "#";
?>
<tr>
<td><?= htmlspecialchars($row['post_code']) ?></td>
<td><?= htmlspecialchars($row['post_title']) ?></td>
<td><?= htmlspecialchars($row['type']) ?></td>
<td><span class="text-success">Submitted</span></td>
<td>
<?php if ($view_url !== "#"): ?>
<a href="<?= $view_url ?>" class="btn btn-secondary btn-sm" target="_blank">View</a>
<?php else: ?>
<span class="text-muted">Not available</span>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<form method="post" action="logout.php" class="text-center mt-4">
<button type="submit" class="btn btn-outline-danger">Logout</button>
</form>
</div>
</body>
</html>