Sindbad~EG File Manager

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

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

checkLogin();

$db = Database::getInstance()->getConnection();

// Initialize default values
$stats = ['total_sent' => 0, 'successful' => 0, 'failed' => 0, 'today' => 0];
$queueStats = ['total' => 0, 'pending' => 0, 'sent' => 0, 'failed' => 0];
$recentSMS = [];
$templates = [];
$smsService = null;

try {
    $smsService = new SMSService();
    
    // Get statistics
    $statsQuery = $db->query("
        SELECT 
            COUNT(*) as total_sent,
            SUM(CASE WHEN status = 'sent' THEN 1 ELSE 0 END) as successful,
            SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed,
            COUNT(CASE WHEN DATE(sent_at) = CURDATE() THEN 1 END) as today
        FROM sms_logs
    ");
    $stats = $statsQuery->fetch(PDO::FETCH_ASSOC);

    // Get queue statistics
    $queueQuery = $db->query("
        SELECT 
            COUNT(*) as total,
            SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending,
            SUM(CASE WHEN status = 'sent' THEN 1 ELSE 0 END) as sent,
            SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed
        FROM sms_queue
    ");
    $queueStats = $queueQuery->fetch(PDO::FETCH_ASSOC);

    // Get recent SMS
    $recentSMS = $db->query("
        SELECT * FROM sms_logs 
        ORDER BY sent_at DESC 
        LIMIT 10
    ")->fetchAll(PDO::FETCH_ASSOC);

    // Get templates
    $templates = $db->query("
        SELECT * FROM sms_templates 
        WHERE is_active = 1
        ORDER BY template_name
    ")->fetchAll(PDO::FETCH_ASSOC);
    
} catch (PDOException $e) {
    // Tables don't exist yet - show installation message
    $installationNeeded = true;
    error_log("SMS Management Error: " . $e->getMessage());
}

$pageTitle = 'SMS Management';
include '../../includes/header.php';
?>

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

<!-- Main Content -->
<main class="flex-1 md:ml-64 mt-16">
    <div class="container mx-auto px-4 py-8">
    
    <?php if (isset($installationNeeded) && $installationNeeded): ?>
        <!-- Installation Required Alert -->
        <div class="bg-red-100 border-l-4 border-red-500 text-red-800 p-6 mb-6 rounded-lg">
            <div class="flex items-start">
                <i class="fas fa-exclamation-triangle text-3xl mr-4 mt-1"></i>
                <div class="flex-1">
                    <h3 class="font-bold text-lg mb-2">SMS Management Not Installed</h3>
                    <p class="mb-4">The SMS Management System needs to be installed before you can use it. Please run the installation script.</p>
                    <?php if (isSuperuser()): ?>
                        <a href="../../install_sms_management.php" class="inline-block bg-red-600 hover:bg-red-700 text-white px-6 py-3 rounded-lg font-semibold transition">
                            <i class="fas fa-download mr-2"></i>Install SMS Management
                        </a>
                    <?php else: ?>
                        <p class="text-sm">Please contact a system administrator to install SMS Management.</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>
    
    <!-- Header -->
    <div class="flex justify-between items-center mb-6">
        <div>
            <h1 class="text-3xl font-bold text-gray-800">
                <i class="fas fa-sms mr-3 text-blue-600"></i>SMS Management
            </h1>
            <p class="text-gray-600 mt-2">Send and manage SMS messages</p>
        </div>
        <div class="flex space-x-3">
            <a href="send.php" class="bg-gradient-to-r from-green-600 to-green-700 hover:from-green-700 hover:to-green-800 text-white px-6 py-3 rounded-lg transition duration-200 transform hover:scale-105 shadow-lg">
                <i class="fas fa-paper-plane mr-2"></i>Send SMS
            </a>
            <?php if (isSuperuser()): ?>
                <a href="settings.php" class="bg-gradient-to-r from-purple-600 to-purple-700 hover:from-purple-700 hover:to-purple-800 text-white px-6 py-3 rounded-lg transition duration-200 transform hover:scale-105 shadow-lg">
                    <i class="fas fa-cog mr-2"></i>Settings
                </a>
            <?php endif; ?>
        </div>
    </div>

    <!-- Service Status Alert -->
    <?php if ($smsService && !$smsService->isEnabled()): ?>
        <div class="bg-yellow-100 border-l-4 border-yellow-500 text-yellow-800 p-4 mb-6 rounded-lg flex items-center">
            <i class="fas fa-exclamation-triangle text-2xl mr-4"></i>
            <div>
                <p class="font-semibold">SMS Service is Disabled</p>
                <p class="text-sm">SMS messages will not be sent. Please enable SMS service in settings.</p>
                <?php if (isSuperuser()): ?>
                    <a href="settings.php" class="text-sm underline font-semibold mt-2 inline-block">Configure SMS Settings →</a>
                <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>

    <!-- Statistics Cards -->
    <div class="grid md:grid-cols-4 gap-6 mb-8">
        <!-- Total Sent -->
        <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-blue-500 transform hover:scale-105 transition duration-200">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-600 text-sm font-medium">Total SMS Sent</p>
                    <p class="text-3xl font-bold text-gray-800 mt-2"><?php echo number_format($stats['total_sent']); ?></p>
                </div>
                <div class="bg-gradient-to-br from-blue-500 to-blue-600 p-4 rounded-full">
                    <i class="fas fa-paper-plane text-white text-2xl"></i>
                </div>
            </div>
        </div>

        <!-- Successful -->
        <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-green-500 transform hover:scale-105 transition duration-200">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-600 text-sm font-medium">Successful</p>
                    <p class="text-3xl font-bold text-gray-800 mt-2"><?php echo number_format($stats['successful']); ?></p>
                </div>
                <div class="bg-gradient-to-br from-green-500 to-green-600 p-4 rounded-full">
                    <i class="fas fa-check-circle text-white text-2xl"></i>
                </div>
            </div>
        </div>

        <!-- Failed -->
        <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-red-500 transform hover:scale-105 transition duration-200">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-600 text-sm font-medium">Failed</p>
                    <p class="text-3xl font-bold text-gray-800 mt-2"><?php echo number_format($stats['failed']); ?></p>
                </div>
                <div class="bg-gradient-to-br from-red-500 to-red-600 p-4 rounded-full">
                    <i class="fas fa-times-circle text-white text-2xl"></i>
                </div>
            </div>
        </div>

        <!-- Today -->
        <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-purple-500 transform hover:scale-105 transition duration-200">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-600 text-sm font-medium">Sent Today</p>
                    <p class="text-3xl font-bold text-gray-800 mt-2"><?php echo number_format($stats['today']); ?></p>
                </div>
                <div class="bg-gradient-to-br from-purple-500 to-purple-600 p-4 rounded-full">
                    <i class="fas fa-calendar-day text-white text-2xl"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Queue Statistics -->
    <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
        <h2 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
            <i class="fas fa-clock mr-2 text-orange-600"></i>SMS Queue Status
        </h2>
        <div class="grid md:grid-cols-4 gap-4">
            <div class="text-center p-4 bg-gray-50 rounded-lg">
                <p class="text-gray-600 text-sm">Total in Queue</p>
                <p class="text-2xl font-bold text-gray-800 mt-2"><?php echo number_format($queueStats['total']); ?></p>
            </div>
            <div class="text-center p-4 bg-yellow-50 rounded-lg">
                <p class="text-yellow-600 text-sm">Pending</p>
                <p class="text-2xl font-bold text-yellow-600 mt-2"><?php echo number_format($queueStats['pending']); ?></p>
            </div>
            <div class="text-center p-4 bg-green-50 rounded-lg">
                <p class="text-green-600 text-sm">Sent</p>
                <p class="text-2xl font-bold text-green-600 mt-2"><?php echo number_format($queueStats['sent']); ?></p>
            </div>
            <div class="text-center p-4 bg-red-50 rounded-lg">
                <p class="text-red-600 text-sm">Failed</p>
                <p class="text-2xl font-bold text-red-600 mt-2"><?php echo number_format($queueStats['failed']); ?></p>
            </div>
        </div>
    </div>

    <!-- Gateway Info & Templates -->
    <div class="grid md:grid-cols-2 gap-6 mb-8">
        <!-- Gateway Info -->
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                <i class="fas fa-network-wired mr-2 text-blue-600"></i>Active Gateway
            </h2>
            <div class="space-y-3">
                <div class="flex justify-between items-center p-3 bg-blue-50 rounded-lg">
                    <span class="text-gray-700 font-medium">Provider:</span>
                    <span class="font-bold text-blue-600 capitalize"><?php echo $smsService ? str_replace('_', ' ', $smsService->getGateway()) : 'Not Configured'; ?></span>
                </div>
                <div class="flex justify-between items-center p-3 bg-gray-50 rounded-lg">
                    <span class="text-gray-700 font-medium">Status:</span>
                    <?php if ($smsService && $smsService->isEnabled()): ?>
                        <span class="px-3 py-1 bg-green-100 text-green-800 rounded-full text-sm font-semibold">
                            <i class="fas fa-check-circle mr-1"></i>Active
                        </span>
                    <?php else: ?>
                        <span class="px-3 py-1 bg-red-100 text-red-800 rounded-full text-sm font-semibold">
                            <i class="fas fa-times-circle mr-1"></i>Inactive
                        </span>
                    <?php endif; ?>
                </div>
            </div>
            <?php if (isSuperuser()): ?>
                <a href="settings.php" class="block mt-4 text-center bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg transition">
                    <i class="fas fa-cog mr-2"></i>Configure Gateway
                </a>
            <?php endif; ?>
        </div>

        <!-- SMS Templates -->
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                <i class="fas fa-file-alt mr-2 text-purple-600"></i>SMS Templates
            </h2>
            <div class="space-y-2 max-h-64 overflow-y-auto">
                <?php foreach ($templates as $template): ?>
                    <div class="p-3 bg-gray-50 hover:bg-gray-100 rounded-lg transition cursor-pointer">
                        <div class="flex justify-between items-center">
                            <div>
                                <p class="font-semibold text-gray-800"><?php echo htmlspecialchars($template['template_name']); ?></p>
                                <p class="text-xs text-gray-500 mt-1"><?php echo substr(htmlspecialchars($template['message_template']), 0, 50); ?>...</p>
                            </div>
                            <a href="send.php?template=<?php echo $template['id']; ?>" class="text-blue-600 hover:text-blue-700">
                                <i class="fas fa-arrow-right"></i>
                            </a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            <?php if (isSuperuser()): ?>
                <a href="templates.php" class="block mt-4 text-center bg-purple-500 hover:bg-purple-600 text-white px-4 py-2 rounded-lg transition">
                    <i class="fas fa-plus mr-2"></i>Manage Templates
                </a>
            <?php endif; ?>
        </div>
    </div>

    <!-- Recent SMS -->
    <div class="bg-white rounded-xl shadow-lg p-6">
        <div class="flex justify-between items-center mb-4">
            <h2 class="text-xl font-bold text-gray-800 flex items-center">
                <i class="fas fa-history mr-2 text-green-600"></i>Recent SMS
            </h2>
            <a href="logs.php" class="text-blue-600 hover:text-blue-700 font-medium">
                View All <i class="fas fa-arrow-right ml-1"></i>
            </a>
        </div>

        <div class="overflow-x-auto">
            <table class="w-full">
                <thead>
                    <tr class="bg-gray-50">
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-600 uppercase">Recipient</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-600 uppercase">Message</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-600 uppercase">Gateway</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-600 uppercase">Status</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-600 uppercase">Sent At</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200">
                    <?php if (empty($recentSMS)): ?>
                        <tr>
                            <td colspan="5" class="px-4 py-8 text-center text-gray-500">
                                <i class="fas fa-inbox text-4xl mb-3 text-gray-300"></i>
                                <p>No SMS sent yet</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($recentSMS as $sms): ?>
                            <tr class="hover:bg-gray-50 transition">
                                <td class="px-4 py-3">
                                    <p class="font-semibold text-gray-800"><?php echo htmlspecialchars($sms['recipient_name'] ?? 'Unknown'); ?></p>
                                    <p class="text-xs text-gray-500"><?php echo htmlspecialchars($sms['recipient_phone']); ?></p>
                                </td>
                                <td class="px-4 py-3">
                                    <p class="text-sm text-gray-700"><?php echo substr(htmlspecialchars($sms['message']), 0, 50); ?><?php echo strlen($sms['message']) > 50 ? '...' : ''; ?></p>
                                </td>
                                <td class="px-4 py-3">
                                    <span class="text-xs px-2 py-1 bg-blue-100 text-blue-800 rounded-full capitalize">
                                        <?php echo str_replace('_', ' ', htmlspecialchars($sms['gateway_provider'])); ?>
                                    </span>
                                </td>
                                <td class="px-4 py-3">
                                    <?php if ($sms['status'] === 'sent'): ?>
                                        <span class="px-3 py-1 bg-green-100 text-green-800 rounded-full text-xs font-semibold">
                                            <i class="fas fa-check-circle mr-1"></i>Sent
                                        </span>
                                    <?php else: ?>
                                        <span class="px-3 py-1 bg-red-100 text-red-800 rounded-full text-xs font-semibold">
                                            <i class="fas fa-times-circle mr-1"></i>Failed
                                        </span>
                                    <?php endif; ?>
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-600">
                                    <?php echo date('M d, Y H:i', strtotime($sms['sent_at'])); ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    </div>
</main>

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

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