"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, COALESCE(COUNT(r.rate), 0) AS rating
FROM emotes e
LEFT JOIN ratings AS r ON r.emote_id = e.id
WHERE e.code LIKE ? AND e.visibility = 1
GROUP BY
e.id, e.code, e.created_at
ORDER BY $sort
LIMIT ? OFFSET ?
");
$stmt->bindParam(1, $user_id, PDO::PARAM_INT);
$stmt->bindParam(2, $search, PDO::PARAM_STR);
$stmt->bindParam(3, $limit, PDO::PARAM_INT);
$stmt->bindParam(4, $offset, PDO::PARAM_INT);
$stmt->execute();
$emotes = [];
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
$uploader = null;
if ($row["uploaded_by"]) {
$stmt = $db->prepare("SELECT id, username FROM users WHERE id = ?");
$stmt->execute([$row["uploaded_by"]]);
$uploader = $stmt->fetch(PDO::FETCH_ASSOC);
}
array_push($emotes, new Emote(
$row["id"],
$row["code"],
"webp",
intval(strtotime($row["created_at"])),
$uploader,
$row["is_in_user_set"],
$row["rating"],
$row["visibility"]
));
}
return $emotes;
}
function display_emote(PDO &$db, string $id)
{
$stmt = $db->prepare("SELECT e.*, COALESCE(COUNT(r.rate), 0) as total_rating,
COALESCE(ROUND(AVG(r.rate), 2), 0) AS average_rating
FROM emotes e
LEFT JOIN ratings AS r ON r.emote_id = ?
WHERE e.id = ?");
$stmt->execute([$id, $id]);
$emote = null;
if ($row = $stmt->fetch()) {
if ($row["id"] != null) {
$emote = new Emote(
$row["id"],
$row["code"],
"webp",
intval(strtotime($row["created_at"])),
$row["uploaded_by"],
false,
["total" => $row["total_rating"], "average" => $row["average_rating"]],
$row["visibility"]
);
}
}
if ($emote == null) {
if (CLIENT_REQUIRES_JSON) {
json_response([
"status_code" => 404,
"message" => "Emote ID $id does not exist",
"data" => null
], 404);
exit;
}
header("Location: /404.php");
exit;
}
return $emote;
}
$emotes = null;
$emote = null;
$id = $_GET["id"] ?? "";
$db = new PDO(DB_URL, DB_USER, DB_PASS);
$page = max(1, intval($_GET["p"] ?? "1"));
$limit = 50;
$total_emotes = 0;
$total_pages = 0;
$search = "%" . ($_GET["q"] ?? "") . "%";
$sort_by = $_GET["sort_by"] ?? "";
if (empty($id)) {
$emotes = display_list_emotes($db, $search, $sort_by, $page, $limit);
$stmt = $db->prepare("SELECT COUNT(*) FROM emotes WHERE code LIKE ? AND visibility = 1");
$stmt->execute([$search]);
$total_emotes = $stmt->fetch()[0];
$total_pages = ceil($total_emotes / $limit);
} else {
$emote = display_emote($db, $id);
}
if (CLIENT_REQUIRES_JSON) {
json_response([
"status_code" => 200,
"message" => null,
"data" => $emotes ?? $emote
]);
exit;
}
?>
get_code() : "Emotes") . ' - ' . INSTANCE_NAME
?>
get_code();
echo '
';
$stmt = $db->prepare("
SELECT MAX(es.is_featured) AS is_featured, MAX(es.is_global) AS is_global
FROM emote_sets es
JOIN emote_set_contents esc ON esc.emote_set_id = es.id
JOIN emotes e ON esc.emote_id = e.id
WHERE e.id = ?
");
$stmt->execute([$emote->get_id()]);
if ($row = $stmt->fetch()) {
if ($row["is_featured"]) {
echo '';
}
if ($row["is_global"]) {
echo '';
}
}
echo '
prepare("SELECT u.id, u.username, a.created_at FROM users u
INNER JOIN mod_actions a ON a.emote_id = ?
WHERE u.id = a.user_id");
$stmt->execute([$emote->get_id()]);
if ($row = $stmt->fetch()) {
echo '