File: /homepages/35/d993672390/htdocs/dermiteseborrheique/social/categorie.php
<?php
define('DS_APP', true);
require_once dirname(__DIR__) . '/includes/init.php';
require_once DS_INCLUDES . 'social.php';
$currentUser = auth_user();
$catSlug = sanitize_string($_GET['slug'] ?? '', 50);
if (!$catSlug) {
preg_match('#/social/categorie/([^/\?]+)#', $_SERVER['REQUEST_URI'], $m);
$catSlug = sanitize_string($m[1] ?? '', 50);
}
if (!$catSlug) { http_response_code(404); include DS_ROOT.'/errors/404.php'; exit; }
$category = db_fetch("SELECT * FROM social_categories WHERE slug=? AND is_active=1", [$catSlug]);
if (!$category) { http_response_code(404); include DS_ROOT.'/errors/404.php'; exit; }
$sort = in_array($_GET['sort']??'',['hot','new','top','active']) ? $_GET['sort'] : 'hot';
$page = max(1,(int)($_GET['page']??1));
$perPage = 20;
$offset = ($page-1)*$perPage;
$orderSQL = match($sort) {
'new' => 'ORDER BY p.created_at DESC',
'top' => 'ORDER BY p.score DESC, p.created_at DESC',
'active' => 'ORDER BY p.updated_at DESC',
default => 'ORDER BY (p.score+LOG(1+p.views/10)-(TIMESTAMPDIFF(HOUR,p.created_at,NOW())/48)) DESC',
};
$total = db_count("SELECT COUNT(*) FROM social_posts p WHERE p.category_id=? AND p.is_deleted=0", [$category['id']]);
$posts = db_fetch_all(
"SELECT p.*, u.forum_username, u.avatar_url, u.forum_avatar, u.name as user_name
FROM social_posts p JOIN users u ON p.user_id=u.id
WHERE p.category_id=? AND p.is_deleted=0 {$orderSQL}
LIMIT {$perPage} OFFSET {$offset}",
[$category['id']]
);
$userVotes = [];
if ($currentUser && !empty($posts)) {
$ids = implode(',', array_column($posts,'id'));
$votes = db_fetch_all(
"SELECT target_id,value FROM social_votes WHERE user_id=? AND target_type='post' AND target_id IN ({$ids})",
[$currentUser['id']]
);
foreach ($votes as $v) $userVotes[$v['target_id']] = $v['value'];
}
$allCats = db_fetch_all("SELECT * FROM social_categories WHERE is_active=1 ORDER BY sort_order");
$totalPages = (int)ceil($total/$perPage);
$pageTitle = $category['name'] . ' — Communauté DermiteSéborrhéique';
$activePage = 'social';
$socialCSS = file_get_contents(DS_ROOT . '/assets/css/social.css');
ob_start();
?>
<style><?= $socialCSS ?></style>
<!-- Breadcrumb -->
<div style="font-size:0.75rem;color:var(--text-dim);margin-bottom:16px;">
<a href="<?= url('social/') ?>" style="color:var(--text-dim);text-decoration:none;">Communauté</a>
→ <span style="color:var(--text)"><?= e($category['name']) ?></span>
</div>
<div class="social-layout">
<main class="feed-main">
<!-- Header catégorie -->
<div class="cat-hero">
<div class="cat-hero-icon"><?= $category['icon'] ?></div>
<div>
<h1 class="cat-hero-title"><?= e($category['name']) ?></h1>
<p class="cat-hero-desc"><?= e($category['description']) ?></p>
<div class="cat-hero-meta"><?= number_format($category['post_count']) ?> post<?= $category['post_count']!==1?'s':'' ?></div>
</div>
</div>
<!-- Toolbar -->
<div class="feed-toolbar">
<div class="sort-tabs">
<?php foreach (['hot'=>'🔥 Populaire','new'=>'🆕 Récent','top'=>'🏆 Top','active'=>'💬 Actif'] as $s=>$l): ?>
<a href="?sort=<?= $s ?>&slug=<?= $catSlug ?>" class="sort-tab <?= $sort===$s?'active':'' ?>"><?= $l ?></a>
<?php endforeach; ?>
</div>
<?php if ($currentUser): ?>
<a href="<?= url('social/new-post.php?cat='.$catSlug) ?>" class="new-post-btn">+ Poster ici</a>
<?php else: ?>
<a href="<?= url('auth/register.php') ?>" class="new-post-btn">Rejoindre pour poster</a>
<?php endif; ?>
</div>
<!-- Posts -->
<?php if (empty($posts)): ?>
<div class="feed-empty">
<div class="feed-empty-icon"><?= $category['icon'] ?></div>
<div class="feed-empty-title">Aucun post dans cette catégorie</div>
<div class="feed-empty-desc">Soyez le premier à partager votre expérience !</div>
<a href="<?= url($currentUser ? 'social/new-post.php?cat='.$catSlug : 'auth/register.php') ?>" class="btn-cta">
<?= $currentUser ? '+ Créer le premier post' : 'Rejoindre pour poster' ?>
</a>
</div>
<?php else: ?>
<?php foreach ($posts as $post):
$tags = $post['tags'] ? json_decode($post['tags'],true) : [];
$userVote = $userVotes[$post['id']] ?? 0;
$username = Social::getForumUsername(['forum_username'=>$post['forum_username'],'id'=>$post['user_id']]);
$excerpt = Social::excerpt($post['body'],180);
$postUrl = Social::postUrl($post);
$scoreColor = $post['score']>0?'var(--emerald)':($post['score']<0?'var(--rose)':'var(--text-dim)');
?>
<article class="post-card" id="post-<?= $post['id'] ?>">
<div class="post-vote-col">
<button class="vote-btn upvote <?= $userVote===1?'active':'' ?>" onclick="vote('post',<?= $post['id'] ?>,1,this)">▲</button>
<span class="post-score" style="color:<?= $scoreColor ?>" id="score-post-<?= $post['id'] ?>"><?= $post['score'] ?></span>
<button class="vote-btn downvote <?= $userVote===-1?'active':'' ?>" onclick="vote('post',<?= $post['id'] ?>,-1,this)">▼</button>
</div>
<div class="post-content">
<div class="post-meta-top">
<div class="post-avatar-placeholder"><?= mb_strtoupper(mb_substr($username,0,1)) ?></div>
<a href="<?= Social::profileUrl($username) ?>" class="post-username">u/<?= e($username) ?></a>
<span class="post-time">· <?= Social::timeAgo($post['created_at']) ?></span>
</div>
<h2 class="post-title"><a href="<?= e($postUrl) ?>"><?= e($post['title']) ?></a></h2>
<?php if ($post['image_url']): ?>
<a href="<?= e($postUrl) ?>"><img src="<?= e($post['image_url']) ?>" class="post-thumbnail" alt="" loading="lazy"></a>
<?php endif; ?>
<p class="post-excerpt"><?= e($excerpt) ?></p>
<?php if (!empty($tags)): ?>
<div class="post-tags">
<?php foreach ($tags as $t): ?>
<a href="<?= url('social/') ?>?tag=<?= urlencode($t) ?>" class="post-tag">#<?= e($t) ?></a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="post-actions">
<a href="<?= e($postUrl) ?>" class="post-action">💬 <?= $post['reply_count'] ?> réponse<?= $post['reply_count']!==1?'s':'' ?></a>
<span class="post-action">👁 <?= number_format($post['views']) ?></span>
<a href="<?= e($postUrl) ?>" class="post-action">Lire →</a>
</div>
</div>
</article>
<?php endforeach; ?>
<?php if ($totalPages > 1): ?>
<nav class="pagination">
<?php if ($page>1): ?><a href="?sort=<?= $sort ?>&slug=<?= $catSlug ?>&page=<?= $page-1 ?>" class="page-btn" rel="prev">← Précédent</a><?php endif; ?>
<?php for ($p=max(1,$page-2);$p<=min($totalPages,$page+2);$p++): ?>
<a href="?sort=<?= $sort ?>&slug=<?= $catSlug ?>&page=<?= $p ?>" class="page-btn <?= $p===$page?'active':'' ?>"><?= $p ?></a>
<?php endfor; ?>
<?php if ($page<$totalPages): ?><a href="?sort=<?= $sort ?>&slug=<?= $catSlug ?>&page=<?= $page+1 ?>" class="page-btn" rel="next">Suivant →</a><?php endif; ?>
</nav>
<?php endif; ?>
<?php endif; ?>
</main>
<!-- Sidebar -->
<aside class="feed-sidebar">
<?php if ($currentUser): ?>
<div class="sidebar-card sidebar-cta">
<div class="sidebar-cta-title"><?= $category['icon'] ?> Poster ici</div>
<a href="<?= url('social/new-post.php?cat='.$catSlug) ?>" class="btn-cta" style="display:block;text-align:center;">+ Créer un post</a>
</div>
<?php else: ?>
<div class="sidebar-card sidebar-cta">
<div class="sidebar-cta-title">Rejoindre la communauté</div>
<a href="<?= url('auth/register.php') ?>" class="btn-cta" style="display:block;text-align:center;margin-bottom:8px;">Créer un compte</a>
<a href="<?= url('auth/login.php') ?>" class="btn-outline" style="display:block;text-align:center;">Se connecter</a>
</div>
<?php endif; ?>
<div class="sidebar-card">
<div class="sidebar-card-title">📂 Catégories</div>
<?php foreach ($allCats as $cat): ?>
<a href="<?= url('social/categorie/'.$cat['slug']) ?>" class="cat-sidebar-item <?= $cat['slug']===$catSlug?'active':'' ?>">
<span class="cat-sidebar-icon"><?= $cat['icon'] ?></span>
<span class="cat-sidebar-name"><?= e($cat['name']) ?></span>
<span class="cat-sidebar-count"><?= $cat['post_count'] ?></span>
</a>
<?php endforeach; ?>
</div>
<div class="sidebar-card">
<a href="<?= url('social/') ?>" class="sidebar-link">← Tous les posts</a>
</div>
</aside>
</div>
<script>
const csrf = '<?= csrf_token() ?>';
const isLoggedIn = <?= $currentUser?'true':'false' ?>;
async function vote(type,id,value,btn) {
if(!isLoggedIn){window.location='<?= url('auth/login.php') ?>';return;}
const fd=new FormData();
fd.append('action','vote');fd.append('type',type);fd.append('id',id);fd.append('value',value);fd.append('_csrf',csrf);
const res=await fetch('<?= url('social/api.php') ?>',{method:'POST',body:fd,headers:{'X-Requested-With':'XMLHttpRequest'}});
const data=await res.json();
if(data.success){
document.getElementById(`score-${type}-${id}`).textContent=data.score;
}
}
</script>
<?php
$content = ob_get_clean();
if ($currentUser) {
require DS_VIEWS . 'layouts/app.php';
} else {
echo '<!DOCTYPE html><html lang="fr" data-theme="light"><head><script>(function(){var t=localStorage.getItem("ds_theme")||"light";document.documentElement.setAttribute("data-theme",t);})();</script><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">';
echo '<meta name="description" content="' . e($category['description']) . '">';
echo '<meta name="robots" content="index, follow">';
echo '<title>' . e($pageTitle) . '</title>';
echo '<link href="https://fonts.googleapis.com/css2?family=Cabinet+Grotesk:wght@400;500;700;800;900&family=Instrument+Serif:ital@0;1&display=swap" rel="stylesheet">';
echo '</head><body>';
include __DIR__ . '/partials/nav.php';
echo $content;
include __DIR__ . '/partials/footer.php';
echo '</body></html>';
}