-
Notifications
You must be signed in to change notification settings - Fork 0
/
productInfo.php
351 lines (277 loc) · 11 KB
/
productInfo.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
session_start();
//error detection
ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', true );
//imports
include 'connect.php';
include('authenticationUser.php');
global $con;
global $user;
//count ALL Products!
$sqlAllProducts = "SELECT COUNT(*) AS count FROM product;";
$resultAllProducts = $con->query($sqlAllProducts);
$rowAP = mysqli_fetch_array($resultAllProducts);
$nrAllProducts = $rowAP['count'];
//get productID from Landing Page
$productID = $_GET['productId'];
//if invalid ProductID -> LANDINGPAGE
if($productID == NULL || $nrAllProducts < $productID) {
header('location: landingPage.php');
}
//DB Calls for Product
$sqlProduct = "SELECT name, price, imgLink, creatorUserID FROM product WHERE id = '$productID'";
$result = $con->query($sqlProduct);
$row = mysqli_fetch_array($result);
$productName = $row['name'];
$productPrice = $row['price'];
$productImgLink = $row['imgLink'];
$productCreatorID = $row['creatorUserID'];
console_log('Product Creator ID: ' . $productCreatorID);
//DB calls for Product Creator Details
$sqlCreator = "SELECT name, email FROM user WHERE id = '$productCreatorID'";
$resultCreator = $con->query($sqlCreator);
$rowC = mysqli_fetch_array($resultCreator);
$productCreatorName = $rowC['name'];
//DB calls for purchased Product
$sqlPurchase = "SELECT COUNT(productID) AS count FROM purchasedBy WHERE productID = '$productID'";
$resultPurchase = $con->query($sqlPurchase);
$rowPur = mysqli_fetch_array($resultPurchase);
$productPurchasedCount = $rowPur['count'];
console_log($productPurchasedCount);
//Verify if loggedInUser is Vendor of this product
$isProductOwner = false;
$isLoggedIn = false;
if(isset($_SESSION['loginsession'])){
$loggedInUserID = $_SESSION['loginsession'];
$isLoggedIn = true;
console_log( 'Logged In User ID: '. $loggedInUserID);
if($productCreatorID == $loggedInUserID)
$isProductOwner = true;
}
//get Name of logged in user
$sqlLoggedIn = "SELECT name FROM user WHERE id = '$loggedInUserID'";
$resultLoggedIn = $con->query($sqlLoggedIn);
$rowLI = mysqli_fetch_array($resultLoggedIn);
$loggedInUserName = $rowLI['name'];
//LOGOUT BUTTON -> only for testing purposes TODO delete this here
if (isset($_POST['logout'])) {
session_destroy();
$isLoggedIn = false;
header('location: login.php');
}
//BACK BUTTON
if(isset($_POST['back'])){
header('location: landingPage.php');
}
//BUY PRODUCT BUTTON
if (isset($_POST['buyProduct'])) {
$date = date('Y-m-d H:i:s');
console_log($date);
if($isLoggedIn){
$sqlBuyProduct = "INSERT INTO purchasedBy (`productID`, `userID`, `buyDate`) VALUES ('$productID', '$loggedInUserID', '$date')";
$result = $con->query($sqlBuyProduct);
if(!$result) {
?>
<script>
window.alert("Purchase Failed");
</script>
<?php
}
else {
?>
<script>
window.alert("Product bought successfully");
</script>
<?php
}
}
console_log('bought product');
header("Refresh:0");
}
//get ALL REVIEWS for ProductID
$sqlReview = "SELECT id, text, replyOfReviewID, userID FROM review WHERE productID = '$productID' ORDER BY replyOfReviewID DESC, id ";
$resultReview = $con->query($sqlReview);
$nrReviews = $resultReview->num_rows;
console_log('result NR: ' . $nrReviews); //TODO delete when done
//get Reviewer User Names
if($nrReviews > 0){
$reviews = [];
while ($row = $resultReview->fetch_assoc()) {
$reviews[] = $row;
}
$ids = array_column($reviews, 'userID');
$reviewerIds = implode(',', $ids);
$reviewUsers = "SELECT name, id FROM user where id in ($reviewerIds)";
$resultAllUserNames = $con->query($reviewUsers);
$allUserIds = [];
$allUserNames = [];
while ($row = $resultAllUserNames->fetch_assoc()) {
$allUserIds[] = $row['id'];
$allUserNames[] = $row['name'];
}
mysqli_data_seek($resultAllUserNames, 0);
$positionArray = [];
$positionNamesArray = [];
$j = 1;
$p = 0;
for($i = 0; $i < count($allUserIds); $i++){
for(;$j <= $allUserIds[count($allUserIds)-1]; $j++){
if($allUserIds[$i] == $j){
$positionArray[$p] = $i;
$positionNamesArray[$p] = $allUserNames[$i];
$j++;
$p++;
break;
}
else {
$positionArray[$j] = 0;
$positionNamesArray[$j-1] = 'null';
$p++;
}
}
}
}
//write Review
if(isset($_POST['writeReview'])){
$text = input($_POST['reviewText']);
$replyOfReviewID = input($_POST['currentReviewID']);
$sqlWriteReview = "INSERT INTO review (`productID`, `userID`, `text`, `replyOfReviewID`) VALUES ('$productID', '$loggedInUserID', '$text', '$replyOfReviewID')";
$result = $con->query($sqlWriteReview);
header("Refresh:0");
}
// submit review FUNCTION
function inputFunction($currentReviewID, $buttonName, $placeholder){
echo "<form action=\"\" method=\"post\" style=\"text-indent:30px;\">";
echo "<input type='hidden' name='currentReviewID' value='$currentReviewID'/>";
echo "<input required type=\"text\" name=\"reviewText\" placeholder=\"$placeholder\" class=\"input-review\">";
echo "<input type=\"submit\" name=\"writeReview\" value=\"$buttonName\" class=\"button\">";
echo "</form>";
}
//chat link or Text
function chatLink($loggedInUserID, $currentUserID, $reviewerName, $productCreatorID){
$setColor = NULL ;
if ($currentUserID == $productCreatorID){
$setColor = '#8282f1';
}
else {
$setColor = '#669986';
}
if ($loggedInUserID == $currentUserID){
echo "$reviewerName";
}
else {
echo "<a style='color: $setColor' href='chat.php?id=$currentUserID'>" . "$reviewerName" ."</a>";
}
}
$con->close();
?>
<!--HTML CODE-->
<!DOCTYPE html>
<html lang="en-us">
<?php include('head.php') ?>
<body>
<?php include('menu.php') ?>
<div class="productInfo">
<!-- DISPLAY PRODUCT-->
<div style="padding: 14px 16px;">
<h1 class="title1"> <?php echo $productName; ?> </h1>
<div class="display-flex">
<img class="imgProductInfo" src="<?php echo $productImgLink; ?>" alt="Product Picture" ><br>
<div class="wrap-text">
Price: <?php echo $productPrice; ?> €<br>
Sold: <?php echo $productPurchasedCount; ?>x<br>
Seller: <?php echo $productCreatorName; ?>
<?php if(!$isProductOwner){ ?>
<a href="chat.php?id=<?php echo $productCreatorID ?>">Start Chat</a><br>
<?php } ?>
<?php if(!$isLoggedIn){ ?>
<p class="textPlain"> login to buy the product</p>
<a href="login.php">To login page</a>
<?php } ?>
<?php if(!$isProductOwner && $isLoggedIn){ ?>
<form action="" method="post">
<input type="submit" name="buyProduct" value="BUY PRODUCT" class="button">
</form>
<?php } ?>
</div>
</div>
</div>
<!-- REVIEWS-->
<div style="padding: 14px 16px;" class="reviews">
<h2 class="title2"> Reviews </h2>
<?php
mysqli_data_seek($resultReview, 0); //resets fetched data pointer to 0
$currentBulkID = -1;
$rowRR = NULL;
if($nrReviews != 0){
$rowRR = mysqli_fetch_array($resultReview);
$currentBulkID = $rowRR['replyOfReviewID'];
}
//NEW REVIEW
if(!$isProductOwner){
echo "<p style=\"color: red\">" . inputFunction($currentBulkID+1, 'Submit Review', 'Add New Review...'). "</p><br/>";
}
//DISPLAY ALL REVIEWS
$x = 0;
while($x < $nrReviews){
//get Review User Name
$reviewerName = $positionNamesArray[$rowRR['userID']-1];
console_log( $x . " name: " . $reviewerName);
//DISPLAY BULK of REVIEWS
if($currentBulkID == $rowRR['replyOfReviewID']){
if($productCreatorID != $rowRR['userID']){
// Display 1st Review
if($x == 0){
echo "<span class='review-indent'>" . "Review by User " . "<i class='reviewer-name'>";
chatLink($loggedInUserID, $rowRR['userID'], $reviewerName, $productCreatorID);
echo "</i> : " . "</span>";
echo "<div class='review-mix' >" . $rowRR['text'] . "</div>";
}
//display next reviews
else{
echo "<div class='review-indent' >" . "<i class='reviewer-name'>";
chatLink($loggedInUserID, $rowRR['userID'], $reviewerName, $productCreatorID);
echo "</i> : ";
echo "<span class='review-text'>" . $rowRR['text'] . "</span> " ."</div>";
}
}
//Display vendor response
else {
echo "<div class='review-indent' >". "Vendor " . "<i class='vendor-name'>";
chatLink($loggedInUserID, $rowRR['userID'], $reviewerName, $productCreatorID);
echo "</i> : " ."<span class='review-text'>" . $rowRR['text'] . "</span> " ."</div>";
}
}
else{
//RESPOND to Review
if(!$isProductOwner){
echo "<p>" . inputFunction($currentBulkID, 'Submit Review', 'Respond to Review...'). "</p>";
}
else {
echo "<p>" . inputFunction($currentBulkID, 'Submit Response', 'Respond to Review...'). "</p>";
}
//DISPLAY NEW BULK
echo "<span class='review-indent'>" . "Review by User " . "<i class='reviewer-name'>";
chatLink($loggedInUserID, $rowRR['userID'], $reviewerName, $productCreatorID);
echo "</i> : " . "</span> ";
echo "<div class='review-mix' >" . $rowRR['text'] . "</div>";
$currentBulkID--;
}
$x++;
$rowRR = mysqli_fetch_array($resultReview);
}
//RESPOND to last Review
if ($nrReviews > 0){
if(!$isProductOwner){
echo "<p>" . inputFunction($currentBulkID, 'Submit Review', 'Respond to Review...'). "</p>";
}
else {
echo "<p>" . inputFunction($currentBulkID, 'Submit Response', 'Respond to Review...'). "</p>";
}
}
?>
</div>
</div>
</body>
</html>