Browse Source

Implemented basic article management

keplyx 4 years ago
parent
commit
8a8f202e67
7 changed files with 112 additions and 145 deletions
  1. 3
    120
      admin/stock.php
  2. 27
    1
      assets/css/categories.css
  3. 10
    10
      assets/js/ajaxManager.js
  4. 27
    0
      assets/js/articles.js
  5. 12
    10
      assets/js/listManager.js
  6. 29
    0
      classes/dao.php
  7. 4
    4
      classes/postHandler.php

+ 3
- 120
admin/stock.php View File

@@ -1,123 +1,6 @@
1 1
 <?php
2 2
 $relativePath = "../";
3
-require_once $relativePath.'classes/dao.php';
4 3
 
5
-$dao = new Dao();
6
-
7
-$stock = $dao->get_articles();
8
-$categories = $dao->get_categories();
9
-
10
-for ($i = 0; $i < sizeof($stock); $i++) {
11
-    $article_categories = $dao->get_article_categories($stock[$i]["id"]);
12
-    $stock[$i]["type"] = $article_categories;
13
-}
14
-
15
-ob_start();
16
-?>
17
-<tr>
18
-    <th class="name-column">Nom</th>
19
-    <th class="description-column">Description</th>
20
-    <th class="quantity-column">Quantité</th>
21
-    <th class="price-column">Prix</th>
22
-    <th class="code-column">Code Barre</th>
23
-    <th class="type-column">Type</th>
24
-    <th class="image-column">Image</th>
25
-</tr>
26
-<?php
27
-$tableHeader = ob_get_clean();
28
-ob_start();
29
-?>
30
-<div class="admin-container">
31
-
32
-    <a href="index.php">
33
-        <button class="btn btn-primary btn-large">Retour</button>
34
-    </a>
35
-
36
-    <h1 class="text-center">Gestion des Stocks</h1>
37
-    <?php if ($stock): ?>
38
-    <h2 class="text-center">Ajouter un article</h2>
39
-    <table>
40
-        <tbody>
41
-        <?= $tableHeader ?>
42
-        <tr>
43
-            <td>
44
-                <input type="text" class="form-control" id="nameInput" placeholder="Nom">
45
-            </td>
46
-            <td>
47
-                <input type="text" class="form-control" id="descriptionInput" placeholder="Description">
48
-            </td>
49
-            <td>
50
-                <input type="number" class="form-control" id="quantityInput" placeholder="Quantité">
51
-            </td>
52
-            <td>
53
-                <input type="text" class="form-control" id="priceInput" placeholder="Prix">
54
-            </td>
55
-            <td>
56
-                <input type="text" class="form-control" id="codeInput" placeholder="Code Barre">
57
-            </td>
58
-            <td id="typeCheckboxesCell">
59
-
60
-            </td>
61
-            <td>
62
-                <input type="text" class="form-control" id="imageInput" placeholder="Lien Image">
63
-            </td>
64
-        </tr>
65
-        </tbody>
66
-    </table>
67
-
68
-    <h2 class="text-center">Liste d'articles</h2>
69
-    <table id="stockTable">
70
-        <tbody>
71
-        <?= $tableHeader ?>
72
-        </tbody>
73
-    </table>
74
-
75
-    <div style="display: flex; margin-top: 100px; margin-bottom: 100px">
76
-        <button class="btn btn-success btn-lg" style="margin: auto"
77
-                onclick="saveDataset()">
78
-            Sauvegarder le stock
79
-        </button>
80
-    </div>
81
-    <?php else: ?>
82
-    <h2 class="text-center">Fichier de stock V2 non trouvé</h2>
83
-        <div style="display: flex; margin-top: 50px; margin-bottom: 50px">
84
-            <button class="btn btn-success btn-lg" style="margin: auto"
85
-                    onclick="AjaxManager.convertToV2()">
86
-                Convertir le stock existant en V2
87
-            </button>
88
-        </div>
89
-    <?php endif; ?>
90
-    <script type="text/javascript">
91
-        let json_dump = {
92
-            types:
93
-            <?php
94
-            if ($categories)
95
-                echo json_encode($categories);
96
-            else
97
-                echo 'undefined';
98
-            ?>,
99
-            articles:
100
-            <?php
101
-            if ($stock)
102
-                echo json_encode($stock);
103
-            else
104
-                echo 'undefined';
105
-            ?>
106
-        }
107
-    </script>
108
-
109
-
110
-</>
111
-
112
-<link type="text/css" rel="stylesheet" href="https://cdn.materialdesignicons.com/4.4.95/css/materialdesignicons.min.css"
113
-      media="screen,projection"/>
114
-<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
115
-<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/stock.css" media="screen,projection"/>
116
-
117
-
118
-<?php
119
-$pageContent = ob_get_clean();
120
-$pageTitle = "Gestion du stock";
121
-$pageScripts = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/ajaxManager.js\"></script><script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/stock.js\"></script>";
122
-include($relativePath . "includes/template.php");
123
-?>
4
+$pageTitle = "Gestion des articles";
5
+$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/articles.js\"></script>";
6
+include("template.php");

