|
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/export.php
Size: 5.32 KB
Permissions: 0666
<?php
require __DIR__ . '/../includes/helpers.php';
require_admin();
ensure_employees_table();
ensure_designations_table();
$q = isset($_GET['q']) ? sanitize_text($_GET['q']) : '';
$statusOptions = ['Active', 'Left', 'Terminated', 'Absconded / Defaulted', 'All'];
$status = isset($_GET['status']) ? sanitize_text($_GET['status']) : 'All';
if (!in_array($status, $statusOptions, true)) {
$status = 'All';
}
$departmentFilter = isset($_GET['department']) ? sanitize_text($_GET['department']) : '';
$departments = list_departments();
if ($departmentFilter !== '' && !in_array($departmentFilter, $departments, true)) {
$departmentFilter = '';
}
$designationRows = list_designations(true);
$designationOptions = array_map(fn($row) => (int) $row['id'], $designationRows);
$designationFilter = isset($_GET['designation']) ? (int) $_GET['designation'] : 0;
if ($designationFilter && !in_array($designationFilter, $designationOptions, true)) {
$designationFilter = 0;
}
$pdo = db();
$sql = 'SELECT e.*, d.name AS designation_name FROM employees e LEFT JOIN designations d ON e.designation_id = d.id';
$where = ['e.deleted_at IS NULL'];
$params = [];
apply_department_filter($where, $params, 'e');
if ($status !== 'All') {
$where[] = 'e.employee_status = :status';
$params[':status'] = $status;
}
if ($departmentFilter !== '') {
$where[] = 'e.department = :department';
$params[':department'] = $departmentFilter;
}
if ($designationFilter) {
$where[] = 'e.designation_id = :designation_id';
$params[':designation_id'] = $designationFilter;
}
if ($q !== '') {
$where[] = '(e.first_name LIKE :q_fn OR e.last_name LIKE :q_ln OR e.email LIKE :q_em OR e.phone LIKE :q_ph)';
$params[':q_fn'] = "%{$q}%";
$params[':q_ln'] = "%{$q}%";
$params[':q_em'] = "%{$q}%";
$params[':q_ph'] = "%{$q}%";
}
if (!empty($where)) {
$sql .= ' WHERE ' . implode(' AND ', $where);
}
$sql .= ' ORDER BY e.created_at DESC';
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll();
$referralLookup = [
'consultancy' => 'Consultancy',
'person' => 'Person',
'walkin' => 'Walk-in (Self)',
];
$headers = [
'ID',
'Full Name',
"Father's Name",
"Mother's Name",
'Email',
'Contact Number',
'Emergency Contact Name',
'Emergency Contact Number',
'Emergency Contact Relation',
'Date of Birth',
'Date of Joining',
'Present Address',
'Permanent Address',
'City',
'State',
'Present ZIP / PIN',
'Aadhaar Number',
'PAN Number',
'Highest Qualification',
'Department / Process',
'Designation',
'Employee Status',
'Last Working Date',
'Reason for Leaving',
'Marital Status',
'Gender',
'Identity Mark',
'Blood Group',
'Referred By',
'Referral Name',
'Account Number',
'IFSC Code',
'Bank Name',
'Aadhaar Uploaded',
'PAN Uploaded',
'Graduation Uploaded',
'10th Marksheet Uploaded',
'12th Marksheet Uploaded',
'Bank Proof Uploaded',
'Photo Uploaded',
'Created At',
];
$hasFile = function (?string $path): string {
return resolve_upload_path($path) ? 'Yes' : 'No';
};
$clean = function ($value) {
$value = (string)($value ?? '');
return str_replace(["\r", "\n"], ' ', $value);
};
$filename = 'employees_export_' . date('Ymd_His') . '.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
$fh = fopen('php://output', 'w');
fputcsv($fh, $headers);
foreach ($rows as $row) {
$referredBy = $referralLookup[$row['referred_by'] ?? ''] ?? $row['referred_by'] ?? '';
fputcsv($fh, [
$row['id'],
$clean($row['first_name']),
$clean($row['last_name']),
$clean($row['mother_name']),
$clean($row['email']),
$clean($row['phone']),
$clean($row['emergency_contact_name']),
$clean($row['emergency_contact_number']),
$clean($row['emergency_contact_relation']),
$clean($row['dob']),
$clean($row['date_of_joining']),
$clean($row['address']),
$clean($row['permanent_address']),
$clean($row['city']),
$clean($row['state']),
$clean($row['zip']),
$clean($row['aadhaar_number']),
$clean($row['pan_number']),
$clean($row['highest_qualification']),
$clean($row['department']),
$clean($row['designation_name'] ?? ''),
$clean($row['employee_status']),
$clean($row['last_working_date']),
$clean($row['reason_for_leaving']),
$clean($row['marital_status']),
$clean($row['gender']),
$clean($row['identity_mark']),
$clean($row['blood_group']),
$clean($referredBy),
$clean($row['referral_name']),
$clean($row['account_number']),
$clean($row['ifsc_code']),
$clean($row['bank_name']),
$hasFile($row['aadhaar_path']),
$hasFile($row['pan_path']),
$hasFile($row['qualification_path']),
$hasFile($row['tenth_marksheet_path']),
$hasFile($row['twelfth_marksheet_path']),
$hasFile($row['bank_proof_path']),
$hasFile($row['photo_path']),
$clean($row['created_at']),
]);
}
fclose($fh);
exit;