Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/programs/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/programs/index.php

<?php
require_once '../../config/config.php';
checkLogin();

$pageTitle = "Programs - " . APP_NAME;
$db = Database::getInstance()->getConnection();
$success = '';
$error = '';

// Get user access parameters
$accessLevel = $_SESSION['access_level'] ?? 'assembly';
$userAreaId = $_SESSION['area_id'] ?? null;
$userDistrictId = $_SESSION['district_id'] ?? null;
$userAssemblyId = $_SESSION['assembly_id'] ?? null;

// Handle program creation/update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        if (isset($_POST['create_program'])) {
            $stmt = $db->prepare("
                INSERT INTO programs (
                    program_name, program_description, program_type, category,
                    area_id, district_id, assembly_id, start_date, end_date,
                    start_time, end_time, recurrence, recurrence_days, venue,
                    coordinator_name, coordinator_phone, max_participants, created_by
                ) VALUES (
                    :name, :description, :type, :category, :area_id, :district_id, :assembly_id,
                    :start_date, :end_date, :start_time, :end_time, :recurrence, :recurrence_days,
                    :venue, :coordinator, :phone, :max_participants, :created_by
                )
            ");
            
            $stmt->execute([
                'name' => $_POST['program_name'],
                'description' => $_POST['program_description'] ?? null,
                'type' => $_POST['program_type'],
                'category' => $_POST['category'] ?? null,
                'area_id' => $_POST['area_id'] ?? $userAreaId,
                'district_id' => $_POST['district_id'] ?? $userDistrictId,
                'assembly_id' => $_POST['assembly_id'] ?? $userAssemblyId,
                'start_date' => $_POST['start_date'] ?? null,
                'end_date' => $_POST['end_date'] ?? null,
                'start_time' => $_POST['start_time'] ?? null,
                'end_time' => $_POST['end_time'] ?? null,
                'recurrence' => $_POST['recurrence'] ?? 'none',
                'recurrence_days' => $_POST['recurrence_days'] ?? null,
                'venue' => $_POST['venue'] ?? null,
                'coordinator' => $_POST['coordinator_name'] ?? null,
                'phone' => $_POST['coordinator_phone'] ?? null,
                'max_participants' => $_POST['max_participants'] ?? null,
                'created_by' => $_SESSION['user_id']
            ]);
            
            $success = "Program created successfully!";
        }
        
        if (isset($_POST['delete_program'])) {
            $stmt = $db->prepare("DELETE FROM programs WHERE id = :id");
            $stmt->execute(['id' => $_POST['program_id']]);
            $success = "Program deleted successfully!";
        }
        
    } catch (Exception $e) {
        $error = "Error: " . $e->getMessage();
    }
}

// Get filter parameters
$filterType = $_GET['type'] ?? '';
$filterView = $_GET['view'] ?? 'list';

// Build query with filters
$query = "
    SELECT p.*, 
           a.area_name, d.district_name, asm.assembly_name,
           (SELECT COUNT(*) FROM program_participants pp WHERE pp.program_id = p.id AND pp.status = 'registered') as participant_count
    FROM programs p
    LEFT JOIN areas a ON p.area_id = a.id
    LEFT JOIN districts d ON p.district_id = d.id
    LEFT JOIN assemblies asm ON p.assembly_id = asm.id
    WHERE 1=1
";

$params = [];

// Access level filters
if ($accessLevel === 'assembly') {
    $query .= " AND p.assembly_id = :assembly_id";
    $params['assembly_id'] = $userAssemblyId;
} elseif ($accessLevel === 'district') {
    $query .= " AND p.district_id = :district_id";
    $params['district_id'] = $userDistrictId;
} elseif ($accessLevel === 'area') {
    $query .= " AND p.area_id = :area_id";
    $params['area_id'] = $userAreaId;
}

if (!empty($filterType)) {
    $query .= " AND p.program_type = :type";
    $params['type'] = $filterType;
}

$query .= " ORDER BY p.start_date DESC, p.created_at DESC";

$stmt = $db->prepare($query);
$stmt->execute($params);
$programs = $stmt->fetchAll();

