Skip to content

Commit

Permalink
New update: UI change in text section
Browse files Browse the repository at this point in the history
  • Loading branch information
HirotakaDango authored Dec 11, 2024
1 parent 445ce1a commit d4f0d5d
Show file tree
Hide file tree
Showing 8 changed files with 743 additions and 248 deletions.
106 changes: 106 additions & 0 deletions preview/text/side_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
// Connect to the SQLite database
$db = new SQLite3('../../database.sqlite');

// Create tables if not exist
$db->exec('CREATE TABLE IF NOT EXISTS texts (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL, tags TEXT, date DATETIME, view_count INTEGER DEFAULT 0)');
$db->exec('CREATE TABLE IF NOT EXISTS text_favorites (id INTEGER PRIMARY KEY AUTOINCREMENT, text_id INTEGER NOT NULL, email TEXT NOT NULL, FOREIGN KEY (text_id) REFERENCES texts(id))');

// Get uid parameter from URL (it may be passed for a specific user, but it's optional now)
$uid = isset($_GET['uid']) ? (int)$_GET['uid'] : null;

// Build the SQL query to fetch posts without user-specific filtering
$query = "SELECT texts.*, users.email AS user_email, users.artist
FROM texts
LEFT JOIN users ON texts.email = users.email
WHERE 1=1"; // No user-specific filtering

if ($uid) {
$query .= " AND users.id = " . $db->escapeString($uid);
}

$query .= " ORDER BY texts.id DESC"; // Order by text ID (most recent first)

// Fetch results
$results = $db->query($query);

// Check if there are no results
if ($results->fetchArray(SQLITE3_ASSOC) === false) {
// No posts found, display a message
$noPosts = true;
} else {
// Reset the result pointer to the beginning for displaying posts
$results->reset();
$noPosts = false;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Texts</title>
<link rel="icon" type="image/png" href="/icon/favicon.png">
<?php include('../../bootstrapcss.php'); ?>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container-fluid mb-2">
<?php if ($noPosts): ?>
<p class="position-absolute start-50 top-50 translate-middle fw-bold fs-3">This user has not posted anything.</p>
<?php else: ?>
<div id="texts-container">
<!-- Texts will be loaded here dynamically -->
</div>
<button id="load-more" class="btn btn-dark fw-medium rounded-4 w-100" data-offset="5">Load More</button>
<?php endif; ?>
</div>

<script>
$(document).ready(function() {
function loadTexts(offset = 0) {
$.ajax({
url: 'side_view_get.php',
method: 'GET',
dataType: 'json',
data: { offset: offset, uid: <?php echo $uid; ?> },
success: function(response) {
if (response.success) {
response.texts.forEach(function(text) {
const cardHtml = `
<div class="card border-0 rounded-4 bg-body-tertiary my-2">
<div class="card-body">
<h5 class="fw-bold text-truncate mb-3" style="max-width: auto;">${text.title}</h5>
<h6 class="fw-medium text-truncate mb-3" style="max-width: auto;">Author: ${text.author}</h6>
<div class="d-flex">
<a class="ms-auto btn border-0 p-0 fw-bold" href="view.php?id=${text.id}&uid=<?php echo $uid; ?>" target="_blank">read <i class="bi bi-arrow-right"></i></a>
</div>
</div>
</div>
`;
$('#texts-container').append(cardHtml);
});
$('#load-more').data('offset', offset + 5);
} else {
$('#load-more').hide(); // Hide the button if no more data
}
}
});
}

<?php if (!$noPosts): ?>
// Initial load
loadTexts(0);
<?php endif; ?>

$('#load-more').click(function() {
const offset = $(this).data('offset');
loadTexts(offset);
});
});
</script>

<?php include('../../bootstrapjs.php'); ?>
</body>
</html>
54 changes: 54 additions & 0 deletions preview/text/side_view_get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
// Connect to the SQLite database
$db = new SQLite3('../../database.sqlite');

// Create tables if not exist
$db->exec('CREATE TABLE IF NOT EXISTS texts (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL, tags TEXT, date DATETIME, view_count INTEGER DEFAULT 0)');
$db->exec('CREATE TABLE IF NOT EXISTS text_favorites (id INTEGER PRIMARY KEY AUTOINCREMENT, text_id INTEGER NOT NULL, email TEXT NOT NULL, FOREIGN KEY (text_id) REFERENCES texts(id))');

// Get uid and offset parameters from URL
$uid = isset($_GET['uid']) ? (int)$_GET['uid'] : null;
$offset = isset($_GET['offset']) ? (int)$_GET['offset'] : 0;
$limit = 5; // Number of cards to show per request

// Ensure that uid is provided
if (!$uid) {
echo json_encode(['success' => false, 'message' => 'User ID is required.']);
exit;
}

// Build the SQL query with pagination and filtering by uid
$query = "SELECT texts.*, users.email AS user_email, users.artist
FROM texts
LEFT JOIN users ON texts.email = users.email
WHERE users.id = :uid
ORDER BY texts.id DESC
LIMIT :limit OFFSET :offset";

// Prepare and execute the query
$stmt = $db->prepare($query);
$stmt->bindValue(':uid', $uid, SQLITE3_INTEGER);
$stmt->bindValue(':limit', $limit, SQLITE3_INTEGER);
$stmt->bindValue(':offset', $offset, SQLITE3_INTEGER);
$results = $stmt->execute();

// Fetch the results
$texts = [];
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
// Prepare the data to return as JSON
$texts[] = [
'id' => $row['id'],
'title' => $row['title'],
'author' => $row['artist'] ?? 'Unknown',
'email' => $row['email']
];
}

