diff --git a/admin/stock.php b/admin/stock.php index e177e45..fe96e72 100644 --- a/admin/stock.php +++ b/admin/stock.php @@ -1,123 +1,6 @@ get_articles(); -$categories = $dao->get_categories(); - -for ($i = 0; $i < sizeof($stock); $i++) { - $article_categories = $dao->get_article_categories($stock[$i]["id"]); - $stock[$i]["type"] = $article_categories; -} - -ob_start(); -?> - - Nom - Description - Quantité - Prix - Code Barre - Type - Image - - -
- - - - - -

Gestion des Stocks

- -

Ajouter un article

- - - - - - - - - - - - - -
- - - - - - - - - - - - - -
- -

Liste d'articles

- - - - -
- -
- -
- -

Fichier de stock V2 non trouvé

-
- -
- - - - - - - - - - - -"; -include($relativePath . "includes/template.php"); -?> +$pageTitle = "Gestion des articles"; +$script = ""; +include("template.php"); diff --git a/assets/css/categories.css b/assets/css/categories.css index 7257e7e..551fe4f 100644 --- a/assets/css/categories.css +++ b/assets/css/categories.css @@ -5,7 +5,6 @@ } #dataList li { - text-align: center; height: 50px; line-height: 50px; display: flex; @@ -21,6 +20,12 @@ #dataList li div { margin: auto; display: flex; + width: 100%; +} + +#dataList li div span { + margin-right: 5px; + height: 50px; } #listContainer { @@ -31,3 +36,24 @@ .mdi { font-size: 2rem; } + +.list-name { + font-weight: bold; + width: 150px; +} + +.list-description { + width: 200px; + overflow: hidden; +} + +.list-price { + width: 70px; + text-align: center; +} + +.list-code { + color: #8e8e8e; + width: 200px; + text-align: right; +} diff --git a/assets/js/ajaxManager.js b/assets/js/ajaxManager.js index b7e697d..a51d0bb 100644 --- a/assets/js/ajaxManager.js +++ b/assets/js/ajaxManager.js @@ -1,9 +1,9 @@ class AjaxManager { - static async getCategories() { + static async get(type) { let data = { - type: 'category', + type: type, action: 'get', }; let response = await $.ajax({ @@ -17,9 +17,9 @@ class AjaxManager { return response['data']; } - static async deleteCategory(id) { + static async delete(id, type) { let data = { - type: 'category', + type: type, action: 'remove', data: id, }; @@ -34,17 +34,17 @@ class AjaxManager { return response['status']; } - static async saveCategory(category, isNew) { - let data = { - type: 'category', + static async save(data, isNew, type) { + let formattedData = { + type: type, action: isNew ? 'create' : 'update', - data: category, + data: data, }; - + console.log(formattedData); let response = await $.ajax({ type: "POST", url: "save_manager.php", - data: JSON.stringify(data), + data: JSON.stringify(formattedData), dataType: "json", contentType: "application/json; charset=utf-8", }); diff --git a/assets/js/articles.js b/assets/js/articles.js new file mode 100644 index 0000000..a71eeca --- /dev/null +++ b/assets/js/articles.js @@ -0,0 +1,27 @@ +let listManager; + +$(document).ready(function () { + listManager = new ListManager($("#dataList"), 'article', + [ + { + name: 'name', + description: 'Nom', + type: 'text' + }, + { + name: 'description', + description: 'Description', + type: 'text' + }, + { + name: 'price', + description: 'Prix', + type: 'number' + }, + { + name: 'code', + description: 'Code', + type: 'text' + }, + ]); +}); diff --git a/assets/js/listManager.js b/assets/js/listManager.js index 2dcaa5b..d5110d8 100644 --- a/assets/js/listManager.js +++ b/assets/js/listManager.js @@ -3,16 +3,17 @@ class ListManager { currentData = []; displayedData = []; editableTypes = []; + type = ''; + constructor(listContainer, type, editableTypes) { this.listContainer = listContainer; this.editableTypes = editableTypes; - if (type === 'category') { - AjaxManager.getCategories().then((data) => { - this.currentData = data; - this.generateList() - }); - } + this.type = type; + AjaxManager.get(type).then((data) => { + this.currentData = data; + this.generateList() + }); } generateList() { @@ -43,7 +44,7 @@ class ListManager { if (this.editableTypes[i]["type"] === "icon") listItem += ""; else - listItem += item["name"] + ''; + listItem += item[this.editableTypes[i]["name"]] + ''; } listItem += "
"; return listItem; @@ -53,6 +54,7 @@ class ListManager { let formData = '
'; for (let i = 0; i < this.editableTypes.length; i++) { let inputId = this.editableTypes[i]['name'] + 'Input'; + let inputType = this.editableTypes[i]['type'] === 'number' ? 'number' : 'text'; let value = defaultValues[this.editableTypes[i]['name']]; let icon = ''; if (this.editableTypes[i]['name'] === 'icon') { @@ -62,7 +64,7 @@ class ListManager { } } formData += '' + icon + - ''; + ''; } formData += "
"; return formData; @@ -139,7 +141,7 @@ class ListManager { thisInstance.currentData[index] = itemToSave; thisInstance.updateListItem(values, index); } - let id = await AjaxManager.saveCategory(itemToSave, index === -1); + let id = await AjaxManager.save(itemToSave, index === -1, thisInstance.type); if (id >= 0 && index === -1) { itemToSave["id"] = id; thisInstance.currentData[thisInstance.displayedData.length] = itemToSave; @@ -184,7 +186,7 @@ class ListManager { btnClass: 'btn-red', action: async function () { let id = thisInstance.currentData[index]['id']; - let status = await AjaxManager.deleteCategory(id); + let status = await AjaxManager.delete(id, thisInstance.type); if (status === 0) thisInstance.deleteListEntry(id); else diff --git a/classes/dao.php b/classes/dao.php index d7ef8c6..7f2274d 100644 --- a/classes/dao.php +++ b/classes/dao.php @@ -84,4 +84,33 @@ class Dao else return 0; } + + public function create_article($article) { + $sql = 'INSERT INTO articles (name, description, price, code) VALUES (?, ?, ?, ?)'; + $cursor = $this->conn->prepare($sql); + $data = [$article["name"], $article["description"], $article["price"], $article["code"]]; + $cursor->execute($data); + return $this->conn->lastInsertId(); + } + + public function update_article($article) + { + $sql = 'UPDATE articles SET name=?, description=?, price=?, code=? WHERE id=?'; + $cursor = $this->conn->prepare($sql); + $data = [$article["name"], $article["description"], $article["price"], $article["code"], $article["id"]]; + $cursor->execute($data); + return $article["id"]; + } + + public function remove_article($id) + { + $sql = 'DELETE FROM articles WHERE id=?'; + $cursor = $this->conn->prepare($sql); + $data = [$id]; + $result = $cursor->execute($data); + if ($result) + return $cursor->rowCount(); + else + return 0; + } } diff --git a/classes/postHandler.php b/classes/postHandler.php index 2e39d5f..486d70c 100644 --- a/classes/postHandler.php +++ b/classes/postHandler.php @@ -59,7 +59,7 @@ class PostHandler { $result = -1; if ($this->type == "article") { - + $result = $this->dao->create_article($this->data); } else if ($this->type == "category") { $result = $this->dao->create_category($this->data); } else @@ -71,7 +71,7 @@ class PostHandler { $result = -1; if ($this->type == "article") { - + $result = $this->dao->update_article($this->data); } else if ($this->type == "category") { $result = $this->dao->update_category($this->data); } else @@ -83,7 +83,7 @@ class PostHandler { $result = -1; if ($this->type == "article") { - + $result = $this->dao->remove_article($this->data); } else if ($this->type == "category") { $result = $this->dao->remove_category($this->data); } else @@ -98,7 +98,7 @@ class PostHandler { $result = -1; if ($this->type == "article") { - + $result = $this->dao->get_articles(); } else if ($this->type == "category") { $result = $this->dao->get_categories(); } else