summaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/add.php55
-rw-r--r--api/change-order.php26
-rw-r--r--api/export.php43
-rw-r--r--api/remove.php19
4 files changed, 143 insertions, 0 deletions
diff --git a/api/add.php b/api/add.php
new file mode 100644
index 0000000..b2e4c2d
--- /dev/null
+++ b/api/add.php
@@ -0,0 +1,55 @@
+<?php
+$req_keys = ["name", "author", "thumbnail", "magazine"];
+header("Location: " . $_SERVER["HTTP_REFERER"]);
+foreach ($req_keys as $key) {
+ if (!isset($_POST[$key])) {
+ http_response_code(400);
+ exit(0);
+ }
+}
+$path = "/home/maks/school/clientApps/mlos/src/db.xml";
+require_once "/home/maks/school/clientApps/mlos/src/data.php";
+
+$name = $_POST["name"];
+$author = $_POST["author"];
+$thumbnail = $_POST["thumbnail"];
+$magazine = $_POST["magazine"];
+$id = $_POST["id"];
+
+$doc = get_doc();
+error_log("ID: " . $id);
+
+if ($id) {
+ $xpath = new DOMXpath($doc);
+ $game = $xpath->query('//game[@id="' . $id . '"]')->item(0);
+ var_dump($game);
+ var_dump($game->getElementsByTagName);
+ $xpath->query('//game[@id="' . $id . '"]/name')->item(0)->nodeValue = $name;
+ $xpath->query('//game[@id="' . $id . '"]/author')->item(0)->nodeValue = $author;
+ $xpath->query('//game[@id="' . $id . '"]/thumbnail')->item(0)->nodeValue = $thumbnail;
+ $xpath->query('//game[@id="' . $id . '"]/magazine')->item(0)->nodeValue = $magazine;
+} else {
+ $root = $doc->getElementsByTagName("root")->item(0);
+ $game = $doc->createElement("game", "");
+
+ $mid = $doc->getElementsByTagName("mid")->item(0);
+ $game->setAttribute("id", $mid->nodeValue);
+ $mid->nodeValue = intval($mid->nodeValue) + 1;
+
+ $count = $doc->getElementsByTagName("count")->item(0);
+ $count->nodeValue = intval($count->nodeValue) + 1;
+ $game->setAttribute("order", $count->nodeValue);
+
+ $name = $doc->createElement("name", $name);
+ $author = $doc->createElement("author", $author);
+ $thumbnail = $doc->createElement("thumbnail", $thumbnail);
+ $magazine = $doc->createElement("magazine", $magazine);
+ $game->append($name);
+ $game->append($author);
+ $game->append($thumbnail);
+ $game->append($magazine);
+
+ $root->appendChild($game);
+}
+
+$doc->save($path);
diff --git a/api/change-order.php b/api/change-order.php
new file mode 100644
index 0000000..b498605
--- /dev/null
+++ b/api/change-order.php
@@ -0,0 +1,26 @@
+<?php
+if (empty($_POST["old-order"]) || empty($_POST["new-order"])) {
+ http_response_code(400);
+ error_log("not enough post");
+ exit(0);
+}
+$path = "/home/maks/school/clientApps/mlos/src/db.xml";
+require_once "/home/maks/school/clientApps/mlos/src/data.php";
+
+$old_order = $_POST["old-order"];
+$new_order = $_POST["new-order"];
+$count = $doc->getElementsByTagName("count")->item(0)->nodeValue;
+if ($new_order === strval(intval($count) + 1) || $new_order === "0") {
+ exit();
+}
+$doc = get_doc();
+$xpath = new DOMXpath($doc);
+$old_game = $xpath->query('//game[@order="' . $old_order . '"]')->item(0);
+$new_game = $xpath->query('//game[@order="' . $new_order . '"]')->item(0);
+$old_game->setAttribute("order", $new_order);
+$new_game->setAttribute("order", $old_order);
+
+// check for first
+
+$doc->save($path);
+header("Location: " . $_SERVER["HTTP_REFERER"]);
diff --git a/api/export.php b/api/export.php
new file mode 100644
index 0000000..77e36a2
--- /dev/null
+++ b/api/export.php
@@ -0,0 +1,43 @@
+<?php
+require_once '../vendor/autoload.php';
+require_once '../src/data.php';
+
+[$data, $count] = get_data();
+
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
+
+header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
+header('Content-Disposition: attachment;filename="mlos-data.xlsx"');
+header('Cache-Control: max-age=0');
+
+$spreadsheet = new Spreadsheet();
+$active_sheet = $spreadsheet->getActiveSheet();
+
+$active_sheet->setCellValue('A1', "Name");
+$active_sheet->setCellValue('B1', "Author");
+$active_sheet->setCellValue('C1', "Magazine");
+$row = 2;
+foreach ($data as $d) {
+ $active_sheet->setCellValue('A' . $row, $d["name"]);
+
+ $drawing = new Drawing();
+ $drawing->setName($d["name"]);
+ $drawing->setPath("../imgs/" . $d["thumbnail"]);
+ $drawing->setHeight(200);
+
+ $comment = $active_sheet->getComment('A' . $row);
+ $comment->setBackgroundImage($drawing);
+ $comment->setSizeAsBackgroundImage();
+
+
+ $active_sheet->setCellValue('B' . $row, $d["author"]);
+ $active_sheet->setCellValue('C' . $row, $d["magazine"]);
+ $row++;
+}
+
+$writer = new Xlsx($spreadsheet);
+$writer->save('php://output');
+
+$spreadsheet->disconnectWorksheets();
diff --git a/api/remove.php b/api/remove.php
new file mode 100644
index 0000000..65f1a82
--- /dev/null
+++ b/api/remove.php
@@ -0,0 +1,19 @@
+<?php
+if (empty($_POST["id"])) {
+ http_response_code(400);
+ exit(0);
+}
+$path = "/home/maks/school/clientApps/mlos/src/db.xml";
+require_once "/home/maks/school/clientApps/mlos/src/data.php";
+
+$id = $_POST["id"];
+$doc = get_doc();
+$xpath = new DOMXpath($doc);
+$game = $xpath->query('//game[@id="' . $id . '"]')->item(0);
+$game->parentNode->removeChild($game);
+
+$count = $doc->getElementsByTagName("count")->item(0);
+$count->nodeValue = intval($count->nodeValue) - 1;
+
+$doc->save($path);
+header("Location: " . $_SERVER["HTTP_REFERER"]);