Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/conference/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/conference/manage_dynamic_tables.php

<?php
// ===== DYNAMIC TABLE CREATION FEATURE - MANAGEMENT UTILITY =====
// Utility script to manage dynamic event tables

require_once 'includes/functions.php';

// Check if user is logged in and has admin privileges
if (!isLoggedIn() || !hasRole('superuser')) {
    die('Access denied. Superuser privileges required.');
}

$db = new CopMadinaDB();
$conn = $db->getConnection();

$action = $_GET['action'] ?? 'list';
$event_id = $_GET['event_id'] ?? null;

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic Tables Manager - COP Madina Admin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body class="bg-gradient-to-br from-blue-50 via-white to-purple-50 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <div class="max-w-6xl mx-auto">
            <div class="bg-white rounded-2xl shadow-xl border border-white/20 p-8">
                <div class="flex items-center justify-between mb-8">
                    <div>
                        <h1 class="text-3xl font-bold bg-gradient-to-r from-blue-600 to-purple-600 bg-clip-text text-transparent">
                            Dynamic Tables Manager
                        </h1>
                        <p class="text-slate-600 mt-2">Manage event-specific dynamic tables</p>
                    </div>
                    <a href="admin/events.php" class="px-6 py-3 bg-slate-600 hover:bg-slate-700 text-white font-medium rounded-xl transition-all duration-200">
                        <i class="fas fa-arrow-left mr-2"></i>Back to Events
                    </a>
                </div>

                <?php if ($action === 'list'): ?>
                    <!-- List all events with dynamic tables -->
                    <div class="space-y-6">
                        <h2 class="text-xl font-semibold text-slate-800">Events with Dynamic Tables</h2>
                        
                        <?php
                        $stmt = $conn->prepare("SELECT id, title, custom_table_name, created_at FROM events WHERE custom_table_name IS NOT NULL ORDER BY created_at DESC");
                        $stmt->execute();
                        $events = $stmt->fetchAll();
                        ?>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                            <?php foreach ($events as $event): ?>
                            <div class="bg-slate-50 rounded-xl p-6 border border-slate-200">
                                <h3 class="font-semibold text-slate-800 mb-2"><?php echo htmlspecialchars($event['title']); ?></h3>
                                <p class="text-sm text-slate-600 mb-4">
                                    Table: <code class="bg-slate-200 px-2 py-1 rounded"><?php echo htmlspecialchars($event['custom_table_name']); ?></code>
                                </p>
                                <p class="text-xs text-slate-500 mb-4">
                                    Created: <?php echo date('M j, Y', strtotime($event['created_at'])); ?>
                                </p>
                                
                                <div class="flex gap-2">
                                    <a href="?action=view&event_id=<?php echo $event['id']; ?>" 
                                       class="flex-1 px-3 py-2 bg-blue-100 hover:bg-blue-200 text-blue-700 text-sm font-medium rounded-lg transition-all duration-200 text-center">
                                        <i class="fas fa-eye mr-1"></i>View Data
                                    </a>
                                    <a href="?action=delete&event_id=<?php echo $event['id']; ?>" 
                                       onclick="return confirm('Are you sure you want to delete this table? This action cannot be undone.')"
                                       class="flex-1 px-3 py-2 bg-red-100 hover:bg-red-200 text-red-700 text-sm font-medium rounded-lg transition-all duration-200 text-center">
                                        <i class="fas fa-trash mr-1"></i>Delete
                        </div>
                        <div class="flex items-center space-x-3">
                            <div class="bg-white/70 backdrop-blur-sm rounded-xl px-4 py-2 border border-slate-200/50">
                                <span class="text-sm font-medium text-slate-600">Total Tables:</span>
                                <span class="text-lg font-bold text-slate-800 ml-2"><?php echo count($dynamicTables); ?></span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Messages -->
                <?php if (isset($success)): ?>
                <div class="bg-green-50/90 backdrop-blur-sm border border-green-200 text-green-800 px-6 py-4 rounded-xl mb-6 animate-fade-in">
                    <div class="flex items-center">
                        <i class="fas fa-check-circle text-green-500 mr-3"></i>
                        <?php echo htmlspecialchars($success); ?>
                    </div>
                </div>
                <?php endif; ?>

                <?php if (isset($error)): ?>
                <div class="bg-red-50/90 backdrop-blur-sm border border-red-200 text-red-800 px-6 py-4 rounded-xl mb-6 animate-fade-in">
                    <div class="flex items-center">
                        <i class="fas fa-exclamation-circle text-red-500 mr-3"></i>
                        <?php echo htmlspecialchars($error); ?>
                    </div>
                </div>
                <?php endif; ?>

                <!-- Dynamic Tables List -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-lg border border-slate-200/50 animate-slide-up">
                    <?php if (empty($dynamicTables)): ?>
                    <div class="p-12 text-center">
                        <div class="w-24 h-24 mx-auto mb-6 bg-gradient-to-br from-slate-100 to-slate-200 rounded-full flex items-center justify-center">
                            <i class="fas fa-table text-3xl text-slate-400"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-slate-800 mb-2">No Dynamic Tables Found</h3>
                        <p class="text-slate-500 mb-4">Dynamic tables are created automatically when events use custom form templates.</p>
                        <a href="events.php" class="inline-flex items-center px-4 py-2 bg-gradient-to-r from-blue-500 to-purple-600 text-white font-medium rounded-lg hover:from-blue-600 hover:to-purple-700 transition-all duration-200">
                            <i class="fas fa-plus mr-2"></i>
                            Create Event with Custom Form
                        </a>
                    </div>
                    <?php else: ?>
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-slate-50/50">
                                <tr>
                                    <th class="px-6 py-4 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Event</th>
                                    <th class="px-6 py-4 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Table Name</th>
                                    <th class="px-6 py-4 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Records</th>
                                    <th class="px-6 py-4 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Structure</th>
                                    <th class="px-6 py-4 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">Actions</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-200/50">
                                <?php foreach ($dynamicTables as $index => $table): ?>
                                <tr class="hover:bg-slate-50/50 transition-colors animate-fade-in" style="animation-delay: <?php echo $index * 0.1; ?>s">
                                    <td class="px-6 py-4">
                                        <div class="flex items-center">
                                            <div class="flex-shrink-0 h-10 w-10">
                                                <div class="h-10 w-10 rounded-full bg-gradient-to-br from-blue-400 to-purple-500 flex items-center justify-center text-white font-medium">
                                                    <i class="fas fa-calendar-alt"></i>
                                                </div>
                                            </div>
                                            <div class="ml-4">
                                                <div class="text-sm font-medium text-slate-900">
                                                    <?php echo htmlspecialchars($table['event_title']); ?>
                                                </div>
                                                <div class="text-sm text-slate-500">Event ID: <?php echo $table['event_id']; ?></div>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4">
                                        <code class="bg-slate-100 px-3 py-1 rounded-lg text-sm font-mono text-slate-800">
                                            <?php echo htmlspecialchars($table['table_name']); ?>
                                        </code>
                                    </td>
                                    <td class="px-6 py-4">
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-800">
                                            <i class="fas fa-database mr-2"></i>
                                            <?php echo number_format($table['record_count']); ?> records
                                        </span>
                                    </td>
                                    <td class="px-6 py-4">
                                        <div class="text-sm text-slate-900 mb-2">
                                            <span class="font-medium"><?php echo count($table['structure']); ?></span> columns
                                        </div>
                                        <div class="flex flex-wrap gap-1">
                                            <?php foreach (array_slice($table['structure'], 0, 3) as $column): ?>
                                            <span class="inline-block bg-slate-200 rounded-full px-2 py-1 text-xs text-slate-700">
                                                <?php echo htmlspecialchars($column['Field']); ?>
                                            </span>
                                            <?php endforeach; ?>
                                            <?php if (count($table['structure']) > 3): ?>
                                            <span class="inline-block bg-slate-100 rounded-full px-2 py-1 text-xs text-slate-500">
                                                +<?php echo count($table['structure']) - 3; ?> more
                                            </span>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4">
                                        <div class="flex items-center space-x-3">
                                            <button @click="viewTable('<?php echo htmlspecialchars($table['table_name']); ?>')" 
                                                    class="inline-flex items-center px-3 py-1 bg-blue-100 hover:bg-blue-200 text-blue-700 text-sm font-medium rounded-lg transition-colors">
                                                <i class="fas fa-eye mr-1"></i>
                                                View
                                            </button>
                                            <button @click="confirmDelete('<?php echo htmlspecialchars($table['table_name']); ?>', '<?php echo htmlspecialchars($table['event_title']); ?>')" 
                                                    class="inline-flex items-center px-3 py-1 bg-red-100 hover:bg-red-200 text-red-700 text-sm font-medium rounded-lg transition-colors">
                                                <i class="fas fa-trash mr-1"></i>
                                                Delete
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php endif; ?>
                </div>
            </main>
        </div>
    </div>

    <!-- Delete Confirmation Modal -->
    <div v-show="showDeleteModal" class="fixed inset-0 bg-black/50 backdrop-blur-sm z-50 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl max-w-md w-full animate-scale-in">
            <div class="p-6">
                <div class="flex items-center mb-4">
                    <div class="w-12 h-12 bg-red-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-exclamation-triangle text-red-600 text-xl"></i>
                    </div>
                    <h3 class="text-lg font-semibold text-slate-900">Confirm Delete</h3>
                </div>
                <p class="text-slate-600 mb-6">
                    Are you sure you want to delete the dynamic table for event "<strong>{{ eventToDelete }}</strong>"? 
                    This action cannot be undone and all data in the table will be lost.
                </p>
                <div class="flex justify-end space-x-3">
                    <button @click="closeDeleteModal()" 
                            class="px-4 py-2 bg-slate-200 hover:bg-slate-300 text-slate-800 font-medium rounded-lg transition-colors">
                        Cancel
                    </button>
                    <form method="POST" style="display: inline;">
                        <input type="hidden" name="action" value="delete">
                        <input type="hidden" name="table_name" :value="tableToDelete">
                        <button type="submit" 
                                class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white font-medium rounded-lg transition-colors">
                            Delete Table
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        const { createApp } = Vue;
        
        createApp({
            data() {
                return {
                    showDeleteModal: false,
                    tableToDelete: '',
                    eventToDelete: ''
                }
            },
            methods: {
                confirmDelete(tableName, eventTitle) {
                    this.tableToDelete = tableName;
                    this.eventToDelete = eventTitle;
                    this.showDeleteModal = true;
                },
                closeDeleteModal() {
                    this.showDeleteModal = false;
                    this.tableToDelete = '';
                    this.eventToDelete = '';
                },
                viewTable(tableName) {
                    // For now, just show an alert. In a full implementation, 
                    // you might open a modal or navigate to a detailed view
                    alert('Viewing table: ' + tableName + '\n\nThis feature can be extended to show table contents in a modal or separate page.');
                }
            }
        }).mount('#app');
    </script>
</body>
</html>

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