diff --git a/app/Controllers/DashboardController.php b/app/Controllers/DashboardController.php index 624b52f..4f3a69b 100644 --- a/app/Controllers/DashboardController.php +++ b/app/Controllers/DashboardController.php @@ -27,13 +27,11 @@ class DashboardController extends Controller $settingModel = new \App\Models\Setting(); $isolationMode = $settingModel->getValue('user_isolation_mode', 'shared'); - // Get statistics based on isolation mode + // Get data based on isolation mode (stats are now handled in base.php) if ($isolationMode === 'isolated') { - $stats = $this->domainModel->getStatistics($userId); $recentDomains = $this->domainModel->getRecent(5, $userId); $groups = $this->groupModel->getAllWithChannelCount($userId); } else { - $stats = $this->domainModel->getStatistics(); $recentDomains = $this->domainModel->getRecent(5); $groups = $this->groupModel->getAllWithChannelCount(); } @@ -59,12 +57,7 @@ class DashboardController extends Controller $formattedRecentDomains = \App\Helpers\DomainHelper::formatMultiple($recentDomains); $formattedExpiringDomains = \App\Helpers\DomainHelper::formatMultiple($expiringThisMonth); - // Get global stats for dashboard cards - $globalStats = \App\Helpers\LayoutHelper::getGlobalStats($userId); - $this->view('dashboard/index', [ - 'stats' => $stats, - 'globalStats' => $globalStats, 'recentDomains' => $formattedRecentDomains, 'expiringThisMonth' => $formattedExpiringDomains, 'expiringCount' => count($allExpiringDomains), diff --git a/app/Controllers/ErrorLogController.php b/app/Controllers/ErrorLogController.php index 217e04c..0758f70 100644 --- a/app/Controllers/ErrorLogController.php +++ b/app/Controllers/ErrorLogController.php @@ -100,7 +100,7 @@ class ErrorLogController extends Controller $notes = $_POST['notes'] ?? null; // Mark error as resolved using model - $this->errorLogModel->markErrorResolved($errorId, $_SESSION['user_id'], $notes); + $this->errorLogModel->markErrorResolved($errorId, \Core\Auth::id(), $notes); $_SESSION['success'] = 'Error marked as resolved'; header('Location: /errors/' . urlencode($errorId)); diff --git a/app/Helpers/LayoutHelper.php b/app/Helpers/LayoutHelper.php index ca48333..12e67b5 100644 --- a/app/Helpers/LayoutHelper.php +++ b/app/Helpers/LayoutHelper.php @@ -34,73 +34,6 @@ class LayoutHelper } } - /** - * Get stats for sidebar (respects user isolation) - */ - public static function getGlobalStats(?int $userId = null): array - { - try { - $pdo = \Core\Database::getConnection(); - - // Check isolation mode - $settingModel = new Setting(); - $isolationMode = $settingModel->getValue('user_isolation_mode', 'shared'); - - // Build WHERE clause based on isolation mode - $whereClause = ''; - $params = []; - - if ($isolationMode === 'isolated' && $userId) { - $whereClause = ' WHERE user_id = ?'; - $params[] = $userId; - } - - // Get total domains - $totalSql = "SELECT COUNT(*) as count FROM domains" . $whereClause; - $totalStmt = $pdo->prepare($totalSql); - $totalStmt->execute($params); - $total = $totalStmt->fetch(\PDO::FETCH_ASSOC)['count'] ?? 0; - - // Get active domains - $activeSql = "SELECT COUNT(*) as count FROM domains WHERE is_active = 1" . $whereClause; - $activeStmt = $pdo->prepare($activeSql); - $activeStmt->execute($params); - $active = $activeStmt->fetch(\PDO::FETCH_ASSOC)['count'] ?? 0; - - // Get expiring soon - $notificationDays = $settingModel->getNotificationDays(); - $threshold = !empty($notificationDays) ? max($notificationDays) : 30; - - $expiringSql = "SELECT COUNT(*) as count FROM domains - WHERE is_active = 1 - AND expiration_date IS NOT NULL - AND expiration_date <= DATE_ADD(NOW(), INTERVAL ? DAY) - AND expiration_date >= NOW()" . $whereClause; - - $expiringParams = [$threshold]; - if ($isolationMode === 'isolated' && $userId) { - $expiringParams[] = $userId; - } - - $expiringSoonStmt = $pdo->prepare($expiringSql); - $expiringSoonStmt->execute($expiringParams); - $expiringSoon = $expiringSoonStmt->fetch(\PDO::FETCH_ASSOC)['count'] ?? 0; - - return [ - 'total' => $total, - 'active' => $active, - 'expiring_soon' => $expiringSoon, - 'expiring_threshold' => $threshold - ]; - } catch (\Exception $e) { - return [ - 'total' => 0, - 'active' => 0, - 'expiring_soon' => 0, - 'expiring_threshold' => 30 - ]; - } - } /** * Convert timestamp to "time ago" format diff --git a/app/Models/Domain.php b/app/Models/Domain.php index 0061db7..d519087 100644 --- a/app/Models/Domain.php +++ b/app/Models/Domain.php @@ -164,6 +164,7 @@ class Domain extends Model 'expiring_soon' => 0, 'expired' => 0, 'inactive' => 0, + 'expiring_threshold' => 30, ]; // Build WHERE clause for user filtering @@ -204,6 +205,25 @@ class Domain extends Model // Add inactive count to total $stats['total'] += $stats['inactive']; + // Get expiring soon count + $settingModel = new \App\Models\Setting(); + $notificationDays = $settingModel->getNotificationDays(); + $threshold = !empty($notificationDays) ? max($notificationDays) : 30; + $stats['expiring_threshold'] = $threshold; + + $expiringWhereClause = "WHERE is_active = 1 AND expiration_date IS NOT NULL AND expiration_date <= DATE_ADD(NOW(), INTERVAL ? DAY) AND expiration_date >= NOW()"; + $expiringParams = [$threshold]; + + if ($userId) { + $expiringWhereClause .= " AND user_id = ?"; + $expiringParams[] = $userId; + } + + $expiringStmt = $this->db->prepare("SELECT COUNT(*) as count FROM domains $expiringWhereClause"); + $expiringStmt->execute($expiringParams); + $expiringResult = $expiringStmt->fetch(); + $stats['expiring_soon'] = $expiringResult['count'] ?? 0; + return $stats; } diff --git a/app/Views/dashboard/index.php b/app/Views/dashboard/index.php index cced667..a1f5ed5 100644 --- a/app/Views/dashboard/index.php +++ b/app/Views/dashboard/index.php @@ -39,8 +39,8 @@ ob_start();

