implemented editing on products and on cupboards

This commit is contained in:
Louis Vallat 2019-11-16 23:12:11 +01:00
parent 0b1b7f43df
commit 7b1a2da9d5
3 changed files with 150 additions and 7 deletions

View File

@ -319,8 +319,8 @@ function get_users_cupboards_array()
}
function delete_cupboard($cupboard_public_id)
{
global $PDO;
{
global $PDO;
$sql = "DELETE cupboards
FROM cupboards
@ -337,8 +337,8 @@ function delete_cupboard($cupboard_public_id)
}
function delete_product($product_public_id)
{
global $PDO;
{
global $PDO;
$sql = "DELETE products
FROM products
@ -353,3 +353,54 @@ function delete_product($product_public_id)
return $query->execute();
}
function update_product(
$product_public_id,
$new_name,
$new_description,
$new_expiration_date
) {
global $PDO;
$sql = "UPDATE products
INNER JOIN accounts
ON products.owner_id = accounts.id
SET products.name = :new_name,
products.description = :new_description,
products.expiration_date = :new_expiration_date
WHERE products.public_id = :id
AND products.owner_id = :owner_id;";
$query = $PDO->prepare($sql);
$query->bindValue(":new_name", $new_name);
$query->bindValue(":new_description", $new_description);
$query->bindValue(":new_expiration_date", $new_expiration_date);
$query->bindValue(":id", $product_public_id);
$query->bindValue(":owner_id", get_user_info_from_session_id("id"));
return $query->execute();
}
function update_cupboard(
$cupboard_public_id,
$new_name,
$new_description
) {
global $PDO;
$sql = "UPDATE cupboards
INNER JOIN accounts
ON cupboards.owner_id = accounts.id
SET cupboards.name = :new_name,
cupboards.description = :new_description
WHERE cupboards.public_id = :id
AND cupboards.owner_id = :owner_id;";
$query = $PDO->prepare($sql);
$query->bindValue(":new_name", $new_name);
$query->bindValue(":new_description", $new_description);
$query->bindValue(":id", $cupboard_public_id);
$query->bindValue(":owner_id", get_user_info_from_session_id("id"));
return $query->execute();
}

View File

@ -6,7 +6,7 @@ if (!is_connected()) {
header("location: login.php");
}
$erreur = "";
$erreur = $edit_id = $edit_name = $edit_description = "";
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete"])) {
if (!delete_cupboard($_POST["delete"])) {
@ -14,6 +14,27 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete"])) {
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["edit"])) {
if (isset($_POST["edit"]))
foreach (get_users_cupboards_array() as $cupboard) {
if ($cupboard["public_id"] === $_POST["edit"]) {
$edit_id = $_POST["edit"];
$edit_name = $cupboard["name"];
$edit_description = $cupboard["description"];
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["edit_completed"])) {
if (isset($_POST["name"]) && isset($_POST["description"])) {
if (!update_cupboard($_POST["edit_completed"], $_POST["name"], $_POST["description"])) {
$erreur = "<p>Something went wrong. Try again later.</p>";
}
} else {
$erreur = "<p>One of the edited section is missing.</p>";
}
}
$cupboard_list = "";
foreach (get_users_cupboards_array() as $row) {
$cupboard_list = $cupboard_list . "<tr><td>"
@ -21,7 +42,11 @@ foreach (get_users_cupboards_array() as $row) {
. "</td><td>"
. htmlspecialchars($row["description"])
. "</td><td>"
. "<form method='post'><button type='publish' name='delete' value='" . $row["public_id"] . "'>Delete</button></form>"
. "<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";
}
@ -46,6 +71,17 @@ foreach (get_users_cupboards_array() as $row) {
<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; ?>">
<button type="publish" name="edit_completed" value="<?php echo $edit_id; ?>">Valider</button>
</form>
<?php
}
?>
<table>
<thead>
<tr>
@ -55,6 +91,9 @@ foreach (get_users_cupboards_array() as $row) {
<th>
Description
</th>
<th>
Editer
</th>
<th>
Supprimer
</th>

View File

@ -2,16 +2,47 @@
require_once("./assets/php/utils.php");
$erreur = $edit_id = $edit_name = $edit_description = $edit_expiration = "";
if (!is_connected()) {
header("location: login.php");
}
/**
* $product_public_id,
* $new_name,
* $new_description,
* $new_expiration_date
*/
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"], $_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>"
@ -25,7 +56,11 @@ foreach (get_users_products_array() as $row) {
. "</td><td>"
. htmlspecialchars($row["cupboard_name"] !== NULL ? $row["cupboard_name"] : "-")
. "</td><td>"
. "<form method='post'><button type='publish' name='delete' value='" . $row["public_id"] . "'>Delete</button></form>"
. "<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";
}
@ -44,6 +79,7 @@ foreach (get_users_products_array() as $row) {
td {
border: 1px solid #333;
}
td {
text-align: center;
}
@ -51,7 +87,21 @@ foreach (get_users_products_array() as $row) {
<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="calendar" 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>
@ -70,6 +120,9 @@ foreach (get_users_products_array() as $row) {
<th>
Rangement associé
</th>
<th>
Editer
</th>
<th>
Supprimer
</th>