svrjs-mods-directory/includes/page_userreviews.php

130 lines
5.7 KiB
PHP
Raw Normal View History

2024-12-27 15:05:54 +01:00
<?php
if (!defined('SVRJS_MOD_DIRECTORY')) die;
$pageTitle = $userData['username'] . "'s reviews";
$pageDescription = 'View reviews by ' . $userData['username'] . '.';
include 'header.php';
?>
<main class="content">
<h1><?php echo htmlspecialchars($userData['username']) ?>'s reviews</h1>
<p><a href="<?php echo htmlspecialchars((URL_REWRITTEN ? APP_ROOT : APP_ROOT . APP_FILENAME . '/') . 'user/' . urlencode($userData['username'])); ?>">Return to user's profile</a></p>
<?php
$countStatement = $connection->prepare('SELECT COUNT(reviews.id) AS count
FROM reviews
JOIN (
SELECT mods.id AS id, mods.is_removed AS is_removed, mods.name AS name, mods.slug AS slug FROM mods
JOIN users ON users.id = mods.user AND users.is_verified = 1 AND users.is_deleted = 0 AND users.is_suspended = 0
) AS mods ON mods.id = reviews.mod
JOIN users ON users.id = reviews.user
WHERE users.id = ?
AND mods.is_removed = 0
AND users.is_suspended = 0
AND users.is_deleted = 0
AND users.is_verified = 1;');
if (!$countStatement) {
echo "<p>An unexpected error occurred while fetching reviews.</p>";
} else {
$countStatement->bind_param('i', $userData['id']);
$countStatement->execute();
$countResult = $countStatement->get_result();
if (!$countResult) {
echo "<p>An unexpected error occurred while fetching reviews.</p>";
$countStatement->close();
} else {
$countRow = $countResult->fetch_assoc();
$countStatement->close();
if (!$countRow) {
echo "<p>An unexpected error occurred while fetching reviews.</p>";
} else {
$modCount = $countRow['count'];
$totalPages = ceil($modCount / PAGE_MODS);
$statement = $connection->prepare('SELECT
reviews.id AS id,
reviews.rating AS rating,
reviews.review AS review,
mods.name AS mod_name,
mods.slug AS mod_slug,
users.username AS user,
users.id AS user_id
FROM reviews
JOIN (
SELECT mods.id AS id, mods.is_removed AS is_removed, mods.name AS name, mods.slug AS slug FROM mods
JOIN users ON users.id = mods.user AND users.is_verified = 1 AND users.is_deleted = 0 AND users.is_suspended = 0
) AS mods ON mods.id = reviews.mod
JOIN users ON users.id = reviews.user
WHERE users.id = ?
AND mods.is_removed = 0
AND users.is_suspended = 0
AND users.is_deleted = 0
AND users.is_verified = 1
ORDER BY reviews.id DESC
LIMIT ?,?;');
if (!$statement) {
echo "<p>An unexpected error occurred while fetching reviews.</p>";
} else {
$pageNumber = isset($_GET['page']) && filter_var($_GET['page'], FILTER_VALIDATE_INT) ? intval($_GET['page']) : 1;
$firstNumber = PAGE_REVIEWS * ($pageNumber - 1);
$pageReviews = PAGE_REVIEWS;
$statement->bind_param('iii', $userData['id'], $firstNumber, $pageReviews);
$statement->execute();
$result = $statement->get_result();
if (!$result) {
echo "<p>An unexpected error occurred while fetching reviews.</p>";
$statement->close();
} else {
$reviewsPresent = false;
while ($review = $result->fetch_assoc()) {
$reviewsPresent = true;
echo '<div class="review"><div class="review-header">';
$stars = round($review['rating']);
echo '<span class="rating-stars">';
for ($i = 0; $i < $stars; $i++) {
echo '<span class="rating-star rating-star-higlighted">&starf;</span>';
}
for ($i = $stars; $i < 5; $i++) {
echo '<span class="rating-star">&starf;</span>';
}
echo ' | by <a href="' . htmlspecialchars((URL_REWRITTEN ? APP_ROOT : APP_ROOT . APP_FILENAME . '/') . 'user/' . urlencode($review['user'])) . '">' . htmlspecialchars($review['user']) . '</a> | on <a href="' . htmlspecialchars((URL_REWRITTEN ? APP_ROOT : APP_ROOT . APP_FILENAME . '/') . 'mod/' . urlencode($review['mod_slug'])) . '">' . htmlspecialchars($review['mod_name']) . '</a></div><p>' . str_replace(["\r\n", "\n", "\r"], '<br/>', htmlspecialchars($review['review'])) . '</div>';
}
if (!$reviewsPresent) {
echo '<p>No reviews.</p>';
}
$statement->close();
if ($totalPages > 1) {
$begPage = $pageNumber - 2;
$endPage = $pageNumber + 2;
if ($endPage > $totalPages) {
$begPage -= $endPage - $totalPages;
$endPage = $totalPages;
}
if ($begPage < 1) {
$endPage += 1 - $begPage;
$begPage = 1;
}
echo '<div class="pagination">';
echo $pageNumber <= 1 ? '<span>&lsaquo;</span>' : '<a href="' . htmlspecialchars((URL_REWRITTEN ? APP_ROOT : APP_ROOT . APP_FILENAME . '/') . 'user-reviews/' . urlencode($userData['username']) . '?page=' . urlencode(strval($pageNumber - 1))) . '">&lsaquo;</a>';
for ($i = 0; $i < ($totalPages > 5 ? 5 : $totalPages); $i++) {
echo $pageNumber == $begPage + $i ? '<span>' . htmlspecialchars(strval($begPage + $i)) . '</span>' : '<a href="' . htmlspecialchars((URL_REWRITTEN ? APP_ROOT : APP_ROOT . APP_FILENAME . '/') . 'user-reviews/' . urlencode($userData['username']) . '?page=' . urlencode(strval($begPage + $i))) . '">' . htmlspecialchars(strval($begPage + $i)) . '</a>';
}
echo $pageNumber >= $totalPages ? '<span>&rsaquo;</span>' : '<a href="' . htmlspecialchars((URL_REWRITTEN ? APP_ROOT : APP_ROOT . APP_FILENAME . '/') . 'user-reviews/' . urlencode($userData['username']) . '?page=' . urlencode(strval($pageNumber + 1))) . '">&rsaquo;</a>';
echo '</div>';
}
}
}
}
}
}
?>
</main>
<?php
include 'footer.php';
?>