Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/event-templates.php

<?php
session_start();
require_once '../includes/functions.php';

// Check if user is logged in and has admin privileges
if (!isLoggedIn() || (!hasRole('superuser') && !hasRole('area_admin'))) {
    header('Location: ../login.php');
    exit;
}

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

$success = '';
$error = '';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'create':
                $name = trim($_POST['name']);
                $description = trim($_POST['description']);
                $template_data = json_encode([
                    'title' => $_POST['template_title'] ?? '',
                    'description' => $_POST['template_description'] ?? '',
                    'event_type' => $_POST['event_type'] ?? 'area',
                    'capacity' => (int)($_POST['capacity'] ?? 0),
                    'registration_fee' => (float)($_POST['registration_fee'] ?? 0),
                    'early_bird_fee' => (float)($_POST['early_bird_fee'] ?? 0),
                    'venue' => $_POST['venue'] ?? '',
                    'banner_settings' => [
                        'show_countdown' => isset($_POST['show_countdown']),
                        'featured' => isset($_POST['featured'])
                    ],
                    'registration_settings' => [
                        'require_approval' => isset($_POST['require_approval']),
                        'send_confirmation' => isset($_POST['send_confirmation'])
                    ]
                ]);

                try {
                    $stmt = $conn->prepare("INSERT INTO event_templates (name, description, template_data, created_by) VALUES (?, ?, ?, ?)");
                    $stmt->execute([$name, $description, $template_data, $user['id']]);
                    $success = "Event template created successfully!";
                } catch (PDOException $e) {
                    $error = "Error creating template: " . $e->getMessage();
                }
                break;

            case 'update':
                $id = (int)$_POST['template_id'];
                $name = trim($_POST['name']);
                $description = trim($_POST['description']);
                $status = $_POST['status'];
                
                $template_data = json_encode([
                    'title' => $_POST['template_title'] ?? '',
                    'description' => $_POST['template_description'] ?? '',
                    'event_type' => $_POST['event_type'] ?? 'area',
                    'capacity' => (int)($_POST['capacity'] ?? 0),
                    'registration_fee' => (float)($_POST['registration_fee'] ?? 0),
                    'early_bird_fee' => (float)($_POST['early_bird_fee'] ?? 0),
                    'venue' => $_POST['venue'] ?? '',
                    'banner_settings' => [
                        'show_countdown' => isset($_POST['show_countdown']),
                        'featured' => isset($_POST['featured'])
                    ],
                    'registration_settings' => [
                        'require_approval' => isset($_POST['require_approval']),
                        'send_confirmation' => isset($_POST['send_confirmation'])
                    ]
                ]);

                try {
                    $stmt = $conn->prepare("UPDATE event_templates SET name = ?, description = ?, template_data = ?, status = ? WHERE id = ?");
                    $stmt->execute([$name, $description, $template_data, $status, $id]);
                    $success = "Event template updated successfully!";
                } catch (PDOException $e) {
                    $error = "Error updating template: " . $e->getMessage();
                }
                break;

            case 'delete':
                $id = (int)$_POST['template_id'];
                try {
                    $stmt = $conn->prepare("DELETE FROM event_templates WHERE id = ?");
                    $stmt->execute([$id]);
                    $success = "Event template deleted successfully!";
                } catch (PDOException $e) {
                    $error = "Error deleting template: " . $e->getMessage();
                }
                break;
        }
    }
}

// Get templates with pagination
$page = (int)($_GET['page'] ?? 1);
$limit = 10;
$offset = ($page - 1) * $limit;

$search = $_GET['search'] ?? '';
$status_filter = $_GET['status'] ?? '';

$where_conditions = [];
$params = [];

