66 lines
2.4 KiB
PHP
66 lines
2.4 KiB
PHP
<?php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
require_once 'php/model/Article.php';
|
|
require_once 'php/model/ArticleManager.php';
|
|
require_once 'php/validator/article-validator.php';
|
|
|
|
if (isset($_GET["category"]) && !empty($_GET["category"]) && articleCategoryValidator($_GET["category"])){
|
|
$category = $_GET["category"];
|
|
|
|
// Sortierung auslesen (Standard: alphabet)
|
|
$sortStyle = isset($_GET['sort']) ? trim($_GET['sort']) : 'alphabet';
|
|
|
|
// Aktuelle Seite auslesen
|
|
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
|
if ($page < 1) { $page = 1; }
|
|
|
|
try {
|
|
$articleManager = ArticleManager::getInstance();
|
|
$userManager = UserManager::getInstance();
|
|
$allArticles = $articleManager->getArticlesByCategory($category);
|
|
foreach ($allArticles as $article) {
|
|
$article->setAuthor($userManager->findUser($article->getAuthor())["vorname"]) . " " . $userManager->findUser($article->getAuthor())["nachname"];
|
|
}
|
|
$totalArticles = count($allArticles);
|
|
|
|
// --- SORTIERUNG LOGIK ---
|
|
if ($sortStyle === 'alphabet') {
|
|
usort($allArticles, function($a, $b) {
|
|
return strcasecmp($a->getTitle(), $b->getTitle());
|
|
});
|
|
} elseif ($sortStyle === 'likes') {
|
|
usort($allArticles, function($a, $b) {
|
|
return $b->getLikes() <=> $a->getLikes(); // Absteigend nach Likes
|
|
});
|
|
} elseif ($sortStyle === 'newest') {
|
|
usort($allArticles, function($a, $b) {
|
|
return strtotime($b->getCreationDate()) <=> strtotime($a->getCreationDate()); // Neueste zuerst
|
|
});
|
|
} elseif ($sortStyle === 'oldest') {
|
|
usort($allArticles, function($a, $b) {
|
|
return strtotime($a->getCreationDate()) <=> strtotime($b->getCreationDate()); // Älteste zuerst
|
|
});
|
|
}
|
|
|
|
// Paginierung konfigurieren
|
|
$limit = 10;
|
|
$totalPages = ceil($totalArticles / $limit);
|
|
if ($page > $totalPages && $totalPages > 0) { $page = $totalPages; }
|
|
|
|
$offset = ($page - 1) * $limit;
|
|
$articles = array_slice($allArticles, $offset, $limit);
|
|
|
|
} catch (Exception $e) {
|
|
$_SESSION["message"] = "internal_error";
|
|
include_once "content/404.php";
|
|
exit();
|
|
}
|
|
} else {
|
|
$_SESSION["message"] = "invalid_category";
|
|
include_once "content/404.php";
|
|
exit();
|
|
}
|
|
?>
|