Mini Shell
| Direktori : /home/mhcadmin/.trash/ |
|
|
| Current File : /home/mhcadmin/.trash/get_admins_by_department.php |
<?php
session_start();
$rootPath = dirname(__FILE__);
require_once $rootPath . '/config/config.php';
require_once $rootPath . '/include/auth_validate.php';
// Enable error reporting for debugging (remove in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Set header to return JSON
header('Content-Type: application/json');
// Create a response array
$response = [
'success' => false,
'message' => '',
'admins' => [],
'debug' => []
];
try {
// Check if user is logged in - more flexible check
if (!isset($_SESSION['user_id']) && !isset($_SESSION['admin_id']) && !isset($_SESSION['id'])) {
$response['message'] = 'Unauthorized access - No user session';
$response['debug']['session'] = $_SESSION;
echo json_encode($response);
exit();
}
// Check if department is provided
if (!isset($_POST['department']) || empty($_POST['department'])) {
$response['message'] = 'No department specified';
echo json_encode($response);
exit();
}
$department = trim($_POST['department']);
// Check database connection
if (!isset($conn) || !$conn) {
$response['message'] = 'Database connection failed';
$response['debug']['conn_isset'] = isset($conn) ? 'true' : 'false';
echo json_encode($response);
exit();
}
// Escape the department
$department = mysqli_real_escape_string($conn, $department);
// IMPORTANT FIX: Check what departments actually exist in the database
$check_query = "SELECT DISTINCT UserDepartment FROM admin_accounts WHERE UserDepartment IS NOT NULL AND UserDepartment != '' ORDER BY UserDepartment";
$check_result = mysqli_query($conn, $check_query);
$available_departments = [];
if ($check_result) {
while ($dept_row = mysqli_fetch_assoc($check_result)) {
$available_departments[] = $dept_row['UserDepartment'];
}
}
$response['debug']['available_departments'] = $available_departments;
$response['debug']['requested_department'] = $department;
// Query to get active admins - FIXED: Using correct column names
$query = "SELECT id, Full_Name, admin_type, UserDepartment
FROM admin_accounts
WHERE UserDepartment = '$department'
AND (Status = 'Active' OR Status IS NULL OR Status = '')
ORDER BY
CASE WHEN admin_type = 'super' THEN 0 ELSE 1 END,
Full_Name ASC";
$response['debug']['query'] = $query;
$result = mysqli_query($conn, $query);
if (!$result) {
$response['message'] = 'Database query error';
$response['debug']['mysql_error'] = mysqli_error($conn);
echo json_encode($response);
exit();
}
$response['debug']['num_rows'] = mysqli_num_rows($result);
$admins = [];
while ($row = mysqli_fetch_assoc($result)) {
$admins[] = [
'id' => $row['id'],
'full_name' => $row['Full_Name'],
'admin_type' => !empty($row['admin_type']) ? $row['admin_type'] : 'admin'
];
}
$response['success'] = true;
$response['admins'] = $admins;
$response['message'] = 'Admins fetched successfully';
$response['count'] = count($admins);
} catch (Exception $e) {
$response['message'] = 'Exception: ' . $e->getMessage();
$response['debug']['exception'] = true;
$response['debug']['exception_message'] = $e->getMessage();
}
// Return the response
echo json_encode($response);
exit();
?>