-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
226 lines (197 loc) · 7.33 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
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
<?php
require_once 'vendor/autoload.php';
require_once 'config.php';
$dsn = "mysql:host=" . $_ENV['DB_HOST'] . ";dbname=" . $_ENV['DB_NAME'] . ";charset=utf8mb4";
$username = $_ENV['DB_USER'];
$password = $_ENV['DB_PASS'];
try {
$pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
$loader = new \Twig\Loader\FilesystemLoader('./templates/');
$twig = new \Twig\Environment($loader);
$twig->addGlobal('isCategoryPage', false);
$uri = $_SERVER['REQUEST_URI'];
$base_url = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
$twig->addGlobal('base_url', $base_url);
// RINGS
$query = "SELECT * FROM rings";
$statement = $pdo->prepare($query);
$statement->execute();
$ringsData = $statement->fetchAll(PDO::FETCH_ASSOC);
shuffle($ringsData);
$query = "SELECT * FROM ring_images";
$statement = $pdo->prepare($query);
$statement->execute();
$ringImages = $statement->fetchAll(PDO::FETCH_ASSOC);
$ringImagesByRingId = [];
foreach ($ringImages as $image) {
$ringId = $image['ring_id'];
if (!isset($ringImagesByRingId[$ringId])) {
$ringImagesByRingId[$ringId] = [];
}
$ringImagesByRingId[$ringId][] = $image;
}
foreach ($ringsData as &$ring) {
$ringId = $ring['id'];
$ring['images'] = isset($ringImagesByRingId[$ringId]) ? $ringImagesByRingId[$ringId] : [];
}
// MEN JEWELRY
$query = "SELECT * FROM men_jewelry";
$statement = $pdo->prepare($query);
$statement->execute();
$menJewelryData = $statement->fetchAll(PDO::FETCH_ASSOC);
shuffle($menJewelryData);
$query = "SELECT * FROM men_jewelry_images";
$statement = $pdo->prepare($query);
$statement->execute();
$menJewelryImages = $statement->fetchAll(PDO::FETCH_ASSOC);
$menJewelryImagesByItemId = [];
foreach ($menJewelryImages as $image) {
$jewelryId = $image['jewelry_id'];
if (!isset($menJewelryImagesByItemId[$jewelryId])) {
$menJewelryImagesByItemId[$jewelryId] = [];
}
$menJewelryImagesByItemId[$jewelryId][] = $image;
}
foreach ($menJewelryData as &$item) {
$jewelryId = $item['id'];
$item['images'] = isset($menJewelryImagesByItemId[$jewelryId]) ? $menJewelryImagesByItemId[$jewelryId] : [];
}
// EARRINGS
$query = "SELECT * FROM earrings";
$statement = $pdo->prepare($query);
$statement->execute();
$earringsData = $statement->fetchAll(PDO::FETCH_ASSOC);
shuffle($earringsData);
$query = "SELECT * FROM earrings_images";
$statement = $pdo->prepare($query);
$statement->execute();
$earringsImages = $statement->fetchAll(PDO::FETCH_ASSOC);
$earringsImagesByItemId = [];
foreach ($earringsImages as $image) {
$earringId = $image['earring_id'];
if (!isset($earringsImagesByItemId[$earringId])) {
$earringsImagesByItemId[$earringId] = [];
}
$earringsImagesByItemId[$earringId][] = $image;
}
foreach ($earringsData as &$item) {
$earringId = $item['id'];
$item['images'] = isset($earringsImagesByItemId[$earringId]) ? $earringsImagesByItemId[$earringId] : [];
}
// STORES
$query = "SELECT * FROM stores";
$statement = $pdo->prepare($query);
$statement->execute();
$storesData = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($uri === $base_url.'/') {
echo $twig->render('index.html.twig', [
'ringsData' => $ringsData,
'ringImages' => $ringImages,
'menJewelryData' => $menJewelryData,
'menJewelryImages' => $menJewelryImages,
'earringsData' => $earringsData,
'earringsImages' => $earringsImages
]);
} else if ($uri === $base_url.'/rings') {
echo $twig->render('rings.html.twig', [
'ringsData' => $ringsData,
'ringImages' => $ringImages,
]);
} else if ($uri === $base_url.'/men-jewelry') {
echo $twig->render('men-jewelry.html.twig', [
'menJewelryData' => $menJewelryData,
'menJewelryImages' => $menJewelryImages,
]);
} else if ($uri === $base_url.'/earrings') {
echo $twig->render('earrings.html.twig', [
'earringsData' => $earringsData,
'earringsImages' => $earringsImages,
]);
} elseif (preg_match('#^'.$base_url.'/category/rings/(\d+)$#', $uri, $matches)) {
$itemId = $matches[1];
$query = "SELECT * FROM rings WHERE id = :id";
$statement = $pdo->prepare($query);
$statement->bindParam(':id', $itemId, PDO::PARAM_INT);
$statement->execute();
$itemData = $statement->fetch(PDO::FETCH_ASSOC);
$query = "SELECT * FROM ring_images WHERE ring_id = $itemId";
$statement = $pdo->prepare($query);
$statement->execute();
$ringImages = $statement->fetchAll(PDO::FETCH_ASSOC);
$ringImagesByRingId = [];
foreach ($ringImages as $image) {
$ringId = $image['ring_id'];
if (!isset($ringImagesByRingId[$ringId])) {
$ringImagesByRingId[$ringId] = [];
}
$ringImagesByRingId[$ringId][] = $image;
}
$twig->addGlobal('isCategoryPage', true);
echo $twig->render('single/item.html.twig', [
'category' => 'ring',
'itemData' => $itemData,
'images' => $ringImages,
'relatedData' => $ringsData,
]);
} elseif (preg_match('#^'.$base_url.'/category/men-jewelry/(\d+)$#', $uri, $matches)) {
$itemId = $matches[1];
$query = "SELECT * FROM men_jewelry WHERE id = :id";
$statement = $pdo->prepare($query);
$statement->bindParam(':id', $itemId, PDO::PARAM_INT);
$statement->execute();
$itemData = $statement->fetch(PDO::FETCH_ASSOC);
$query = "SELECT * FROM men_jewelry_images WHERE jewelry_id = $itemId";
$statement = $pdo->prepare($query);
$statement->execute();
$menJewelryImages = $statement->fetchAll(PDO::FETCH_ASSOC);
$menJewelryImagesByItemId = [];
foreach ($menJewelryImages as $image) {
$jewelryId = $image['jewelry_id'];
if (!isset($menJewelryImagesByItemId[$jewelryId])) {
$menJewelryImagesByItemId[$jewelryId] = [];
}
$menJewelryImagesByItemId[$jewelryId][] = $image;
}
$twig->addGlobal('isCategoryPage', true);
echo $twig->render('single/item.html.twig', [
'category' => 'men-jewelry',
'itemData' => $itemData,
'images' => $menJewelryImages,
'relatedData' => $menJewelryData,
]);
} elseif (preg_match('#^'.$base_url.'/category/earrings/(\d+)$#', $uri, $matches)) {
$itemId = $matches[1];
$query = "SELECT * FROM earrings WHERE id = :id";
$statement = $pdo->prepare($query);
$statement->bindParam(':id', $itemId, PDO::PARAM_INT);
$statement->execute();
$itemData = $statement->fetch(PDO::FETCH_ASSOC);
$query = "SELECT * FROM earrings_images WHERE earring_id = $itemId";
$statement = $pdo->prepare($query);
$statement->execute();
$earringsImages = $statement->fetchAll(PDO::FETCH_ASSOC);
$earringsImagesByItemId = [];
foreach ($earringsImages as $image) {
$jewelryId = $image['earring_id'];
if (!isset($earringsImagesByItemId[$jewelryId])) {
$earringsImagesByItemId[$jewelryId] = [];
}
$earringsImagesByItemId[$jewelryId][] = $image;
}
$twig->addGlobal('isCategoryPage', true);
echo $twig->render('single/item.html.twig', [
'category' => 'earrings',
'itemData' => $itemData,
'images' => $earringsImages,
'relatedData' => $earringsData,
]);
} else if ($uri === $base_url.'/stores') {
echo $twig->render('stores.html.twig', [
'storesData' => $storesData,
]);
} else {
echo $twig->render('404.html.twig');
}