From 7bfb239ec4706ebe552ef2ae19c9e2850ef860d7 Mon Sep 17 00:00:00 2001 From: keplyx Date: Thu, 20 Feb 2020 12:20:42 +0100 Subject: [PATCH] Generate stock on button press --- admin/index.php | 14 ++++++++++++- admin/write_json.php | 23 ++++----------------- assets/js/index.js | 44 +++++++++++++++++++++++++++++++++++++++++ classes/postHandler.php | 30 ++++++++++++++++++++++------ 4 files changed, 85 insertions(+), 26 deletions(-) create mode 100644 assets/js/index.js diff --git a/admin/index.php b/admin/index.php index 87bda95..7d22b16 100644 --- a/admin/index.php +++ b/admin/index.php @@ -6,7 +6,7 @@ ob_start();
@@ -18,6 +18,11 @@ ob_start();
+ +
+ +
@@ -26,5 +31,12 @@ ob_start(); + + "; + + include($relativePath . "includes/template.php"); ?> diff --git a/admin/write_json.php b/admin/write_json.php index 3526666..581409a 100644 --- a/admin/write_json.php +++ b/admin/write_json.php @@ -1,22 +1,7 @@ write_json()); diff --git a/assets/js/index.js b/assets/js/index.js new file mode 100644 index 0000000..a1ad484 --- /dev/null +++ b/assets/js/index.js @@ -0,0 +1,44 @@ +async function sendRequest() { + let response = await $.ajax({ + type: "POST", + url: "write_json.php", + }); + response = JSON.parse(response); + console.log(response); + return response["status"]; +} + + +$('#uploadButton').on('click', function () { + $.confirm({ + title: 'Confirmer', + content: "Voulez vous vraiment mettre en ligne le stock actuel du Proximo ? Il sera visible depuis l'application CAMPUS.", + type: "orange", + buttons: { + formSubmit: { + text: 'Confirmer', + btnClass: "btn-warning", + action: async function () { + let result = await sendRequest(); + console.log(result); + if (result !== 0) { + $.alert({ + title: "Erreur", + content: "Une erreur est survenue, merci de réessayer plus tard.", + type: "red", + }) + } else { + $.alert({ + title: "Succès", + content: "Le stock a bien été mis à jour.", + type: "green", + }) + } + } + }, + cancel: { + text: 'Annuler', + } + } + }); +}); diff --git a/classes/postHandler.php b/classes/postHandler.php index 873070d..f6647c3 100644 --- a/classes/postHandler.php +++ b/classes/postHandler.php @@ -13,6 +13,7 @@ class PostHandler private $data; private $dao; private $uploadBaseDir = '../uploaded_images/'; + private $stockFile = "../data/stock-v2.json"; private $responseArray = array( "status" => 0, @@ -59,16 +60,31 @@ class PostHandler return $this->responseArray; } + public function write_json() + { + $result = 0; + $fp = fopen($this->stockFile, "w"); + $array = array( + "types" => $this->dao->get_categories(), + "articles" => $this->dao->get_articles(), + ); + fwrite($fp, json_encode($array)); + fclose($fp); + + $this->responseArray["data"] = $result; + return $this->responseArray; + } + private function save_image() { $success = true; if ($this->filesData["image"]["size"] > 0 && $this->data != null) { - $uploadPath = $this->uploadBaseDir.$this->data.".jpg"; + $uploadPath = $this->uploadBaseDir . $this->data . ".jpg"; if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) { $this->responseArray["message"] = "Image upload success"; } else { - $this->responseArray["message"] = "Image upload failure: ". $uploadPath; + $this->responseArray["message"] = "Image upload failure: " . $uploadPath; $this->responseArray["status"] = 1; $success = false; } @@ -80,11 +96,12 @@ class PostHandler if ($success) return 0; else - return json_encode($this->filesData)."id: ".$this->data; + return json_encode($this->filesData) . "id: " . $this->data; } - private function remove_image() { - $uploadPath = $this->uploadBaseDir.$this->data["id"].".jpg"; + private function remove_image() + { + $uploadPath = $this->uploadBaseDir . $this->data["id"] . ".jpg"; if (file_exists($uploadPath) && unlink($uploadPath)) { $this->responseArray["message"] = "Success: Deleted image"; } else if (!file_exists($uploadPath)) { @@ -153,7 +170,8 @@ class PostHandler return $result; } - function updateStock() { + function updateStock() + { $result = 0; foreach ($this->data as $row) { $value = $row["value"];