2019-11-10 16:04:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once("./assets/php/utils.php");
|
|
|
|
|
2019-11-17 23:44:38 +01:00
|
|
|
$erreur = $edit_id = $edit_name = $edit_description = $edit_expiration = $edit_cupboard = "";
|
2019-11-16 23:12:11 +01:00
|
|
|
|
2019-11-10 16:04:46 +01:00
|
|
|
if (!is_connected()) {
|
|
|
|
header("location: login.php");
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:41:10 +01:00
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete"])) {
|
|
|
|
if (!delete_product($_POST["delete"])) {
|
|
|
|
$erreur = "<p>An error happened.</p>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 23:12:11 +01:00
|
|
|
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"];
|
2019-11-17 23:44:38 +01:00
|
|
|
$edit_cupboard = $product["cupboard_public_id"];
|
2019-11-16 23:12:11 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-17 23:44:38 +01:00
|
|
|
if ($edit_id === "") {
|
|
|
|
$erreur = "<p>Unknown product.</>p>";
|
|
|
|
}
|
|
|
|
|
|
|
|
$cupboard_list = "";
|
|
|
|
foreach (get_users_cupboards_array() as $row) {
|
|
|
|
$cupboard_list = $cupboard_list . "<option value=\""
|
|
|
|
. htmlspecialchars($row["public_id"]) . "\"";
|
|
|
|
if ($row["public_id"] === $edit_cupboard) $cupboard_list = $cupboard_list . " selected ";
|
|
|
|
$cupboard_list = $cupboard_list . ">"
|
|
|
|
. htmlspecialchars($row["name"]) . "</option>\n";
|
|
|
|
}
|
2019-11-16 23:12:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["edit_completed"])) {
|
2019-11-17 23:44:38 +01:00
|
|
|
if (isset($_POST["name"]) && isset($_POST["description"]) && isset($_POST["expiration"]) && isset($_POST["cupboard"])) {
|
|
|
|
$cupboard_id = null;
|
|
|
|
foreach (get_users_cupboards_array() as $cupboards) {
|
|
|
|
if ($cupboards["public_id"] === $_POST["cupboard"]) $cupboard_id = $cupboards["id"];
|
|
|
|
}
|
2019-11-17 17:00:54 +01:00
|
|
|
if (!update_product(
|
|
|
|
$_POST["edit_completed"],
|
|
|
|
$_POST["name"],
|
|
|
|
$_POST["description"],
|
2019-11-17 23:44:38 +01:00
|
|
|
empty(trim($_POST["expiration"])) ? null : $_POST["expiration"],
|
|
|
|
$cupboard_id
|
2019-11-17 17:00:54 +01:00
|
|
|
)) {
|
2019-11-16 23:12:11 +01:00
|
|
|
$erreur = "<p>Something went wrong. Try again later.</p>";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$erreur = "<p>One of the edited section is missing.</p>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 16:04:46 +01:00
|
|
|
$product_list = "";
|
|
|
|
foreach (get_users_products_array() as $row) {
|
2019-11-11 14:53:30 +01:00
|
|
|
$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"] : "-")
|
2019-11-12 10:41:10 +01:00
|
|
|
. "</td><td>"
|
2019-11-17 17:00:54 +01:00
|
|
|
. "<form method='post'><button type='publish' name='edit' value='"
|
2019-11-16 23:12:11 +01:00
|
|
|
. $row["public_id"] . "'>Editer</button></form>"
|
|
|
|
. "</td><td>"
|
2019-11-17 17:00:54 +01:00
|
|
|
. "<form method='post'><button type='publish' name='delete' value='"
|
2019-11-16 23:12:11 +01:00
|
|
|
. $row["public_id"] . "'>Delete</button></form>"
|
2019-11-11 14:53:30 +01:00
|
|
|
. "</td><tr>\n";
|
2019-11-10 16:04:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
2019-11-11 14:53:30 +01:00
|
|
|
|
2019-11-10 16:04:46 +01:00
|
|
|
<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">
|
2019-11-11 14:53:30 +01:00
|
|
|
<style type="text/css">
|
|
|
|
table,
|
|
|
|
th,
|
|
|
|
td {
|
|
|
|
border: 1px solid #333;
|
|
|
|
}
|
2019-11-16 23:12:11 +01:00
|
|
|
|
2019-11-11 14:53:30 +01:00
|
|
|
td {
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
</style>
|
2019-11-10 16:04:46 +01:00
|
|
|
<title>List products</title>
|
|
|
|
</head>
|
2019-11-11 14:53:30 +01:00
|
|
|
|
2019-11-16 23:12:11 +01:00
|
|
|
|
2019-11-10 16:04:46 +01:00
|
|
|
<body>
|
2019-11-16 23:12:11 +01:00
|
|
|
<?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; ?>">
|
2019-11-16 23:21:20 +01:00
|
|
|
<label>Expiration : </label><input type="date" name="expiration" value="<?php echo $edit_expiration; ?>">
|
2019-11-17 23:44:38 +01:00
|
|
|
<label>Cupboard:</label>
|
|
|
|
<select name="cupboard">
|
|
|
|
<option value=""></option>
|
|
|
|
<?php echo $cupboard_list; ?>
|
|
|
|
</select>
|
2019-11-16 23:12:11 +01:00
|
|
|
<button type="publish" name="edit_completed" value="<?php echo $edit_id; ?>">Valider</button>
|
|
|
|
</form>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
?>
|
2019-11-11 14:53:30 +01:00
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>
|
|
|
|
Nom
|
|
|
|
</th>
|
|
|
|
<th>
|
|
|
|
Description
|
|
|
|
</th>
|
|
|
|
<th>
|
|
|
|
Ajouté le
|
|
|
|
</th>
|
|
|
|
<th>
|
|
|
|
Expiration
|
|
|
|
</th>
|
|
|
|
<th>
|
|
|
|
Rangement associé
|
|
|
|
</th>
|
2019-11-16 23:12:11 +01:00
|
|
|
<th>
|
|
|
|
Editer
|
|
|
|
</th>
|
2019-11-12 10:41:10 +01:00
|
|
|
<th>
|
|
|
|
Supprimer
|
|
|
|
</th>
|
2019-11-11 14:53:30 +01:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<?php echo $product_list; ?>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2019-11-10 16:04:46 +01:00
|
|
|
</body>
|
2019-11-11 14:53:30 +01:00
|
|
|
|
|
|
|
</html>
|