// Get statistics
$stats = [
    'total' => count($programs),
    'active' => count(array_filter($programs, fn($p) => $p['is_active'])),
    'daily' => count(array_filter($programs, fn($p) => $p['program_type'] === 'daily')),
    'weekly' => count(array_filter($programs, fn($p) => $p['program_type'] === 'weekly')),
    'monthly' => count(array_filter($programs, fn($p) => $p['program_type'] === 'monthly')),
];

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

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

<main class="flex-1 md:ml-64 mt-16">
<div class="container mx-auto px-4 py-8">
    <div class="max-w-7xl mx-auto">
        <div class="mb-6 flex justify-between items-center">
            <div>
                <h1 class="text-3xl font-bold text-gray-800">
                    <i class="fas fa-calendar-alt mr-2 text-blue-500"></i>Programs
                </h1>
                <p class="text-gray-600 mt-2">Manage church programs and schedules</p>
            </div>
            <div class="flex gap-3">
                <a href="?view=calendar" class="bg-purple-600 text-white px-6 py-3 rounded-lg hover:bg-purple-700 transition">
                    <i class="fas fa-calendar mr-2"></i>Calendar View
                </a>
                <button onclick="document.getElementById('createModal').classList.remove('hidden')" 
                        class="btn-gradient text-white px-6 py-3 rounded-lg transition">
                    <i class="fas fa-plus mr-2"></i>Create Program
                </button>
            </div>
        </div>
        
        <?php if ($success): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-6">
                <i class="fas fa-check-circle mr-2"></i><?php echo $success; ?>
            </div>
        <?php endif; ?>
        
        <?php if ($error): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-6">
                <i class="fas fa-exclamation-circle mr-2"></i><?php echo $error; ?>
            </div>
        <?php endif; ?>
        
        <!-- Statistics -->
        <div class="grid grid-cols-1 md:grid-cols-5 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-sm text-gray-600">Total Programs</p>
                        <p class="text-2xl font-bold text-gray-800"><?php echo $stats['total']; ?></p>
                    </div>
                    <i class="fas fa-calendar-alt 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-sm text-gray-600">Active</p>
                        <p class="text-2xl font-bold text-gray-800"><?php echo $stats['active']; ?></p>
                    </div>
                    <i class="fas fa-check-circle 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-sm text-gray-600">Daily</p>
                        <p class="text-2xl font-bold text-gray-800"><?php echo $stats['daily']; ?></p>
                    </div>
                    <i class="fas fa-sun 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-sm text-gray-600">Weekly</p>
                        <p class="text-2xl font-bold text-gray-800"><?php echo $stats['weekly']; ?></p>
                    </div>
                    <i class="fas fa-calendar-week text-3xl text-purple-500"></i>
                </div>
            </div>
            
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-indigo-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">Monthly</p>
                        <p class="text-2xl font-bold text-gray-800"><?php echo $stats['monthly']; ?></p>
                    </div>
                    <i class="fas fa-calendar-day text-3xl text-indigo-500"></i>
                </div>
            </div>
        </div>
        
        <!-- Filters -->
        <div class="bg-white rounded-lg shadow p-4 mb-6">
            <form method="GET" class="flex gap-4">
                <select name="type" class="px-4 py-2 border border-gray-300 rounded-lg">
                    <option value="">All Types</option>
                    <option value="daily" <?php echo $filterType === 'daily' ? 'selected' : ''; ?>>Daily</option>
                    <option value="weekly" <?php echo $filterType === 'weekly' ? 'selected' : ''; ?>>Weekly</option>
                    <option value="monthly" <?php echo $filterType === 'monthly' ? 'selected' : ''; ?>>Monthly</option>
                    <option value="yearly" <?php echo $filterType === 'yearly' ? 'selected' : ''; ?>>Yearly</option>
                    <option value="special" <?php echo $filterType === 'special' ? 'selected' : ''; ?>>Special</option>
                </select>
                <button type="submit" class="bg-blue-500 text-white px-6 py-2 rounded-lg hover:bg-blue-600">
                    <i class="fas fa-filter mr-2"></i>Filter
                </button>
                <a href="?" class="text-gray-600 hover:text-gray-800 px-4 py-2">
                    <i class="fas fa-times mr-2"></i>Clear
                </a>
            </form>
        </div>
        
        <?php if ($filterView === 'calendar'): ?>
        <!-- Calendar View -->
        <div class="bg-white rounded-lg shadow p-6 mb-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-xl font-bold text-gray-800">
                    <i class="fas fa-calendar-alt mr-2"></i>Program Calendar
                </h3>
                <a href="?" class="text-blue-600 hover:text-blue-800">
                    <i class="fas fa-list mr-2"></i>Switch to List View
                </a>
            </div>
            
            <div class="grid grid-cols-7 gap-2 mb-4 text-center font-semibold text-gray-700">
                <div class="p-2 bg-gray-100 rounded">Sun</div>
                <div class="p-2 bg-gray-100 rounded">Mon</div>
                <div class="p-2 bg-gray-100 rounded">Tue</div>
                <div class="p-2 bg-gray-100 rounded">Wed</div>
                <div class="p-2 bg-gray-100 rounded">Thu</div>
                <div class="p-2 bg-gray-100 rounded">Fri</div>
                <div class="p-2 bg-gray-100 rounded">Sat</div>
            </div>
            
            <?php
            $currentMonth = date('m');
            $currentYear = date('Y');
            $firstDay = mktime(0, 0, 0, $currentMonth, 1, $currentYear);
            $firstDayOfWeek = date('w', $firstDay);
            $daysInMonth = date('t', $firstDay);
            
            echo '<div class="grid grid-cols-7 gap-2">';
            
            // Empty cells before first day
            for ($i = 0; $i < $firstDayOfWeek; $i++) {
                echo '<div class="p-2 h-24 border border-gray-200 rounded bg-gray-50"></div>';
            }
            
            // Days of month
            for ($day = 1; $day <= $daysInMonth; $day++) {
                $date = sprintf('%04d-%02d-%02d', $currentYear, $currentMonth, $day);
                $isToday = ($date === date('Y-m-d'));
                
                // Find programs for this day
                $dayPrograms = array_filter($programs, function($p) use ($date) {
                    return ($p['start_date'] === $date) || 
                           ($p['recurrence'] === 'daily') ||
                           ($p['recurrence'] === 'weekly' && date('w', strtotime($date)) === date('w', strtotime($p['start_date'] ?? $date)));
                });
                
                $bgClass = $isToday ? 'bg-blue-50 border-blue-500' : 'bg-white';
                echo '<div class="p-2 h-24 border border-gray-200 rounded ' . $bgClass . ' overflow-hidden">';
                echo '<div class="font-semibold text-gray-700 mb-1">' . $day . '</div>';
                
                foreach (array_slice($dayPrograms, 0, 2) as $prog) {
                    echo '<div class="text-xs bg-blue-100 text-blue-800 px-1 py-0.5 rounded mb-1 truncate">';
                    echo htmlspecialchars(substr($prog['program_name'], 0, 15));
                    echo '</div>';
                }
                
                if (count($dayPrograms) > 2) {
                    echo '<div class="text-xs text-gray-500">+' . (count($dayPrograms) - 2) . ' more</div>';
                }
                
                echo '</div>';
            }
            
            echo '</div>';
            ?>
        </div>
        <?php else: ?>
        <!-- Programs List -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            <?php foreach ($programs as $program): ?>
            <div class="bg-white rounded-lg shadow-lg hover:shadow-xl transition overflow-hidden">
                <div class="bg-gradient-to-r from-blue-500 to-purple-600 text-white p-4">
                    <h3 class="font-bold text-lg"><?php echo htmlspecialchars($program['program_name']); ?></h3>
                    <p class="text-sm opacity-90">
                        <i class="fas fa-clock mr-1"></i>
                        <?php echo ucfirst($program['program_type']); ?> Program
                    </p>
                </div>
                
                <div class="p-4">
                    <?php if ($program['program_description']): ?>
                    <p class="text-gray-600 text-sm mb-3"><?php echo htmlspecialchars(substr($program['program_description'], 0, 100)); ?>...</p>
                    <?php endif; ?>
                    
                    <div class="space-y-2 text-sm text-gray-700">
                        <?php if ($program['start_date']): ?>
                        <div class="flex items-center">
                            <i class="fas fa-calendar w-5 text-blue-500"></i>
                            <span><?php echo date('M j, Y', strtotime($program['start_date'])); ?></span>
                        </div>
                        <?php endif; ?>
                        
                        <?php if ($program['start_time']): ?>
                        <div class="flex items-center">
                            <i class="fas fa-clock w-5 text-green-500"></i>
                            <span><?php echo date('g:i A', strtotime($program['start_time'])); ?></span>
                        </div>
                        <?php endif; ?>
                        
                        <?php if ($program['venue']): ?>
                        <div class="flex items-center">
                            <i class="fas fa-map-marker-alt w-5 text-red-500"></i>
                            <span><?php echo htmlspecialchars($program['venue']); ?></span>
                        </div>
                        <?php endif; ?>
                        
                        <div class="flex items-center">
                            <i class="fas fa-users w-5 text-purple-500"></i>
                            <span><?php echo $program['participant_count']; ?> Participants</span>
                        </div>
                    </div>
                    
                    <div class="mt-4 pt-4 border-t border-gray-200 flex justify-between items-center">
                        <span class="px-3 py-1 text-xs font-medium rounded-full <?php echo $program['is_active'] ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800'; ?>">
                            <?php echo $program['is_active'] ? 'Active' : 'Inactive'; ?>
                        </span>
                        
                        <div class="flex gap-2">
                            <a href="realtime-attendance.php?program_id=<?php echo $program['id']; ?>" 
                               class="text-blue-600 hover:text-blue-800" title="Real-Time Attendance">
                                <i class="fas fa-qrcode"></i>
                            </a>
                            <button onclick="editProgram(<?php echo $program['id']; ?>)" 
                                    class="text-green-600 hover:text-green-800" title="Edit">
                                <i class="fas fa-edit"></i>
                            </button>
                            <form method="POST" class="inline" onsubmit="return confirm('Delete this program?')">
                                <input type="hidden" name="program_id" value="<?php echo $program['id']; ?>">
                                <button type="submit" name="delete_program" 
                                        class="text-red-600 hover:text-red-800" title="Delete">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
            
            <?php if (empty($programs)): ?>
            <div class="col-span-full text-center py-12">
                <i class="fas fa-calendar-alt text-6xl text-gray-300 mb-4"></i>
                <p class="text-gray-600">No programs found</p>
            </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>
