Browse Source

Added ability to edit article categories, added compatibility conversion between stock json versions

keplyx 2 years ago
parent
commit
4e1ec778c1

+ 87
- 0
admin/categories.php View File

@@ -0,0 +1,87 @@
1
+<?php
2
+$relativePath = "../";
3
+
4
+$file = '../data/stock-v2.json';
5
+$fp = fopen($file, 'r');
6
+$result = fread($fp, filesize($file));
7
+fclose($fp);
8
+
9
+ob_start();
10
+?>
11
+<tr>
12
+    <th class="id-column">Id</th>
13
+    <th class="name-column">Nom</th>
14
+    <th class="icon-column">
15
+        <a href="https://materialdesignicons.com/" target="_blank">
16
+            Icone
17
+            <i class="fas fa-external-link-alt"></i>
18
+        </a>
19
+    </th>
20
+    <th class="actions-column">Actions</th>
21
+</tr>
22
+<?php
23
+$tableHeader = ob_get_clean();
24
+ob_start();
25
+?>
26
+<div class="admin-container">
27
+
28
+    <a href="index.php">
29
+        <button class="btn btn-primary btn-large">Retour</button>
30
+    </a>
31
+
32
+    <h1 class="text-center">Gestion des Catégories</h1>
33
+    <h2 class="text-center">Ajouter une catégorie</h2>
34
+    <table>
35
+        <tbody>
36
+        <?= $tableHeader ?>
37
+        <tr>
38
+            <td>
39
+                <input type="text" class="form-control" id="idInput" placeholder="Id">
40
+            </td>
41
+            <td>
42
+                <input type="text" class="form-control" id="nameInput" placeholder="Nom">
43
+            </td>
44
+            <td>
45
+                <input type="text" class="form-control" id="iconInput" placeholder="Icone">
46
+            </td>
47
+            <td>
48
+                <button type="submit" class="btn btn-success" onclick="addNewItem()"><i class="fas fa-check"></i>
49
+                </button>
50
+            </td>
51
+        </tr>
52
+        </tbody>
53
+    </table>
54
+
55
+    <h2 class="text-center">Liste des catégories</h2>
56
+    <table id="categoriesTable">
57
+        <tbody>
58
+        <?= $tableHeader ?>
59
+        </tbody>
60
+    </table>
61
+
62
+    <div style="display: flex; margin-top: 100px; margin-bottom: 100px">
63
+        <button class="btn btn-success btn-lg" style="margin: auto"
64
+                onclick="saveDataset()">
65
+            Sauvegarder les catégories
66
+        </button>
67
+    </div>
68
+
69
+    <script type="text/javascript">
70
+        let json_dump = <?php echo $result; ?>;
71
+    </script>
72
+
73
+
74
+</>
75
+
76
+<link type="text/css" rel="stylesheet" href="https://cdn.materialdesignicons.com/4.4.95/css/materialdesignicons.min.css"
77
+      media="screen,projection"/>
78
+<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
79
+<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/categories.css" media="screen,projection"/>
80
+
81
+
82
+<?php
83
+$pageContent = ob_get_clean();
84
+$pageTitle = "Gestion des catégories";
85
+$pageScripts = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/saveManager.js\"></script><script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/categories.js\"></script>";
86
+include($relativePath . "includes/template.php");
87
+?>

+ 57
- 0
admin/convert_json.php View File