Expiring Soon

-

-

within days

+

+

within days

@@ -255,7 +255,7 @@ ob_start();

No domains expiring soon

-

within days

+

within days

diff --git a/app/Views/layout/base.php b/app/Views/layout/base.php index 9d1da00..8cc82f6 100644 --- a/app/Views/layout/base.php +++ b/app/Views/layout/base.php @@ -4,9 +4,12 @@ * Contains: HTML structure, meta tags, CSS/JS includes, global stats */ +// Get current user ID (used for both notifications and stats) +$userId = \Core\Auth::id(); + // Fetch notifications for top nav (available on all pages) -if (isset($_SESSION['user_id'])) { - $notificationData = \App\Helpers\LayoutHelper::getNotifications($_SESSION['user_id']); +if ($userId) { + $notificationData = \App\Helpers\LayoutHelper::getNotifications($userId); $recentNotifications = $notificationData['items']; $unreadNotifications = $notificationData['unread_count']; } else { @@ -14,10 +17,17 @@ if (isset($_SESSION['user_id'])) { $unreadNotifications = 0; } -// Fetch global stats for sidebar (available on all pages) -if (!isset($globalStats)) { - $userId = \Core\Auth::id(); - $globalStats = \App\Helpers\LayoutHelper::getGlobalStats($userId); +// Get stats for sidebar (available on all pages) +if (!isset($stats)) { + $domainModel = new \App\Models\Domain(); + $settingModel = new \App\Models\Setting(); + $isolationMode = $settingModel->getValue('user_isolation_mode', 'shared'); + + if ($isolationMode === 'isolated') { + $stats = $domainModel->getStatistics($userId); + } else { + $stats = $domainModel->getStatistics(); + } } // Get application settings from database diff --git a/app/Views/layout/sidebar.php b/app/Views/layout/sidebar.php index 14d10b2..eb3a37d 100644 --- a/app/Views/layout/sidebar.php +++ b/app/Views/layout/sidebar.php @@ -84,7 +84,7 @@
Total - + @@ -94,9 +94,9 @@
- Expiring + Expiring - + @@ -108,7 +108,7 @@ Active - + diff --git a/app/Views/users/index.php b/app/Views/users/index.php index e53ef06..530f19f 100644 --- a/app/Views/users/index.php +++ b/app/Views/users/index.php @@ -197,7 +197,7 @@ $pagination = $pagination ?? [ - + @@ -261,7 +261,7 @@ $pagination = $pagination ?? [ - +