summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/emotes/index.php18
-rw-r--r--src/partials.php17
2 files changed, 32 insertions, 3 deletions
diff --git a/public/emotes/index.php b/public/emotes/index.php
index b074e0a..de0e6ef 100644
--- a/public/emotes/index.php
+++ b/public/emotes/index.php
@@ -15,15 +15,27 @@ function display_list_emotes(PDO &$db, int $page, int $limit): array
$search = "%" . ($_GET["q"] ?? "") . "%";
$user_id = $_SESSION["user_id"] ?? "-1";
$offset = $page * $limit;
+
+ $sort = match ($_GET["sort_by"] ?? "") {
+ "low_ratings" => "rating ASC",
+ "recent" => "e.created_at DESC",
+ "oldest" => "e.created_at ASC",
+ default => "rating DESC"
+ };
+
$stmt = $db->prepare("SELECT e.*,
CASE WHEN EXISTS (
SELECT 1
FROM emote_set_contents ec
INNER JOIN emote_sets es ON es.id = ec.emote_set_id
WHERE ec.emote_id = e.id AND es.owner_id = ?
- ) THEN 1 ELSE 0 END AS is_in_user_set
- FROM emotes e WHERE e.code LIKE ?
- ORDER BY e.created_at ASC
+ ) THEN 1 ELSE 0 END AS is_in_user_set, COALESCE(SUM(r.rate), 0) AS rating
+ FROM emotes e
+ LEFT JOIN ratings AS r ON r.emote_id = e.id
+ WHERE e.code LIKE ?
+ GROUP BY
+ e.id, e.code, e.created_at
+ ORDER BY $sort
LIMIT ? OFFSET ?
");
diff --git a/src/partials.php b/src/partials.php
index d245708..71f4f63 100644
--- a/src/partials.php
+++ b/src/partials.php
@@ -37,6 +37,23 @@ function html_navigation_search()
<div class="box content">
<form action="<?php echo $_SERVER["REQUEST_URI"] ?>" method="GET">
<input type="text" name="q" style="padding:4px;" value="<?php echo $_GET["q"] ?? "" ?>"><br>
+ <?php
+ if (str_starts_with($_SERVER["REQUEST_URI"], "/emotes")) {
+ ?>
+ <label for="sort_by">Sort by</label>
+ <select name="sort_by">
+ <option value="high_ratings" <?php echo ($_GET["sort_by"] ?? "") == "high_ratings" ? "selected" : "" ?>>
+ High ratings</option>
+ <option value="low_ratings" <?php echo ($_GET["sort_by"] ?? "") == "low_ratings" ? "selected" : "" ?>>Low
+ ratings</option>
+ <option value="recent" <?php echo ($_GET["sort_by"] ?? "") == "recent" ? "selected" : "" ?>>Recent
+ </option>
+ <option value="oldest" <?php echo ($_GET["sort_by"] ?? "") == "oldest" ? "selected" : "" ?>>Oldest
+ </option>
+ </select>
+ <?php
+ }
+ ?>
<button type="submit" style="width:100%;margin-top:6px;">Find</button>
</form>
</div>