if (!empty($search)) {
    $where_conditions[] = "(name LIKE ? OR description LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

if (!empty($status_filter)) {
    $where_conditions[] = "status = ?";
    $params[] = $status_filter;
}

$where_clause = !empty($where_conditions) ? "WHERE " . implode(" AND ", $where_conditions) : "";

// Get total count
$count_sql = "SELECT COUNT(*) FROM event_templates $where_clause";
$count_stmt = $conn->prepare($count_sql);
$count_stmt->execute($params);
$total_templates = $count_stmt->fetchColumn();
$total_pages = ceil($total_templates / $limit);

// Get templates
$sql = "SELECT et.*, u.first_name, u.last_name 
        FROM event_templates et 
        LEFT JOIN users u ON et.created_by = u.id 
        $where_clause 
        ORDER BY et.created_at DESC 
        LIMIT $limit OFFSET $offset";
$stmt = $conn->prepare($sql);
$stmt->execute($params);
$templates = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get template for editing if requested
$edit_template = null;
if (isset($_GET['edit'])) {
    $edit_id = (int)$_GET['edit'];
    $stmt = $conn->prepare("SELECT * FROM event_templates WHERE id = ?");
    $stmt->execute([$edit_id]);
    $edit_template = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($edit_template) {
        $edit_template['template_data'] = json_decode($edit_template['template_data'], true);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Event Templates - 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">
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body class="bg-gradient-to-br from-blue-50 via-white to-purple-50 min-h-screen">
    <?php include 'includes/admin_sidebar.php'; ?>
    
    <div class="ml-72 p-8">
        <div class="max-w-7xl mx-auto">
            <!-- Header -->
            <div class="mb-8">
                <div class="flex items-center justify-between">
                    <div>
                        <h1 class="text-3xl font-bold bg-gradient-to-r from-blue-600 to-purple-600 bg-clip-text text-transparent">
                            Event Templates
                        </h1>
                        <p class="text-slate-600 mt-2">Create and manage reusable event templates</p>
                    </div>
                    <button onclick="showCreateModal()" class="px-6 py-3 bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 text-white font-medium rounded-xl transition-all duration-200 shadow-lg hover:shadow-xl">
                        <i class="fas fa-plus mr-2"></i>
                        Create Template
                    </button>
                </div>
            </div>

            <!-- Alerts -->
            <?php if ($success): ?>
                <div class="mb-6 p-4 bg-green-50 border border-green-200 rounded-xl text-green-800">
                    <i class="fas fa-check-circle mr-2"></i>
                    <?php echo htmlspecialchars($success); ?>
                </div>
            <?php endif; ?>

            <?php if ($error): ?>
                <div class="mb-6 p-4 bg-red-50 border border-red-200 rounded-xl text-red-800">
                    <i class="fas fa-exclamation-circle mr-2"></i>
                    <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>

            <!-- Filters -->
            <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-white/20 p-6 mb-8">
                <form method="GET" class="flex flex-wrap gap-4 items-end">
                    <div class="flex-1 min-w-64">
                        <label class="block text-sm font-medium text-slate-700 mb-2">Search Templates</label>
                        <input type="text" name="search" value="<?php echo htmlspecialchars($search); ?>" 
                               placeholder="Search by name or description..." 
                               class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-slate-700 mb-2">Status</label>
                        <select name="status" class="px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                            <option value="">All Statuses</option>
                            <option value="active" <?php echo $status_filter === 'active' ? 'selected' : ''; ?>>Active</option>
                            <option value="inactive" <?php echo $status_filter === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                        </select>
                    </div>
                    <button type="submit" 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-search mr-2"></i>
                        Search
                    </button>
                    <a href="event-templates.php" class="px-6 py-3 bg-slate-200 hover:bg-slate-300 text-slate-700 font-medium rounded-xl transition-all duration-200">
                        <i class="fas fa-times mr-2"></i>
                        Clear
                    </a>
                </form>
            </div>

            <!-- Templates Grid -->
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-8">
                <?php foreach ($templates as $template): ?>
                    <?php $template_data = json_decode($template['template_data'], true); ?>
                    <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-white/20 p-6 hover:shadow-2xl transition-all duration-200">
                        <div class="flex items-start justify-between mb-4">
                            <div class="flex-1">
                                <h3 class="text-lg font-semibold text-slate-800 mb-1"><?php echo htmlspecialchars($template['name']); ?></h3>
                                <p class="text-sm text-slate-600"><?php echo htmlspecialchars($template['description']); ?></p>
                            </div>
                            <span class="px-3 py-1 text-xs font-medium rounded-full <?php echo $template['status'] === 'active' ? 'bg-green-100 text-green-800' : 'bg-slate-100 text-slate-600'; ?>">
                                <?php echo ucfirst($template['status']); ?>
                            </span>
                        </div>

                        <div class="space-y-2 mb-4">
                            <div class="flex items-center text-sm text-slate-600">
                                <i class="fas fa-tag mr-2 text-blue-500"></i>
                                Type: <?php echo ucfirst($template_data['event_type'] ?? 'area'); ?>
                            </div>
                            <div class="flex items-center text-sm text-slate-600">
                                <i class="fas fa-users mr-2 text-green-500"></i>
                                Capacity: <?php echo $template_data['capacity'] ?? 'Unlimited'; ?>
                            </div>
                            <div class="flex items-center text-sm text-slate-600">
                                <i class="fas fa-dollar-sign mr-2 text-yellow-500"></i>
                                Fee: GH₵<?php echo number_format($template_data['registration_fee'] ?? 0, 2); ?>
                            </div>
                        </div>

                        <div class="text-xs text-slate-500 mb-4">
                            Created by <?php echo htmlspecialchars($template['first_name'] . ' ' . $template['last_name']); ?>
                            <br>
                            <?php echo date('M j, Y', strtotime($template['created_at'])); ?>
                        </div>

                        <div class="flex gap-2">
                            <a href="?edit=<?php echo $template['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-edit mr-1"></i>
                                Edit
                            </a>
                            <button onclick="deleteTemplate(<?php echo $template['id']; ?>, '<?php echo htmlspecialchars($template['name']); ?>')"
                                    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">
                                <i class="fas fa-trash mr-1"></i>
                                Delete
                            </button>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>

            <!-- Pagination -->
            <?php if ($total_pages > 1): ?>
                <div class="flex justify-center">
                    <nav class="flex space-x-2">
                        <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                            <a href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>&status=<?php echo urlencode($status_filter); ?>" 
                               class="px-4 py-2 <?php echo $i === $page ? 'bg-blue-600 text-white' : 'bg-white text-slate-600 hover:bg-slate-50'; ?> rounded-lg transition-all duration-200">
                                <?php echo $i; ?>
                            </a>
                        <?php endfor; ?>
                    </nav>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Create/Edit Modal -->
    <div id="templateModal" class="fixed inset-0 bg-black/50 backdrop-blur-sm hidden items-center justify-center z-50">
        <div class="bg-white rounded-2xl shadow-2xl max-w-4xl w-full mx-4 max-h-[90vh] overflow-y-auto">
            <form method="POST" class="p-8">
                <div class="flex items-center justify-between mb-6">
                    <h2 class="text-2xl font-bold text-slate-800" id="modalTitle">Create Event Template</h2>
                    <button type="button" onclick="hideModal()" class="text-slate-400 hover:text-slate-600">
                        <i class="fas fa-times text-xl"></i>
                    </button>
                </div>

                <input type="hidden" name="action" id="modalAction" value="create">
                <input type="hidden" name="template_id" id="templateId">

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <!-- Template Info -->
                    <div class="space-y-4">
                        <h3 class="text-lg font-semibold text-slate-700 border-b pb-2">Template Information</h3>
                        
                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-2">Template Name *</label>
                            <input type="text" name="name" id="templateName" required 
                                   class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-2">Description</label>
                            <textarea name="description" id="templateDescription" rows="3" 
                                      class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent"></textarea>
                        </div>

                        <div id="statusField" class="hidden">
                            <label class="block text-sm font-medium text-slate-700 mb-2">Status</label>
                            <select name="status" id="templateStatus" 
                                    class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                                <option value="active">Active</option>
                                <option value="inactive">Inactive</option>
                            </select>
                        </div>
                    </div>

                    <!-- Event Settings -->
                    <div class="space-y-4">
                        <h3 class="text-lg font-semibold text-slate-700 border-b pb-2">Default Event Settings</h3>
                        
                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-2">Event Title Template</label>
                            <input type="text" name="template_title" id="eventTitle" 
                                   class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-2">Event Type</label>
                            <select name="event_type" id="eventType" 
                                    class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                                <option value="area">Area Event</option>
                                <option value="district">District Event</option>
                                <option value="assembly">Assembly Event</option>
                            </select>
                        </div>

                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-2">Capacity</label>
                                <input type="number" name="capacity" id="eventCapacity" min="0" 
                                       class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-2">Registration Fee (GH₵)</label>
                                <input type="number" name="registration_fee" id="registrationFee" min="0" step="0.01" 
                                       class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                            </div>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-2">Early Bird Fee (GH₵)</label>
                            <input type="number" name="early_bird_fee" id="earlyBirdFee" min="0" step="0.01" 
                                   class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-2">Default Venue</label>
                            <input type="text" name="venue" id="eventVenue" 
                                   class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        </div>
                    </div>
                </div>

                <!-- Additional Settings -->
                <div class="mt-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <h3 class="text-lg font-semibold text-slate-700 border-b pb-2 mb-4">Display Settings</h3>
                        <div class="space-y-3">
                            <label class="flex items-center">
                                <input type="checkbox" name="show_countdown" id="showCountdown" class="mr-3 rounded">
                                <span class="text-sm text-slate-700">Show countdown timer</span>
                            </label>
                            <label class="flex items-center">
                                <input type="checkbox" name="featured" id="featuredEvent" class="mr-3 rounded">
                                <span class="text-sm text-slate-700">Mark as featured event</span>
                            </label>
                        </div>
                    </div>

                    <div>
                        <h3 class="text-lg font-semibold text-slate-700 border-b pb-2 mb-4">Registration Settings</h3>
                        <div class="space-y-3">
                            <label class="flex items-center">
                                <input type="checkbox" name="require_approval" id="requireApproval" class="mr-3 rounded">
                                <span class="text-sm text-slate-700">Require admin approval</span>
                            </label>
                            <label class="flex items-center">
                                <input type="checkbox" name="send_confirmation" id="sendConfirmation" class="mr-3 rounded">
                                <span class="text-sm text-slate-700">Send confirmation emails</span>
                            </label>
                        </div>
                    </div>
                </div>

                <div class="flex justify-end gap-4 mt-8 pt-6 border-t">
                    <button type="button" onclick="hideModal()" 
                            class="px-6 py-3 bg-slate-200 hover:bg-slate-300 text-slate-700 font-medium rounded-xl transition-all duration-200">
                        Cancel
                    </button>
                    <button type="submit" 
                            class="px-6 py-3 bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 text-white font-medium rounded-xl transition-all duration-200">
                        <span id="submitText">Create Template</span>
                    </button>
                </div>
            </form>
        </div>
    </div>

    <script>
        function showCreateModal() {
            document.getElementById('modalTitle').textContent = 'Create Event Template';
            document.getElementById('modalAction').value = 'create';
            document.getElementById('submitText').textContent = 'Create Template';
            document.getElementById('statusField').classList.add('hidden');
            
            // Reset form
            document.querySelector('#templateModal form').reset();
            document.getElementById('templateId').value = '';
            
            document.getElementById('templateModal').classList.remove('hidden');
            document.getElementById('templateModal').classList.add('flex');
        }

        function showEditModal(template) {
            document.getElementById('modalTitle').textContent = 'Edit Event Template';
            document.getElementById('modalAction').value = 'update';
            document.getElementById('submitText').textContent = 'Update Template';
            document.getElementById('statusField').classList.remove('hidden');
            
            // Populate form
            document.getElementById('templateId').value = template.id;
            document.getElementById('templateName').value = template.name;
            document.getElementById('templateDescription').value = template.description;
            document.getElementById('templateStatus').value = template.status;
            
            const data = template.template_data;
            document.getElementById('eventTitle').value = data.title || '';
            document.getElementById('eventType').value = data.event_type || 'area';
            document.getElementById('eventCapacity').value = data.capacity || '';
            document.getElementById('registrationFee').value = data.registration_fee || '';
            document.getElementById('earlyBirdFee').value = data.early_bird_fee || '';
            document.getElementById('eventVenue').value = data.venue || '';
            
            document.getElementById('showCountdown').checked = data.banner_settings?.show_countdown || false;
            document.getElementById('featuredEvent').checked = data.banner_settings?.featured || false;
            document.getElementById('requireApproval').checked = data.registration_settings?.require_approval || false;
            document.getElementById('sendConfirmation').checked = data.registration_settings?.send_confirmation || false;
            
            document.getElementById('templateModal').classList.remove('hidden');
            document.getElementById('templateModal').classList.add('flex');
        }

        function hideModal() {
            document.getElementById('templateModal').classList.add('hidden');
            document.getElementById('templateModal').classList.remove('flex');
        }

        function deleteTemplate(id, name) {
            if (confirm(`Are you sure you want to delete the template "${name}"? This action cannot be undone.`)) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.innerHTML = `
                    <input type="hidden" name="action" value="delete">
                    <input type="hidden" name="template_id" value="${id}">
                `;
                document.body.appendChild(form);
                form.submit();
            }
        }

        // Auto-open edit modal if edit parameter is present
        <?php if ($edit_template): ?>
            showEditModal(<?php echo json_encode($edit_template); ?>);
        <?php endif; ?>
    </script>
</body>
</html>

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