-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.php
373 lines (326 loc) · 15.5 KB
/
store.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
session_start();
require_once 'includes/config/database.php';
require_once 'includes/classes/WebSender.php';
// Hata raporlamayı etkinleştir
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Kullanıcı girişi kontrolü
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
try {
// Aktif sayfa
$current_page = 'store';
// Sunucuları çek
$servers_query = $db->query("SELECT * FROM websender_servers ORDER BY id");
if (!$servers_query) {
throw new Exception("Sunucu sorgusu başarısız oldu");
}
$servers = $servers_query->fetchAll(PDO::FETCH_ASSOC);
// Debug için sunucu verilerini kontrol edelim
error_log("Store - Active Servers: " . print_r($servers, true));
// Seçili sunucu - varsayılan olarak ilk sunucuyu seç
$selected_server = isset($_GET['server']) ? (int)$_GET['server'] : ($servers[0]['id'] ?? null);
if (!$selected_server && !empty($servers)) {
$selected_server = $servers[0]['id'];
}
// Kategorileri çek
$categories_query = $db->prepare("
SELECT * FROM store_categories
WHERE server_id = ? AND status = 1
ORDER BY display_order
");
$categories_query->execute([$selected_server]);
$categories = $categories_query->fetchAll(PDO::FETCH_ASSOC);
// Seçili kategori
$selected_category = isset($_GET['category']) ? (int)$_GET['category'] : 0;
// Ürünleri çek
if ($selected_category === 0) {
$items_query = $db->prepare("
SELECT i.*
FROM store_items i
JOIN store_categories c ON i.category_id = c.id
WHERE c.server_id = ? AND i.status = 1
ORDER BY i.price
");
$items_query->execute([$selected_server]);
} else {
$items_query = $db->prepare("
SELECT * FROM store_items
WHERE category_id = ? AND status = 1
ORDER BY price
");
$items_query->execute([$selected_category]);
}
$items = $items_query->fetchAll(PDO::FETCH_ASSOC);
// Header'ı dahil et
include 'includes/header.php';
// HTML çıktısı başlat
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Market - <?php echo htmlspecialchars($settings['site_name'] ?? 'Market'); ?></title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<link rel="stylesheet" href="assets/css/style.css">
<link rel="stylesheet" href="assets/css/store.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
<script src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
</head>
<body>
<main>
<div class="store-container">
<?php if (empty($servers)): ?>
<div class="alert alert-warning">
<h2>Aktif Sunucu Bulunamadı</h2>
<p>Lütfen yönetici panelinden en az bir sunucu ekleyin.</p>
<?php if (isset($_SESSION['is_admin']) && $_SESSION['is_admin']): ?>
<a href="admin/servers.php">Sunucu Yönetimine Git</a>
<?php endif; ?>
</div>
<?php else: ?>
<!-- Sunucu Seçimi -->
<div class="server-selector">
<?php foreach ($servers as $server): ?>
<a href="javascript:void(0)"
onclick="loadServerContent(<?php echo $server['id']; ?>)"
data-server-id="<?php echo $server['id']; ?>"
class="server-item <?php echo $server['id'] === $selected_server ? 'active' : ''; ?>">
<?php if ($server['image']): ?>
<img src="<?php echo htmlspecialchars($server['image']); ?>"
alt="<?php echo htmlspecialchars($server['name']); ?>"
class="server-image">
<?php else: ?>
<i class="fas fa-server"></i>
<?php endif; ?>
<span class="server-name"><?php echo htmlspecialchars($server['name']); ?></span>
</a>
<?php endforeach; ?>
</div>
<!-- Store Content -->
<div class="store-content">
<!-- Kategoriler -->
<div class="store-categories" id="store-categories">
<a href="javascript:void(0)"
class="category-item <?php echo $selected_category === 0 ? 'active' : ''; ?>"
data-category-id="0">
<i class="fas fa-border-all"></i>
<span>Tümü</span>
</a>
<?php foreach ($categories as $category): ?>
<a href="javascript:void(0)"
class="category-item <?php echo $selected_category === $category['id'] ? 'active' : ''; ?>"
data-category-id="<?php echo $category['id']; ?>">
<i class="<?php echo htmlspecialchars($category['icon']); ?>"></i>
<span><?php echo htmlspecialchars($category['name']); ?></span>
</a>
<?php endforeach; ?>
</div>
<!-- Ürünler -->
<div id="store-items" class="store-items">
<!-- Ürünler AJAX ile yüklenecek -->
</div>
</div>
<?php endif; ?>
</div>
</main>
<!-- Onay Popup'ı -->
<div class="confirm-popup" id="confirmPopup">
<div class="confirm-content">
<div class="confirm-header">
<i class="fas fa-shopping-cart"></i>
<h3>Satın Alma Onayı</h3>
</div>
<p>Bu ürünü satın almak istediğinize emin misiniz?</p>
<div class="confirm-buttons">
<button class="confirm-btn cancel" onclick="closeConfirmPopup()">
<i class="fas fa-times"></i>
İptal
</button>
<button class="confirm-btn confirm" onclick="confirmBuy()">
<i class="fas fa-check"></i>
Satın Al
</button>
</div>
</div>
</div>
<script>
let currentItemId = null;
// Bildirim gösterme fonksiyonu
function showNotification(message, type = 'success') {
Toastify({
text: message,
duration: 3000,
gravity: "top",
position: "right",
stopOnFocus: true,
className: `notification ${type}`,
style: {
background: type === 'success' ? 'rgba(0, 179, 126, 0.9)' : 'rgba(255, 68, 68, 0.9)',
borderRadius: '8px',
padding: '1rem 1.5rem',
fontSize: '0.9rem',
fontWeight: '500'
}
}).showToast();
}
function showConfirmPopup(itemId) {
currentItemId = itemId;
document.getElementById('confirmPopup').classList.add('active');
}
function closeConfirmPopup() {
document.getElementById('confirmPopup').classList.remove('active');
}
function confirmBuy() {
if (currentItemId) {
const itemId = currentItemId;
closeConfirmPopup();
buyItem(itemId);
}
}
function buyItem(itemId) {
// Seçili sunucuyu kontrol et
const serverId = <?php echo $selected_server ?: 'null'; ?>;
if (!serverId) {
showNotification('Lütfen bir sunucu seçin!', 'error');
return;
}
// Buy butonu devre dışı bırak
const buyButton = event.target;
if (buyButton) buyButton.disabled = true;
// URL parametreleri oluştur
const url = `ajax/buy_item.php?item_id=${itemId}&server_id=${serverId}`;
console.log('İstek URL:', url); // Debug için
fetch(url)
.then(response => {
console.log('Sunucu yanıtı:', response); // Debug için
return response.json();
})
.then(data => {
console.log('İşlem sonucu:', data); // Debug için
if (data.success) {
showNotification('Ürün başarıyla satın alındı!', 'success');
// Bakiyeyi güncelle
if (data.newBalance !== undefined) {
const balanceElement = document.querySelector('.user-balance');
if (balanceElement) {
balanceElement.textContent = formatCoin(data.newBalance);
}
}
setTimeout(() => location.reload(), 2000);
} else {
showNotification(data.error || 'Bir hata oluştu!', 'error');
}
})
.catch(error => {
console.error('Satın alma hatası:', error);
showNotification('Bir hata oluştu! Lütfen daha sonra tekrar deneyin.', 'error');
})
.finally(() => {
// Buy butonu tekrar aktif et
if (buyButton) buyButton.disabled = false;
});
}
// Para formatı için yardımcı fonksiyon
function formatCoin(amount) {
return new Intl.NumberFormat('tr-TR').format(amount);
}
function loadServerContent(serverId) {
// Aktif sunucu butonunu güncelle
document.querySelectorAll('.server-item').forEach(item => {
item.classList.remove('active');
});
// Tıklanan sunucu butonunu aktif et
const clickedServer = document.querySelector(`.server-item[data-server-id="${serverId}"]`);
if (clickedServer) {
clickedServer.classList.add('active');
}
// URL'yi güncelle
window.history.pushState({}, '', '?server=' + serverId);
// Yükleniyor animasyonu göster
document.getElementById('store-items').innerHTML = '<div class="loading">Yükleniyor...</div>';
// Kategorileri ve ürünleri AJAX ile yükle
fetch('ajax/get_categories.php?server_id=' + serverId)
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById('store-categories').innerHTML = data.categories;
document.getElementById('store-items').innerHTML = data.items;
// Kategori linklerini güncelle
updateCategoryLinks(serverId);
// "Tümü" kategorisini aktif et
const allCategoryButton = document.querySelector('.category-item[data-category-id="0"]');
if (allCategoryButton) {
allCategoryButton.classList.add('active');
}
} else {
document.getElementById('store-items').innerHTML =
'<div class="error-message">İçerik yüklenirken bir hata oluştu</div>';
}
})
.catch(error => {
console.error('Error:', error);
document.getElementById('store-items').innerHTML =
'<div class="error-message">Bir hata oluştu: ' + error.message + '</div>';
});
}
function updateCategoryLinks(serverId) {
document.querySelectorAll('.category-item').forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
// Aktif kategoriyi güncelle
document.querySelectorAll('.category-item').forEach(item => {
item.classList.remove('active');
});
this.classList.add('active');
// Kategori ID'sini al
const categoryId = this.dataset.categoryId;
// Ürünleri filtrele
loadCategoryItems(serverId, categoryId);
});
});
}
// Sayfa yüklendiğinde çalışacak fonksiyon
document.addEventListener('DOMContentLoaded', function() {
// İlk sunucuyu seç
const defaultServerId = <?php echo $servers[0]['id'] ?? 'null'; ?>;
if (defaultServerId) {
// URL'den sunucu ID'sini kontrol et
const urlParams = new URLSearchParams(window.location.search);
const serverId = urlParams.get('server') || defaultServerId;
// Sunucu içeriğini yükle
loadServerContent(serverId);
// İlgili sunucu butonunu aktif et
const serverButton = document.querySelector(`.server-item[data-server-id="${serverId}"]`);
if (serverButton) {
serverButton.classList.add('active');
}
} else {
// Sunucu yoksa hata mesajı göster
document.getElementById('store-items').innerHTML = `
<div class="alert alert-warning">
<h2>Aktif Sunucu Bulunamadı</h2>
<p>Lütfen yönetici panelinden en az bir sunucu ekleyin.</p>
</div>
`;
}
});
// Sunucu butonlarına data-server-id ekle
document.querySelectorAll('.server-item').forEach(item => {
const serverId = item.getAttribute('onclick').match(/\d+/)[0];
item.setAttribute('data-server-id', serverId);
});
</script>
</body>
</html>
<?php
} catch (Exception $e) {
error_log("Store Error: " . $e->getMessage());
die("Bir hata oluştu: " . htmlspecialchars($e->getMessage()));
}
?>