157 lines
7.4 KiB
PHP
157 lines
7.4 KiB
PHP
<?php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
require_once __DIR__ . '/../includes/resultsHelper.php';
|
|
|
|
$rawResults = $_SESSION["search_results"] ?? [];
|
|
$query = $_SESSION["search_query"] ?? "";
|
|
|
|
// Sortierung / Kategorie / Limit: GET-Parameter haben Vorrang, sonst
|
|
// Rückfall auf die zuletzt in der Session gemerkten Werte. So bleibt
|
|
// z.B. ein reiner Pagination-Link (?page=2) bei der aktuellen
|
|
// Sortierung/Filterung.
|
|
$currentSort = $_GET['sort'] ?? ($_SESSION['search_sort'] ?? 'alphabet');
|
|
if (!in_array($currentSort, ['alphabet', 'likes', 'newest', 'oldest'])) {
|
|
$currentSort = 'alphabet';
|
|
}
|
|
$_SESSION['search_sort'] = $currentSort;
|
|
|
|
$currentCategory = strtolower($_GET['category'] ?? ($_SESSION['search_category'] ?? 'all'));
|
|
$_SESSION['search_category'] = $currentCategory;
|
|
|
|
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : ($_SESSION['search_limit'] ?? 10);
|
|
if (!in_array($limit, [10, 20, 50, 100])) {
|
|
$limit = 10;
|
|
}
|
|
$_SESSION['search_limit'] = $limit;
|
|
|
|
// Serverseitig sortieren + filtern
|
|
$filteredResults = sortSearchResults($rawResults, $currentSort);
|
|
$filteredResults = filterSearchResultsByCategory($filteredResults, $currentCategory);
|
|
|
|
$totalResultsCount = count($filteredResults);
|
|
$totalPages = max(1, (int)ceil($totalResultsCount / $limit));
|
|
|
|
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
|
if ($currentPage < 1) {
|
|
$currentPage = 1;
|
|
} elseif ($currentPage > $totalPages) {
|
|
$currentPage = $totalPages;
|
|
}
|
|
|
|
$offset = ($currentPage - 1) * $limit;
|
|
|
|
// Dies ist die No-JS-Basisversion: nur die aktuelle Seite wird gerendert.
|
|
// Bei aktiviertem JS lädt js/results-app.js zusätzlich den kompletten
|
|
// Datensatz nach (ein einziger Request) und übernimmt Sortierung, Filter
|
|
// und Pagination danach komplett im Browser, ohne weitere Serveranfragen.
|
|
$results = array_slice($filteredResults, $offset, $limit);
|
|
$resultCount = count($results);
|
|
?>
|
|
|
|
<noscript>
|
|
Bitte JavaScript aktivieren!
|
|
</noscript>
|
|
|
|
<!-- Das Formular umschließt jetzt das gesamte Layout -->
|
|
<form id="search-form-id" action="index.php" method="GET" style="display: contents;">
|
|
<input type="hidden" name="pfad" value="showCategory">
|
|
<input type="hidden" name="category" value="<?php echo (isset($category) && !empty($category)) ? htmlspecialchars($category) : ''; ?>">
|
|
<input type="hidden" id="s-res-page-input" name="page" value="<?php echo $currentPage; ?>">
|
|
|
|
<div class="cat-view-container">
|
|
|
|
<!-- LINKE SPALTE: SEITENLEISTE FÜR SORTIERUNG -->
|
|
<aside class="cat-sidebar">
|
|
<div class="cat-sidebar-box">
|
|
<h3 class="cat-sidebar-title">Sortierung</h3>
|
|
<div class="cat-filter-group">
|
|
<label class="cat-filter-option">
|
|
<input type="radio" name="sort" value="alphabet" <?php echo ($currentSort === 'alphabet') ? 'checked' : ''; ?> onchange="this.form.submit();">
|
|
Alphabetisch
|
|
</label>
|
|
<label class="cat-filter-option">
|
|
<input type="radio" name="sort" value="likes" <?php echo ($currentSort === 'likes') ? 'checked' : ''; ?> onchange="this.form.submit();">
|
|
Beliebtheit (Likes)
|
|
</label>
|
|
<label class="cat-filter-option">
|
|
<input type="radio" name="sort" value="newest" <?php echo ($currentSort === 'newest') ? 'checked' : ''; ?> onchange="this.form.submit();">
|
|
Neueste Beiträge
|
|
</label>
|
|
<label class="cat-filter-option">
|
|
<input type="radio" name="sort" value="oldest" <?php echo ($currentSort === 'oldest') ? 'checked' : ''; ?> onchange="this.form.submit();">
|
|
Älteste Beiträge
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</aside>
|
|
|
|
<!-- RECHTE SPALTE: INHALT & PAGINATOR -->
|
|
<main class="cat-view-main">
|
|
<div class="cat-view-header">
|
|
<h1 class="cat-view-title">Kategorie: <?php echo (isset($category) && !empty($category)) ? htmlspecialchars($category) : 'Unbekannt'; ?></h1>
|
|
<p class="cat-view-meta-text">
|
|
<?php echo (isset($totalArticles)) ? $totalArticles : 0; ?> Treffer in dieser Kategorie
|
|
</p>
|
|
</div>
|
|
|
|
<!-- Beiträge -->
|
|
<div class="cat-view-list">
|
|
<?php if (isset($articles) && !empty($articles)): ?>
|
|
<?php foreach ($articles as $article): ?>
|
|
<div class="cat-view-item">
|
|
<div class="cat-view-content">
|
|
<h2 class="cat-view-item-title">
|
|
<a href="index.php?pfad=showArticle&id=<?php echo htmlspecialchars($article->getID()); ?>" class="cat-view-link">
|
|
<?php echo (isset($article) && !empty($article->getTitle())) ? htmlspecialchars($article->getTitle()) : 'Kein Titel'; ?>
|
|
</a>
|
|
</h2>
|
|
|
|
<div class="cat-view-meta-row">
|
|
<p class="cat-view-author">
|
|
von <span class="cat-view-author-name"><?php echo (isset($article) && !empty($article->getAuthor())) ? htmlspecialchars($article->getAuthor()) : 'Anonym'; ?></span>
|
|
</p>
|
|
<span class="cat-view-likes">
|
|
❤️ <?php echo (isset($article) && !empty($article->getLikes())) ? htmlspecialchars($article->getLikes()) : '0'; ?>
|
|
</span>
|
|
</div>
|
|
|
|
</div>
|
|
<div class="cat-view-arrow">→</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php else: ?>
|
|
<p class="cat-view-meta-text">Keine Artikel in dieser Kategorie gefunden.</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<!-- Footer Paginator -->
|
|
<?php if (isset($totalPages) && $totalPages > 1): ?>
|
|
<div class="cat-view-pagination-footer">
|
|
<div></div>
|
|
<div class="s-res-page-navigation">
|
|
<button type="button" class="s-res-page-btn" data-page="<?php echo $currentPage - 1; ?>" <?php echo ($currentPage <= 1) ? 'disabled' : ''; ?>>
|
|
«
|
|
</button>
|
|
|
|
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
|
|
<button type="button"
|
|
class="s-res-page-btn <?php echo ($i === $currentPage) ? 'cat-active-btn' : ''; ?>"
|
|
data-page="<?php echo $i; ?>"
|
|
<?php echo ($i === $currentPage) ? 'disabled' : ''; ?>>
|
|
<?php echo $i; ?>
|
|
</button>
|
|
<?php endfor; ?>
|
|
|
|
<button type="button" class="s-res-page-btn" data-page="<?php echo $currentPage + 1; ?>" <?php echo ($currentPage >= $totalPages) ? 'disabled' : ''; ?>>
|
|
»
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
|
|
</div>
|
|
</form>
|