diff options
Diffstat (limited to 'pages/rules.php')
-rw-r--r-- | pages/rules.php | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/pages/rules.php b/pages/rules.php new file mode 100644 index 0000000..b1759dc --- /dev/null +++ b/pages/rules.php @@ -0,0 +1,254 @@ +<?php + +require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/session.php"; global $isLoggedIn; +if (!$isLoggedIn) header("Location: /-/login") and die(); + +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.json", utf8_encode(json_encode($_POST["payload"]))); + + header("Location: /-/rules"); + die(); +} + +$title = "Systems rules"; require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/header.php'; + +?> + +<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.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 protectors 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('"', """, 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.php'; ?> |