+ 27
- 1
assets/css/categories.css View File

@@ -5,7 +5,6 @@
5 5
 }
6 6
 
7 7
 #dataList li {
8
-    text-align: center;
9 8
     height: 50px;
10 9
     line-height: 50px;
11 10
     display: flex;
@@ -21,6 +20,12 @@
21 20
 #dataList li div {
22 21
     margin: auto;
23 22
     display: flex;
23
+    width: 100%;
24
+}
25
+
26
+#dataList li div span {
27
+    margin-right: 5px;
28
+    height: 50px;
24 29
 }
25 30
 
26 31
 #listContainer {
@@ -31,3 +36,24 @@
31 36
 .mdi {
32 37
     font-size: 2rem;
33 38
 }
39
+
40
+.list-name {
41
+    font-weight: bold;
42
+    width: 150px;
43
+}
44
+
45
+.list-description {
46
+    width: 200px;
47
+    overflow: hidden;
48
+}
49
+
50
+.list-price {
51
+    width: 70px;
52
+    text-align: center;
53
+}
54
+
55
+.list-code {
56
+    color: #8e8e8e;
57
+    width: 200px;
58
+    text-align: right;
59
+}

+ 10
- 10
assets/js/ajaxManager.js View File

@@ -1,9 +1,9 @@
1 1
 