// Check if there are more records to load
$moreRecords = $results->fetchArray() ? true : false;

echo json_encode([
'success' => true,
'texts' => $texts,
'hasMore' => $moreRecords
]);
79 changes: 70 additions & 9 deletions preview/text/text_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,82 @@
<div class="card-body">
<h5 class="fw-bold text-truncate mb-3" style="max-width: auto;"><?php echo $row['title']; ?></h5>
<?php
// Get the email from the texts table
$email = $row['email']; // Email from the texts table

// Assuming you have a database connection $db
$stmt = $db->prepare("SELECT id, artist FROM users WHERE email = :email");
$stmt->bindValue(':email', $email, SQLITE3_TEXT); // Bind the email parameter
$userResult = $stmt->execute();
$user = $userResult->fetchArray(SQLITE3_ASSOC);

// If the user is found, retrieve the user ID and artist name
if ($user) {
$userId = $user['id'];
$artistName = $user['artist'];
echo "<h6 class='fw-medium text-truncate mb-3' style='max-width: auto;'>Author: <a class='btn border-0 p-0 pb-1 m-0 fw-medium' href='/preview/text/?uid=$userId'>$artistName</a></h6>";
} else {
echo "<h6 class='fw-medium text-truncate mb-3' style='max-width: auto;'>Author: Unknown</h6>";
}
?>
<?php
if (!empty($row['tags'])) {
$tags = explode(',', $row['tags']);
$tagCount = 0; // Counter for the number of tags processed
$displayedTags = []; // Array to store tags that are displayed

foreach ($tags as $tag) {
$tag = trim($tag);
if (!empty($tag)) {
// Merge the current query parameters with the new tag parameter
$queryParams = array_merge($_GET, ['tag' => $tag]);
$tagUrl = '?' . http_build_query($queryParams);
?>
<a href="<?php echo $tagUrl; ?>" class="btn btn-sm fw-medium btn-dark rounded-pill">
<?php echo htmlspecialchars($tag); ?>
</a>
<?php
$tagCount++; // Increment the tag counter
if ($tagCount <= 5) {
// Merge the current query parameters with the new tag parameter
$queryParams = array_merge($_GET, ['tag' => $tag]);
$tagUrl = '?' . http_build_query($queryParams);
$displayedTags[] = '<a href="' . $tagUrl . '" style="margin: 0.2em;" class="btn btn-sm fw-medium btn-dark rounded-pill"><i class="bi bi-tag-fill"></i> ' . $tag . '</a>';
}
}
}

// Output the displayed tags
foreach ($displayedTags as $tagLink) {
echo $tagLink;
}

// If there are more than 5 tags, display the "View All Tags" button
if ($tagCount > 5) {
?>
<!-- Button trigger modal -->
<button type="button" style="margin: 0.2em; margin-left: -2px;" class="btn btn-sm fw-medium btn-dark rounded-pill" data-bs-toggle="modal" data-bs-target="#tagsModal">
<i class="bi bi-tags-fill"></i> all tags
</button>

<!-- Modal -->
<div class="modal fade" id="tagsModal" tabindex="-1" aria-labelledby="tagsModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content rounded-4">
<div class="modal-header border-0">
<h1 class="modal-title fs-5" id="tagsModalLabel">All Tags from <?php echo $row['title']; ?></h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<?php
// Display all tags in the modal
foreach ($tags as $tag) {
$tag = trim($tag);
if (!empty($tag)) {
$queryParams = array_merge($_GET, ['tag' => $tag]);
$tagUrl = '?' . http_build_query($queryParams);
echo '<a href="' . $tagUrl . '" style="margin: 0.2em;" class="btn btn-sm fw-medium btn-dark rounded-pill"><i class="bi bi-tag-fill"></i> ' . $tag . '</a>';
}
}
?>
</div>
</div>
</div>
</div>
<?php
}
} else {
echo "<p class='text-muted'>No tags available.</p>";
}
Expand Down Expand Up @@ -49,7 +110,7 @@
echo "Sorry, no text...";
}
?>
<a class="link-body-emphasis text-decoration-none position-absolute start-0 bottom-0 m-3" href="view.php?id=<?php echo $row['id']; ?>">Read more</a>
<a class="link-body-emphasis text-decoration-none position-absolute start-0 bottom-0 m-3 fw-bold" href="view.php?id=<?php echo $row['id']; ?>&uid=<?php echo $userId; ?>">Read more</a>
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit d4f0d5d

Please sign in to comment.