89 lines
3.4 KiB
PHP
89 lines
3.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$user_id = $_SESSION['user_id'];
|
|
$username = $_SESSION['username'];
|
|
$post_message = '';
|
|
$error_message = '';
|
|
|
|
// Handle new post submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_post'])) {
|
|
$post_content = trim($_POST['post_content']);
|
|
|
|
if (empty($post_content)) {
|
|
$error_message = 'Post content cannot be empty.';
|
|
} else {
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->prepare("INSERT INTO posts (user_id, content) VALUES (:user_id, :content)");
|
|
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
|
$stmt->bindParam(':content', $post_content, PDO::PARAM_STR);
|
|
$stmt->execute();
|
|
$post_message = 'Your post has been shared!';
|
|
// Clear the post content after successful submission
|
|
$_POST['post_content'] = '';
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch all posts
|
|
$posts = [];
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->prepare("SELECT p.content, p.created_at, u.username FROM posts p JOIN users u ON p.user_id = u.id ORDER BY p.created_at DESC");
|
|
$stmt->execute();
|
|
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Failed to load posts: ' . $e->getMessage();
|
|
}
|
|
|
|
$pageTitle = "User Dashboard - Flatlogic";
|
|
include 'includes/header.php';
|
|
?>
|
|
<div class="container">
|
|
<header>
|
|
<h1>Welcome, <?php echo htmlspecialchars($username); ?>!</h1>
|
|
</header>
|
|
<main>
|
|
<?php if ($post_message): ?>
|
|
<div class="message success"><?php echo $post_message; ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($error_message): ?>
|
|
<div class="message error"><?php echo $error_message; ?></div>
|
|
<?php endif; ?>
|
|
|
|
<section class="post-creation-section">
|
|
<h2>Share something new</h2>
|
|
<form action="dashboard.php" method="post" class="form-card">
|
|
<div class="form-group">
|
|
<textarea id="post_content" name="post_content" rows="4" placeholder="What's on your mind?" required><?php echo htmlspecialchars($_POST['post_content'] ?? ''); ?></textarea>
|
|
</div>
|
|
<button type="submit" name="new_post" class="button">Post</button>
|
|
</form>
|
|
</section>
|
|
|
|
<section class="feed-section">
|
|
<h2>Recent Posts</h2>
|
|
<?php if (empty($posts)): ?>
|
|
<p>No posts yet. Be the first to share something!</p>
|
|
<?php else: ?>
|
|
<?php foreach ($posts as $post): ?>
|
|
<div class="post-card">
|
|
<p class="post-author"><strong><?php echo htmlspecialchars($post['username']); ?></strong> <span class="post-date"><?php echo date('F j, Y, g:i a', strtotime($post['created_at'])); ?></span></p>
|
|
<p class="post-content"><?php echo nl2br(htmlspecialchars($post['content'])); ?></p>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</section>
|
|
|
|
<a href="logout.php" class="button button-secondary">Logout</a>
|
|
</main>
|
|
<?php include 'includes/footer.php'; ?>
|