food-inventory/list-products.php

130 lines
3.8 KiB
PHP

<?php
require_once("./assets/php/utils.php");
$erreur = $edit_id = $edit_name = $edit_description = $edit_expiration = "";
if (!is_connected()) {
header("location: login.php");
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete"])) {
if (!delete_product($_POST["delete"])) {
$erreur = "<p>An error happened.</p>\n";
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["edit"])) {
if (isset($_POST["edit"]))
foreach (get_users_products_array() as $product) {
if ($product["public_id"] === $_POST["edit"]) {
$edit_id = $_POST["edit"];
$edit_name = $product["name"];
$edit_description = $product["description"];
$edit_expiration = $product["expiration_date"];
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["edit_completed"])) {
if (isset($_POST["name"]) && isset($_POST["description"]) && isset($_POST["expiration"])) {
if (!update_product($_POST["edit_completed"], $_POST["name"], $_POST["description"], empty(trim($_POST["expiration"])) ? null : $_POST["expiration"])) {
$erreur = "<p>Something went wrong. Try again later.</p>";
}
} else {
$erreur = "<p>One of the edited section is missing.</p>";
}
}
$product_list = "";
foreach (get_users_products_array() as $row) {
$product_list = $product_list . "<tr><td>"
. htmlspecialchars($row["name"])
. "</td><td>"
. htmlspecialchars($row["description"])
. "</td><td>"
. htmlspecialchars($row["added_date"])
. "</td><td>"
. htmlspecialchars($row["expiration_date"] !== NULL ? $row["expiration_date"] : "-")
. "</td><td>"
. htmlspecialchars($row["cupboard_name"] !== NULL ? $row["cupboard_name"] : "-")
. "</td><td>"
. "<form method='post'><button type='publish' name='edit' value='"
. $row["public_id"] . "'>Editer</button></form>"
. "</td><td>"
. "<form method='post'><button type='publish' name='delete' value='"
. $row["public_id"] . "'>Delete</button></form>"
. "</td><tr>\n";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style type="text/css">
table,
th,
td {
border: 1px solid #333;
}
td {
text-align: center;
}
</style>
<title>List products</title>
</head>
<body>
<?php echo $erreur; ?>
<?php
if ($edit_id !== "") {
?>
<form method="post">
<label>Nom : </label><input type="text" name="name" value="<?php echo $edit_name; ?>">
<label>Description : </label><input type="text" name="description" value="<?php echo $edit_description; ?>">
<label>Expiration : </label><input type="date" name="expiration" value="<?php echo $edit_expiration; ?>">
<button type="publish" name="edit_completed" value="<?php echo $edit_id; ?>">Valider</button>
</form>
<?php
}
?>
<table>
<thead>
<tr>
<th>
Nom
</th>
<th>
Description
</th>
<th>
Ajouté le
</th>
<th>
Expiration
</th>
<th>
Rangement associé
</th>
<th>
Editer
</th>
<th>
Supprimer
</th>
</tr>
</thead>
<tbody>
<?php echo $product_list; ?>
</tbody>
</table>
</body>
</html>