Sindbad~EG File Manager
<?php
require_once '../../config/config.php';
checkLogin();
// Membership module requires assembly level access or higher
checkAccess('assembly');
$pageTitle = "Member Management - " . APP_NAME;
$db = Database::getInstance()->getConnection();
// Get user access parameters
$accessLevel = $_SESSION['access_level'] ?? 'assembly';
$areaId = $_SESSION['area_id'] ?? null;
$districtId = $_SESSION['district_id'] ?? null;
$assemblyId = $_SESSION['assembly_id'] ?? null;
// Load districts and assemblies for filters
$districts = [];
$assemblies = [];
try {
$districtsStmt = $db->query("SELECT id, district_name FROM districts WHERE is_active = 1 ORDER BY district_name");
$districts = $districtsStmt->fetchAll();
} catch (PDOException $e) {
// ignore if table not ready
}
try {
// Include district_id so we can filter assemblies by district on the client side
$assembliesStmt = $db->query("SELECT id, assembly_name, district_id FROM assemblies WHERE is_active = 1 ORDER BY assembly_name");
$assemblies = $assembliesStmt->fetchAll();
} catch (PDOException $e) {
// ignore if table not ready
}
// Pagination settings
$allowedLimits = [20, 50, 100];
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 20;
if (!in_array($limit, $allowedLimits, true)) {
$limit = 20;
}
$currentPage = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$offset = ($currentPage - 1) * $limit;
// Check if membershipcard_id column exists
$columnExists = false;
try {
$checkStmt = $db->query("SHOW COLUMNS FROM members LIKE 'membershipcard_id'");
$columnExists = $checkStmt->fetch() !== false;
} catch (PDOException $e) {
// Column doesn't exist
}
// Base parts of query
$memberIdField = $columnExists ? 'm.membershipcard_id' : 'CONCAT("MC", YEAR(CURDATE()), LPAD(m.id, 6, "0")) as membershipcard_id';
$baseFrom = " FROM members m
JOIN areas a ON m.area_id = a.id
JOIN districts d ON m.district_id = d.id
JOIN assemblies asm ON m.assembly_id = asm.id
WHERE 1=1";
$where = '';
$params = [];
// Access level filters
if ($accessLevel === 'assembly') {
$where .= " AND m.assembly_id = :assembly_id";
$params['assembly_id'] = $assemblyId;
} elseif ($accessLevel === 'district') {
$where .= " AND m.district_id = :district_id";
$params['district_id'] = $districtId;
} elseif ($accessLevel === 'area') {
$where .= " AND m.area_id = :area_id";
$params['area_id'] = $areaId;
}
// Additional filters by district and assembly (from GET)
if (isset($_GET['district_filter']) && $_GET['district_filter'] !== '') {
$where .= " AND m.district_id = :filter_district_id";
$params['filter_district_id'] = (int)$_GET['district_filter'];
}
if (isset($_GET['assembly_filter']) && $_GET['assembly_filter'] !== '') {
$where .= " AND m.assembly_id = :filter_assembly_id";
$params['filter_assembly_id'] = (int)$_GET['assembly_filter'];
}
// Search filter
if (isset($_GET['search']) && !empty($_GET['search'])) {
$search = '%' . $_GET['search'] . '%';
$searchField = $columnExists ? 'm.membershipcard_id' : 'CONCAT("MC", YEAR(CURDATE()), LPAD(m.id, 6, "0"))';
$where .= " AND (m.first_name LIKE :search1 OR m.last_name LIKE :search2 OR {$searchField} LIKE :search3 OR m.email LIKE :search4)";
$params['search1'] = $search;
$params['search2'] = $search;
$params['search3'] = $search;
$params['search4'] = $search;
}
// Total count for pagination
$countQuery = "SELECT COUNT(*) AS total" . $baseFrom . $where;
$countStmt = $db->prepare($countQuery);
$countStmt->execute($params);
$totalMembers = (int)$countStmt->fetchColumn();
// Total active members (using same filters)
$activeWhere = $where . " AND (m.is_active = 1 OR m.is_active IS NULL)";
$activeCountQuery = "SELECT COUNT(*) AS total" . $baseFrom . $activeWhere;
$activeCountStmt = $db->prepare($activeCountQuery);
$activeCountStmt->execute($params);
$totalActiveMembers = (int)$activeCountStmt->fetchColumn();
// Total members created this month (using same filters)
$monthWhere = $where . " AND DATE_FORMAT(m.created_at, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')";
$monthCountQuery = "SELECT COUNT(*) AS total" . $baseFrom . $monthWhere;
$monthCountStmt = $db->prepare($monthCountQuery);
$monthCountStmt->execute($params);
$totalThisMonth = (int)$monthCountStmt->fetchColumn();
// Compute total pages and normalize current page
$totalPages = $totalMembers > 0 ? (int)ceil($totalMembers / $limit) : 1;
$totalPages = max(1, $totalPages);
$currentPage = (int)$currentPage;
if ($currentPage < 1) {
$currentPage = 1;
} elseif ($currentPage > $totalPages) {
$currentPage = $totalPages;
}
$offset = ($currentPage - 1) * $limit;
// Main query with pagination
$query = "SELECT m.id, {$memberIdField}, m.first_name, m.middle_name, m.last_name,
m.gender, m.phone, m.email, m.member_type, m.profile_photo, m.created_at,
m.title, m.is_active, m.communicant,
a.area_name, d.district_name, asm.assembly_name "
. $baseFrom . $where .
" ORDER BY m.created_at DESC LIMIT {$limit} OFFSET {$offset}";
$stmt = $db->prepare($query);
$stmt->execute($params);
$members = $stmt->fetchAll();
include '../../includes/header.php';
?>
<?php include '../../includes/sidebar.php'; ?>
<!-- Main Content -->
<main class="flex-1 md:ml-64 mt-16">
<div class="container mx-auto px-4 py-8">
<!-- Page Header -->
<div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-6 gap-4">
<div>
<h1 class="text-3xl font-bold text-gray-800">
<i class="fas fa-users mr-2 text-blue-500"></i>Member Management
</h1>
<p class="text-gray-600 mt-2">Manage church members and their information</p>
</div>
<div class="flex gap-3">
<a href="upload.php" class="btn-gradient-orange text-white px-6 py-3 rounded-full font-semibold transition shadow-lg hover:shadow-xl">
<i class="fas fa-upload mr-2"></i>Upload Members
</a>
<a href="add.php" class="btn-gradient text-white px-6 py-3 rounded-full font-semibold transition shadow-lg hover:shadow-xl">
<i class="fas fa-plus mr-2"></i>Add New Member
</a>
<a href="members_edit.php" class="bg-indigo-600 text-white px-6 py-3 rounded-full font-semibold transition shadow-lg hover:bg-indigo-700">
<i class="fas fa-user-edit mr-2"></i>Edit Members
</a>
<a href="members_export.php" class="bg-green-600 text-white px-6 py-3 rounded-full font-semibold transition shadow-lg hover:bg-green-700">
<i class="fas fa-file-export mr-2"></i>Members Export
</a>
</div>
</div>
<!-- Stats Cards -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
<div class="bg-white rounded-lg shadow p-4 border-l-4 border-blue-500">
<div class="flex items-center justify-between">
<div>
<p class="text-gray-500 text-sm">Total Members</p>
<p class="text-2xl font-bold text-gray-800"><?php echo $totalMembers; ?></p>
</div>
<i class="fas fa-users text-3xl text-blue-500"></i>
</div>
</div>
<div class="bg-white rounded-lg shadow p-4 border-l-4 border-green-500">
<div class="flex items-center justify-between">
<div>
<p class="text-gray-500 text-sm">Active Members</p>
<p class="text-2xl font-bold text-gray-800"><?php echo $totalActiveMembers; ?></p>
</div>
<i class="fas fa-user-check text-3xl text-green-500"></i>
</div>
</div>
<div class="bg-white rounded-lg shadow p-4 border-l-4 border-yellow-500">
<div class="flex items-center justify-between">
<div>
<p class="text-gray-500 text-sm">Communicants</p>
<p class="text-2xl font-bold text-gray-800">
<?php echo count(array_filter($members, fn($m) => ($m['communicant'] ?? 0))); ?>
</p>
</div>
<i class="fas fa-cross text-3xl text-yellow-500"></i>
</div>
</div>
<div class="bg-white rounded-lg shadow p-4 border-l-4 border-purple-500">
<div class="flex items-center justify-between">
<div>
<p class="text-gray-500 text-sm">This Month</p>
<p class="text-2xl font-bold text-gray-800"><?php echo $totalThisMonth; ?></p>
</div>
<i class="fas fa-calendar-plus text-3xl text-purple-500"></i>
</div>
</div>
</div>
<!-- Search and Filters -->
<div class="bg-white rounded-lg shadow-lg p-6 mb-6">
<form method="GET" action="" class="flex flex-col md:flex-row gap-4 items-center">
<div class="flex-1 w-full">
<div class="relative">
<input type="text"
name="search"
placeholder="Search by name, member ID, or email..."
value="<?php echo htmlspecialchars($_GET['search'] ?? ''); ?>"
class="w-full pl-10 pr-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
<i class="fas fa-search absolute left-3 top-4 text-gray-400"></i>
</div>
</div>
<div class="flex items-center space-x-3">
<div>
<label class="block text-xs font-semibold text-gray-600 mb-1">District</label>
<select name="district_filter" id="filterDistrictSelect" class="px-3 py-2 border border-gray-300 rounded-lg text-sm">
<option value="">All</option>
<?php foreach ($districts as $dist): ?>
<option value="<?php echo $dist['id']; ?>" <?php echo (isset($_GET['district_filter']) && $_GET['district_filter'] == $dist['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($dist['district_name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<label class="block text-xs font-semibold text-gray-600 mb-1">Assembly</label>
<select name="assembly_filter" id="filterAssemblySelect" class="px-3 py-2 border border-gray-300 rounded-lg text-sm">
<option value="">All</option>
<?php foreach ($assemblies as $asm): ?>
<option value="<?php echo $asm['id']; ?>" data-district-id="<?php echo $asm['district_id']; ?>" <?php echo (isset($_GET['assembly_filter']) && $_GET['assembly_filter'] == $asm['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($asm['assembly_name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<label class="block text-xs font-semibold text-gray-600 mb-1">Page Size</label>
<select name="limit" onchange="this.form.submit()" class="px-3 py-2 border border-gray-300 rounded-lg text-sm">
<option value="20" <?php echo $limit == 20 ? 'selected' : ''; ?>>20</option>
<option value="50" <?php echo $limit == 50 ? 'selected' : ''; ?>>50</option>
<option value="100" <?php echo $limit == 100 ? 'selected' : ''; ?>>100</option>
</select>
</div>
<button type="submit" class="bg-blue-500 text-white px-6 py-3 rounded-lg hover:bg-blue-600 transition">
<i class="fas fa-filter mr-2"></i>Filter
</button>
<a href="index.php" class="bg-gray-500 text-white px-6 py-3 rounded-lg hover:bg-gray-600 transition text-center">
<i class="fas fa-redo mr-2"></i>Reset
</a>
</div>
</form>
</div>
<!-- Members Table -->
<div class="bg-white rounded-lg shadow-lg overflow-hidden">
<?php if (!empty($members)): ?>
<div class="flex justify-between items-center px-4 py-3 border-b border-gray-200 bg-gray-50">
<div class="flex space-x-2">
<button type="button" onclick="submitBulkDelete()" class="bg-red-600 text-white px-4 py-2 rounded-lg hover:bg-red-700 transition">
<i class="fas fa-trash mr-1"></i>Bulk Delete
</button>
<a href="bulk-update.php" class="bg-purple-600 text-white px-4 py-2 rounded-lg hover:bg-purple-700 transition inline-flex items-center">
<i class="fas fa-file-upload mr-1"></i>Bulk Update
</a>
<a href="duplicate-entries.php" class="bg-yellow-600 text-white px-4 py-2 rounded-lg hover:bg-yellow-700 transition inline-flex items-center">
<i class="fas fa-copy mr-1"></i>Duplicate Entries
</a>
</div>
</div>
<?php endif; ?>
<div class="overflow-x-auto">
<form id="bulkForm" method="POST">
<table class="min-w-full divide-y divide-gray-200" id="membersTable">
<thead class="bg-gradient-to-r from-blue-500 to-blue-600">
<tr>
<th class="px-4 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">
<input type="checkbox" id="selectAll" class="rounded">
</th>
<th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Member</th>
<th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Member ID</th>
<th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Contact</th>
<th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Location</th>
<th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Status</th>
<th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Actions</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
<?php if (empty($members)): ?>
<tr>
<td colspan="6" class="px-6 py-12 text-center text-gray-500">
<i class="fas fa-users text-5xl mb-4 text-gray-300"></i>
<p class="text-lg">No members found</p>
<a href="add.php" class="text-blue-500 hover:text-blue-600 mt-2 inline-block">
Add your first member <i class="fas fa-arrow-right ml-1"></i>
</a>
</td>
</tr>
<?php else: ?>
<?php foreach ($members as $member): ?>
<tr class="hover:bg-gray-50 transition">
<td class="px-4 py-4 whitespace-nowrap">
<input type="checkbox" name="member_ids[]" value="<?php echo $member['id']; ?>" class="memberCheckbox rounded">
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="flex items-center">
<img src="<?php echo !empty($member['profile_photo']) ? BASE_URL . 'uploads/members/' . $member['profile_photo'] : BASE_URL . 'assets/images/default-avatar.png'; ?>"
alt="<?php echo htmlspecialchars($member['first_name']); ?>"
class="w-10 h-10 rounded-full object-cover mr-3 border-2 border-blue-200">
<div>
<div class="font-semibold text-gray-800">
<?php echo htmlspecialchars(($member['title'] ?? '') . ' ' . $member['first_name'] . ' ' . $member['last_name']); ?>
</div>
<div class="text-sm text-gray-500"><?php echo htmlspecialchars($member['member_type']); ?></div>
</div>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="font-mono text-sm bg-blue-100 text-blue-800 px-2 py-1 rounded">
<?php echo htmlspecialchars($member['membershipcard_id']); ?>
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm">
<div class="text-gray-800">
<i class="fas fa-phone text-blue-500 mr-1"></i>
<?php echo htmlspecialchars($member['phone'] ?: 'N/A'); ?>
</div>
<div class="text-gray-500">
<i class="fas fa-envelope text-blue-500 mr-1"></i>
<?php echo htmlspecialchars($member['email'] ?: 'N/A'); ?>
</div>
</div>
</td>
<td class="px-6 py-4">
<div class="text-sm text-gray-800">
<?php echo htmlspecialchars($member['assembly_name']); ?>
</div>
<div class="text-xs text-gray-500">
<?php echo htmlspecialchars($member['district_name']); ?>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="space-y-1">
<?php if ($member['is_active'] ?? 1): ?>
<span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800">
<i class="fas fa-check-circle mr-1"></i>Active
</span>
<?php else: ?>
<span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-red-100 text-red-800">
<i class="fas fa-times-circle mr-1"></i>Inactive
</span>
<?php endif; ?>
<?php if ($member['communicant'] ?? 0): ?>
<span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-blue-100 text-blue-800">
<i class="fas fa-cross mr-1"></i>Communicant
</span>
<?php endif; ?>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm">
<div class="flex space-x-2">
<a href="view.php?id=<?php echo $member['id']; ?>"
class="text-blue-600 hover:text-blue-800 transition"
title="View">
<i class="fas fa-eye"></i>
</a>
<a href="edit.php?id=<?php echo $member['id']; ?>"
class="text-yellow-600 hover:text-yellow-800 transition"
title="Edit">
<i class="fas fa-edit"></i>
</a>
<button onclick="deleteMember(<?php echo $member['id']; ?>)"
class="text-red-600 hover:text-red-800 transition"
title="Delete">
<i class="fas fa-trash"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</form>
</div>
</div>
<!-- Pagination & Export Options -->
<?php if (!empty($members)): ?>
<div class="mt-6 flex flex-col md:flex-row justify-between items-center gap-4">
<div class="flex items-center space-x-2 text-sm text-gray-600">
<span>Showing
<?php
$start = $totalMembers > 0 ? ($offset + 1) : 0;
$end = min($offset + $limit, $totalMembers);
echo $start . ' - ' . $end . ' of ' . $totalMembers;
?>
</span>
</div>
<div class="flex items-center space-x-2">
<?php
// Helper to keep search and limit when changing page
$baseQuery = [];
if (!empty($_GET['search'])) {
$baseQuery['search'] = $_GET['search'];
}
$baseQuery['limit'] = $limit;
// Use local integer copies for pagination math
$currentPageInt = (int)$currentPage;
$totalPagesInt = (int)$totalPages;
if ($currentPageInt < 1) { $currentPageInt = 1; }
if ($totalPagesInt < 1) { $totalPagesInt = 1; }
$prevPage = $currentPageInt > 1 ? $currentPageInt - 1 : 1;
$nextPage = $currentPageInt < $totalPagesInt ? $currentPageInt + 1 : $totalPagesInt;
?>
<nav class="inline-flex rounded-md shadow-sm" aria-label="Pagination">
<?php if ($currentPageInt > 1): ?>
<?php $prevQuery = http_build_query(array_merge($baseQuery, ['page' => $prevPage])); ?>
<a href="?<?php echo $prevQuery; ?>" class="px-3 py-2 border border-gray-300 bg-white text-sm text-gray-700 hover:bg-gray-50">Prev</a>
<?php else: ?>
<span class="px-3 py-2 border border-gray-200 bg-gray-100 text-sm text-gray-400 cursor-not-allowed">Prev</span>
<?php endif; ?>
<span class="px-3 py-2 border-t border-b border-gray-300 bg-white text-sm text-gray-700">
Page <?php echo $currentPageInt; ?> of <?php echo $totalPagesInt; ?>
</span>
<?php if ($currentPageInt < $totalPagesInt): ?>
<?php $nextQuery = http_build_query(array_merge($baseQuery, ['page' => $nextPage])); ?>
<a href="?<?php echo $nextQuery; ?>" class="px-3 py-2 border border-gray-300 bg-white text-sm text-gray-700 hover:bg-gray-50">Next</a>
<?php else: ?>
<span class="px-3 py-2 border border-gray-200 bg-gray-100 text-sm text-gray-400 cursor-not-allowed">Next</span>
<?php endif; ?>
</nav>
</div>
<div class="flex justify-end items-center space-x-4">
<button onclick="exportTable('membersTable', 'members', 'csv')"
class="bg-green-500 text-white px-4 py-2 rounded-lg hover:bg-green-600 transition">
<i class="fas fa-file-csv mr-2"></i>Export CSV
</button>
<button onclick="window.print()"
class="bg-gray-500 text-white px-4 py-2 rounded-lg hover:bg-gray-600 transition">
<i class="fas fa-print mr-2"></i>Print
</button>
</div>
</div>
<?php endif; ?>
</div>
<script>
function deleteMember(id) {
confirmAction('Are you sure you want to delete this member?', function() {
window.location.href = 'delete.php?id=' + id;
});
}
const selectAll = document.getElementById('selectAll');
const checkboxes = document.querySelectorAll('.memberCheckbox');
if (selectAll) {
selectAll.addEventListener('change', function() {
checkboxes.forEach(cb => cb.checked = selectAll.checked);
});
}
function getSelectedCount() {
let count = 0;
checkboxes.forEach(cb => { if (cb.checked) count++; });
return count;
}
function submitBulkDelete() {
if (getSelectedCount() === 0) {
alert('Please select at least one member.');
return;
}
confirmAction('Are you sure you want to delete the selected members?', function() {
const form = document.getElementById('bulkForm');
form.action = 'bulk_delete.php';
form.method = 'POST';
form.submit();
});
}
// Filter Assembly options in the filter form based on selected District
document.addEventListener('DOMContentLoaded', function() {
var districtSelect = document.getElementById('filterDistrictSelect');
var assemblySelect = document.getElementById('filterAssemblySelect');
if (!districtSelect || !assemblySelect) return;
function filterAssemblyOptions() {
var selectedDistrict = districtSelect.value;
var options = assemblySelect.querySelectorAll('option');
options.forEach(function(opt) {
if (!opt.dataset.districtId) {
// Keep the "All" option always visible
opt.style.display = '';
return;
}
if (selectedDistrict === '' || opt.dataset.districtId === selectedDistrict) {
opt.style.display = '';
} else {
opt.style.display = 'none';
}
});
}
// Apply filtering on load and when district changes
filterAssemblyOptions();
districtSelect.addEventListener('change', function() {
// Clear assembly selection when district changes
assemblySelect.value = '';
filterAssemblyOptions();
});
});
</script>
</main>
<?php include '../../includes/footer.php'; ?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists