-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreviews.php
268 lines (239 loc) · 9.7 KB
/
reviews.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
// require resource: Connection Object
require_once __DIR__ . DIRECTORY_SEPARATOR . 'dbSource.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'dbController.php';
$connection = new DbConnection($serverName = "localhost", $userName = "root", $password = "", $database = "hotelreservation");
$conn = $connection->getConnection();
$operation = new DatabaseTableOperations($conn);
$tableFields = ['reviews.name', 'reviews.rating', 'reviews.reviews', 'reviews.datecreated'];
$joins = [
[
'type' => 'INNER',
'table' => 'services',
],
[
'type' => 'INNER',
'table' => 'bookings',
],
];
$joinConditions = ['reviews.serviceID = services.serviceID', 'reviews.email = bookings.email'];
$validreviews = $operation->retrieveTableReport("reviews", $tableFields, $joins, $joinConditions);
// ***************************************** Function: Validating and Cleaning Userinputs ******************************************
function validate_name(string $name): bool
{
/**
* @param string $name
* returns bool
*/
$name = ucwords(trim(htmlspecialchars(($name))));
return preg_match('/^[a-zA-Z ]+$/', $name); // returns true if argument matches regex pattern
}
function validate_email(string $email): bool
{
/**
* @param string $email
* returns bool
*/
return filter_var(htmlspecialchars($email), FILTER_VALIDATE_EMAIL); // returns true if argument is a valid email address
}
function validate_serviceType(string $serviceType): bool
{
/**
* @param string $serviceType
* returns bool
*/
$serviceType = trim(htmlspecialchars($serviceType)); // remove whitespaces and language tags
$validserviceType = ["RM", "EH", "CR", "PP"]; // define an array of allowed values
return in_array($serviceType, $validserviceType); // returns true if argument is in allowed list of values
}
function clean_review(string $review): string
{
/**
* @param string $review
* return string
*/
if (is_string($review)) {
$review = ucfirst(trim(htmlspecialchars($review))); // remove whitespaces and language tags
return $review;
}
$review = "";
return $review;
}
// ***************************************** Form Handling ******************************************
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve form field values
$name = clean_review($_POST['customerName']);
$email = $_POST['customerEmail'];
$serviceType = $_POST['serviceType'];
$rating = $_POST['reviewRating'];
$review = clean_review($_POST['reviewText']);
// Validate and Clean Userinputs
$errors = [];
if (!validate_name($name))
$errors[] = "Invalid Name! Please enter a valid name without numbers or meta-characters.";
if (!validate_email($email))
$errors[] = "Invalid Email! Please enter a valid email address.";
if (!validate_serviceType($serviceType))
$errors[] = "Invalid! Selection doesn't exist.";
// Check for Errors and redirect to reviews.php with error message
if (!empty($errors)) {
$errorMessage = implode("", $errors);
$address = "Location: reviews.php?reviewErrorMessage=" . urlencode($errorMessage);
// ob_end_flush();
header($address);
exit();
}
// Create an array of values for each customer review
$newReview = array(
"name" => $name,
"email" => $email,
"serviceID" => $serviceType,
"rating" => (int)$rating,
"review" => $review
);
$status = $operation->createRecords("reviews", $newReview); // Add review to database
if ($status === true) {
// Redirect to reviews.php with success message
$successMessage = "Thanks for the feedback! We hope to continue putting a smile on your face.";
$address = "Location: reviews.php?reviewSuccessMessage=" . urlencode($successMessage);
// ob_end_flush();
header($address);
exit();
}
$errorMessage = "Error! Please try again.";
$address = "Location: reviews.php?reviewErrorMessage=" . urlencode($errorMessage);
// ob_end_flush();
header($address);
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Review</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
<link rel="stylesheet" href="reviewstyles.css" />
</head>
<body class="container-fluid">
<div class="mt-4 mb-0">
<header>
<div class=" left-container">
<img src="img/water.png" alt="" />
<!-- <a href=" https://www.flaticon.com/free-icons/architecture-and-city" - Flaticon></a>" -->
</div>
<div class="right-container">
<div>
<form method="post" action="">
<input type="text" class="rounded" id="userId" name="userId" placeholder="Enter email" autocomplete="off" />
<input type="password" class="rounded" id="userPassword" name="password" placeholder="Enter password" />
<button type="submit" class=" btn btn-sm btn-primary rounded">
Log in
</button>
</form>
</div>
</div>
</header>
<!-- Check Review Success and Error Alert -->
<?php
if (isset($_GET['reviewSuccessMessage'])) {
$successMessage = $_GET['reviewSuccessMessage'];
echo '<div class="alert alert-success">' . $successMessage . '</div>';
}
?>
<?php
if (isset($_GET['reviewErrorMessage'])) {
$errorMessage = $_GET['reviewErrorMessage'];
echo '<div class="alert alert-danger">' . $errorMessage . '</div>';
}
?>
<main>
<div class="row">
<div id="reviewSubmission" class="leftform-container">
<h4>Add Review</h4>
<span class="welcome-note"><em>All reviews and ratings are valid and are from people who have
used our reservation service</em></span>
<hr />
<form id="reviewForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label for="customerName"><strong>Name:</strong></label>
<input type="text" class="form-control" id="customerName" name="customerName" autocomplete="off" placeholder="Please enter your name" />
</div>
<div class="form-group">
<label for="customerEmail"><strong>Email:</strong></label>
<input type="email" class="form-control" id="customerEmail" name="customerEmail" autocomplete="off" placeholder="Please enter your email address" />
</div>
<div class="form-group">
<label for="serviceType"><strong>Select Reservation Type:</strong></label>
<select class="form-control" id="serviceType" name="serviceType">
<option value="">--Click to Select--</option>
<option value="RM">Room</option>
<option value="EH">Event Hall</option>
<option value="CR">Conference Room</option>
<option value="PP">Private Pool</option>
</select>
</div>
<div class="form-row">
<div class="left-container">
<label><strong>Review:</strong></label>
</div>
<div class="right-container">
<div class="star-rating">
<i class="star far fa-star" data-rating-value="1"></i>
<i class="star far fa-star" data-rating-value="2"></i>
<i class="star far fa-star" data-rating-value="3"></i>
<i class="star far fa-star" data-rating-value="4"></i>
<i class="star far fa-star" data-rating-value="5"></i>
<input type="hidden" name="reviewRating" id="reviewRating" value="" />
</div>
</div>
</div>
<div class="form-group">
<textarea class="form-control" name="reviewText" rows="3" autocomplete="off" placeholder="Kindly describe your reservation experience" maxlength="150"></textarea>
</div>
<button type="submit" class="btn btn-sm btn-primary">Submit</button>
</form>
</div>
<div class="rightform-container">
<h4>Reservation Ratings & Reviews</h4>
<div id="reviewReport">
<!-- Review report will be dynamically generated here -->
<?php
if (!empty($validreviews)) {
foreach ($validreviews as $review) {
?>
<div class="review-card">
<div class="review-header">
<div class="review-name"><?php echo $review["name"]; ?></div>
<div class="review-rating"><strong>Rating:</strong> <?php echo $review["rating"]; ?>/5</div>
<div class="review-date"><strong>Date:</strong> <?php echo isset($review["dateupdated"]) ? $review["dateupdated"] : $review["datecreated"]; ?></div>
</div>
<div class="review-text"><?php echo $review["reviews"]; ?></div>
</div>
<?php
}
} else {
echo "<p>No reviews found.</p>";
}
?>
</div>
</div>
</div>
</main>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$(".star").click(function() {
var rating = $(this).data("rating-value");
$("#reviewRating").val(rating);
$(".star").removeClass("fas").addClass("far"); // Reset all stars to white
$(this).prevAll(".star").removeClass("far").addClass("fas"); // Change previous stars to gold
$(this).removeClass("far").addClass("fas"); // Change clicked star to gold
});
});
</script>
</body>
</html>