summaryrefslogtreecommitdiff
path: root/badges.php
blob: 57475ba961060b2c106112f790557bd1c16ffd0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/utils.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/config.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/user.php";

$db = new PDO(CONFIG['database']['url'], CONFIG['database']['user'], CONFIG['database']['pass']);

$stmt = $db->prepare("SELECT
    u.id, u.username,
    r.name AS role_name,
    r.badge_id AS role_badge_id,
    ub.badge_id AS custom_badge_id,
    co.alias_id AS connection_alias_id,
    co.platform AS connection_platform
    FROM users u
    JOIN role_assigns ra ON ra.user_id = u.id
    JOIN roles r ON r.id = ra.role_id
    LEFT JOIN user_badges ub ON ub.user_id = u.id
    LEFT JOIN connections co ON co.user_id = u.id
    WHERE r.badge_id IS NOT NULL OR ub.badge_id IS NOT NULL
");
$stmt->execute();

$rows = $stmt->fetchAll();

$badges = [];

foreach ($rows as $row) {
    $badge = [
        "id" => $row["id"],
        "username" => $row["username"],
        "role" => Role::from_array($row),
        "custom_badge" => Badge::from_array($row, "custom"),
        "connection" => match (isset($row["connection_alias_id"], $row["connection_platform"])) {
            true => [
                "alias_id" => $row["connection_alias_id"],
                "platform" => $row["connection_platform"]
            ],
            false => null
        }
    ];

    array_push($badges, $badge);
}

json_response([
    "status_code" => 200,
    "message" => null,
    "data" => $badges
]);