summaryrefslogtreecommitdiff
path: root/Application/TransactionsList/index.php
blob: 4bb56e0fe7f09c9d5631a7fb965382e5245a5dbd (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
51
52
53
54
55
56
<?php

function timeAgo($time): string {
    if (!is_numeric($time)) {
        $time = strtotime($time);
    }

    $periods = ["second", "minute", "hour", "day", "week", "month", "year", "age"];
    $lengths = array("60", "60", "24", "7", "4.35", "12", "100");

    $now = time();

    $difference = $now - $time;
    if ($difference <= 10 && $difference >= 0) {
        return $tense = "now";
    } elseif ($difference > 0) {
        $tense = "ago";
    } else {
        $tense = "later";
    }

    for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    $period =  $periods[$j] . ($difference >1 ? "s" :'');
    return "{$difference} {$period} {$tense} ";
}

require_once $_SERVER['DOCUMENT_ROOT'] . "/Private/SessionManager.php";
header("Content-Type: application/json");

$users = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/Private/Data/Users.json"), true);
$list = array_reverse(array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/Private/Data/Transactions"), function ($i) {
    return !str_starts_with($i, ".") && str_ends_with($i, ".json");
}));
$plist = [];

foreach ($list as $id) {
    $item = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/Private/Data/Transactions/" . $id), true);

    $item["author"] = [
        "id" => $item["author"],
        "name" => $users[$item["author"]] ?? $item["author"],
        "avatar" => "https://account.minteck.org/hub/api/rest/avatar/" . $item["author"] . "?dpr=2&size=48"
    ];
    $item["date"] = [
        "absolute" => $item["date"],
        "relative" => trim(timeAgo($item["date"]))
    ];
    $plist[] = $item;
}

die(json_encode($plist));