@@ -0,0 +1,57 @@
1
+<?php
2
+
3
+echo 'Ouverture du fichier V1...
4
+';
5
+$file = '../data/stock.json';
6
+$fp = fopen($file, 'r');
7
+$result = json_decode(fread($fp, filesize($file)));
8
+fclose($fp);
9
+
10
+
11
+function getConvertedTypes($oldTypes) {
12
+    $newTypes = [];
13
+    $counter = 1;
14
+    foreach ($oldTypes as $type) {
15
+        array_push($newTypes, (object) ["id" => strval($counter), "name" => $type, "icon" => "file"]);
16
+        $counter += 1;
17
+    }
18
+    return $newTypes;
19
+}
20
+
21
+function getConvertedArticles($articles, $newTypes) {
22
+    foreach ($articles as $article) {
23
+        $article->description = "Pas de description";
24
+        foreach ($article->type as $key=>$value) {
25
+            $article->type[$key] = getTypeIdFromName($value, $newTypes);
26
+        }
27
+    }
28
+    return $articles;
29
+}
30
+
31
+function getTypeIdFromName($name, $types) {
32
+    foreach ($types as $type) {
33
+        if ($type->name == $name)
34
+            return $type->id;
35
+    }
36
+}
37
+
38
+if ($result) {
39
+    echo 'Conversion en cours...
40
+    ';
41
+    $newTypes = getConvertedTypes($result->types);
42
+    $newArticles = getConvertedArticles($result->articles, $newTypes);
43
+    $v2File = json_encode(["types"=>$newTypes, "articles"=>$newArticles]);
44
+    echo 'Conversion réussie...
45
+    ';
46
+    echo 'Sauvegarde dans fichier V2...
47
+    ';
48
+    $fp = fopen('../data/stock-v2.json', 'w');
49
+    $result = fwrite($fp, $v2File);
50
+    fclose($fp);
51
+    echo 'Sauvegarde réussie...
52
+    ';
53
+} else {
54
+    echo 'Echec!
55
+    '; // Allows to create a newline
56
+    var_dump($_POST);
57
+}

+ 10
- 77
admin/index.php View File

@@ -1,87 +1,20 @@
1 1
 <?php
2 2
 $relativePath = "../";
3
-
4
-$file='../data/stock.json';
5
-$fp = fopen($file, 'r');
6
-$result = fread($fp,filesize($file));
7
-fclose($fp);
8
-
9 3
 ob_start();
10 4
 ?>
11
-<tr>
12
-    <th class="name-column">Nom</th>
13
-    <th class="quantity-column">Quantité</th>
14
-    <th class="price-column">Prix</th>
15
-    <th class="code-column">Code Barre</th>
16
-    <th class="type-column">Type</th>
17
-    <th class="image-column">Image</th>
18
-    <th class="actions-column">Actions</th>
19
-</tr>
20
-<?php
21
-$tableHeader = ob_get_clean();
22
-ob_start();
23
-?>
24
-<div class="stock-container">
25
-
26
-    <h1 class="text-center">Gestion des Stocks</h1>
27
-    <h2 class="text-center">Ajouter un article</h2>
28
-    <table>
29
-        <tbody>
30
-        <?= $tableHeader ?>
31
-        <tr>
32
-            <td>
33
-                <input type="text" class="form-control" id="nameInput" placeholder="Nom">
34
-            </td>
35
-            <td>
36
-                <input type="number" class="form-control" id="quantityInput" placeholder="Quantité">
37
-            </td>
38
-            <td>
39
-                <input type="text" class="form-control" id="priceInput" placeholder="Prix">
40
-            </td>
41
-            <td>
42
-                <input type="text" class="form-control" id="codeInput" placeholder="Code Barre">
43
-            </td>
44
-            <td id="typeCheckboxesCell">
45
-
46
-            </td>
47
-            <td>
48
-                <input type="text" class="form-control" id="imageInput" placeholder="Lien Image">
49
-            </td>
50
-            <td>
51
-                <button type="submit" class="btn btn-success" onclick="addNewItem()"><i class="fas fa-check"></i>
52
-                </button>
53
-            </td>
54
-        </tr>
55
-        </tbody>
56
-    </table>
57
-
58
-    <h2 class="text-center">Liste d'articles</h2>
59
-    <table id="stockTable">
60
-        <tbody>
61
-        <?= $tableHeader ?>
62
-        </tbody>
63
-    </table>
64
-
65
-    <div style="display: flex; margin-top: 100px">
66
-        <button class="btn btn-success btn-lg" style="margin: auto"
67
-                onclick="saveDataset()">
68
-            Sauvegarder le stock
69
-        </button>
70
-    </div>
71
-
72
-    <script type="text/javascript">
73
-        let json_dump = <?php echo $result; ?>;
74
-    </script>
75
-
76
-
77
-</>
78
-
79
-<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/stock.css" media="screen,projection"/>
5
+<div class="admin-container">
6
+    <a href="stock.php">
7
+        <button class="btn btn-primary btn-large">Gestion des stocks</button>
8
+    </a>
9
+    <a href="categories.php">
10
+        <button class="btn btn-primary btn-large">Gestion des catégories</button>
11
+    </a>
12
+</div>
80 13
 
