refactor: split config.php into modules and lazy-load categories
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* Fonctions de formatage des données (vidéos, durées, dates, tailles).
|
||||
*
|
||||
* Extrait de includes/config.php (ARC-1) — chargé par ce dernier.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Formate les données brutes des vidéos venant de l'API
|
||||
*
|
||||
* @param array $videosData Données brutes des vidéos
|
||||
* @return array Données formatées
|
||||
*/
|
||||
function formatVideosData($videosData) {
|
||||
$videos = [];
|
||||
|
||||
foreach ($videosData as $video) {
|
||||
// Ignorer les entrées sans uuid : l'identifiant est indispensable
|
||||
if (empty($video['uuid'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Récupérer la vignette (thumbnail)
|
||||
$thumbnail = isset($video['previewPath'])
|
||||
? PEERTUBE_URL . $video['previewPath']
|
||||
: 'img/default-thumbnail.jpg';
|
||||
|
||||
// Récupérer l'avatar de la chaîne
|
||||
$channelAvatar = isset($video['channel']['avatars'][0]['path'])
|
||||
? PEERTUBE_URL . $video['channel']['avatars'][0]['path']
|
||||
: 'img/default-avatar.png';
|
||||
|
||||
// Formater les données (valeurs par défaut pour les champs absents)
|
||||
$videos[] = [
|
||||
'id' => $video['uuid'],
|
||||
'title' => $video['name'] ?? '',
|
||||
'thumbnail' => $thumbnail,
|
||||
'duration' => $video['duration'] ?? 0,
|
||||
'channel' => $video['channel']['displayName'] ?? '',
|
||||
'channelAvatar' => $channelAvatar,
|
||||
'views' => $video['views'] ?? 0,
|
||||
'date' => $video['publishedAt'] ?? '',
|
||||
'aspectRatio' => $video['aspectRatio'] ?? null,
|
||||
'description' => $video['description'] ?? '',
|
||||
'tags' => $video['tags'] ?? [],
|
||||
'isLive' => $video['isLive'] ?? false
|
||||
];
|
||||
}
|
||||
|
||||
return $videos;
|
||||
}
|
||||
|
||||
// Fonctions utilitaires pour formater les données d'affichage
|
||||
function formatDuration($seconds) {
|
||||
$hours = floor($seconds / 3600);
|
||||
$minutes = floor(($seconds % 3600) / 60);
|
||||
$remainingSeconds = $seconds % 60;
|
||||
|
||||
if ($hours > 0) {
|
||||
return sprintf('%d:%02d:%02d', $hours, $minutes, $remainingSeconds);
|
||||
} else {
|
||||
return sprintf('%d:%02d', $minutes, $remainingSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
function formatViewCount($views) {
|
||||
if ($views >= 1000000) {
|
||||
return round($views / 1000000, 1) . 'M';
|
||||
} elseif ($views >= 1000) {
|
||||
return round($views / 1000, 1) . 'K';
|
||||
} else {
|
||||
return $views;
|
||||
}
|
||||
}
|
||||
|
||||
function formatDate($dateString) {
|
||||
// Chaîne vide : rien à formater (new DateTime('') renverrait « maintenant »)
|
||||
if (!is_string($dateString) || trim($dateString) === '') {
|
||||
return (string) $dateString;
|
||||
}
|
||||
|
||||
try {
|
||||
$date = new DateTime($dateString);
|
||||
} catch (Exception $e) {
|
||||
// Date malformée (données API inattendues) : afficher la chaîne brute
|
||||
return $dateString;
|
||||
}
|
||||
|
||||
$now = new DateTime();
|
||||
$interval = $now->diff($date);
|
||||
|
||||
if ($interval->days == 0) {
|
||||
return 'Aujourd\'hui';
|
||||
} elseif ($interval->days == 1) {
|
||||
return 'Hier';
|
||||
} elseif ($interval->days < 7) {
|
||||
return 'Il y a ' . $interval->days . ' jours';
|
||||
} elseif ($interval->days < 30) {
|
||||
$weeks = floor($interval->days / 7);
|
||||
return 'Il y a ' . $weeks . ' semaine' . ($weeks > 1 ? 's' : '');
|
||||
} elseif ($interval->days < 365) {
|
||||
$months = floor($interval->days / 30);
|
||||
return 'Il y a ' . $months . ' mois';
|
||||
} else {
|
||||
$years = floor($interval->days / 365);
|
||||
return 'Il y a ' . $years . ' an' . ($years > 1 ? 's' : '');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formate la taille d'un fichier en format lisible
|
||||
* @param int $bytes Taille en octets
|
||||
* @return string Taille formatée
|
||||
*/
|
||||
function formatFileSize($bytes) {
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
|
||||
$bytes = max($bytes, 0);
|
||||
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
||||
$pow = min($pow, count($units) - 1);
|
||||
|
||||
$bytes /= (1 << (10 * $pow));
|
||||
|
||||
return round($bytes, 2) . ' ' . $units[$pow];
|
||||
}
|
||||
Reference in New Issue
Block a user