-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
122 lines (105 loc) · 3.5 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
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace App;
include('vendor/autoload.php');
use App\Controller;
session_start ();
$controller = new Controller();
$action = isset($_GET['action']) ? $_GET['action'] : 'none';
if ($action == 'add_item' && isset($_POST['item_name'])) {
$itemName = $_POST['item_name'];
$result = $controller->add_item($itemName);
if ($result == false){
$_SESSION['message'] = 'Item added failed :(';
$_SESSION['message_type'] = 'fail';
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
$_SESSION['message'] = 'Item added successfully!';
$_SESSION['message_type'] = 'alert';
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
if ($action == 'delete_item' && isset($_POST['item_id'])) {
$itemId = $_GET['id'];
$controller->delete_item($itemId);
$_SESSION['message'] = 'Item deleted successfully!';
$_SESSION['message_type'] = 'alert';
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
if ($action == 'edit_item' && isset($_POST['item_new_name'])) {
$itemIdString = $_GET['id'];
$itemId = (int)$itemIdString;
$newName = $_POST['item_new_name'];
$controller->edit_item($itemId, $newName);
$_SESSION['message'] = 'Item edited successfully!';
$_SESSION['message_type'] = 'alert';
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
if ($action == 'delete_all' && isset($_POST['delete_all_items'])) {
$result = $controller->delete_all();
$_SESSION['message'] = 'All items deleted successfully!';
$_SESSION['message_type'] = 'alert';
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
if ($action == 'change_item_status_1' && isset($_POST['change_item_status'])) {
$itemIdString = $_GET['id'];
$itemId = (int)$itemIdString;
$result = $controller->change_status_true($itemId);
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
if ($action == 'change_item_status_0' && isset($_POST['change_item_status'])) {
$itemIdString = $_GET['id'];
$itemId = (int)$itemIdString;
$result = $controller->change_status_false($itemId);
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./src/styles/styles.css">
<title>Shopping List</title>
</head>
<body>
<?php
if(isset($_SESSION ["message"])) {
?>
<div class="alert alert-<?= $_SESSION["message_type"];?>">
<p class="message"><?php echo $_SESSION["message"]?></p>
</div>
<?php
session_unset();
}
?>
<header>
<h1><a href="index.php">Shopping List</a></h1>
</header>
<div class="main_footer">
<main>
<div class="itemNameForm">
<form action="index.php?action=add_item" method="POST">
<input class="newItemInput" type="text" name="item_name" placeholder="Enter item name...">
<!-- <input type="submit" name="submitNewItem" class="cta"> -->
</form>
</div>
<div class="itemList">
<?php
echo $controller->read_list();
?>
</div>
</main>
<footer>
<form action="index.php?action=delete_all" class="footerForm" method="POST">
<button type="submit" name="delete_all_items" class="cta">Delete all items</button>
</form>
</footer>
</div>
</body>
</html>