summaryrefslogtreecommitdiff
path: root/includes/system/history.inc
blob: ee7f7621fbf44f20795e77d654efec8f7f70b955 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php global $system; global $lang; global $pages; global $systemCommonName; $pages = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/pages.json"), true); $title = $pages["s:history"]["name"][$lang["_name"]] . " · " . $systemCommonName; global $systemID; require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/header.inc';

function getMember(string $id) {
    global $systemID;

    $members = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$systemID/members.json"), true);
    $member = null;

    foreach ($members as $m) {
        if ($m["id"] === $id) $member = $m;
    }

    return $member;
}

?>

    <br>
    <div class="container" id="page-content">
        <?php

        $switches = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$systemID/switches.json"), true);
        uksort($switches, function ($a, $b) {
            if (isset($b["timestamp"]) && isset($a["timestamp"])) {
                return strtotime($b["timestamp"]) - strtotime($a["timestamp"]);
            } else {
                return null;
            }
        });

        function frenchIfRequired($original) {
            global $lang;

            if ($lang["_french"]) {
                if ($original === "Today") return "Aujourd'hui";
                if ($original === "Yesterday") return "Hier";

                $original = str_replace("Jan", "janv.", $original);
                $original = str_replace("Feb", "fév.", $original);
                $original = str_replace("Mar", "mars", $original);
                $original = str_replace("Apr", "avr.", $original);
                $original = str_replace("May", "mai", $original);
                $original = str_replace("Jun", "juin", $original);
                $original = str_replace("Jul", "juil.", $original);
                $original = str_replace("Aug", "août", $original);
                $original = str_replace("Sep", "sept.", $original);
                $original = str_replace("Oct", "oct.", $original);
                $original = str_replace("Nov", "nov.", $original);
                $original = str_replace("Dec", "déc.", $original);

                $original = str_replace("Mon", "Lun.", $original);
                $original = str_replace("Tue", "Mar.", $original);
                $original = str_replace("Wed", "Mer.", $original);
                $original = str_replace("Thu", "Jeu.", $original);
                $original = str_replace("Fri", "Ven.", $original);
                $original = str_replace("Sat", "Sam.", $original);
                $original = str_replace("Sun", "Dim.", $original);

            }
            
            return $original;
        }

        function getSwitchesForDay(int $day) {
            global $switches;

            $filtered = array_values(array_filter($switches, function ($i) use ($day) {
                $diff = strtotime(date("Y-m-d")) - strtotime(explode("T", $i["timestamp"])[0]);
                return $diff <= (86400 * $day) && $diff > (86400 * ($day - 1));
            }));

            uksort($filtered, function ($a, $b) {
                if (isset($b["timestamp"]) && isset($a["timestamp"])) {
                    return strtotime($b["timestamp"]) - strtotime($a["timestamp"]);
                } else {
                    return null;
                }
            });

            return $filtered;
        }

        function getSwitchBefore(string $id) {
            global $switches;

            $currentPassed = false;
            $before = null;

            foreach ($switches as $switch) {
                if ($currentPassed) {
                    $before = $switch;
                    break;
                } else {
                    if ($switch["id"] === $id) {
                        $currentPassed = true;
                    }
                }
            }

            return $before;
        }

        function isNotToday(int $timestamp, int $offset) {
            if (date('Y-m-d', $timestamp) !== date('Y-m-d', time() - (86400 * $offset))) {
                return true;
            } else {
                return false;
            }
        }

        ?>
        <h2><?= $lang["history"]["title"] ?> <?= $systemCommonName ?></h2>
        <div class="alert alert-warning" id="timezone" style="display: none;">
            <?= str_replace("%1", '<span id="timezone-name">-</span>', str_replace("%2", '<span id="day-start">-</span>', $lang["history"]["timezone"])) ?>
            <script>
                document.getElementById("day-start").innerText = new Date(<?= strtotime(date('Y-m-d')) ?> * 1000).toTimeString().substring(0, 5);
                document.getElementById("timezone-name").innerText = new Date().toTimeString().split("(")[1].split(")")[0];
            </script>
        </div>
        <?php foreach ([
            [ "Today", 0 ],
            [ "Yesterday", 1 ],
            [ date('D j M', time() - (86400 * 2)), 2 ],
            [ date('D j M', time() - (86400 * 3)), 3 ],
            [ date('D j M', time() - (86400 * 4)), 4 ],
            [ date('D j M', time() - (86400 * 5)), 5 ],
            [ date('D j M', time() - (86400 * 6)), 6 ],
            [ date('D j M', time() - (86400 * 7)), 7 ],
            [ date('D j M', time() - (86400 * 8)), 8 ],
            [ date('D j M', time() - (86400 * 9)), 9 ],
        ] as $day):
            $switchesDay = getSwitchesForDay($day[1]);
            $switchesDay[] = getSwitchBefore($switchesDay[count($switchesDay) - 1]["id"]);
            ?>
        <h4 style="margin-top:15px;"><?= frenchIfRequired($day[0]) ?></h4>
            <?php foreach ($switchesDay as $switch): $switch["timestamp"] = strtotime($switch["timestamp"]); ?>
                <?php if (isset($switch["members"][0])): $member = getMemberWithoutSystem($switch["members"][0]); ?>
                <div class="fronter">
                    <span class="fronter-date" data-date-time="<?= isNotToday($switch["timestamp"], $day[1]) ? strtotime(date('Y-m-d', $switch["timestamp"])) : $switch["timestamp"] ?>" style="opacity:.5;font-family: monospace;font-size:14px;vertical-align: middle;">
                        <?= isNotToday($switch["timestamp"], $day[1]) ? "00:00" : date('H:i', $switch["timestamp"]) ?>
                    </span>
                    <span class="fronter-profile" style="vertical-align: middle;">
                        <a class="member-link" href="/<?= $member["name"] ?>">
                            <img src="<?= getAsset($systemID, $member["id"], "heads") ?>" style="width:24px;"> <?= $member["display_name"] ?? $member["name"] ?>
                        </a>
                        <?php if (count($switch["members"]) > 1): ?>
                            and
                            <?php if (isset($switch["members"][1])): $member2 = getMember($switch["members"][1]); ?><a class="member-link" href="/<?= $member2["name"] ?>"><img src="<?= getAsset($systemID, $member2["id"], "heads") ?>" style="width:24px;"> <?= $member2["display_name"] ?? $member2["name"] ?></a><?php endif; ?><?php if (isset($switch["members"][2])): $member2 = getMember($switch["members"][2]); ?>, <a class="member-link" href="/<?= $member2["name"] ?>"><img src="<?= getAsset($systemID, $member2["id"], "heads") ?>" style="width:24px;"> <?= $member2["display_name"] ?? $member2["name"] ?></a><?php endif; ?><?php if (isset($switch["members"][3])): $member2 = getMember($switch["members"][3]); ?>, <a class="member-link" href="/<?= $member2["name"] ?>"><img src="<?= getAsset($systemID, $member2["id"], "heads") ?>" style="width:24px;"> <?= $member2["display_name"] ?? $member2["name"] ?></a><?php endif; ?>
                        <?php endif; ?>
                    </span>
                </div>
                <?php else: ?>
                <div class="fronter">
                    <span class="fronter-date" data-date-time="<?= isNotToday($switch["timestamp"], $day[1]) ? strtotime(date('Y-m-d', $switch["timestamp"])) : $switch["timestamp"] ?>" style="opacity:.5;font-family: monospace;font-size:14px;vertical-align: middle;">
                        <?= isNotToday($switch["timestamp"], $day[1]) ? "00:00" : date('H:i', $switch["timestamp"]) ?>
                    </span>
                    <span class="fronter-profile" style="vertical-align: middle;">
                        <span class="text-muted"><?= $lang["history"]["fallback"] ?></span>
                    </span>
                </div>
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endforeach; ?>
    </div>
    <script>
        Array.from(document.getElementsByClassName("fronter-date")).forEach((i) => { i.innerText = new Date(parseInt(i.getAttribute("data-date-time").trim()) * 1000).toTimeString().split(" ")[0].substring(0, 5) + " " });
        document.getElementById("timezone").style.display = "";
    </script>

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.inc'; ?>