-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
111 lines (74 loc) · 2.53 KB
/
index.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
require "functions.class.php";
require "models/item.model.php";
require "models/connect.php";
// Get all items from inventory table
$items = null;
$data = array();
$action = isset($_GET['action']) ? htmlspecialchars($_GET['action']) : "";
$data['status'] = isset($_GET['status']) ? htmlspecialchars($_GET['status']) : "";
if($conn) {
$items = $conn->get_all_items();
}
// Add item to inventory table in database
if (isset($_POST['add'])) {
$id = "";
$name = trim($_POST['name']);
$description = trim($_POST['description']);
$category = trim($_POST['category']);
$week_price = trim($_POST['week_price']);
$month_price = trim($_POST['month_price']);
$three_month_price = trim($_POST['three_month_price']);
$stock = trim($_POST['stock']);
$asset_number = trim($_POST['asset_number']);
$item = new Item($id, $name, $description, $category, $week_price, $month_price, $three_month_price, $stock, $asset_number);
if($conn) {
$conn->add_item($item);
$data['status'] = 'Successfully added a new product';
}
header('location: index.php?status=Successfully added a new product');
}
// Edit action
if($action == 'edit') {
header("content-type:application/json");
$results= array();
if(isset($_GET['id'])) {
$id = $_GET['id'];
$results = $conn->get_item($id);
echo json_encode($results);
}
exit();
}
// Update action
if(isset($_POST['update'])) {
$item = array(
'id' => trim($_POST['update']),
'pName' => trim($_POST['name']),
'description' => trim($_POST['description']),
'category' => trim($_POST['category']),
'week_price' => trim($_POST['week_price']),
'month_price' => trim($_POST['month_price']),
'three_month_price' => trim($_POST['three_month_price']),
'stock' => trim($_POST['stock']),
'asset_number' => trim($_POST['asset_number'])
);
if ($conn) {
$conn->update_item($item);
header('location: index.php?status=successfully updated item');
} else {
echo "Error: No DB connection";
}
}
// Delete action
if($action == 'delete') {
$id = $_GET['id'];
$conn->delete_item($id);
$data['status'] = "Deleted item: $id";
header("location: index.php?status=deleted item: $id");
}
//Call view class method and pass in view name and data
$data['title'] = 'Inventory';
$data['items'] = $items;
$data['date'] = new DateTime('today');
$data['categories'] = $conn->get_categories();
Functions::view('index', $data);