14
+<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
81 15
 
82 16
 <?php
83 17
 $pageContent = ob_get_clean();
84
-$pageTitle = "Gestion";
85
-$pageScripts = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/stock.js\"></script>";
18
+$pageTitle = "Admin";
86 19
 include($relativePath . "includes/template.php");
87 20
 ?>

+ 113
- 0
admin/stock.php View File

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

+ 5
- 1
admin/write_json.php View File

@@ -5,8 +5,12 @@ $_POST = json_decode($rest_json, true);
5 5
 
6 6
 //var_dump($_POST);
7 7
 
8
+$fp = fopen('../data/stock-v2.json', 'w');
9
+$result = fwrite($fp, json_encode($_POST["v2"]));
10
+fclose($fp);
11
+
8 12
 $fp = fopen('../data/stock.json', 'w');
9
-$result = fwrite($fp, json_encode($_POST));
13
+$result = fwrite($fp, json_encode($_POST["v1"]));
10 14
 fclose($fp);
11 15
 
12 16
 if ($result) {

+ 1
- 1
ajax/scan_article.php View File

@@ -11,7 +11,7 @@ if ($_POST["password"] != $password)
11 11
     die("Wrong Password");
12 12
 
13 13
 // open the file and get the stock
14
-$file = '../data/stock.json';
14
+$file = '../data/stock-v2.json';
15 15
 $fp = fopen($file, 'r');
16 16
 $result = json_decode(fread($fp, filesize($file)));
17 17
 fclose($fp);

+ 9
- 0
assets/css/admin.css View File

@@ -0,0 +1,9 @@
1
+.admin-container {
2
+    width: 90%;
3
+    margin: 50px auto auto auto;
4
+}
5
+
6
+table {
7
+    width: 100%;
8
+    margin-bottom: 20px;
9
+}

+ 19
- 0
assets/css/categories.css View File

@@ -0,0 +1,19 @@
1
+#categoriesTable tr {
2
+    border-bottom: 1px solid #dedede;
3
+}
4
+
5
+.mdi {
6
+    font-size: 2rem;
7
+}
8
+
9
+.actions-column {
10
+    width: 100px;
11
+}
12
+
13
+.id-column {
14
+    width: 200px;
15
+}
16
+
17
+.icon-column {
18
+    width: 200px;
19
+}

+ 6
- 8
assets/css/stock.css View File

