summaryrefslogtreecommitdiff
path: root/src/alert.php
blob: 62f1887aa276efac92c89ff527c819a357aa4ae8 (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
<?php
function generate_alert(string $path, string $error, int $status = 400)
{
    http_response_code($status);

    if (isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json") {
        echo json_encode([
            "status_code" => $status,
            "message" => $error,
            "data" => null
        ]);
    } else {
        header("Location: $path?error_status=$status&error_reason=$error");
    }
}

function display_alert()
{
    if (!isset($_GET["error_status"], $_GET["error_reason"])) {
        return;
    }

    $status = $_GET["error_status"];
    $reason = str_safe($_GET["error_reason"], 50);
    $ok = substr($status, 0, 1) == '2';

    echo '' ?>
    <div class="box row alert <?php echo $ok ? '' : 'red' ?>" style="gap:8px;" id="alert-box">
        <img src="/static/img/icons/<?php echo $ok ? 'yes' : 'no' ?>.png" alt="">
        <p><b><?php echo $reason ?></b></p>
    </div>
    <script>
        setTimeout(() => {
            const alertBox = document.getElementById("alert-box");
            alertBox.remove();
        }, 5000);
    </script>
    <?php
}