Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/setup_special_codes.php

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

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

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

$success_message = '';
$error_message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        // Create special codes table
        $sql = "CREATE TABLE IF NOT EXISTS special_codes (
            id INT AUTO_INCREMENT PRIMARY KEY,
            code VARCHAR(20) NOT NULL UNIQUE,
            description TEXT,
            created_by INT NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            expires_at TIMESTAMP NULL,
            is_active BOOLEAN DEFAULT TRUE,
            usage_count INT DEFAULT 0,
            max_usage INT DEFAULT NULL,
            FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE,
            INDEX idx_code (code),
            INDEX idx_active (is_active),
            INDEX idx_expires (expires_at)
        )";
        
        $conn->exec($sql);
        
        // Check if default code exists
        $check_query = "SELECT COUNT(*) FROM special_codes WHERE code = 'ADMIN2024'";
        $stmt = $conn->prepare($check_query);
        $stmt->execute();
        $exists = $stmt->fetchColumn();
        
        if ($exists == 0) {
            // Insert sample special code
            $insert_query = "INSERT INTO special_codes (code, description, created_by) 
                            SELECT 'ADMIN2024', 'Default admin access code for special reports', id 
                            FROM users WHERE role IN ('superuser', 'admin') LIMIT 1";
            $conn->exec($insert_query);
        }
        
        $success_message = 'Special codes table created successfully! Default code "ADMIN2024" is ready to use.';
        
        // Log activity
        logActivity($_SESSION['user_id'], 'special_codes_setup', 'Special codes table created');
        
    } catch (Exception $e) {
        $error_message = 'Error setting up special codes: ' . $e->getMessage();
    }
}

