|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/emp.outerorbittech.in/admin/ | |
|
Path: /home2/outerorb/emp.outerorbittech.in/admin/status_update.php
Size: 2.43 KB
Permissions: 0666
<?php
require __DIR__ . '/../includes/helpers.php';
require_admin();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
redirect_with_message('dashboard.php', 'Invalid request.', 'error');
}
if (!isset($_POST['csrf_token']) || !verify_csrf($_POST['csrf_token'])) {
redirect_with_message('dashboard.php', 'Session expired. Try again.', 'error');
}
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
if ($id <= 0) {
redirect_with_message('dashboard.php', 'Invalid record.', 'error');
}
$statusOptions = ['Active', 'Left', 'Terminated', 'Absconded / Defaulted'];
$employeeStatus = sanitize_text($_POST['employee_status'] ?? '');
$lastWorkingDateInput = sanitize_text($_POST['last_working_date'] ?? '');
$reasonForLeaving = sanitize_text($_POST['reason_for_leaving'] ?? '');
$returnTo = sanitize_text($_POST['return_to'] ?? 'dashboard.php');
$returnTo = $returnTo ?: 'dashboard.php';
if (stripos($returnTo, 'http://') === 0 || stripos($returnTo, 'https://') === 0) {
$returnTo = 'dashboard.php';
}
if (!in_array($employeeStatus, $statusOptions, true)) {
redirect_with_message($returnTo, 'Please select a valid status.', 'error');
}
ensure_employees_table();
$pdo = db();
$check = $pdo->prepare('SELECT department FROM employees WHERE id = ? LIMIT 1');
$check->execute([$id]);
$emp = $check->fetch();
if (!$emp) {
redirect_with_message($returnTo, 'Record not found or is in Trash.', 'error');
}
enforce_department_access($emp);
$lastWorkingDate = null;
if ($employeeStatus !== 'Active' && $lastWorkingDateInput !== '') {
$normalized = normalize_date($lastWorkingDateInput);
if ($normalized === null) {
redirect_with_message($returnTo, 'Last working date must be a valid date (YYYY-MM-DD).', 'error');
}
$lastWorkingDate = $normalized;
}
if ($employeeStatus === 'Active') {
$lastWorkingDate = null;
$reasonForLeaving = '';
}
$stmt = $pdo->prepare('UPDATE employees SET employee_status = :status, last_working_date = :last_working_date, reason_for_leaving = :reason_for_leaving WHERE id = :id AND deleted_at IS NULL');
$stmt->execute([
':status' => $employeeStatus,
':last_working_date' => $lastWorkingDate,
':reason_for_leaving' => $reasonForLeaving ?: null,
':id' => $id,
]);
if ($stmt->rowCount() === 0) {
redirect_with_message($returnTo, 'Record not found or is in Trash.', 'error');
}
redirect_with_message($returnTo, 'Status updated.');