diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/emote.php | 148 | ||||
| -rw-r--r-- | src/user.php | 106 |
2 files changed, 215 insertions, 39 deletions
diff --git a/src/emote.php b/src/emote.php index 74db4ca..01a204a 100644 --- a/src/emote.php +++ b/src/emote.php @@ -1,4 +1,6 @@ <?php +include_once "user.php"; + class Emote { public string $id; @@ -14,18 +16,39 @@ class Emote public array $tags; - function __construct($id, $code, $ext, $created_at, $uploaded_by, $is_in_user_set, $rating, $visibility, $source, $tags) + public static function from_array(array $arr): Emote { - $this->id = $id; - $this->code = $code; - $this->ext = $ext; - $this->created_at = $created_at; - $this->uploaded_by = $uploaded_by; - $this->is_in_user_set = $is_in_user_set; - $this->rating = $rating; - $this->visibility = $visibility; - $this->source = $source; - $this->tags = $tags; + $e = new Emote(); + + $e->id = $arr["id"]; + $e->code = $arr["code"]; + $e->ext = $arr["ext"] ?? "webp"; + $e->uploaded_by = $arr["uploaded_by"]; + $e->created_at = strtotime($arr["created_at"] ?? 0); + $e->is_in_user_set = $arr["is_in_user_set"] ?? false; + $e->visibility = $arr["visibility"]; + $e->source = $arr["source"] ?? null; + $e->tags = $arr["tags"] ?? []; + + if (isset($arr["total_rating"], $arr["average_rating"])) { + $e->rating = [ + "total" => $arr["total_rating"], + "average" => $arr["average_rating"] + ]; + } else { + $e->rating = $arr["rating"] ?? null; + } + + return $e; + } + + public static function from_array_with_user(array $arr, PDO &$db): Emote + { + if ($arr["uploaded_by"]) { + $arr["uploaded_by"] = User::get_user_by_id($db, $arr["uploaded_by"]); + } + + return Emote::from_array($arr); } function get_id() @@ -79,6 +102,58 @@ class Emote } } +class Emoteset +{ + public string $id; + public string $name; + public User|null $owner; + public array $emotes; + + public bool $is_default; + + public static function from_array(array $arr): Emoteset + { + $s = new Emoteset(); + + $s->id = $arr["id"]; + $s->name = $arr["name"]; + $s->owner = $arr["owner_id"]; + $s->emotes = $arr["emotes"] ?? []; + $s->is_default = $arr["is_default"] ?? false; + + return $s; + } + + public static function from_array_extended(array $arr, string $user_id, PDO &$db): Emoteset + { + if ($arr["owner_id"]) { + $arr["owner_id"] = User::get_user_by_id($db, $arr["owner_id"]); + } + + $arr["emotes"] = fetch_all_emotes_from_emoteset($db, $arr["id"], $user_id); + + return Emoteset::from_array($arr); + } + + public static function get_all_user_emotesets(PDO &$db, string $user_id): array + { + $stmt = $db->prepare("SELECT es.*, aes.is_default FROM emote_sets es + INNER JOIN acquired_emote_sets aes ON aes.emote_set_id = es.id + WHERE aes.user_id = ? + "); + $stmt->execute([$user_id]); + + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + $emote_sets = []; + + foreach ($rows as $row) { + array_push($emote_sets, Emoteset::from_array_extended($row, $user_id, $db)); + } + + return $emote_sets; + } +} + function fetch_all_emotes_from_emoteset(PDO &$db, string $emote_set_id, string $user_id, int|null $limit = null): array { // fetching emotes @@ -106,17 +181,16 @@ function fetch_all_emotes_from_emoteset(PDO &$db, string $emote_set_id, string $ $stmt = $db->prepare($sql); $stmt->execute([$user_id, $emote_set_id]); - $emotes = $stmt->fetchAll(PDO::FETCH_ASSOC); + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + $emotes = []; // fetching uploaders - foreach ($emotes as &$e) { - if ($e["uploaded_by"]) { - $stmt = $db->prepare("SELECT id, username FROM users WHERE id = ?"); - $stmt->execute([$e["uploaded_by"]]); - - $e["uploaded_by"] = $stmt->fetch(PDO::FETCH_ASSOC); + foreach ($rows as $row) { + if ($row["uploaded_by"]) { + $row["uploaded_by"] = User::get_user_by_id($db, $row["uploaded_by"]); } - unset($e); + + array_push($emotes, Emote::from_array($row)); } return $emotes; @@ -171,4 +245,40 @@ function html_featured_emote(PDO &$db) <?php ; } +} + +function html_display_emotes(array $emotes) +{ + foreach ($emotes as $e) { + echo '<a class="box emote" href="/emotes?id=' . $e->get_id() . '">'; + + if ($e->is_added_by_user()) { + echo '<img src="/static/img/icons/yes.png" class="emote-check" />'; + } + + echo '<img src="/static/userdata/emotes/' . $e->get_id() . '/2x.webp" alt="' . $e->get_code() . '"/>'; + echo '<h1>' . $e->get_code() . '</h1>'; + echo '<p>' . ($e->get_uploaded_by() == null ? (ANONYMOUS_DEFAULT_NAME . "*") : $e->get_uploaded_by()->username) . '</p>'; + echo '</a>'; + } +} + +function html_display_emoteset(array $emotesets) +{ + foreach ($emotesets as $es) { + echo "<a href='/emotesets.php?id={$es->id}' class='box column small-gap'>"; + + echo '<div>'; + echo "<p>$es->name</p>"; + echo '</div>'; + + echo '<div class="small-gap row">'; + + foreach ($es->emotes as $e) { + echo "<img src='/static/userdata/emotes/{$e->id}/1x.webp' alt='{$e->code}' title='{$e->code}' height='16' />"; + } + + echo '</div></a>'; + + } }
\ No newline at end of file diff --git a/src/user.php b/src/user.php index e9fec0b..d22eeb4 100644 --- a/src/user.php +++ b/src/user.php @@ -1,36 +1,102 @@ <?php -class User +class Badge { - private string $id; - private string $username; - private int $joined_at; - private int $last_active_at; + public string $id; - function __construct($row) + public static function from_array(array $arr, string $prefix = ""): Badge|null { - $this->id = $row["id"]; - $this->username = $row["username"]; - $this->joined_at = strtotime($row["joined_at"]); - $this->last_active_at = strtotime($row["last_active_at"]); - } + if (!empty($prefix)) { + $prefix .= "_"; + } + if (!isset($arr["{$prefix}badge_id"])) { + return null; + } - function id() - { - return $this->id; + $b = new Badge(); + $b->id = $arr["{$prefix}badge_id"]; + + return $b; } +} + +class Role +{ + public string $name; + public Badge|null $badge; - function username() + public static function from_array(array $arr): Role|null { - return $this->username; + if (!isset($arr["role_name"])) { + return null; + } + + $r = new Role(); + + $r->name = $arr["role_name"]; + $r->badge = Badge::from_array($arr, "role"); + + return $r; } +} + +class User +{ + public string $id; + public string $username; + public int $joined_at; + public int $last_active_at; - function joined_at() + public Badge|null $custom_badge; + + public Role|null $role; + + public bool $private_profile; + + public static function from_array(array $arr): User { - return $this->joined_at; + $u = new User(); + + $u->id = $arr["id"]; + $u->username = $arr["username"]; + $u->joined_at = strtotime($arr["joined_at"] ?? "0"); + $u->last_active_at = strtotime($arr["last_active_at"] ?? "0"); + + $u->private_profile = $row["private_profile"] ?? false; + + $u->custom_badge = Badge::from_array($arr, "custom"); + + $u->role = Role::from_array($arr); + + return $u; } - function last_active_at() + public static function get_user_by_id(PDO &$db, string $user_id): User|null { - return $this->last_active_at; + $stmt = $db->prepare("SELECT + u.id, + u.username, + u.joined_at, + u.last_active_at, + + up.private_profile, + r.name AS role_name, + r.badge_id AS role_badge_id, + ub.badge_id AS custom_badge_id + FROM users u + INNER JOIN user_preferences up ON up.id = u.id + LEFT JOIN role_assigns ra ON ra.user_id = u.id + LEFT JOIN roles r ON r.id = ra.role_id + LEFT JOIN user_badges ub ON ub.user_id = u.id + WHERE u.id = ? + "); + $stmt->execute([$user_id]); + + $u = null; + + if ($uploader_row = $stmt->fetch()) { + $u = User::from_array($uploader_row); + } + + return $u; } }
\ No newline at end of file |
