-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_tournament.php
executable file
·291 lines (271 loc) · 10.9 KB
/
insert_tournament.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
<?php
// require_once 'admin_auth.php'; // Protects this script
ob_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'header.php';
require_once 'conn.php';
require 'auth.php';
redirect_if_not_logged_in();
require_non_player();
$currentUserId = $_SESSION['user_id'];
$currentUserRole = $_SESSION['role'];
// Fetch tournaments: Admins see all, users see only their own
function fetchTournaments($conn, $userId, $userRole) {
$query = "
SELECT
t.id AS tournament_id,
t.name AS tournament_name,
GROUP_CONCAT(DISTINCT c.name ORDER BY c.name SEPARATOR ', ') AS categories,
GROUP_CONCAT(DISTINCT m.username ORDER BY m.username SEPARATOR ', ') AS moderators
FROM tournaments t
LEFT JOIN tournament_categories tc ON t.id = tc.tournament_id
LEFT JOIN categories c ON tc.category_id = c.id
LEFT JOIN tournament_moderators tm ON t.id = tm.tournament_id
LEFT JOIN users m ON tm.user_id = m.id
". ($userRole === 'admin' ? "" : "WHERE t.created_by = ?") . "
GROUP BY t.id
ORDER BY t.name;
";
$stmt = $conn->prepare($query);
if ($userRole !== 'admin') {
$stmt->bind_param('i', $userId);
}
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Add a new tournament
if (isset($_POST['add_tournament'])) {
$tournament_name = trim($_POST['tournament_name']);
$created_by = $currentUserId;
if (empty($tournament_name)) {
die("Tournament name cannot be empty.");
}
$tournament_name = $conn->real_escape_string($tournament_name);
$sql = "INSERT INTO tournaments (name, created_by) VALUES ('$tournament_name', $created_by)";
if ($conn->query($sql)) {
echo "<p style='color: green;'>Tournament added successfully.</p>";
} else {
die("Error adding tournament: " . $conn->error);
}
}
// Restrict category assignment unless admin
if (isset($_POST['add_category'])) {
$tournament_id = intval($_POST['tournament_id']);
$category_id = intval($_POST['category_id']);
// Check if the tournament belongs to the current user unless admin
if ($currentUserRole !== 'admin') {
$checkQuery = "SELECT id FROM tournaments WHERE id = ? AND created_by = ?";
$stmt = $conn->prepare($checkQuery);
$stmt->bind_param('ii', $tournament_id, $currentUserId);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows === 0) {
die("Unauthorized access.");
}
$stmt->close();
}
$insertQuery = "INSERT INTO tournament_categories (tournament_id, category_id) VALUES (?, ?)
ON DUPLICATE KEY UPDATE category_id = category_id";
$stmt = $conn->prepare($insertQuery);
$stmt->bind_param('ii', $tournament_id, $category_id);
if ($stmt->execute()) {
echo "<p style='color: green;'>Category assigned successfully.</p>";
} else {
echo "<p style='color: red;'>Error assigning category: " . htmlspecialchars($stmt->error) . "</p>";
}
$stmt->close();
}
// Restrict moderator assignment unless admin
if (isset($_POST['add_moderator'])) {
$tournament_id = intval($_POST['tournament_id']);
$moderator_id = intval($_POST['moderator_id']);
// Check if the tournament belongs to the current user unless admin
if ($currentUserRole !== 'admin') {
$checkQuery = "SELECT id FROM tournaments WHERE id = ? AND created_by = ?";
$stmt = $conn->prepare($checkQuery);
$stmt->bind_param('ii', $tournament_id, $currentUserId);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows === 0) {
die("Unauthorized access.");
}
$stmt->close();
}
$insertQuery = "INSERT INTO tournament_moderators (tournament_id, user_id) VALUES (?, ?)
ON DUPLICATE KEY UPDATE user_id = user_id";
$stmt = $conn->prepare($insertQuery);
$stmt->bind_param('ii', $tournament_id, $moderator_id);
if ($stmt->execute()) {
echo "<p style='color: green;'>Moderator assigned successfully.</p>";
} else {
echo "<p style='color: red;'>Error assigning moderator: " . htmlspecialchars($stmt->error) . "</p>";
}
$stmt->close();
}
}
$tournaments = fetchTournaments($conn, $currentUserId, $currentUserRole);
$categories_result = $conn->query("SELECT * FROM categories");
$users_result = $conn->query("SELECT id, username FROM users ORDER BY username");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Tournaments</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 20px;
color: #333;
}
h1 {
text-align: center;
color: #4a90e2;
margin-bottom: 20px;
}
.form-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
max-width: 1200px;
margin: 0 auto;
}
form {
flex: 1;
min-width: 300px;
padding: 20px;
background: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
input, select, button {
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 6px;
width: 100%;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
padding: 10px;
border: 1px solid #ddd;
}
th {
background-color: #4a90e2;
color: white;
}
.red-x {
color: red;
font-size: 16px;
text-decoration: none;
cursor: pointer;
}
.red-x:hover {
color: darkred;
}
</style>
</head>
<body>
<h1>Manage Tournaments</h1>
<div class="form-container">
<!-- Form to Add a Tournament -->
<form method="POST">
<h2>Add Tournament</h2>
<input type="text" name="tournament_name" placeholder="Tournament Name" required>
<button type="submit" name="add_tournament">Add Tournament</button>
</form>
<!-- Form to Assign Categories -->
<form method="POST">
<h2>Assign Categories</h2>
<select name="tournament_id" required>
<option value="">Select Tournament</option>
<?php foreach ($tournaments as $tournament): ?>
<option value="<?= $tournament['tournament_id'] ?>"><?= htmlspecialchars($tournament['tournament_name']) ?></option>
<?php endforeach; ?>
</select>
<select name="category_id" required>
<option value="">Select Category</option>
<?php while ($category = $categories_result->fetch_assoc()): ?>
<option value="<?= $category['id'] ?>"><?= htmlspecialchars($category['name']) ?></option>
<?php endwhile; ?>
</select>
<button type="submit" name="add_category">Assign Category</button>
</form>
<!-- Form to Assign Moderators -->
<form method="POST">
<h2>Assign Moderators</h2>
<select name="tournament_id" required>
<option value="">Select Tournament</option>
<?php foreach ($tournaments as $tournament): ?>
<option value="<?= $tournament['tournament_id'] ?>"><?= htmlspecialchars($tournament['tournament_name']) ?></option>
<?php endforeach; ?>
</select>
<select name="moderator_id" required>
<option value="">Select Moderator</option>
<?php while ($user = $users_result->fetch_assoc()): ?>
<option value="<?= $user['id'] ?>"><?= htmlspecialchars($user['username']) ?></option>
<?php endwhile; ?>
</select>
<button type="submit" name="add_moderator">Assign Moderator</button>
</form>
</div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Owner</th>
<th>Categories</th>
<th>Moderators</th>
</tr>
</thead>
<tbody>
<?php foreach ($tournaments as $row): ?>
<tr>
<td><?= htmlspecialchars($row['tournament_id']) ?></td>
<td><?= htmlspecialchars($row['tournament_name']) ?></td>
<td><?= htmlspecialchars($row['owner_name'] ?? 'Unknown') ?></td>
<td><?= htmlspecialchars($row['categories'] ?? 'None') ?></td>
<td>
<table>
<?php
if (!empty($row['moderators'])):
$moderators = explode(', ', $row['moderators']);
foreach ($moderators as $moderator):
$moderator_id_query = $conn->prepare("SELECT id FROM users WHERE username = ?");
$moderator_id_query->bind_param('s', $moderator);
$moderator_id_query->execute();
$moderator_id_query->bind_result($moderator_id);
$moderator_id_query->fetch();
$moderator_id_query->close();
?>
<tr>
<td><?= htmlspecialchars($moderator) ?></td>
<td>
<form method="POST" style="display: inline;">
<input type="hidden" name="tournament_id" value="<?= $row['tournament_id'] ?>">
<input type="hidden" name="moderator_id" value="<?= $moderator_id ?>">
<button type="submit" name="remove_moderator" class="red-x">X</button>
</form>
</td>
</tr>
<?php endforeach; endif; ?>
</table>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>