-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory_handler.php
More file actions
90 lines (70 loc) · 3.46 KB
/
inventory_handler.php
File metadata and controls
90 lines (70 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
require("php/util/desc_reader.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST)) {
$product_name = $_POST['product_name'];
$description = $_POST['description'];
$product_price = floatval($_POST['product_price']);
$aisle = strcmp($_POST['aisle'], "") == 0 ? "Fruits" : $_POST['aisle'];
$quantity = intval($_POST['quantity']);
$id = get_unset_id("database/inventory.json");
$img_src = "assets/aisles/" . strtolower($aisle) . "/" . basename($_FILES["productImage"]["name"]);
move_uploaded_file($_FILES["productImage"]["tmp_name"], $img_src);
$products[] = null;
if (file_exists("database/inventory.json")) {
$input = file_get_contents("database/inventory.json", true);
$products = json_decode($input);
}
if (!isset_prod($product_name)) {
$thisProduct = array(
"id" => $id,
"product_name" => $product_name,
"description" => $description,
"img_src" => filesize($_FILES["productImage"]["tmp_name"]) == 0 ? "../assets/aisles/other/default_image.png" : $img_src,
"product_price" => $product_price,
"aisle" => $aisle,
"quantity" => $quantity,
);
//add products to product list, or create product list if doesn't already exist
$products[] = $thisProduct;
$json = json_encode($products);
//write json to file
if (file_put_contents("database/inventory.json", $json)) {
//redirect to selected page after submission
echo "<script type='text/javascript'> document.location = 'backstore/inventory_edit.php?msg=Successfully Added'; </script>";
} else {
echo "Oops! Error creating json file...";
}
} else {
//checks if product already exists, and if it can be edited
//if products isnt empty
if (!is_null($products)) {
//find where product is in the json list
$key = array_search($product_name, array_column($products, 'product_name'));
if (filesize($_FILES["productImage"]["tmp_name"]) == 0)
$thisProduct = array(
"description" => $description,
"product_price" => $product_price,
"aisle" => $aisle,
"quantity" => $quantity,
);
else
$thisProduct = array(
"description" => $description,
"img_src" => $img_src,
"product_price" => $product_price,
"aisle" => $aisle,
"quantity" => $quantity,
);
$replacementArray = array_replace((array)$products[$key], $thisProduct);
$products[$key] = $replacementArray;
$json = json_encode($products);
if (file_put_contents("database/inventory.json", $json)) {
//redirect to selected page after submission
echo "<script type='text/javascript'> document.location = 'backstore/inventory_edit.php?product=" . $product_name . "&msg=Successfully Modified'; </script>";
} else
echo "Oops! Error creating json file...";
}
}
}
}