FVS-Social-Projekt-Neu/FVS-Social/index.php
erik c3b42cccac Version 0.9
-Nachrichten Senden
-Design verbessert
-Kleine Verbesserungen

Signed-off-by: erik <micheler@steinbeis.schule>
2025-03-10 10:48:39 +01:00

210 lines
No EOL
8.4 KiB
PHP

<?php
global $pdo;
session_start();
$page = isset($_GET['page']) ? $_GET['page'] : 'posts';
$user_page = isset($_GET['user']) ? $_GET['user'] : 'posts';
if(!isset($_SESSION['user_id'])) {
header("Location: login/login-page.html");
}
include 'db_connect.php';
try {
$userId = $_SESSION['user_id'];
//Daten von Angemeldeten benutzer werden ausgelesen
$stmt1 = $pdo->prepare("SELECT username, email, created_at FROM users WHERE id = :id");
$stmt1->execute([':id' => $userId]);
$user = $stmt1->fetch(PDO::FETCH_ASSOC);
//Profilbild wird vom angemeldeten benutzer ausgelesen
$stmt2 = $pdo->prepare("SELECT file_path FROM profile_pictures WHERE user_id = :user_id");
$stmt2->execute([':user_id' => $userId]);
$profile_pic = $stmt2->fetch(PDO::FETCH_ASSOC);
//Benutzername und Profilbild werden von allen benutzer ausgelesen
$stmt3 = $pdo->prepare("
SELECT users.username, profile_pictures.file_path
FROM users
LEFT JOIN profile_pictures ON users.id = profile_pictures.user_id
ORDER BY profile_pictures.uploaded_at DESC
");
$stmt3->execute();
$users = $stmt3->fetchAll(PDO::FETCH_ASSOC);
//Alle beiträge von allen benutzer werden ausgelesen
$stmt4 = $pdo->prepare("
SELECT posts.*, profile_pictures.file_path AS profile_picture, users.username
FROM posts
LEFT JOIN profile_pictures ON posts.user_id = profile_pictures.user_id
LEFT JOIN users ON posts.user_id = users.id
ORDER BY posts.created_at DESC
");
$stmt4->execute();
$posts = $stmt4->fetchAll(PDO::FETCH_ASSOC);
if ($profile_pic) {
$profile_image = $profile_pic['file_path'];
} else {
$profile_image = 'profile-pics/default.jpeg';
}
if(!$user){
die("Benutzer nicht gefunden!");
}
}catch (PDOException $e){
die("Fehler: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php echo '<link rel="stylesheet" href="style.css?v='.time().'">'; ?>
<title>Profile</title>
</head>
<body>
<div class="container">
<div class="col-3">
<div class="navigation-body">
<div class="profile-name-header">
<?php echo "<img class = 'profile-pictures-header' src='$profile_image' alt='pv' width='50'>";?>
<h1>
<a href="">@<?php echo htmlspecialchars($user['username']); ?></a>
</h1>
</div>
<ul class="icons">
<li><a href="index.php"><img src="icons/home.svg" alt="">Home</a></li>
<li><a href="profile.php?user=<?= htmlspecialchars($user['username']) ?>"><img src="icons/user.svg" alt="">Profil</a></li>
<li><a href="chat.php"><img src="icons/envelope.svg" alt="">Nachrichten</a></li>
<li><a href="logout.php"><img src="icons/exit.svg" alt="">Abmelden</a></li>
</ul>
</div>
<div class="navigation-body">
<ul>
<?php foreach ($users as $benutzer): ?>
<li>
<form class="user-card" action="profile.php" method="post">
<a href="profile.php?user=<?= urlencode($benutzer['username']) ?>"> <img class="profile-pictures" src="<?= htmlspecialchars($benutzer['file_path']) ?: 'profile-pics/default.jpeg' ?>" alt="Profilbild"><?= htmlspecialchars($benutzer['username']) ?></a>
</form>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<div class="profile-body col-9">
<div class="profile-header">
<h1>Willkommen, <?php echo htmlspecialchars($user['username']); ?>!</h1>
<p>Email: <?php echo htmlspecialchars($user['email']); ?>!</p>
<p>Regestriert seit:, <?php echo htmlspecialchars($user['created_at']); ?>!</p>
<?php
if($user_page == $user['username']) {
echo htmlspecialchars($user['username']);
}
?>
</div>
<div class="post-formular-body">
<form action='upload-post/upload-post.php' method='post' enctype='multipart/form-data'>
<textarea name='text_content' required placeholder='Schreibe etwas...'></textarea>
<input type='file' name='image'>
<button type='submit'>Posten</button>
</form>
</div>
<?php foreach ($posts as $post): ?>
<?php
$stmt5 = $pdo->prepare("SELECT COUNT(post_id) AS likes_count FROM likes WHERE post_id = :post_id");
$stmt5->execute([':post_id' => $post['id']]);
$likes = $stmt5->fetch(PDO::FETCH_ASSOC)['likes_count'];
$stmt6 = $pdo->prepare("SELECT user_id, post_id FROM likes WHERE post_id = :post_id AND user_id = :user_id");
$stmt6->execute([':post_id' => $post['id'], ':user_id' => $userId]);
$isLiked = $stmt6->fetch(PDO::FETCH_ASSOC);
?>
<div class="post">
<div class="post-nav">
<div class="post-header">
<img class="profile-pictures" src="<?= htmlspecialchars($post['profile_picture']) ?: 'profile-pics/default.jpeg'; ?>" width="50px" alt="">
<p><strong><?php echo $post['username']; ?></strong></p>
</div>
<?php if ($userId == $post['user_id']): ?>
<form action="upload-post/delete-post.php" method="post">
<input type="hidden" name="post_id" value="<?= htmlspecialchars($post['id']) ?>">
<button class="delete-post" type="submit"></button>
</form>
<?php endif; ?>
</div>
<p><?php echo nl2br(htmlspecialchars($post['text_content'])); ?></p>
<?php if (!empty($post['image_path'])): ?>
<img class="post-pics" src="<?php echo htmlspecialchars($post['image_path']); ?>" alt="Bild zum Post">
<?php endif; ?>
<h5>Likes: <?php echo htmlspecialchars($likes)?></h5>
<p><small>Erstellt am: <?php echo $post['created_at']; ?></small></p>
<div class="post-footer">
<?php if(!$isLiked):?>
<form action="like/like.php" method="post">
<input type="hidden" name="post_id" value="<?= htmlspecialchars($post['id']) ?>">
<button class="like-button unlike" type="submit"></button>
</form>
<?php else: ?>
<form action="like/unlike.php" method="post">
<input type="hidden" name="post_id" value="<?= htmlspecialchars($post['id']) ?>">
<button class="like-button liked" type="submit"></button>
</form>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
// Scroll-Position aus dem localStorage abrufen
let scrollPos = localStorage.getItem("scrollPosition");
if (scrollPos !== null) {
// Verzögert setzen, um das Springen zu minimieren
requestAnimationFrame(() => {
window.scrollTo(0, parseInt(scrollPos));
});
}
// Speichert die Scroll-Position beim Verlassen der Seite
window.addEventListener("beforeunload", function() {
localStorage.setItem("scrollPosition", window.scrollY);
});
});
</script>
</body>
</html>