File: /srv/www/rectt-csmcri.res.in/TEMP/non-tech-process.php
<?php
session_start();
require 'sites/config/config.php';
require_once "mail_pdf.php"; // Still using for PDF generation
if (!isset($_SESSION['registered']) || $_SERVER['REQUEST_METHOD'] !== 'POST') {
header("location: login.php");
exit;
}
$user_id = $_SESSION['user_id'] ?? null;
$ad_id = $_POST['ad_id'] ?? null;
$post_id = $_POST['post_id'] ?? null;
if (!$user_id || !$ad_id || !$post_id) {
die("Invalid access or session expired.");
}
// META
$category = $_SESSION['category'] ?? 'General';
$disability = $_SESSION['disability'] ?? 'No';
$gender = $ex_servicemen = $csir_employee = '';
$stmt = $conn->prepare("SELECT gender, ex_servicemen, csir_employee FROM primary_details WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$res = $stmt->get_result();
if ($row = $res->fetch_assoc()) {
$gender = $row['gender'];
$ex_servicemen = $row['ex_servicemen'];
$csir_employee = $row['csir_employee'];
}
$stmt->close();
$is_exempt = ($category == 'SC' || $category == 'ST' || $gender == 'Female' || $ex_servicemen == 'Yes' || $csir_employee == 'Yes' || $disability == 'Yes');
// Required Fields
$errors = [];
$computer_knowledge = $_POST['computer_knowledge'] ?? '';
$payment_reference = $_POST['paymentReferenceNo'] ?? '';
$declaration = isset($_POST['agree']) ? 1 : 0;
if (!$computer_knowledge) $errors[] = "Computer knowledge is required.";
if (!$declaration) $errors[] = "You must agree to the declaration.";
if (!$is_exempt && empty($payment_reference)) $errors[] = "Payment reference is required.";
$required_files = ['ssc_marksheet', 'hsc_marksheet', 'photograph', 'signature'];
if ($category !== 'General') $required_files[] = 'category_certificate';
if ($disability === 'Yes') $required_files[] = 'disability_certificate';
foreach ($required_files as $f) {
if (!isset($_FILES[$f]) || $_FILES[$f]['error'] !== UPLOAD_ERR_OK) {
$errors[] = ucfirst(str_replace('_', ' ', $f)) . " is required.";
}
}
if (!empty($errors)) {
$_SESSION['form_errors'] = $errors;
header("Location: non_tech_form.php?post_id=$post_id&submit_error=1");
exit;
}
// โ
Begin transaction
$conn->begin_transaction();
try {
// 1. Insert into applications
$status = 'submitted';
$stmt = $conn->prepare("INSERT INTO applications (user_id, advertisement_id, post_id, computer_knowledge, payment_reference_no, declaration, status) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("iiissis", $user_id, $ad_id, $post_id, $computer_knowledge, $payment_reference, $declaration, $status);
$stmt->execute();
$application_id = $conn->insert_id;
$stmt->close();
// 2. Insert into application_posts
// $stmt = $conn->prepare("INSERT INTO application_posts (application_id, post_id) VALUES (?, ?)");
// $stmt->bind_param("ii", $application_id, $post_id);
// $stmt->execute();
// $stmt->close();
// 3. Qualifications
$qual_map = [
'SSC' => ['ssc_school', 'ssc_board', 'ssc_subjects', 'ssc_marks', 'ssc_scale', 'ssc_pass_date', 'ssc_mode', 'ssc_remarks'],
'HSC' => ['hsc_school', 'hsc_board', 'hsc_subjects', 'hsc_marks', 'hsc_scale', 'hsc_pass_date', 'hsc_mode', 'hsc_remarks'],
'UG' => ['ug_school', 'ug_board', 'ug_subjects', 'ug_marks', 'ug_scale', 'ug_pass_date', 'ug_mode', 'ug_remarks'],
'PG' => ['pg_school', 'pg_board', 'pg_subjects', 'pg_marks', 'pg_scale', 'pg_pass_date', 'pg_mode', 'pg_remarks'],
];
$stmt = $conn->prepare("INSERT INTO qualifications (application_id, qualification_type, school_name, board_or_university, subjects, marks, scale, pass_date, mode_of_study, remarks) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
foreach ($qual_map as $type => $fields) {
$school = $_POST[$fields[0]] ?? '';
$board = $_POST[$fields[1]] ?? '';
$subjects = $_POST[$fields[2]] ?? '';
$marks = isset($_POST[$fields[3]]) ? floatval($_POST[$fields[3]]) : null;
$scale = $_POST[$fields[4]] ?? '';
$pass_date = $_POST[$fields[5]] ?? null;
$mode = $_POST[$fields[6]] ?? '';
$remarks = $_POST[$fields[7]] ?? '';
if ($school || $board || $subjects || $marks || $scale || $pass_date || $mode || $remarks) {
$stmt->bind_param("isssssssss", $application_id, $type, $school, $board, $subjects, $marks, $scale, $pass_date, $mode, $remarks);
$stmt->execute();
}
}
$stmt->close();
// 4. Work Experience (if submitted)
if (!empty($_POST['experience'])) {
$stmt = $conn->prepare("INSERT INTO experiences (application_id, organization, designation, nature, `from`, `to`, govt) VALUES (?, ?, ?, ?, ?, ?, ?)");
foreach ($_POST['experience'] as $exp) {
$org = $exp['organization'] ?? '';
$des = $exp['designation'] ?? '';
$nature = $exp['nature'] ?? '';
$from = $exp['from'] ?? null;
$to = $exp['to'] ?? null;
$govt = $exp['govt'] ?? '';
if ($org || $des || $nature || $from || $to || $govt) {
$stmt->bind_param("issssss", $application_id, $org, $des, $nature, $from, $to, $govt);
$stmt->execute();
}
}
$stmt->close();
}
// 5. Upload files
$uploadRelBase = "sites/upload/applications/";
$uploadAbsBase = dirname(__DIR__) . "/public_html/sites/upload/applications/";
// Append application_id to both relative and absolute path
$uploadRel = $uploadRelBase . $application_id . '/';
$uploadAbs = $uploadAbsBase . $application_id . '/';
// Create application-specific directory if it does not exist
if (!is_dir($uploadAbs)) mkdir($uploadAbs, 0777, true);
$allowedMimes = ['image/jpeg', 'image/png', 'application/pdf'];
$maxSize = 2 * 1024 * 1024;
$stmt = $conn->prepare("INSERT INTO uploads (application_id, document_type, file_path) VALUES (?, ?, ?)");
foreach ($required_files as $f) {
$file = $_FILES[$f];
$mime = (new finfo(FILEINFO_MIME_TYPE))->file($file['tmp_name']);
if (!in_array($mime, $allowedMimes)) throw new Exception("Invalid file type: $f");
if ($file['size'] > $maxSize) throw new Exception("File too large: $f");
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$safeName = uniqid($f . '_', true) . '.' . $ext;
$destAbs = $uploadAbs . $safeName; // Now in app-specific dir
$destRel = $uploadRel . $safeName; // Now in app-specific dir
if (!move_uploaded_file($file['tmp_name'], $destAbs)) throw new Exception("Upload failed: $f");
$stmt->bind_param("iss", $application_id, $f, $destRel);
$stmt->execute();
}
$stmt->close();
// ... (your previous inserts: applications, application_posts, qualifications, experiences, uploads) ...
// 6. Insert into all_applications (master ledger)
$app_type = 'non-tech';
$status = 'submitted'; // or reuse the variable $status
$stmt = $conn->prepare("INSERT INTO all_applications (user_id, post_id, application_type, application_table_id, status) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("iisis", $user_id, $post_id, $app_type, $application_id, $status);
$stmt->execute();
$stmt->close();
// Now commit the transaction
$conn->commit();
$_SESSION['app_submitted'] = "True";
// ๐งพ Generate PDF
// $outputPath = generateApplicationPDF($user_id, $application_id, $conn);
// Saving All details to Folder
// TO DO
// ๐ Build Folder: sites/documents/{registration_id}/
$date_only = date("dmY");
$registration_id = $date_only . $user_id . $application_id . "P" . $post_id;
// $targetDir = "sites/documents/" . $registration_id . "/";
// if (!is_dir($targetDir)) {
// mkdir($targetDir, 0777, true);
// }
// ๐ Move Uploaded Files
$uploads = [];
$res = $conn->query("SELECT document_type, file_path FROM uploads WHERE application_id = $application_id");
if ($res && $res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
$uploads[$row["document_type"]] = $row["file_path"];
}
}
// foreach ($uploads as $docType => $filePathRelative) {
// $sourcePath = realpath(__DIR__ . "/" . $filePathRelative);
// if ($sourcePath && file_exists($sourcePath)) {
// $originalFileName = basename($sourcePath);
// $destinationPath = $targetDir . $originalFileName;
// if (!copy($sourcePath, $destinationPath)) {
// throw new Exception("Failed to copy file: {$docType}");
// }
// } else {
// error_log("Missing file: {$docType} โ {$sourcePath}");
// }
// }
// ๐งพ Copy PDF
// if (isset($outputPath) && file_exists($outputPath)) {
// $finalName = basename($outputPath);
// $finalDest = $targetDir . $finalName;
// if (!copy($outputPath, $finalDest)) {
// throw new Exception("Failed to copy PDF to {$finalDest}");
// }
// }
header("Location: dashboard.php?submitted=1");
exit;
} catch (Exception $e) {
$conn->rollback();
$_SESSION['form_errors'] = [$e->getMessage()];
header("Location: non_tech_form.php?post_id=$post_id&submit_error=1");
exit;
} finally {
$conn->close();
}