// Check if table exists
$table_exists = false;
try {
    $check_table = "SHOW TABLES LIKE 'special_codes'";
    $stmt = $conn->prepare($check_table);
    $stmt->execute();
    $table_exists = $stmt->rowCount() > 0;
} catch (Exception $e) {
    // Table doesn't exist
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Setup Special Codes - Admin Panel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
    <div class="flex h-screen">
        <!-- Sidebar -->
        <?php include 'includes/sidebar.php'; ?>

        <!-- Main Content -->
        <div class="flex-1 flex flex-col overflow-hidden">
            <!-- Header -->
            <header class="bg-white shadow-sm border-b border-gray-200">
                <div class="max-w-7xl mx-auto py-4 px-4 sm:px-6 lg:px-8">
                    <div class="flex items-center justify-between">
                        <h1 class="text-2xl font-bold text-gray-900">
                            <i class="fas fa-cogs mr-2 text-blue-600"></i>Setup Special Codes
                        </h1>
                        <a href="special_codes.php" class="text-blue-600 hover:text-blue-800">
                            <i class="fas fa-arrow-right mr-1"></i>Go to Special Codes
                        </a>
                    </div>
                </div>
            </header>

            <!-- Main Content Area -->
            <main class="flex-1 overflow-x-hidden overflow-y-auto bg-gray-100 p-6">
                <div class="max-w-4xl mx-auto">
                    <!-- Success/Error Messages -->
                    <?php if ($success_message): ?>
                        <div class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg mb-6">
                            <i class="fas fa-check-circle mr-2"></i>
                            <?php echo $success_message; ?>
                        </div>
                    <?php endif; ?>

                    <?php if ($error_message): ?>
                        <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6">
                            <i class="fas fa-exclamation-triangle mr-2"></i>
                            <?php echo $error_message; ?>
                        </div>
                    <?php endif; ?>

                    <!-- Setup Status -->
                    <div class="bg-white rounded-lg shadow-lg p-6 mb-8">
                        <h2 class="text-xl font-semibold text-gray-900 mb-4">
                            <i class="fas fa-info-circle mr-2 text-blue-600"></i>Setup Status
                        </h2>
                        
                        <div class="space-y-4">
                            <div class="flex items-center">
                                <?php if ($table_exists): ?>
                                    <i class="fas fa-check-circle text-green-600 text-xl mr-3"></i>
                                    <span class="text-green-700 font-medium">Special codes table exists</span>
                                <?php else: ?>
                                    <i class="fas fa-times-circle text-red-600 text-xl mr-3"></i>
                                    <span class="text-red-700 font-medium">Special codes table not found</span>
                                <?php endif; ?>
                            </div>
                            
                            <?php if ($table_exists): ?>
                                <?php
                                // Get code count
                                $count_query = "SELECT COUNT(*) FROM special_codes";
                                $stmt = $conn->prepare($count_query);
                                $stmt->execute();
                                $code_count = $stmt->fetchColumn();
                                ?>
                                <div class="flex items-center">
                                    <i class="fas fa-key text-blue-600 text-xl mr-3"></i>
                                    <span class="text-gray-700">
                                        <strong><?php echo $code_count; ?></strong> special code(s) configured
                                    </span>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <?php if (!$table_exists): ?>
                        <!-- Setup Form -->
                        <div class="bg-white rounded-lg shadow-lg p-6">
                            <h2 class="text-xl font-semibold text-gray-900 mb-4">
                                <i class="fas fa-play-circle mr-2 text-green-600"></i>Initialize Special Codes System
                            </h2>
                            
                            <div class="mb-6">
                                <p class="text-gray-600 mb-4">
                                    This will create the special codes table and set up the default access code for special attendance reports.
                                </p>
                                
                                <div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
                                    <h3 class="font-semibold text-blue-900 mb-2">What will be created:</h3>
                                    <ul class="text-blue-800 space-y-1">
                                        <li><i class="fas fa-check mr-2"></i>Special codes database table</li>
                                        <li><i class="fas fa-check mr-2"></i>Default access code: <code class="bg-blue-100 px-2 py-1 rounded">ADMIN2024</code></li>
                                        <li><i class="fas fa-check mr-2"></i>Database indexes for performance</li>
                                        <li><i class="fas fa-check mr-2"></i>Foreign key relationships</li>
                                    </ul>
                                </div>
                            </div>
                            
                            <form method="POST">
                                <button type="submit" class="bg-green-600 text-white py-3 px-6 rounded-lg hover:bg-green-700 transition duration-300 font-semibold">
                                    <i class="fas fa-rocket mr-2"></i>Initialize Special Codes System
                                </button>
                            </form>
                        </div>
                    <?php else: ?>
                        <!-- Already Setup -->
                        <div class="bg-white rounded-lg shadow-lg p-6">
                            <h2 class="text-xl font-semibold text-gray-900 mb-4">
                                <i class="fas fa-check-circle mr-2 text-green-600"></i>System Ready
                            </h2>
                            
                            <p class="text-gray-600 mb-6">
                                The special codes system is already set up and ready to use.
                            </p>
                            
                            <div class="flex space-x-4">
                                <a href="special_codes.php" class="bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 transition duration-300">
                                    <i class="fas fa-key mr-2"></i>Manage Special Codes
                                </a>
                                <a href="../special_attendance_reports.php" class="bg-green-600 text-white py-2 px-4 rounded-lg hover:bg-green-700 transition duration-300">
                                    <i class="fas fa-chart-bar mr-2"></i>View Special Reports
                                </a>
                            </div>
                        </div>
                    <?php endif; ?>

                    <!-- Instructions -->
                    <div class="mt-8 bg-gray-50 border border-gray-200 rounded-lg p-6">
                        <h3 class="text-lg font-semibold text-gray-900 mb-3">
                            <i class="fas fa-question-circle mr-2"></i>How Special Codes Work
                        </h3>
                        <div class="text-gray-700 space-y-2">
                            <p><strong>1.</strong> Admins generate special access codes in the Special Codes management page</p>
                            <p><strong>2.</strong> These codes can be shared with authorized users for accessing special reports</p>
                            <p><strong>3.</strong> Users visit the Special Attendance Reports page and enter the code</p>
                            <p><strong>4.</strong> Once verified, users can view comprehensive attendance analytics and export data</p>
                            <p><strong>5.</strong> Codes can have expiration dates and usage limits for security</p>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
</body>
</html>

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