Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/directory/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/directory/standalone.php

<?php
require_once '../../config/config.php';
require_once '../../classes/DirectoryManager.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit;
}

$page_title = "Standalone Directory";
$directoryManager = new DirectoryManager();

// Handle delete action
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) {
    if ($directoryManager->deleteEntry($_GET['id'])) {
        $success = "Entry deleted successfully!";
    } else {
        $error = "Failed to delete entry.";
    }
}

// Get filters
$filters = [
    'search' => $_GET['search'] ?? '',
    'district_id' => $_GET['district_id'] ?? '',
    'assembly_id' => $_GET['assembly_id'] ?? ''
];

// Get standalone directory entries
$entries = $directoryManager->getAllEntries($filters);

// Get database connection
$db = Database::getInstance()->getConnection();

// Get districts for filter
$districts_query = "SELECT * FROM districts ORDER BY district_name";
$stmt = $db->query($districts_query);
$districts = $stmt->fetchAll();

// Get assemblies for filter
if (!empty($filters['district_id'])) {
    $assemblies_query = "SELECT * FROM assemblies WHERE district_id = :district_id ORDER BY assembly_name";
    $stmt = $db->prepare($assemblies_query);
    $stmt->execute([':district_id' => $filters['district_id']]);
    $assemblies = $stmt->fetchAll();
} else {
    $assemblies = [];
}

include '../../includes/header.php';
include '../../includes/sidebar.php';
?>