@@ -1,6 +1,6 @@
1
-table {
2
-    width: 100%;
3
-    margin-bottom: 20px;
1
+
2
+.name-column {
3
+    width: 200px;
4 4
 }
5 5
 
6 6
 .quantity-column, .price-column {
@@ -24,12 +24,10 @@ table {
24 24
     width: 100px;
25 25
 }
26 26
 
27
-.stock-container {
28
-    width: 90%;
29
-    margin: 50px auto auto auto;
30
-}
31
-
32 27
 #stockTable tr {
33 28
     border-bottom: 1px solid #dedede;
34 29
 }
35 30
 
31
+.row-img {
32
+    height: 50px;
33
+}

+ 135
- 0
assets/js/categories.js View File

@@ -0,0 +1,135 @@
1
+let currentDataset = [];
2
+let currentTypes = [];
3
+let originalEditedItem = {};
4
+
5
+function initValuesFromPHPDump() {
6
+    currentDataset = json_dump.articles; // json_dump is set using PHP
7
+    currentTypes = json_dump.types;
8
+    console.log(currentDataset);
9
+    console.log(currentTypes);
10
+    generateTable(currentTypes);
11
+}
12
+
13
+
14
+$(document).ready(function () {
15
+    initValuesFromPHPDump();
16
+    setEditFieldValues('', '', '');
17
+});
18
+
19
+
20
+function generateTable(dataset) {
21
+    for (let i = 0; i < dataset.length; i++) {
22
+        generateLine(dataset[i]);
23
+    }
24
+}
25
+
26
+function generateLine(item) {
27
+    $.selector_cache('#categoriesTable').append(
28
+        '<tr id="row_' + item.id + '">' +
29
+        '<td>' + item.id + '</td>' +
30
+        '<td>' + item.name + '</td>' +
31
+        '<td><span class="mdi mdi-' + item.icon + '"></span></td>' +
32
+        '<td>' +
33
+        '<button class="btn btn-danger" id="delete_' + item.id + '" onclick="deleteItem(this)"><i class="fas fa-times"></i></button>' +
34
+        '<button class="btn btn-primary" id="edit_' + item.id + '" onclick="editItem(this)"><i class="fas fa-edit"></i></button>' +
35
+        '</td>' +
36
+        '</tr>'
37
+    );
38
+}
39
+
40
+function addNewItem() {
41
+    if (isItemInputFilled()) {
42
+        let item = {
43
+            id: $.selector_cache('#idInput').val(),
44
+            name: $.selector_cache('#nameInput').val(),
45
+            icon: $.selector_cache('#iconInput').val(),
46
+        };
47
+        if (isIdAvailable(item.id)) {
48
+            updateExistingItemsType(originalEditedItem.id, item.id);
49
+            setEditFieldValues('', '', '');
50
+            currentTypes.push(item);
51
+            generateLine(item);
52
+        }
53
+    }
54
+}
55
+
56
+function editItem(elem) {
57
+    if (isItemInputEmpty()) {
58
+        let id = elem.id.replace('edit_', '');
59
+        let item = getTypeOfId(id);
60
+        originalEditedItem = item;
61
+        setEditFieldValues(item.id, item.name, item.icon);
62
+        removeItemFromList(item); // Move the item in the edit fields
63
+    }
64
+}
65
+
66
+function deleteItem(elem) {
67
+    let id = elem.id.replace('delete_', '');
68
+    let item = getTypeOfId(id);
69
+    updateExistingItemsType(id, undefined);
70
+    removeItemFromList(item);
71
+}
72
+
73
+function removeItemFromList(item) {
74
+    currentTypes.splice(currentTypes.indexOf(item), 1);
75
+    $('#row_' + item.id).remove();
76
+}
77
+
78
+function setEditFieldValues(id, name, icon) {
79
+    $.selector_cache('#idInput').val(id);
80
+    $.selector_cache('#nameInput').val(name);
81
+    $.selector_cache('#iconInput').val(icon);
82
+}
83
+
84
+function updateExistingItemsType(oldTypeID, newTypeID) {
85
+    for (let i = 0; i < currentDataset.length; i++) {
86
+        for (let k = 0; k < currentDataset[i].type.length; k++) {
87
+            if (currentDataset[i].type[k] === oldTypeID) {
88
+                console.log(newTypeID);
89
+                if (newTypeID !== undefined)
90
+                    currentDataset[i].type[k] = newTypeID;
91
+                else
92
+                    currentDataset[i].type.splice(k, 1);
93
+                break;
94
+            }
95
+        }
96
+    }
97
+}
98
+
99
+function getTypeOfId(id) {
100
+    let item = {};
101
+    for (let i = 0; i < currentTypes.length; i++) {
102
+        if (currentTypes[i].id === id) {
103
+            item = currentTypes[i];
104
+            break;
105
+        }
106
+    }
107
+    return item;
108
+}
109
+
110
+function isIdAvailable(id) {
111
+    let isAvailable = true;
112
+    for (let i = 0; i < currentTypes.length; i++) {
113
+        if (currentTypes[i].id === id) {
114
+            isAvailable = false;
115
+            break;
116
+        }
117
+    }
118
+    return isAvailable;
119
+}
120
+
121
+function isItemInputEmpty() {
122
+    return $.selector_cache('#idInput').val() === '' &&
123
+        $.selector_cache('#nameInput').val() === '' &&
124
+        $.selector_cache('#iconInput').val() === '';
125
+}
126
+
127
+function isItemInputFilled() {
128
+    return $.selector_cache('#idInput').val() !== '' &&
129
+        $.selector_cache('#nameInput').val() !== '' &&
130
+        $.selector_cache('#iconInput').val() !== '';
131
+}
132
+
133
+function saveDataset() {
134
+    SaveManager.saveData(currentTypes, currentDataset);
135
+}

+ 78
- 0
assets/js/saveManager.js View File

@@ -0,0 +1,78 @@
1
+
2
+class SaveManager {
3
+
4
+    static saveData(types, dataset) {
5
+        let finalDataset = {
6
+            v1: SaveManager.generateV1JSON(types, dataset),
7
+            v2: SaveManager.generateV2JSON(types, dataset)
8
+        };
9
+        console.log(finalDataset);
10
+        $.ajax({
11
+            type: "POST",
12
+            url: "write_json.php",
13
+            data: JSON.stringify(finalDataset),
14
+            dataType: "json",
15
+            contentType: "application/json; charset=utf-8",
16
+            complete: function (data) {
17
+                alert(data.responseText);
18
+                console.log(data);
19
+            },
20
+        });
21
+    }
22
+
23
+    static generateV1JSON(savedTypes, savedDataset) {
24
+        let types = [];
25
+        // Replace type objects by names
26
+        for (let i = 0; i < savedTypes.length; i++) {
27
+            types.push(savedTypes[i].name);
28
+        }
29
+        let dataset = JSON.parse(JSON.stringify(savedDataset)); // Deep copy of object
30
+        for (let i = 0; i < dataset.length; i++) {
31
+            // Replace type ids by names
32
+            for (let k = 0; k < dataset[i].type.length; k++) {
33
+                dataset[i].type[k] = SaveManager.getTypeOfId(savedTypes, dataset[i].type[k]).name;
34
+            }
35
+            // remove the description field
36
+            delete dataset[i].description
37
+        }
38
+        return {
39
+            types: types,
40
+            articles: dataset
41
+        }
42
+    }
43
+
44
+    static generateV2JSON(savedTypes, savedDataset) {
45
+        return {
46
+            types: savedTypes,
47
+            articles: savedDataset
48
+        }
49
+    }
50
+
51
+    static getTypeOfId(types, id) {
52
+        let item = {};
53
+        for (let i = 0; i < types.length; i++) {
54
+            if (types[i].id === id) {
55
+                item = types[i];
56
+                break;
57
+            }
58
+        }
59
+        return item;
60
+    }
61
+
62
+    static convertToV2() {
63
+        $.ajax({
64
+            type: "POST",
65
+            url: "convert_json.php",
66
+            dataType: "json",
67
+            contentType: "application/json; charset=utf-8",
68
+            complete: function (data) {
69
+                alert(data.responseText);
70
+                console.log(data);
71
+                window.location.reload();
72
+            },
73
+        });
74
+    }
75
+}
76
+
77
+
78
+

+ 36
- 50
assets/js/stock.js View File

@@ -1,23 +1,3 @@
1
-let defaultTypes = [
2
-    'Nouveau',
3
-    'Alimentaire',
4
-    'Boissons',
5
-    'Utilitaires'
6
-];
7
-
8
-let defaultStock = [
9
-    {
10
-        name: 'Granola',
11
-        quantity: '2',
12
-        price: '1.3',
13
-        code: '7622210601988',
14
-        image : 'https://cookieandkate.com/images/2015/10/granola-mixed-in-bowl.jpg',
15
-        type: [
16
-            'Alimentaire',
17
-        ]
18
-    }
19
-];
20
-
21 1
 let currentDataset = [];
22 2
 let currentTypes = [];
23 3
 
@@ -32,8 +12,11 @@ function initValuesFromPHPDump() {
32 12
 
33 13
 
34 14
 $(document).ready(function () {
35
-    initValuesFromPHPDump();
36
-    generateTypeCheckboxes();
15
+    if (json_dump !== undefined) {
16
+        initValuesFromPHPDump();
17
+        generateTypeCheckboxes();
18
+    }
19
+    setEditFieldValues('', '', '', '', '', '', '');
37 20
 });
38 21
 
39 22
 
@@ -47,11 +30,12 @@ function generateLine(item) {
47 30
     $.selector_cache('#stockTable').append(
48 31
         '<tr id="row_' + item.code + '">' +
49 32
         '<td>' + item.name + '</td>' +
33
+        '<td>' + item.description + '</td>' +
50 34
         '<td>' + item.quantity + '</td>' +
51 35
         '<td>' + item.price + '</td>' +
52 36
         '<td>' + item.code + '</td>' +
53
-        '<td><ul id="typeList_' + item.code + '"></ul></td>' +
54
-        '<td><a href="' + item.image + '" target="_blank">LINK</a></td>' +
37
+        '<td><span id="typeList_' + item.code + '"></span></td>' +
38
+        '<td><a href="' + item.image + '" target="_blank"><img class="row-img" src="' + item.image + '"/></a></td>' +
55 39
         '<td>' +
56 40
         '<button class="btn btn-danger" id="delete_' + item.code + '" onclick="deleteItem(this)"><i class="fas fa-times"></i></button>' +
57 41
         '<button class="btn btn-primary" id="edit_' + item.code + '" onclick="editItem(this)"><i class="fas fa-edit"></i></button>' +
@@ -60,24 +44,37 @@ function generateLine(item) {
60 44
     );
61 45
     // Fill in the type cell
62 46
     for (let i = 0; i < item.type.length; i++) {
47
+        let type = getTypeOfId(item.type[i]);
63 48
         $('#typeList_' + item.code).append(
64
-            '<li>' + item.type[i] + '</li>'
49
+            '<span class="mdi mdi-' + type.icon + '"></span>'
65 50
         );
66 51
     }
67 52
 }
68 53
 
69 54
 function generateTypeCheckboxes() {
70 55
     for (let i = 0; i < currentTypes.length; i++) {
71
-        let id = 'typeCheck_' + currentTypes[i];
56
+        let id = 'typeCheck_' + currentTypes[i].id;
72 57
         $('#typeCheckboxesCell').append(
73 58
             '<div class="form-check">' +
74 59
             '<input type="checkbox" class="form-check-input" id="' + id + '">' +
75
-            '<label class="form-check-label" for="' + id + '">' + currentTypes[i] + '</label>' +
60
+            '<label class="form-check-label" for="' + id + '">' +
61
+            '<span class="mdi mdi-' + currentTypes[i].icon + '"></span>' + currentTypes[i].name + '</label>' +
76 62
             '</div>'
77 63
         );
78 64
     }
79 65
 }
80 66
 
67
+function getTypeOfId(id) {
68
+    let item = {};
69
+    for (let i = 0; i < currentTypes.length; i++) {
70
+        if (currentTypes[i].id === id) {
71
+            item = currentTypes[i];
72
+            break;
73
+        }
74
+    }
75
+    return item;
76
+}
77
+
81 78
 function getItemOfCode(code) {
82 79
     let item = {};
83 80
     for (let i = 0; i < currentDataset.length; i++) {
@@ -93,6 +90,7 @@ function addNewItem() {
93 90
     if (isItemInputFilled()) {
94 91
         let item = {
95 92
             name: $.selector_cache('#nameInput').val(),
93
+            description: $.selector_cache('#descriptionInput').val(),
96 94
             quantity: $.selector_cache('#quantityInput').val(),
97 95
             price: $.selector_cache('#priceInput').val(),
98 96
             code: $.selector_cache('#codeInput').val(),
@@ -100,7 +98,7 @@ function addNewItem() {
100 98
             image: $.selector_cache('#imageInput').val(),
101 99
         };
102 100
         if (isCodeAvailable(item.code)) {
103
-            setEditFieldValues('', '', '', '', [], '');
101
+            setEditFieldValues('', '', '', '', [], '', '');
104 102
             currentDataset.push(item);
105 103
             generateLine(item);
106 104
         }
@@ -111,7 +109,7 @@ function editItem(elem) {
111 109
     if (isItemInputEmpty()) {
112 110
         let code = elem.id.replace('edit_', '');
113 111
         let item = getItemOfCode(code);
114
-        setEditFieldValues(item.name, item.quantity, item.price, item.code, item.type, item.image);
112
+        setEditFieldValues(item.name, item.description, item.quantity, item.price, item.code, item.type, item.image);
115 113
         removeItemFromList(item); // Move the item in the edit fields
116 114
     }
117 115
 }
@@ -130,9 +128,9 @@ function removeItemFromList(item) {
130 128
 function getTypesChecked() {
131 129
     let types = [];
132 130
     for (let i = 0; i < currentTypes.length; i++) {
133
-        let id = 'typeCheck_' + currentTypes[i];
131
+        let id = 'typeCheck_' + currentTypes[i].id;
134 132
         if ($('#' + id).is(':checked')) {
135
-            types.push(currentTypes[i]);
133
+            types.push(currentTypes[i].id);
136 134
         }
137 135
     }
138 136
     return types;
@@ -140,14 +138,15 @@ function getTypesChecked() {
140 138
 
141 139
 function setTypesChecked(types) {
142 140
     for (let i = 0; i < currentTypes.length; i++) {
143
-        let id = 'typeCheck_' + currentTypes[i];
144
-        $('#' + id).prop('checked', types.indexOf(currentTypes[i]) !== -1);
141
+        let id = 'typeCheck_' + currentTypes[i].id;
142
+        $('#' + id).prop('checked', types.indexOf(currentTypes[i].id) !== -1);
145 143
     }
146 144
     return types;
147 145
 }
148 146
 
149
-function setEditFieldValues(name, quantity, price, code, type, image) {
147
+function setEditFieldValues(name, description, quantity, price, code, type, image) {
150 148
     $.selector_cache('#nameInput').val(name);
149
+    $.selector_cache('#descriptionInput').val(description);
151 150
     $.selector_cache('#quantityInput').val(quantity);
152 151
     $.selector_cache('#priceInput').val(price);
153 152
     $.selector_cache('#codeInput').val(code);
@@ -169,6 +168,7 @@ function isCodeAvailable (code) {
169 168
 
170 169
 function isItemInputEmpty() {
171 170
     return $.selector_cache('#nameInput').val() === '' &&
171
+        $.selector_cache('#descriptionInput').val() === '' &&
172 172
         $.selector_cache('#quantityInput').val() === '' &&
173 173
         $.selector_cache('#priceInput').val() === '' &&
174 174
         $.selector_cache('#codeInput').val() === '' &&
@@ -178,6 +178,7 @@ function isItemInputEmpty() {
178 178
 
179 179
 function isItemInputFilled() {
180 180
     return $.selector_cache('#nameInput').val() !== '' &&
181
+        $.selector_cache('#descriptionInput').val() !== '' &&
181 182
         $.selector_cache('#quantityInput').val() !== '' &&
182 183
         $.selector_cache('#priceInput').val() !== '' &&
183 184
         $.selector_cache('#codeInput').val() !== '' &&
@@ -188,22 +189,7 @@ function isItemInputFilled() {
188 189
 
189 190
 
190 191
 function saveDataset() {
191
-    let finalDataset = {
192
-        types: currentTypes,
193
-        articles: currentDataset
194
-    };
195
-    console.log(finalDataset);
196
-    $.ajax({
197
-        type: "POST",
198
-        url: "write_json.php",
199
-        data: JSON.stringify(finalDataset),
200
-        dataType: "json",
201
-        contentType: "application/json; charset=utf-8",
202
-        complete: function (data) {
203
-            alert(data.responseText);
204
-            console.log(data);
205
-        },
206
-    });
192
+    SaveManager.saveData(currentTypes, currentDataset);
207 193
 }
208 194
 
209 195
 

Loading…
Cancel
Save