format('d-m-Y'); } catch (Exception $e) { return '-'; } } // Handle AJAX request for designations if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['ajax']) && $_GET['ajax'] === 'get_designations') { header('Content-Type: application/json'); if (!isset($_GET['membership_type']) || empty($_GET['membership_type'])) { echo json_encode(['success' => false, 'message' => 'Membership type is required']); exit; } $membership_type = sanitizeInput($_GET['membership_type']); // Validate membership type $valid_memberships = [ 'active', 'gram_panchayat', 'block', 'tehsil', 'district', 'mandal', 'state', 'national' ]; if (!in_array($membership_type, $valid_memberships)) { echo json_encode(['success' => false, 'message' => 'Invalid membership type']); exit; } try { $designations = getDesignationsByMembershipType($membership_type); echo json_encode([ 'success' => true, 'designations' => $designations ]); } catch (Exception $e) { logError('Error fetching designations: ' . $e->getMessage()); echo json_encode(['success' => false, 'message' => 'Failed to fetch designations']); } exit; } // Handle form submissions if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!isset($_POST['csrf_token']) || !verifyCSRF($_POST['csrf_token'])) { $error = 'अमान्य CSRF टोकन।'; } else if (isset($_POST['action'])) { switch ($_POST['action']) { case 'add': case 'edit': try { $name = sanitizeInput($_POST['name']); $email = sanitizeInput($_POST['email']); $mobile = sanitizeInput($_POST['mobile']); $gender = sanitizeInput($_POST['gender']); $dob = sanitizeInput($_POST['dob']); $profession = sanitizeInput($_POST['profession']); $designation = sanitizeInput($_POST['designation']); $state = sanitizeInput($_POST['state']); $district = sanitizeInput($_POST['district']); $address = sanitizeInput($_POST['address']); $pincode = sanitizeInput($_POST['pincode']); $status = sanitizeInput($_POST['status']); $membership_type = sanitizeInput($_POST['membership_type']); $registration_id = sanitizeInput($_POST['registration_id']); $valid_until = sanitizeInput($_POST['valid_until']); $user_type = isAdmin() ? sanitizeInput($_POST['user_type'] ?? 'member') : 'member'; $working_area = sanitizeInput($_POST['working_area']); // Validate designation $valid_designations = array_column(getDesignationsByMembershipType($membership_type), 'designation'); if (!in_array($designation, $valid_designations)) { throw new Exception('अमान्य पद चयन। कृपया उपलब्ध विकल्पों में से चुनें।'); } // Validate valid_until date if (!DateTime::createFromFormat('Y-m-d', $valid_until)) { throw new Exception('अमान्य वैधता तिथि। कृपया सही तारीख चुनें।'); } $today = date('Y-m-d'); if ($valid_until < $today) { throw new Exception('वैधता तिथि आज की तारीख से पहले नहीं हो सकती।'); } // Handle photo upload $profile_image = ''; if (!empty($_FILES['photo']['name']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) { $upload_dir = '../img/profiles/'; if (!file_exists($upload_dir)) { mkdir($upload_dir, 0755, true); } $file_ext = strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION)); $allowed_ext = ['jpg', 'jpeg', 'png', 'gif']; if (in_array($file_ext, $allowed_ext)) { if ($_FILES['photo']['size'] <= 5 * 1024 * 1024) { $profile_image = uniqid('user_') . '.' . $file_ext; if (!move_uploaded_file($_FILES['photo']['tmp_name'], $upload_dir . $profile_image)) { throw new Exception('फोटो अपलोड में त्रुटि।'); } } else { throw new Exception('फोटो का साइज़ 5MB से अधिक नहीं होना चाहिए।'); } } else { throw new Exception('अनुमत नहीं फ़ाइल प्रकार। केवल JPG, JPEG, PNG, या GIF फ़ाइलें अपलोड करें।'); } } // Handle ID card photo upload $id_card_photo = ''; if (!empty($_FILES['id_card_photo']['name']) && $_FILES['id_card_photo']['error'] === UPLOAD_ERR_OK) { $upload_dir = '../img/profiles/'; if (!file_exists($upload_dir)) { mkdir($upload_dir, 0755, true); } $file_ext = strtolower(pathinfo($_FILES['id_card_photo']['name'], PATHINFO_EXTENSION)); $allowed_ext = ['jpg', 'jpeg', 'png', 'gif']; if (in_array($file_ext, $allowed_ext)) { if ($_FILES['id_card_photo']['size'] <= 5 * 1024 * 1024) { $id_card_photo = uniqid('idcard_') . '.' . $file_ext; if (!move_uploaded_file($_FILES['id_card_photo']['tmp_name'], $upload_dir . $id_card_photo)) { throw new Exception('आईडी कार्ड फोटो अपलोड में त्रुटि।'); } } else { throw new Exception('आईडी कार्ड फोटो का साइज़ 5MB से अधिक नहीं होना चाहिए।'); } } else { throw new Exception('अनुमत नहीं फ़ाइल प्रकार। केवल JPG, JPEG, PNG, या GIF फ़ाइलें अपलोड करें।'); } } if ($_POST['action'] === 'add') { // Generate registration_id if not provided if (empty($registration_id)) { $stmt = $db->prepare("SELECT MAX(CAST(SUBSTRING(registration_id, 5) AS UNSIGNED)) as max_num FROM users WHERE registration_id LIKE ?"); $stmt->execute(["NDFR%"]); $result = $stmt->fetch(); $next_num = ($result['max_num'] ?? 0) + 1; $registration_id = "NDFR" . str_pad($next_num, 5, '0', STR_PAD_LEFT); } $stmt = $db->prepare(" INSERT INTO users ( registration_id, name, email, mobile, gender, dob, profession, designation, state, district, address, pincode, membership_type, profile_image, aadhar_image, status, user_type, created_at, valid_until, working_area ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?) "); $stmt->execute([ $registration_id, $name, $email, $mobile, $gender, $dob, $profession, $designation, $state, $district, $address, $pincode, $membership_type, $profile_image, $id_card_photo, $status, $user_type, $valid_until, $working_area ]); $message = 'सदस्य सफलतापूर्वक जोड़ा गया!'; } else { // Keep existing photos if no new photos uploaded if (empty($profile_image) || empty($id_card_photo)) { $stmt = $db->prepare("SELECT profile_image, aadhar_image FROM users WHERE id = ?"); $stmt->execute([$id]); $existing = $stmt->fetch(); $profile_image = empty($profile_image) ? ($existing['profile_image'] ?? '') : $profile_image; $id_card_photo = empty($id_card_photo) ? ($existing['aadhar_image'] ?? '') : $id_card_photo; } $stmt = $db->prepare(" UPDATE users SET registration_id = ?, name = ?, email = ?, mobile = ?, gender = ?, dob = ?, profession = ?, designation = ?, state = ?, district = ?, address = ?, pincode = ?, membership_type = ?, profile_image = ?, aadhar_image = ?, status = ?, user_type = ?, updated_at = NOW(), valid_until = ?, working_area = ? WHERE id = ? "); $stmt->execute([ $registration_id, $name, $email, $mobile, $gender, $dob, $profession, $designation, $state, $district, $address, $pincode, $membership_type, $profile_image, $id_card_photo, $status, $user_type, $valid_until, $working_area, $id ]); $message = 'सदस्य जानकारी अपडेट की गई!'; } $action = 'list'; } catch (Exception $e) { $error = 'त्रुटि: ' . htmlspecialchars($e->getMessage()); } break; case 'delete': if (!isAdmin()) { $error = 'केवल व्यवस्थापक ही उपयोगकर्ता हटा सकते हैं।'; break; } try { $delete_id = $_POST['id']; // Get photo filenames before deleting $stmt = $db->prepare("SELECT profile_image, aadhar_image FROM users WHERE id = ?"); $stmt->execute([$delete_id]); $user = $stmt->fetch(); // Delete user $stmt = $db->prepare("DELETE FROM users WHERE id = ?"); $stmt->execute([$delete_id]); // Delete photos if they exist if (!empty($user['profile_image']) && file_exists('../img/profiles/' . $user['profile_image'])) { unlink('../img/profiles/' . $user['profile_image']); } if (!empty($user['aadhar_image']) && file_exists('../img/profiles/' . $user['aadhar_image'])) { unlink('../img/profiles/' . $user['aadhar_image']); } $message = 'सदस्य सफलतापूर्वक हटाया गया!'; } catch (Exception $e) { $error = 'हटाने में त्रुटि: ' . htmlspecialchars($e->getMessage()); } break; } } } // Get user data for editing $user_data = null; if ($action === 'edit' && $id > 0) { $stmt = $db->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$id]); $user_data = $stmt->fetch(); if (!$user_data) { $error = 'सदस्य नहीं मिला!'; $action = 'list'; } } // Initialize $user_data as empty array if null to prevent null access $user_data = $user_data ?: []; // Get users list $users = []; if ($action === 'list') { $where_clause = ''; $params = []; if (!empty($status_filter)) { $where_clause = 'WHERE status = ?'; $params[] = $status_filter; } $stmt = $db->prepare("SELECT * FROM users $where_clause ORDER BY created_at DESC"); $stmt->execute($params); $users = $stmt->fetchAll(); } // Function to get proper image URL function getImageUrl($filename) { if (empty($filename)) { return ''; } $imagePath = '../img/profiles/' . $filename; if (file_exists($imagePath)) { return SITE_URL . '/img/profiles/' . $filename; } return ''; } include 'includes/header.php'; ?>