2 2
 class AjaxManager {
3 3
 
4
-    static async getCategories() {
4
+    static async get(type) {
5 5
         let data = {
6
-            type: 'category',
6
+            type: type,
7 7
             action: 'get',
8 8
         };
9 9
         let response = await $.ajax({
@@ -17,9 +17,9 @@ class AjaxManager {
17 17
         return response['data'];
18 18
     }
19 19
 
20
-    static async deleteCategory(id) {
20
+    static async delete(id, type) {
21 21
         let data = {
22
-            type: 'category',
22
+            type: type,
23 23
             action: 'remove',
24 24
             data: id,
25 25
         };
@@ -34,17 +34,17 @@ class AjaxManager {
34 34
         return response['status'];
35 35
     }
36 36
 
37
-    static async saveCategory(category, isNew) {
38
-        let data = {
39
-            type: 'category',
37
+    static async save(data, isNew, type) {
38
+        let formattedData = {
39
+            type: type,
40 40
             action: isNew ? 'create' : 'update',
41
-            data: category,
41
+            data: data,
42 42
         };
43
-
43
+        console.log(formattedData);
44 44
         let response = await $.ajax({
45 45
             type: "POST",
46 46
             url: "save_manager.php",
47
-            data: JSON.stringify(data),
47
+            data: JSON.stringify(formattedData),
48 48
             dataType: "json",
49 49
             contentType: "application/json; charset=utf-8",
50 50
         });

+ 27
- 0
assets/js/articles.js View File

@@ -0,0 +1,27 @@
1
+let listManager;
2
+
3
+$(document).ready(function () {
4
+    listManager = new ListManager($("#dataList"), 'article',
5
+        [
6
+            {
7
+                name: 'name',
8
+                description: 'Nom',
9
+                type: 'text'
10
+            },
11
+            {
12
+                name: 'description',
13
+                description: 'Description',
14
+                type: 'text'
15
+            },
16
+            {
17
+                name: 'price',
18
+                description: 'Prix',
19
+                type: 'number'
20
+            },
21
+            {
22
+                name: 'code',
23
+                description: 'Code',
24
+                type: 'text'
25
+            },
26
+        ]);
27
+});

+ 12
- 10
assets/js/listManager.js View File

@@ -3,16 +3,17 @@ class ListManager {
3 3
     currentData = [];
4 4
     displayedData = [];
5 5
     editableTypes = [];
6
+    type = '';
7
+
6 8
 
7 9
     constructor(listContainer, type, editableTypes) {
8 10
         this.listContainer = listContainer;
9 11
         this.editableTypes = editableTypes;
10
-        if (type === 'category') {
11
-            AjaxManager.getCategories().then((data) => {
12
-                this.currentData = data;
13
-                this.generateList()
14
-            });
15
-        }
12
+        this.type = type;
13
+        AjaxManager.get(type).then((data) => {
14
+            this.currentData = data;
15
+            this.generateList()
16
+        });
16 17
     }
17 18
 
18 19
     generateList() {
@@ -43,7 +44,7 @@ class ListManager {
43 44
             if (this.editableTypes[i]["type"] === "icon")
44 45
                 listItem += "<i class='mdi mdi-" + item["icon"] + "' style='margin-right: 5px'></i></span>";
45 46
             else
46
-                listItem += item["name"] + '</span>';
47
+                listItem += item[this.editableTypes[i]["name"]] + '</span>';
47 48
         }
48 49
         listItem += "</div></li>";
49 50
         return listItem;
@@ -53,6 +54,7 @@ class ListManager {
53 54
         let formData = '<form action="" class="categoryForm"><div class="form-group">';
54 55
         for (let i = 0; i < this.editableTypes.length; i++) {
55 56
             let inputId = this.editableTypes[i]['name'] + 'Input';
57
+            let inputType = this.editableTypes[i]['type'] === 'number' ? 'number' : 'text';
56 58
             let value = defaultValues[this.editableTypes[i]['name']];
57 59
             let icon = '';
58 60
             if (this.editableTypes[i]['name'] === 'icon') {
@@ -62,7 +64,7 @@ class ListManager {
62 64
                 }
63 65
             }
64 66
             formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>' + icon +
65
-                '<input id="' + inputId + '" type="text" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
67
+                '<input id="' + inputId + '" type="' + inputType + '" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
66 68
         }
67 69
         formData += "</div></form>";
68 70
         return formData;
@@ -139,7 +141,7 @@ class ListManager {
139 141
                             thisInstance.currentData[index] = itemToSave;
140 142
                             thisInstance.updateListItem(values, index);
141 143
                         }
142
-                        let id = await AjaxManager.saveCategory(itemToSave, index === -1);
144
+                        let id = await AjaxManager.save(itemToSave, index === -1, thisInstance.type);
143 145
                         if (id >= 0 && index === -1) {
144 146
                             itemToSave["id"] = id;
145 147
                             thisInstance.currentData[thisInstance.displayedData.length] = itemToSave;
@@ -184,7 +186,7 @@ class ListManager {
184 186
                     btnClass: 'btn-red',
185 187
                     action: async function () {
186 188
                         let id = thisInstance.currentData[index]['id'];
187
-                        let status = await AjaxManager.deleteCategory(id);
189
+                        let status = await AjaxManager.delete(id, thisInstance.type);
188 190
                         if (status === 0)
189 191
                             thisInstance.deleteListEntry(id);
190 192
                         else

+ 29
- 0
classes/dao.php View File

@@ -84,4 +84,33 @@ class Dao
84 84
         else
85 85
             return 0;
86 86
     }
87
+
88
+    public function create_article($article) {
89
+        $sql = 'INSERT INTO articles (name, description, price, code) VALUES (?, ?, ?, ?)';
90
+        $cursor = $this->conn->prepare($sql);
91
+        $data = [$article["name"], $article["description"], $article["price"], $article["code"]];
92
+        $cursor->execute($data);
93
+        return $this->conn->lastInsertId();
94
+    }
95
+
96
+    public function update_article($article)
97
+    {
98
+        $sql = 'UPDATE articles SET name=?, description=?, price=?, code=? WHERE id=?';
99
+        $cursor = $this->conn->prepare($sql);
100
+        $data = [$article["name"], $article["description"], $article["price"], $article["code"], $article["id"]];
101
+        $cursor->execute($data);
102
+        return $article["id"];
103
+    }
104
+
105
+    public function remove_article($id)
106
+    {
107
+        $sql = 'DELETE FROM articles WHERE id=?';
108
+        $cursor = $this->conn->prepare($sql);
109
+        $data = [$id];
110
+        $result = $cursor->execute($data);
111
+        if ($result)
112
+            return $cursor->rowCount();
113
+        else
114
+            return 0;
115
+    }
87 116
 }

+ 4
- 4
classes/postHandler.php View File

@@ -59,7 +59,7 @@ class PostHandler
59 59
     {
60 60
         $result = -1;
61 61
         if ($this->type == "article") {
62
-
62
+            $result = $this->dao->create_article($this->data);
63 63
         } else if ($this->type == "category") {
64 64
             $result = $this->dao->create_category($this->data);
65 65
         } else
@@ -71,7 +71,7 @@ class PostHandler
71 71
     {
72 72
         $result = -1;
73 73
         if ($this->type == "article") {
74
-
74
+            $result = $this->dao->update_article($this->data);
75 75
         } else if ($this->type == "category") {
76 76
             $result = $this->dao->update_category($this->data);
77 77
         } else
@@ -83,7 +83,7 @@ class PostHandler
83 83
     {
84 84
         $result = -1;
85 85
         if ($this->type == "article") {
86
-
86
+            $result = $this->dao->remove_article($this->data);
87 87
         } else if ($this->type == "category") {
88 88
             $result = $this->dao->remove_category($this->data);
89 89
         } else
@@ -98,7 +98,7 @@ class PostHandler
98 98
     {
99 99
         $result = -1;
100 100
         if ($this->type == "article") {
101
-
101
+            $result = $this->dao->get_articles();
102 102
         } else if ($this->type == "category") {
103 103
             $result = $this->dao->get_categories();
104 104
         } else

Loading…
Cancel
Save