From 7b1a2da9d54b0a643542fcff4c3110e839305043 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Sat, 16 Nov 2019 23:12:11 +0100 Subject: [PATCH] implemented editing on products and on cupboards --- assets/php/utils.php | 59 +++++++++++++++++++++++++++++++++++++++++--- list-cupboards.php | 43 ++++++++++++++++++++++++++++++-- list-products.php | 55 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 150 insertions(+), 7 deletions(-) diff --git a/assets/php/utils.php b/assets/php/utils.php index aba3f0c..95aa101 100644 --- a/assets/php/utils.php +++ b/assets/php/utils.php @@ -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(); +} diff --git a/list-cupboards.php b/list-cupboards.php index db91cde..36d9ac7 100644 --- a/list-cupboards.php +++ b/list-cupboards.php @@ -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 = "

Something went wrong. Try again later.

"; + } + } else { + $erreur = "

One of the edited section is missing.

"; + } +} + $cupboard_list = ""; foreach (get_users_cupboards_array() as $row) { $cupboard_list = $cupboard_list . "" @@ -21,7 +42,11 @@ foreach (get_users_cupboards_array() as $row) { . "" . htmlspecialchars($row["description"]) . "" - . "
" + . "
" + . "" + . "
" . "\n"; } @@ -46,6 +71,17 @@ foreach (get_users_cupboards_array() as $row) { + +
+ + + +
+ @@ -55,6 +91,9 @@ foreach (get_users_cupboards_array() as $row) { + diff --git a/list-products.php b/list-products.php index bcb161e..045224e 100644 --- a/list-products.php +++ b/list-products.php @@ -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 = "

An error happened.

\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 = "

Something went wrong. Try again later.

"; + } + } else { + $erreur = "

One of the edited section is missing.

"; + } +} + $product_list = ""; foreach (get_users_products_array() as $row) { $product_list = $product_list . "\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) { List products + + + + + + + + + +
Description + Editer + Supprimer
" @@ -25,7 +56,11 @@ foreach (get_users_products_array() as $row) { . "" . htmlspecialchars($row["cupboard_name"] !== NULL ? $row["cupboard_name"] : "-") . "" - . "
" + . "
" + . "
" + . "
" . "
@@ -70,6 +120,9 @@ foreach (get_users_products_array() as $row) { +
Rangement associƩ + Editer + Supprimer