Implemented basic article management
This commit is contained in:
parent
fce5b89657
commit
8a8f202e67
7 changed files with 112 additions and 145 deletions
123
admin/stock.php
123
admin/stock.php
|
@ -1,123 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
$relativePath = "../";
|
$relativePath = "../";
|
||||||
require_once $relativePath.'classes/dao.php';
|
|
||||||
|
|
||||||
$dao = new Dao();
|
$pageTitle = "Gestion des articles";
|
||||||
|
$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/articles.js\"></script>";
|
||||||
$stock = $dao->get_articles();
|
include("template.php");
|
||||||
$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");
|
|
||||||
?>
|
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
#dataList li {
|
#dataList li {
|
||||||
text-align: center;
|
|
||||||
height: 50px;
|
height: 50px;
|
||||||
line-height: 50px;
|
line-height: 50px;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
@ -21,6 +20,12 @@
|
||||||
#dataList li div {
|
#dataList li div {
|
||||||
margin: auto;
|
margin: auto;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
#dataList li div span {
|
||||||
|
margin-right: 5px;
|
||||||
|
height: 50px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#listContainer {
|
#listContainer {
|
||||||
|
@ -31,3 +36,24 @@
|
||||||
.mdi {
|
.mdi {
|
||||||
font-size: 2rem;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
|
|
||||||
class AjaxManager {
|
class AjaxManager {
|
||||||
|
|
||||||
static async getCategories() {
|
static async get(type) {
|
||||||
let data = {
|
let data = {
|
||||||
type: 'category',
|
type: type,
|
||||||
action: 'get',
|
action: 'get',
|
||||||
};
|
};
|
||||||
let response = await $.ajax({
|
let response = await $.ajax({
|
||||||
|
@ -17,9 +17,9 @@ class AjaxManager {
|
||||||
return response['data'];
|
return response['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
static async deleteCategory(id) {
|
static async delete(id, type) {
|
||||||
let data = {
|
let data = {
|
||||||
type: 'category',
|
type: type,
|
||||||
action: 'remove',
|
action: 'remove',
|
||||||
data: id,
|
data: id,
|
||||||
};
|
};
|
||||||
|
@ -34,17 +34,17 @@ class AjaxManager {
|
||||||
return response['status'];
|
return response['status'];
|
||||||
}
|
}
|
||||||
|
|
||||||
static async saveCategory(category, isNew) {
|
static async save(data, isNew, type) {
|
||||||
let data = {
|
let formattedData = {
|
||||||
type: 'category',
|
type: type,
|
||||||
action: isNew ? 'create' : 'update',
|
action: isNew ? 'create' : 'update',
|
||||||
data: category,
|
data: data,
|
||||||
};
|
};
|
||||||
|
console.log(formattedData);
|
||||||
let response = await $.ajax({
|
let response = await $.ajax({
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: "save_manager.php",
|
url: "save_manager.php",
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(formattedData),
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
contentType: "application/json; charset=utf-8",
|
contentType: "application/json; charset=utf-8",
|
||||||
});
|
});
|
||||||
|
|
27
assets/js/articles.js
Normal file
27
assets/js/articles.js
Normal 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'
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
|
@ -3,17 +3,18 @@ class ListManager {
|
||||||
currentData = [];
|
currentData = [];
|
||||||
displayedData = [];
|
displayedData = [];
|
||||||
editableTypes = [];
|
editableTypes = [];
|
||||||
|
type = '';
|
||||||
|
|
||||||
|
|
||||||
constructor(listContainer, type, editableTypes) {
|
constructor(listContainer, type, editableTypes) {
|
||||||
this.listContainer = listContainer;
|
this.listContainer = listContainer;
|
||||||
this.editableTypes = editableTypes;
|
this.editableTypes = editableTypes;
|
||||||
if (type === 'category') {
|
this.type = type;
|
||||||
AjaxManager.getCategories().then((data) => {
|
AjaxManager.get(type).then((data) => {
|
||||||
this.currentData = data;
|
this.currentData = data;
|
||||||
this.generateList()
|
this.generateList()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
generateList() {
|
generateList() {
|
||||||
for (let i = 0; i < this.currentData.length; i++) {
|
for (let i = 0; i < this.currentData.length; i++) {
|
||||||
|
@ -43,7 +44,7 @@ class ListManager {
|
||||||
if (this.editableTypes[i]["type"] === "icon")
|
if (this.editableTypes[i]["type"] === "icon")
|
||||||
listItem += "<i class='mdi mdi-" + item["icon"] + "' style='margin-right: 5px'></i></span>";
|
listItem += "<i class='mdi mdi-" + item["icon"] + "' style='margin-right: 5px'></i></span>";
|
||||||
else
|
else
|
||||||
listItem += item["name"] + '</span>';
|
listItem += item[this.editableTypes[i]["name"]] + '</span>';
|
||||||
}
|
}
|
||||||
listItem += "</div></li>";
|
listItem += "</div></li>";
|
||||||
return listItem;
|
return listItem;
|
||||||
|
@ -53,6 +54,7 @@ class ListManager {
|
||||||
let formData = '<form action="" class="categoryForm"><div class="form-group">';
|
let formData = '<form action="" class="categoryForm"><div class="form-group">';
|
||||||
for (let i = 0; i < this.editableTypes.length; i++) {
|
for (let i = 0; i < this.editableTypes.length; i++) {
|
||||||
let inputId = this.editableTypes[i]['name'] + 'Input';
|
let inputId = this.editableTypes[i]['name'] + 'Input';
|
||||||
|
let inputType = this.editableTypes[i]['type'] === 'number' ? 'number' : 'text';
|
||||||
let value = defaultValues[this.editableTypes[i]['name']];
|
let value = defaultValues[this.editableTypes[i]['name']];
|
||||||
let icon = '';
|
let icon = '';
|
||||||
if (this.editableTypes[i]['name'] === 'icon') {
|
if (this.editableTypes[i]['name'] === 'icon') {
|
||||||
|
@ -62,7 +64,7 @@ class ListManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>' + icon +
|
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>";
|
formData += "</div></form>";
|
||||||
return formData;
|
return formData;
|
||||||
|
@ -139,7 +141,7 @@ class ListManager {
|
||||||
thisInstance.currentData[index] = itemToSave;
|
thisInstance.currentData[index] = itemToSave;
|
||||||
thisInstance.updateListItem(values, index);
|
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) {
|
if (id >= 0 && index === -1) {
|
||||||
itemToSave["id"] = id;
|
itemToSave["id"] = id;
|
||||||
thisInstance.currentData[thisInstance.displayedData.length] = itemToSave;
|
thisInstance.currentData[thisInstance.displayedData.length] = itemToSave;
|
||||||
|
@ -184,7 +186,7 @@ class ListManager {
|
||||||
btnClass: 'btn-red',
|
btnClass: 'btn-red',
|
||||||
action: async function () {
|
action: async function () {
|
||||||
let id = thisInstance.currentData[index]['id'];
|
let id = thisInstance.currentData[index]['id'];
|
||||||
let status = await AjaxManager.deleteCategory(id);
|
let status = await AjaxManager.delete(id, thisInstance.type);
|
||||||
if (status === 0)
|
if (status === 0)
|
||||||
thisInstance.deleteListEntry(id);
|
thisInstance.deleteListEntry(id);
|
||||||
else
|
else
|
||||||
|
|
|
@ -84,4 +84,33 @@ class Dao
|
||||||
else
|
else
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ class PostHandler
|
||||||
{
|
{
|
||||||
$result = -1;
|
$result = -1;
|
||||||
if ($this->type == "article") {
|
if ($this->type == "article") {
|
||||||
|
$result = $this->dao->create_article($this->data);
|
||||||
} else if ($this->type == "category") {
|
} else if ($this->type == "category") {
|
||||||
$result = $this->dao->create_category($this->data);
|
$result = $this->dao->create_category($this->data);
|
||||||
} else
|
} else
|
||||||
|
@ -71,7 +71,7 @@ class PostHandler
|
||||||
{
|
{
|
||||||
$result = -1;
|
$result = -1;
|
||||||
if ($this->type == "article") {
|
if ($this->type == "article") {
|
||||||
|
$result = $this->dao->update_article($this->data);
|
||||||
} else if ($this->type == "category") {
|
} else if ($this->type == "category") {
|
||||||
$result = $this->dao->update_category($this->data);
|
$result = $this->dao->update_category($this->data);
|
||||||
} else
|
} else
|
||||||
|
@ -83,7 +83,7 @@ class PostHandler
|
||||||
{
|
{
|
||||||
$result = -1;
|
$result = -1;
|
||||||
if ($this->type == "article") {
|
if ($this->type == "article") {
|
||||||
|
$result = $this->dao->remove_article($this->data);
|
||||||
} else if ($this->type == "category") {
|
} else if ($this->type == "category") {
|
||||||
$result = $this->dao->remove_category($this->data);
|
$result = $this->dao->remove_category($this->data);
|
||||||
} else
|
} else
|
||||||
|
@ -98,7 +98,7 @@ class PostHandler
|
||||||
{
|
{
|
||||||
$result = -1;
|
$result = -1;
|
||||||
if ($this->type == "article") {
|
if ($this->type == "article") {
|
||||||
|
$result = $this->dao->get_articles();
|
||||||
} else if ($this->type == "category") {
|
} else if ($this->type == "category") {
|
||||||
$result = $this->dao->get_categories();
|
$result = $this->dao->get_categories();
|
||||||
} else
|
} else
|
||||||
|
|
Loading…
Reference in a new issue