-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlot.php
82 lines (63 loc) · 2.11 KB
/
lot.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
<?php
require_once 'init.php';
/** @var string $userName */
/** @var mysqli $dbConnection */
/** @var int|string $userId */
/** @var array $categories */
$lotId = getLotIdFromQueryParams($dbConnection);
$lot = getLotById($dbConnection, $lotId);
$remainingTime = calculatesRemainingTime($lot["ended_at"]);
$hours = $remainingTime[0];
$minutes = $remainingTime[1];
$class = ($hours < 1) ? 'timer--finishing' : '';
$lotPrices = calculateLotPrices($lot);
$currentPrice = $lotPrices['current_price'];
$minRate = $lotPrices['min_rate'];
$isLotOwner = (int) $lot['author_id'] === $userId;
$isLastRateByUser = lastRateUser($dbConnection, $lotId) === $userId;
$errors = [];
handleEndedAuction($dbConnection, $lotId);
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['cost'])) {
if (!$userId) {
http_response_code(403);
exit('Вы должны войти, чтобы делать ставки.');
}
$rateValue = trim($_POST['cost']);
$lotPrices = calculateLotPrices($lot);
$minRate = $lotPrices['min_rate'];
$lastRateUserId = lastRateUser($dbConnection, $lotId);
$error = validateRate($rateValue, $minRate, $userId, $lastRateUserId);
if ($error) {
$errors['cost'] = $error;
} else {
addRate($dbConnection, $userId, $lotId, (int) $rateValue);
header("Location: lot.php?id=$lotId");
exit();
}
}
$rates = getLotRates($dbConnection, $lotId);
$content = includeTemplate('lot.php', [
'categories' => $categories,
'userName' => $userName,
'lot' => $lot,
'hours' => $hours,
'minutes' => $minutes,
'class' => $class,
'currentPrice' => $currentPrice,
'minRate' => $minRate,
'errors' => $errors,
'lotId' => $lotId,
'isAuctionEnded' => strtotime($lot['ended_at']) < time(),
'isLotOwner' => $isLotOwner,
'isLastRateByUser' => $isLastRateByUser,
'rates' => $rates,
]);
$lotTitle = $lot['title'];
$layoutContent = includeTemplate('layout.php', [
'content' => $content,
'title' => $lotTitle,
'userName' => $userName,
'categories' => $categories,
'pagination' => '',
]);
print($layoutContent);