summaryrefslogtreecommitdiff
path: root/pages/rules.inc
blob: 21ae7e649e36e383ad40eb8b2f29e43546ee3e8e (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/init.inc"; global $title; global $isLoggedIn;

if (isset($_POST["updateRules"])) {
    header("Content-Type: text/plain");

    if (!isset($_POST['payload'])) {
        header("Location: /-/rules");
        die();
    }

    foreach ($_POST['payload'] as $index => $rule) {
        if (!isset($rule["name"]) || !isset($rule["content"]) && !is_numeric($index)) {
            header("Location: /-/rules");
            die();
        }

        if (trim($rule["name"]) === "") {
            unset($_POST["payload"][$index]);
            continue;
        }

        if (!isset($rule["approved"])) $rule["approved"] = [];
        if (isset($rule["approved"][0])) $rule["approved"][0] = true; else $rule["approved"][0] = false;
        if (isset($rule["approved"][1])) $rule["approved"][1] = true; else $rule["approved"][1] = false;

        $_POST["payload"][$index] = $rule;
    }

    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/rules/rules.json", utf8_encode(json_encode($_POST["payload"])));

    header("Location: /-/rules");
    die();
}

require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/header.inc';

?>

<br>
<div class="container">
    <div id="page-content">
        <h2>Systems rules</h2>
        <p>Click on a rule to view additional details. <a onclick="event.target.blur();" href="#" data-bs-toggle="modal" data-bs-target="#editor">Edit rules</a></p>

        <?php

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

        $protectorCloudburst = array_values(array_filter(scoreOrderGlobal(), function ($i) {
            return $i["_system"] === "ynmuc" && $i["_metadata"]["protector"];
        }))[0];
        $protectorRaindrops = array_values(array_filter(scoreOrderGlobal(), function ($i) {
            return $i["_system"] === "gdapd" && $i["_metadata"]["protector"];
        }))[0];

        $pcName = getMiniName($protectorCloudburst["display_name"] ?? $protectorCloudburst["name"]);
        $prName = getMiniName($protectorRaindrops["display_name"] ?? $protectorRaindrops["name"]);

        ?>

        <ul class="list-group">
            <?php $index = 1; foreach ($rules as $rule): ?>
            <li class="list-group-item rule-outer">
                <details>
                    <summary class="rule">
                        <b><?= $index ?>. <?= strip_tags($rule["name"]) ?></b>
                        <?php if (in_array(false, $rule["approved"])): ?>
                        <span class="badge bg-warning text-black rounded-pill">Unapproved</span>
                        <?php endif; ?>
                    </summary>
                    <?php if (in_array(false, $rule["approved"])): ?>
                    <div style="margin-top:10px;" class="alert alert-warning">
                        <b>This rule has not yet been approved.</b> All rules need to be approved by the leaders from both systems. This rule is still missing approval from <?php

                        if ($rule["approved"][0] === false) {
                            if ($rule["approved"][1] === false) {
                                echo($pcName . " and " . $prName);
                            } else {
                                echo($pcName);
                            }
                        } else if ($rule["approved"][1] === false) {
                            echo($prName);
                        }

                        ?>.
                    </div>
                    <?php endif; ?>
                    <div <?= !in_array(false, $rule["approved"]) ? 'style="margin-top:10px;"' : '' ?> class="list-group-item">
                        <?= strip_tags($rule["content"]) ?>
                    </div>
                </details>
            </li>
            <?php $index++; endforeach; ?>
        </ul>
    </div>
</div>

<div class="modal fade" id="editor">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Rules editor</h4>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <p>Rules with an empty name are automatically deleted. Buttons to add new rules and save the changes are at the bottom of the list.</p>
                <hr>
                <form method="post">
                    <div id="rules-editor">
                        <?php $index = 1; foreach ($rules as $rule): ?>
                            <div <?= $index === 1 ? 'id="default-rule"' : '' ?>>
                                <p><b <?= $index === 1 ? 'id="default-rule--number"' : '' ?>>Rule #<?= $index ?>:</b></p>
                                <input <?= $index === 1 ? 'id="default-rule--name"' : '' ?> type="text" placeholder="Rule name" class="form-control" style="margin-bottom:15px;color:white;background:#111;border-color:#222;" name="payload[<?= $index - 1 ?>][name]" value="<?= str_replace('"', "&quot;", strip_tags($rule["name"])) ?>">

                                <textarea <?= $index === 1 ? 'id="default-rule--content"' : '' ?> name="payload[<?= $index - 1 ?>][content]" class="form-control" style="resize: none;color:white;background:#111;border-color:#222;" placeholder="Rule details"><?= strip_tags($rule["content"] ?? "") ?></textarea>

                                <label style="margin-top:10px;margin-left:5px;">
                                    <input <?= $index === 1 ? 'id="default-rule--approval-1"' : '' ?> <?= ($rule["approved"][0] ?? false) ? "checked" : "" ?> type="checkbox" name="payload[<?= $index - 1 ?>][approved][0]">
                                    Approved by <?= $pcName ?>
                                </label><br>
                                <label style="margin-left:5px;">
                                    <input <?= $index === 1 ? 'id="default-rule--approval-2"' : '' ?> <?= ($rule["approved"][1] ?? false) ? "checked" : "" ?> type="checkbox" name="payload[<?= $index - 1 ?>][approved][1]">
                                    Approved by <?= $prName ?>
                                </label><br>

                                <hr>
                            </div>
                        <?php $index++; endforeach; ?>
                    </div>
                    <input type="submit" value="Save" class="btn btn-primary">
                    <a onclick="editorNewRule();" class="btn btn-secondary">New rule</a>
                    <input type="hidden" name="updateRules">
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    window.numberOfRules = <?= count($rules) ?>;

    Array.from(document.getElementsByClassName("rule-outer")).forEach((el) => {
        let details = el.children[0];

        el.onclick = () => {
            Array.from(document.getElementsByClassName("rule-outer")).forEach((sel) => {
                if (el === sel) return;
                sel.children[0].open = false;
                sel.classList.remove("open");
            });

            details.open = !details.open;

            if (details.open) {
                el.classList.add("open");
            } else {
                el.classList.remove("open");
            }
        }
    })

    function editorNewRule() {
        let id = Math.random(36).toString().split(".")[1];

        document.getElementById("default-rule").id = "added-" + id + "-1";
        document.getElementById("default-rule--number").id = "added-" + id + "-2";
        document.getElementById("default-rule--name").id = "added-" + id + "-3";
        document.getElementById("default-rule--content").id = "added-" + id + "-4";
        document.getElementById("default-rule--approval-1").id = "added-" + id + "-5";
        document.getElementById("default-rule--approval-2").id = "added-" + id + "-6";

        let child = document.createElement("div");
        child.id = "temp-" + id;
        window.numberOfRules++;

        document.getElementById("rules-editor").appendChild(child);
        document.getElementById("temp-" + id).outerHTML = document.getElementById("added-" + id + "-1").outerHTML;

        document.getElementById("added-" + id + "-1").id = "default-rule";
        document.getElementById("added-" + id + "-2").id = "default-rule--number";
        document.getElementById("added-" + id + "-3").id = "default-rule--name";
        document.getElementById("added-" + id + "-4").id = "default-rule--content";
        document.getElementById("added-" + id + "-5").id = "default-rule--approval-1";
        document.getElementById("added-" + id + "-6").id = "default-rule--approval-2";

        document.getElementById("added-" + id + "-3").name = "payload[" + (numberOfRules - 1) + "][name]";
        document.getElementById("added-" + id + "-4").name = "payload[" + (numberOfRules - 1) + "][content]";
        document.getElementById("added-" + id + "-5").name = "payload[" + (numberOfRules - 1) + "][approved][0]";
        document.getElementById("added-" + id + "-6").name = "payload[" + (numberOfRules - 1) + "][approved][1]";

        document.getElementById("added-" + id + "-5").checked = false;
        document.getElementById("added-" + id + "-6").checked = false;
        document.getElementById("added-" + id + "-3").value = "";
        document.getElementById("added-" + id + "-4").value = "";
        document.getElementById("added-" + id + "-2").innerText = "Rule #" + numberOfRules + ":";
    }
</script>

<style>
    .list-group-item {
        color: #fff;
        background-color: #222;
        border: 1px solid rgba(255, 255, 255, .125);
    }

    .list-group-item.disabled {
        color: #fff;
        background-color: #222;
        border-color: rgba(255, 255, 255, .125);
        opacity: .75;
    }

    .rule-outer:hover {
        background-color: #252525;
        color: #ddd;
    }

    .rule-outer:active, .rule-outer:focus {
        background-color: #272727;
        color: #bbb;
    }

    .rule-outer.open {
        background-color: #333;
    }

    .rule {
        list-style: none;
        pointer-events: none;
    }

    .rule-outer {
        cursor: pointer;
    }

    .modal-header {
        border-bottom: 1px solid #353738;
    }

    .modal-content {
        border: 1px solid rgba(255, 255, 255, .2);
        background-color: #111;
    }

    .btn-close {
        filter: invert(1);
    }
</style>

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