prepare("
SELECT working_area, district, state
FROM users
WHERE id = ? AND user_type = 'coordinator'
");
$stmt->execute([$currentUserId]);
$coordinator = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$coordinator) return false;
$stmt = $db->prepare("
SELECT id FROM users
WHERE id = ? AND (
working_area = ? OR
district = ? OR
state = ?
) AND user_type IN ('member', 'coordinator')
");
$stmt->execute([
$targetUserId,
$coordinator['working_area'] ?? '',
$coordinator['district'] ?? '',
$coordinator['state'] ?? ''
]);
return $stmt->fetch() !== false;
case 'member':
default:
return $targetUserId == $currentUserId;
}
}
// Build WHERE clause for role-based filtering
function buildUserWhereClause($currentUserId, $currentUserType, $db) {
$whereConditions = [];
$params = [];
switch ($currentUserType) {
case 'admin':
break;
case 'coordinator':
$stmt = $db->prepare("
SELECT working_area, district, state
FROM users
WHERE id = ? AND user_type = 'coordinator'
");
$stmt->execute([$currentUserId]);
$coordinator = $stmt->fetch(PDO::FETCH_ASSOC);
if ($coordinator) {
$whereConditions[] = "(
id = :current_user_id OR
working_area = :working_area OR
district = :district OR
state = :state
) AND user_type IN ('member', 'coordinator')";
$params[':current_user_id'] = $currentUserId;
$params[':working_area'] = $coordinator['working_area'] ?? '';
$params[':district'] = $coordinator['district'] ?? '';
$params[':state'] = $coordinator['state'] ?? '';
} else {
$whereConditions[] = "id = :current_user_id";
$params[':current_user_id'] = $currentUserId;
}
break;
case 'member':
default:
$whereConditions[] = "id = :current_user_id";
$params[':current_user_id'] = $currentUserId;
break;
}
return ['conditions' => $whereConditions, 'params' => $params];
}
// Handle AJAX requests
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax'])) {
header('Content-Type: application/json');
if ($_POST['ajax'] === 'generate_id_card') {
if (!verifyCSRF($_POST['csrf_token'])) {
echo json_encode(['success' => false, 'message' => 'Invalid CSRF token']);
exit;
}
$memberId = (int)$_POST['member_id'];
if (!canAccessUser($memberId, $currentUserId, $currentUserType, $db)) {
echo json_encode(['success' => false, 'message' => 'Access denied']);
exit;
}
$memberData = $idCardGenerator->getMemberData($memberId, 'id');
if ($memberData) {
echo json_encode([
'success' => true,
'data' => $memberData,
'javascript' => $idCardGenerator->generateJavaScript($memberData)
]);
} else {
echo json_encode(['success' => false, 'message' => 'User not found or not approved']);
}
exit;
}
if ($_POST['ajax'] === 'save_id_card') {
if (!verifyCSRF($_POST['csrf_token'])) {
echo json_encode(['success' => false, 'message' => 'Invalid CSRF token']);
exit;
}
$memberId = (int)$_POST['member_id'];
if (!canAccessUser($memberId, $currentUserId, $currentUserType, $db)) {
echo json_encode(['success' => false, 'message' => 'Access denied']);
exit;
}
$imageData = $_POST['image_data'] ?? '';
if (empty($imageData)) {
echo json_encode(['success' => false, 'message' => 'No image data provided']);
exit;
}
$result = $idCardGenerator->saveIdCardImage($memberId, $imageData, 'users');
echo json_encode($result);
exit;
}
}
// Get users for list with pagination and filters based on role
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$limit = 20;
$offset = ($page - 1) * $limit;
$search = isset($_GET['search']) ? trim(sanitizeInput($_GET['search'])) : '';
$status_filter = isset($_GET['status']) ? sanitizeInput($_GET['status']) : '';
$membership_filter = isset($_GET['membership']) ? sanitizeInput($_GET['membership']) : '';
$user_type_filter = isset($_GET['user_type']) ? sanitizeInput($_GET['user_type']) : '';
try {
// Validate filter inputs
$valid_statuses = ['pending', 'approved', 'rejected', ''];
$valid_membership_types = ['active', 'gram_panchayat', 'block', 'tehsil', 'district', 'mandal', 'state', 'national', ''];
$valid_user_types = ['member', 'coordinator', 'admin', ''];
if (!in_array($status_filter, $valid_statuses)) {
$status_filter = '';
}
if (!in_array($membership_filter, $valid_membership_types)) {
$membership_filter = '';
}
if (!in_array($user_type_filter, $valid_user_types)) {
$user_type_filter = '';
}
// Build role-based WHERE clause
$roleAccess = buildUserWhereClause($currentUserId, $currentUserType, $db);
$whereConditions = $roleAccess['conditions'];
$params = $roleAccess['params'];
// Add additional filters
if (!empty($search)) {
$whereConditions[] = "(name LIKE :search OR email LIKE :search OR mobile LIKE :search OR registration_id LIKE :search)";
$params[':search'] = '%' . $search . '%';
}
if (!empty($status_filter)) {
$whereConditions[] = "status = :status";
$params[':status'] = $status_filter;
}
if (!empty($membership_filter)) {
$whereConditions[] = "membership_type = :membership";
$params[':membership'] = $membership_filter;
}
if (!empty($user_type_filter)) {
$whereConditions[] = "user_type = :user_type";
$params[':user_type'] = $user_type_filter;
}
$whereClause = !empty($whereConditions) ? 'WHERE ' . implode(' AND ', $whereConditions) : '';
// Get users count
$countQuery = "SELECT COUNT(*) FROM users $whereClause";
$stmt = $db->prepare($countQuery);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value, is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR);
}
$stmt->execute();
$totalRecords = $stmt->fetchColumn();
// Get users with pagination
$query = "SELECT id, name, mobile, email, profile_image, designation, registration_id, status, membership_type, user_type, blood_group, valid_from, valid_until, created_at
FROM users $whereClause
ORDER BY created_at DESC
LIMIT :limit OFFSET :offset";
$stmt = $db->prepare($query);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value, is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR);
}
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
$totalPages = ceil($totalRecords / $limit);
} catch (PDOException $e) {
$error = "Database error: " . $e->getMessage();
error_log("Database error in user query: " . $e->getMessage());
$users = [];
$totalPages = 0;
}
// Get user for generation
$user = null;
if ($action === 'generate' && $id > 0) {
if (!canAccessUser($id, $currentUserId, $currentUserType, $db)) {
$error = "Access denied. You can only generate ID cards for authorized users.";
$action = 'list';
} else {
$user = $idCardGenerator->getMemberData($id, 'id');
if (!$user) {
$error = "User not found or not approved.";
$action = 'list';
}
}
}
// Page title based on role
$rolePageTitles = [
'admin' => 'ID Card Generator - All Users',
'coordinator' => 'ID Card Generator - Your Area',
'member' => 'My ID Card'
];
include 'includes/header.php';
?>
Access Level:
You can generate ID cards for users in your working area and your own ID card.
You can only view and generate your own ID card.
| # |
Name |
Mobile |
Email |
Designation |
Registration ID |
Status |
Actions |
| No users found. |
|
|
|
|
|
|
|
Generate
|
1): ?>
| Name: |
|
| ID: |
|
| User Type: |
|
| Designation: |
|
| Mobile: |
|
| Email: |
|
| Blood Group: |
|
| Valid From: |
|
| Valid Until: |
|
No user data available.
Loading...
Generating ID Card...
generateJavaScript($user, 'idCardCanvas'); ?>