Refactor global stats handling and user ID access

Moved global stats logic from LayoutHelper to Domain model and updated views/controllers to use the new stats structure. Replaced direct $_SESSION['user_id'] access with Core\Auth::id() for consistency. Cleaned up redundant code and improved isolation mode handling for statistics.
This commit is contained in:
Hosteroid
2025-10-20 18:38:58 +03:00
parent 719fb86c7a
commit 7ad01a7da0
8 changed files with 47 additions and 91 deletions

View File

@@ -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