diff --git a/api/RequestBase.php b/api/RequestBase.php new file mode 100644 index 0000000..e95c62b --- /dev/null +++ b/api/RequestBase.php @@ -0,0 +1,24 @@ +actions[$action] = $callback; + } + + function handleAction() { + $this->conn = Database::getInstance()->getConnection(); + + if (isset($_POST['action'])) { + $this->initIdentifiers(); + + $action = $_POST['action']; + call_user_func($this->actions[$action]); + } + } +} diff --git a/api/Settings.php b/api/Settings.php index 273446e..3fbe1e9 100644 --- a/api/Settings.php +++ b/api/Settings.php @@ -1,29 +1,25 @@ getConnection(); -$settings = new SSettings(); - -if (isset($_POST['action'])) { - $action = $_POST['action']; - switch ($action) { - case "loadGeneralSettings": +class Settings extends RequestBase { + function initIdentifiers() { + $this->addIdentifier("loadGeneralSettings", function () { $query = "SELECT * from settings"; - $result = $conn->query($query); + $result = $this->conn->query($query); if ($result->num_rows > 1) { // todo throw error } $r = mysqli_fetch_assoc($result); // booleans need to be set manually - $r['passwordEnabled'] = $r['password'] != "-1"; + $r['passwordEnabled'] = $r['password'] != "-1"; $r['TMDB_grabbing'] = ($r['TMDB_grabbing'] != '0'); echo json_encode($r); - break; - case "saveGeneralSettings": + }); + + $this->addIdentifier("saveGeneralSettings", function () { $mediacentername = $_POST['mediacentername']; $password = $_POST['password']; $videopath = $_POST['videopath']; @@ -38,16 +34,17 @@ if (isset($_POST['action'])) { TMDB_grabbing=$tmdbsupport WHERE 1"; - if ($conn->query($query) === true) { + if ($this->conn->query($query) === true) { echo '{"success": true}'; } else { echo '{"success": true}'; } - break; - case "loadInitialData": + }); + + $this->addIdentifier("loadInitialData", function () { $query = "SELECT * from settings"; - $result = $conn->query($query); + $result = $this->conn->query($query); if ($result->num_rows > 1) { // todo throw error } @@ -60,6 +57,9 @@ if (isset($_POST['action'])) { } unset($r['password']); echo json_encode($r); - break; + }); } } + +$sett = new Settings(); +$sett->handleAction();