<!-- Main Content -->
<main class="main-content md:ml-64 pt-16">
    <div class="min-h-screen bg-gray-50 py-8">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Header -->
        <div class="mb-8">
            <div class="gradient-bg rounded-xl shadow-lg p-8 text-white">
                <div class="flex flex-col md:flex-row md:items-center md:justify-between">
                    <div>
                        <h1 class="text-3xl font-bold mb-2">Standalone Directory</h1>
                        <p class="text-blue-100">Manage non-member directory entries</p>
                    </div>
                    <div class="mt-4 md:mt-0 flex gap-3">
                        <a href="index.php" class="bg-white text-primary px-6 py-3 rounded-lg font-semibold hover:bg-blue-50 transition-all duration-300 shadow-md inline-flex items-center">
                            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
                            </svg>
                            Members Directory
                        </a>
                        <a href="standalone-add.php" class="bg-gradient-secondary text-white px-6 py-3 rounded-lg font-semibold hover:shadow-lg transition-all duration-300 inline-flex items-center">
                            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                            </svg>
                            Add New Entry
                        </a>
                        <a href="standalone-upload.php" class="bg-green-600 text-white px-6 py-3 rounded-lg font-semibold hover:bg-green-700 transition-all duration-300 inline-flex items-center">
                            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"/>
                            </svg>
                            Bulk Upload
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Success/Error Messages -->
        <?php if (isset($success)): ?>
        <div class="bg-green-50 border border-green-200 text-green-800 px-6 py-4 rounded-lg mb-6 flex items-center">
            <svg class="w-5 h-5 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
            </svg>
            <?= $success ?>
        </div>
        <?php endif; ?>
        
        <?php if (isset($error)): ?>
        <div class="bg-red-50 border border-red-200 text-red-800 px-6 py-4 rounded-lg mb-6 flex items-center">
            <svg class="w-5 h-5 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
            </svg>
            <?= $error ?>
        </div>
        <?php endif; ?>

        <!-- Filters and Actions -->
        <div class="bg-white rounded-xl shadow-md p-6 mb-6">
            <form method="GET" action="" id="filterForm" class="space-y-4">
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                    <!-- Search -->
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Search</label>
                        <input type="text" 
                               name="search" 
                               value="<?= htmlspecialchars($filters['search']) ?>"
                               placeholder="Name, phone, email..." 
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                    </div>
                    
                    <!-- District Filter -->
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">District</label>
                        <select name="district_id" id="districtFilter" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                            <option value="">All Districts</option>
                            <?php foreach ($districts as $district): ?>
                                <option value="<?= $district['id'] ?>" <?= ($filters['district_id'] ?? '') == $district['id'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($district['district_name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <!-- Assembly Filter -->
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Assembly</label>
                        <select name="assembly_id" id="assemblyFilter" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                            <option value="">All Assemblies</option>
                            <?php foreach ($assemblies as $assembly): ?>
                                <option value="<?= $assembly['id'] ?>" <?= ($filters['assembly_id'] ?? '') == $assembly['id'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($assembly['assembly_name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <!-- Filter Button -->
                    <div class="flex items-end">
                        <button type="submit" class="w-full btn-gradient px-6 py-2 rounded-lg font-semibold hover:shadow-lg transition-all duration-300">
                            Apply Filters
                        </button>
                    </div>
                </div>
                
                <?php if (!empty($filters['search']) || !empty($filters['district_id']) || !empty($filters['assembly_id'])): ?>
                <div class="flex items-center justify-between bg-blue-50 border border-blue-200 rounded-lg p-3">
                    <span class="text-sm text-blue-800">
                        Showing <strong><?= count($entries) ?></strong> filtered result(s)
                    </span>
                    <a href="standalone.php" class="text-blue-600 hover:text-blue-800 text-sm font-semibold">Clear Filters</a>
                </div>
                <?php endif; ?>
            </form>
        </div>

        <!-- Export Actions -->
        <div class="bg-white rounded-xl shadow-md p-4 mb-6">
            <div class="flex flex-wrap gap-3">
                <button onclick="exportToCSV()" class="bg-green-600 text-white px-6 py-2 rounded-lg font-semibold hover:bg-green-700 transition-all duration-300 inline-flex items-center">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                    </svg>
                    Export CSV
                </button>
                <button onclick="window.print()" class="bg-gray-700 text-white px-6 py-2 rounded-lg font-semibold hover:bg-gray-800 transition-all duration-300 inline-flex items-center">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"/>
                    </svg>
                    Print
                </button>
            </div>
        </div>

        <!-- Entries Table -->
        <div class="bg-white rounded-xl shadow-md overflow-hidden">
            <div class="px-6 py-4 bg-gradient-primary text-white">
                <h2 class="text-xl font-bold">Directory Entries (<?= count($entries) ?>)</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full" id="dataTable">
                    <thead class="bg-gray-50 border-b-2 border-gray-200">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider cursor-pointer hover:bg-gray-100" onclick="sortTable(0)">
                                Full Name ↕
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider cursor-pointer hover:bg-gray-100" onclick="sortTable(1)">
                                Phone ↕
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider cursor-pointer hover:bg-gray-100" onclick="sortTable(2)">
                                Email ↕
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider cursor-pointer hover:bg-gray-100" onclick="sortTable(3)">
                                Position ↕
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider cursor-pointer hover:bg-gray-100" onclick="sortTable(4)">
                                Location ↕
                            </th>
                            <th class="px-6 py-3 text-center text-xs font-semibold text-gray-700 uppercase tracking-wider">
                                Actions
                            </th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <?php if (count($entries) > 0): ?>
                            <?php foreach ($entries as $entry): ?>
                            <tr class="hover:bg-gray-50 transition-colors duration-200">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-semibold text-gray-900">
                                        <?= htmlspecialchars(trim(($entry['title'] ?? '') . ' ' . $entry['first_name'] . ' ' . ($entry['middle_name'] ?? '') . ' ' . $entry['last_name'])) ?>
                                    </div>
                                    <div class="text-xs text-gray-500"><?= htmlspecialchars($entry['gender']) ?></div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm text-gray-900">
                                        <?= !empty($entry['phone']) ? htmlspecialchars($entry['phone']) : '<span class="text-gray-400">N/A</span>' ?>
                                    </div>
                                </td>
                                <td class="px-6 py-4">
                                    <div class="text-sm text-gray-900">
                                        <?= !empty($entry['email']) ? htmlspecialchars($entry['email']) : '<span class="text-gray-400">N/A</span>' ?>
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm text-gray-700">
                                        <?= !empty($entry['position']) ? htmlspecialchars($entry['position']) : '<span class="text-gray-400">N/A</span>' ?>
                                    </div>
                                </td>
                                <td class="px-6 py-4">
                                    <div class="text-sm text-gray-700">
                                        <?php
                                        $location = array_filter([
                                            $entry['assembly_name'] ?? null,
                                            $entry['district_name'] ?? null,
                                            $entry['area_name'] ?? null
                                        ]);
                                        echo !empty($location) ? htmlspecialchars(implode(', ', $location)) : '<span class="text-gray-400">N/A</span>';
                                        ?>
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-center">
                                    <div class="flex items-center justify-center gap-2">
                                        <a href="standalone-add.php?id=<?= $entry['id'] ?>" class="text-blue-600 hover:text-blue-800 font-semibold" title="Edit">
                                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                                            </svg>
                                        </a>
                                        <a href="?action=delete&id=<?= $entry['id'] ?>" class="text-red-600 hover:text-red-800 font-semibold" title="Delete" onclick="return confirm('Are you sure you want to delete this entry?')">
                                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                                            </svg>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="6" class="px-6 py-12 text-center">
                                    <div class="text-gray-400">
                                        <svg class="w-16 h-16 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4"/>
                                        </svg>
                                        <p class="text-lg font-semibold">No entries found</p>
                                        <p class="text-sm mb-4">Start by adding a new directory entry</p>
                                        <a href="standalone-add.php" class="btn-gradient px-6 py-3 rounded-lg font-semibold hover:shadow-lg transition-all duration-300 inline-flex items-center">
                                            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                                            </svg>
                                            Add New Entry
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</main>

<!-- Print Styles -->
<style media="print">
    .no-print { display: none !important; }
    body { background: white; }
    table { border-collapse: collapse; width: 100%; }
    th, td { border: 1px solid #ddd; padding: 8px; }
    thead { background-color: #f3f4f6 !important; }
</style>

<script>
// Dynamic assembly loading
document.getElementById('districtFilter').addEventListener('change', function() {
    const districtId = this.value;
    const assemblyFilter = document.getElementById('assemblyFilter');
    
    if (!districtId) {
        assemblyFilter.innerHTML = '<option value="">All Assemblies</option>';
        return;
    }
    
    fetch(`../../api/get-assemblies.php?district_id=${districtId}`)
        .then(response => response.json())
        .then(data => {
            assemblyFilter.innerHTML = '<option value="">All Assemblies</option>';
            data.forEach(assembly => {
                assemblyFilter.innerHTML += `<option value="${assembly.id}">${assembly.assembly_name}</option>`;
            });
        });
});

// Table sorting
function sortTable(columnIndex) {
    const table = document.getElementById('dataTable');
    const tbody = table.querySelector('tbody');
    const rows = Array.from(tbody.querySelectorAll('tr'));
    
    rows.sort((a, b) => {
        const aText = a.cells[columnIndex].textContent.trim();
        const bText = b.cells[columnIndex].textContent.trim();
        return aText.localeCompare(bText);
    });
    
    rows.forEach(row => tbody.appendChild(row));
}

// Export to CSV
function exportToCSV() {
    const table = document.getElementById('dataTable');
    let csv = [];
    
    // Headers
    const headers = Array.from(table.querySelectorAll('thead th')).map(th => 
        th.textContent.replace(' ↕', '').trim()
    );
    csv.push(headers.slice(0, -1).join(',')); // Exclude Actions column
    
    // Rows
    const rows = table.querySelectorAll('tbody tr');
    rows.forEach(row => {
        const cells = Array.from(row.querySelectorAll('td')).slice(0, -1).map(td => {
            let text = td.textContent.trim();
            text = text.replace(/N\/A/g, '');
            text = text.replace(/"/g, '""');
            return `"${text}"`;
        });
        if (cells.length > 0 && cells[0] !== '""') {
            csv.push(cells.join(','));
        }
    });
    
    // Download
    const blob = new Blob([csv.join('\n')], { type: 'text/csv' });
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = `standalone_directory_${new Date().toISOString().split('T')[0]}.csv`;
    a.click();
}
</script>

<?php include '../../includes/footer.php'; ?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists