Browse Source

Generate stock on button press

keplyx 4 years ago
parent
commit
7bfb239ec4
4 changed files with 85 additions and 26 deletions
  1. 13
    1
      admin/index.php
  2. 4
    19
      admin/write_json.php
  3. 44
    0
      assets/js/index.js
  4. 24
    6
      classes/postHandler.php

+ 13
- 1
admin/index.php View File

@@ -6,7 +6,7 @@ ob_start();
6 6
     <div style="margin: auto">
7 7
         <div style="display: flex" class="my-5">
8 8
             <a href="stock.php" style="margin: auto">
9
-                <button class="btn btn-primary btn-lg">Gestion du stocks</button>
9
+                <button class="btn btn-success btn-lg">Gestion du stocks</button>
10 10
             </a>
11 11
         </div>
12 12
 
@@ -18,6 +18,11 @@ ob_start();
18 18
                 <button class="btn btn-primary btn-lg">Créer/Éditer des catégories</button>
19 19
             </a>
20 20
         </div>
21
+
22
+        <div style="display: flex" class="my-5">
23
+            <button id="uploadButton" class="btn btn-warning btn-lg" style="margin: auto">Mettre l'inventaire en ligne
24
+            </button>
25
+        </div>
21 26
     </div>
22 27
 </div>
23 28
 
@@ -26,5 +31,12 @@ ob_start();
26 31
 <?php
27 32
 $pageContent = ob_get_clean();
28 33
 $pageTitle = "Admin";
34
+$pageScripts =
35
+    "
36
+    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/jquery-confirm-defaults.js\"></script>
37
+    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/index.js\"></script>
38
+    ";
39
+
40
+
29 41
 include($relativePath . "includes/template.php");
30 42
 ?>

+ 4
- 19
admin/write_json.php View File

@@ -1,22 +1,7 @@
1 1
 <?php
2
-$rest_json = file_get_contents("php://input");
3
-$_POST = json_decode($rest_json, true);
2
+$relativePath = "../";
3
+require_once $relativePath.'classes/postHandler.php';
4 4
 
5
+$handler = new PostHandler(null, null);
5 6
 
6
-//var_dump($_POST);
7
-
8
-$fp = fopen('../data/stock-v2.json', 'w');
9
-$result = fwrite($fp, json_encode($_POST["v2"]));
10
-fclose($fp);
11
-
12
-$fp = fopen('../data/stock.json', 'w');
13
-$result = fwrite($fp, json_encode($_POST["v1"]));
14
-fclose($fp);
15
-
16
-if ($result) {
17
-    echo 'Réussite !';
18
-} else {
19
-    echo 'Echec!
20
-    '; // Allows to create a newline
21
-    var_dump($_POST);
22
-}
7
+echo json_encode($handler->write_json());

+ 44
- 0
assets/js/index.js View File

@@ -0,0 +1,44 @@
1
+async function sendRequest() {
2
+    let response = await $.ajax({
3
+        type: "POST",
4
+        url: "write_json.php",
5
+    });
6
+    response = JSON.parse(response);
7
+    console.log(response);
8
+    return response["status"];
9
+}
10
+
11
+
12
+$('#uploadButton').on('click', function () {
13
+    $.confirm({
14
+        title: 'Confirmer',
15
+        content: "Voulez vous vraiment mettre en ligne le stock actuel du Proximo ? Il sera visible depuis l'application CAMPUS.",
16
+        type: "orange",
17
+        buttons: {
18
+            formSubmit: {
19
+                text: 'Confirmer',
20
+                btnClass: "btn-warning",
21
+                action: async function () {
22
+                    let result = await sendRequest();
23
+                    console.log(result);
24
+                    if (result !== 0) {
25
+                        $.alert({
26
+                            title: "Erreur",
27
+                            content: "Une erreur est survenue, merci de réessayer plus tard.",
28
+                            type: "red",
29
+                        })
30
+                    } else {
31
+                        $.alert({
32
+                            title: "Succès",
33
+                            content: "Le stock a bien été mis à jour.",
34
+                            type: "green",
35
+                        })
36
+                    }
37
+                }
38
+            },
39
+            cancel: {
40
+                text: 'Annuler',
41
+            }
42
+        }
43
+    });
44
+});

+ 24
- 6
classes/postHandler.php View File

@@ -13,6 +13,7 @@ class PostHandler
13 13
     private $data;
14 14
     private $dao;
15 15
     private $uploadBaseDir = '../uploaded_images/';
16
+    private $stockFile = "../data/stock-v2.json";
16 17
 
17 18
     private $responseArray = array(
18 19
         "status" => 0,
@@ -59,16 +60,31 @@ class PostHandler
59 60
         return $this->responseArray;
60 61
     }
61 62
 
63
+    public function write_json()
64
+    {
65
+        $result = 0;
66
+        $fp = fopen($this->stockFile, "w");
67
+        $array = array(
68
+            "types" => $this->dao->get_categories(),
69
+            "articles" => $this->dao->get_articles(),
70
+        );
71
+        fwrite($fp, json_encode($array));
72
+        fclose($fp);
73
+
74
+        $this->responseArray["data"] = $result;
75
+        return $this->responseArray;
76
+    }
77
+
62 78
     private function save_image()
63 79
     {
64 80
         $success = true;
65 81
         if ($this->filesData["image"]["size"] > 0 && $this->data != null) {
66
-            $uploadPath = $this->uploadBaseDir.$this->data.".jpg";
82
+            $uploadPath = $this->uploadBaseDir . $this->data . ".jpg";
67 83
 
68 84
             if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) {
69 85
                 $this->responseArray["message"] = "Image upload success";
70 86
             } else {
71
-                $this->responseArray["message"] = "Image upload failure: ". $uploadPath;
87
+                $this->responseArray["message"] = "Image upload failure: " . $uploadPath;
72 88
                 $this->responseArray["status"] = 1;
73 89
                 $success = false;
74 90
             }
@@ -80,11 +96,12 @@ class PostHandler
80 96
         if ($success)
81 97
             return 0;
82 98
         else
83
-            return json_encode($this->filesData)."id: ".$this->data;
99
+            return json_encode($this->filesData) . "id: " . $this->data;
84 100
     }
85 101
 
86
-    private function remove_image() {
87
-        $uploadPath = $this->uploadBaseDir.$this->data["id"].".jpg";
102
+    private function remove_image()
103
+    {
104
+        $uploadPath = $this->uploadBaseDir . $this->data["id"] . ".jpg";
88 105
         if (file_exists($uploadPath) && unlink($uploadPath)) {
89 106
             $this->responseArray["message"] = "Success: Deleted image";
90 107
         } else if (!file_exists($uploadPath)) {
@@ -153,7 +170,8 @@ class PostHandler
153 170
         return $result;
154 171
     }
155 172
 
156
-    function updateStock() {
173
+    function updateStock()
174
+    {
157 175
         $result = 0;
158 176
         foreach ($this->data as $row) {
159 177
             $value = $row["value"];

Loading…
Cancel
Save