Implemented basic article management

This commit is contained in:
keplyx 2020-02-11 15:12:31 +01:00
parent fce5b89657
commit 8a8f202e67
7 changed files with 112 additions and 145 deletions

View file

@ -1,123 +1,6 @@
<?php
$relativePath = "../";
require_once $relativePath.'classes/dao.php';
$dao = new Dao();
$stock = $dao->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();
?>
<tr>
<th class="name-column">Nom</th>
<th class="description-column">Description</th>
<th class="quantity-column">Quantité</th>
<th class="price-column">Prix</th>
<th class="code-column">Code Barre</th>
<th class="type-column">Type</th>
<th class="image-column">Image</th>
</tr>
<?php
$tableHeader = ob_get_clean();
ob_start();
?>
<div class="admin-container">
<a href="index.php">
<button class="btn btn-primary btn-large">Retour</button>
</a>
<h1 class="text-center">Gestion des Stocks</h1>
<?php if ($stock): ?>
<h2 class="text-center">Ajouter un article</h2>
<table>
<tbody>
<?= $tableHeader ?>
<tr>
<td>
<input type="text" class="form-control" id="nameInput" placeholder="Nom">
</td>
<td>
<input type="text" class="form-control" id="descriptionInput" placeholder="Description">
</td>
<td>
<input type="number" class="form-control" id="quantityInput" placeholder="Quantité">
</td>
<td>
<input type="text" class="form-control" id="priceInput" placeholder="Prix">
</td>
<td>
<input type="text" class="form-control" id="codeInput" placeholder="Code Barre">
</td>
<td id="typeCheckboxesCell">
</td>
<td>
<input type="text" class="form-control" id="imageInput" placeholder="Lien Image">
</td>
</tr>
</tbody>
</table>
<h2 class="text-center">Liste d'articles</h2>
<table id="stockTable">
<tbody>
<?= $tableHeader ?>
</tbody>
</table>
<div style="display: flex; margin-top: 100px; margin-bottom: 100px">
<button class="btn btn-success btn-lg" style="margin: auto"
onclick="saveDataset()">
Sauvegarder le stock
</button>
</div>
<?php else: ?>
<h2 class="text-center">Fichier de stock V2 non trouvé</h2>
<div style="display: flex; margin-top: 50px; margin-bottom: 50px">
<button class="btn btn-success btn-lg" style="margin: auto"
onclick="AjaxManager.convertToV2()">
Convertir le stock existant en V2
</button>
</div>
<?php endif; ?>
<script type="text/javascript">
let json_dump = {
types:
<?php
if ($categories)
echo json_encode($categories);
else
echo 'undefined';
?>,
articles:
<?php
if ($stock)
echo json_encode($stock);
else
echo 'undefined';
?>
}
</script>
</>
<link type="text/css" rel="stylesheet" href="https://cdn.materialdesignicons.com/4.4.95/css/materialdesignicons.min.css"
media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/stock.css" media="screen,projection"/>
<?php
$pageContent = ob_get_clean();
$pageTitle = "Gestion du stock";
$pageScripts = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/ajaxManager.js\"></script><script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/stock.js\"></script>";
include($relativePath . "includes/template.php");
?>
$pageTitle = "Gestion des articles";
$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/articles.js\"></script>";
include("template.php");

View file

@ -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;
}

View file

@ -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",
});

27
assets/js/articles.js Normal file
View file

@ -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'
},
]);
});

View file

@ -3,17 +3,18 @@ class ListManager {
currentData = [];
displayedData = [];
editableTypes = [];
type = '';
constructor(listContainer, type, editableTypes) {
this.listContainer = listContainer;
this.editableTypes = editableTypes;
if (type === 'category') {
AjaxManager.getCategories().then((data) => {
this.type = type;
AjaxManager.get(type).then((data) => {
this.currentData = data;
this.generateList()
});
}
}
generateList() {
for (let i = 0; i < this.currentData.length; i++) {
@ -43,7 +44,7 @@ class ListManager {
if (this.editableTypes[i]["type"] === "icon")
listItem += "<i class='mdi mdi-" + item["icon"] + "' style='margin-right: 5px'></i></span>";
else
listItem += item["name"] + '</span>';
listItem += item[this.editableTypes[i]["name"]] + '</span>';
}
listItem += "</div></li>";
return listItem;
@ -53,6 +54,7 @@ class ListManager {
let formData = '<form action="" class="categoryForm"><div class="form-group">';
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 += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>' + icon +
'<input id="' + inputId + '" type="text" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
'<input id="' + inputId + '" type="' + inputType + '" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
}
formData += "</div></form>";
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

View file

@ -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;
}
}

View file

@ -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