</div>
</main>

<!-- Create Program Modal -->
<div id="createModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 overflow-y-auto">
    <div class="flex items-center justify-center min-h-screen p-4">
        <div class="bg-white rounded-lg max-w-2xl w-full p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-semibold">Create New Program</h3>
                <button onclick="document.getElementById('createModal').classList.add('hidden')" 
                        class="text-gray-400 hover:text-gray-600">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            
            <form method="POST" class="space-y-4">
                <div class="grid grid-cols-2 gap-4">
                    <div class="col-span-2">
                        <label class="block text-sm font-medium text-gray-700 mb-2">Program Name*</label>
                        <input type="text" name="program_name" required
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div class="col-span-2">
                        <label class="block text-sm font-medium text-gray-700 mb-2">Description</label>
                        <textarea name="program_description" rows="3"
                                  class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"></textarea>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Program Type*</label>
                        <select name="program_type" required
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                            <option value="daily">Daily</option>
                            <option value="weekly" selected>Weekly</option>
                            <option value="monthly">Monthly</option>
                            <option value="yearly">Yearly</option>
                            <option value="special">Special</option>
                        </select>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Category</label>
                        <input type="text" name="category"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Start Date</label>
                        <input type="date" name="start_date"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">End Date</label>
                        <input type="date" name="end_date"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Start Time</label>
                        <input type="time" name="start_time"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">End Time</label>
                        <input type="time" name="end_time"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div class="col-span-2">
                        <label class="block text-sm font-medium text-gray-700 mb-2">Venue</label>
                        <input type="text" name="venue"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Coordinator Name</label>
                        <input type="text" name="coordinator_name"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Coordinator Phone</label>
                        <input type="tel" name="coordinator_phone"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div class="flex justify-end space-x-3 pt-4">
                    <button type="button" 
                            onclick="document.getElementById('createModal').classList.add('hidden')"
                            class="px-4 py-2 text-gray-600 hover:text-gray-800">
                        Cancel
                    </button>
                    <button type="submit" name="create_program" 
                            class="bg-blue-500 text-white px-6 py-2 rounded-lg hover:bg-blue-600">
                        <i class="fas fa-plus mr-2"></i>Create Program
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

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

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