-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
181 lines (160 loc) · 5.42 KB
/
index.php
File metadata and controls
181 lines (160 loc) · 5.42 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
// Makes PHP behave in a more strict way
declare(strict_types=1);
// Enable error messages
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Enable sessions
session_start();
// Add classes
require "php/Product.php";
require "php/Order.php";
require "php/product-list.php";
// Enable overview of these variables
//function whatIsHappening()
//{
// var_dump("<pre>");
// echo '<h2>$_GET</h2>';
// var_dump($_GET);
// echo '<h2>$_POST</h2>';
// var_dump($_POST);
// echo '<h2>$_COOKIE</h2>';
// var_dump($_COOKIE);
// echo '<h2>$_SESSION</h2>';
// var_dump($_SESSION);
// echo '<h2>$_SERVER</h2>';
// var_dump($_SERVER["REQUEST_URI"]);
// var_dump("</pre>");
//}
//whatIsHappening();
//
// List products
$products1 = [
$product1,
$product2,
$product3,
$product4,
$product5,
];
$products2 = [
$product6,
$product7,
$product8,
$product9,
$product10,
];
$totalValue = 0;
if (isset($_GET['products1'])) {
$uselessProductsSelected = true;
$products = $products1;
} else {
$uselessProductsSelected = false;
$products = $products2;
}
// Validate submitted field values
function validate(): array
{
// Create and return invalid fields array
$invalidFields = [];
if (empty($_POST["email"])) {
array_push($invalidFields, "email");
}
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
array_push($invalidFields, "emailInvalid");
}
if (empty($_POST["street"])) {
array_push($invalidFields, "street");
}
if (empty($_POST["streetnumber"])) {
array_push($invalidFields, "streetnumber");
}
if (empty($_POST["city"])) {
array_push($invalidFields, "city");
}
if (empty($_POST["zipcode"])) {
array_push($invalidFields, "zipcode");
}
if (!is_numeric($_POST["zipcode"])) {
array_push($invalidFields, "zipcodeInvalid");
}
return $invalidFields;
}
function handleForm($products, &$totalValue): array
{
// Validation (return invalidly submitted fields)
$invalidFields = validate();
// Handle errors caused by invalidly submitted fields
if (!empty($invalidFields)) {
if (in_array("email", $invalidFields)) {
$errorMsg = "Please fill out your e-mail address.";
$errorMsg .= "<br>";
} // Check if email address is valid
elseif (in_array("emailInvalid", $invalidFields)) {
$errorMsg = "Invalid e-mail format.";
$errorMsg .= "<br>";
}
if (in_array("street", $invalidFields)) {
$errorMsg = "Please fill out your street.";
$errorMsg .= "<br>";
}
if (in_array("streetnumber", $invalidFields)) {
$errorMsg = "Please fill out your street number.";
$errorMsg .= "<br>";
}
if (in_array("city", $invalidFields)) {
$errorMsg = "Please fill out your city.";
$errorMsg .= "<br>";
}
if (in_array("zipcode", $invalidFields)) {
$errorMsg = "Please fill out your zip code.";
$errorMsg .= "<br>";
} // Check if zip code consists of only numbers
elseif (in_array("zipcodeInvalid", $invalidFields)) {
$errorMsg = "Zip code can only have numeric values.";
}
// Display any empty or invalid data with corresponding error message
return [
"order" => null,
"message" => "<div class='alert alert-danger'>" . $errorMsg . "</div>",
];
} else {
// Loop through product arrays
$productNumbers = array_keys($_POST["products"]);
$orderedProducts = [];
foreach ($productNumbers as $productNumber) {
$orderedProducts[] = $products[$productNumber];
}
// Set address data
$order = new Order ($_POST["email"], $_POST["street"], (int)$_POST["streetnumber"], (int)$_POST["zipcode"], $_POST["city"], $orderedProducts);
// Save data in session on submit to keep it displayed after error message
$_SESSION["email"] = $order->getEmail();
$_SESSION["street"] = $order->getStreet();
$_SESSION["streetnumber"] = $order->getStreetNumber();
$_SESSION["city"] = $order->getCity();
$_SESSION["zipcode"] = $order->getZipCode();
// Return selected products and address data
return [
"order" => $order,
"message" => "<div class='alert alert-success'>" . $order->confirmationMsg() . "</div>",
];
}
}
// Check if form is not empty when submitted
$formSubmitted = !empty($_POST);
$confirmationMsg = [];
if ($formSubmitted) {
$result = handleForm($products, $totalValue);
$confirmationMsg = $result["message"];
$order = $result["order"];
}
// Includes and evaluates the specified file
require "php/form-view.php";
//Nice-to-have features
//TODO Show the expected delivery time in the confirmation message (2h by default).
//TODO A user can opt for express delivery (5$ for delivery in 45min).
//TODO Show statistics about how much money has been spent. This info should be kept (can you use the session or cookies for this?) when the browser closes.
//TODO Include the most popular product (by this user) and amount of products bought by this user.
//TODO Add a color schema and a suitable font
//TODO improve validation with html and JS
//TODO Allow user to specify how much he or she wants to buy of a certain products