-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_match_bd.php
executable file
·253 lines (220 loc) · 11.1 KB
/
insert_match_bd.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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'header.php';
require 'auth.php';
redirect_if_not_logged_in();
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
require_non_player();
$userId = $_SESSION['user_id']; // Assuming user_id is stored in the session after login
$message = '';
$lockedTournament = $_SESSION['locked_tournament'] ?? null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['lock_tournament'])) {
$lockedTournament = intval($_POST['tournament_id']);
$_SESSION['locked_tournament'] = $lockedTournament;
$stmt = $conn->prepare("SELECT name FROM tournaments WHERE id = ? AND (created_by = ? OR id IN (SELECT tournament_id FROM tournament_moderators WHERE user_id = ?))");
$stmt->bind_param("iii", $lockedTournament, $userId, $userId);
$stmt->execute();
$stmt->bind_result($lockedTournamentName);
if ($stmt->fetch()) {
$_SESSION['locked_tournament_name'] = $lockedTournamentName;
$message = "Tournament locked: " . htmlspecialchars($lockedTournamentName);
} else {
$message = "Unauthorized access to the selected tournament.";
unset($_SESSION['locked_tournament']);
}
$stmt->close();
} elseif (isset($_POST['unlock_tournament'])) {
unset($_SESSION['locked_tournament'], $_SESSION['locked_tournament_name']);
$lockedTournament = null;
} else {
// Fetch inputs with default values
$tournament_id = $lockedTournament ?? ($_POST['tournament_id'] ?? null);
$category_id = $_POST['category_id'] ?? null;
$team1_player1_id = $_POST['team1_player1_id'] ?? null;
$team1_player2_id = $_POST['team1_player2_id'] ?? null;
$team2_player1_id = $_POST['team2_player1_id'] ?? null;
$team2_player2_id = $_POST['team2_player2_id'] ?? null;
$stage = $_POST['stage'] ?? null;
$dateInput = $_POST['date'] ?? null;
$match_date = $dateInput ? DateTime::createFromFormat('Y-m-d', $dateInput) : null;
if ($match_date === false) {
$message = "Invalid date format!";
} else {
$match_date = $match_date ? $match_date->format('Y-m-d') : null;
}
// Improved time handling
$match_time = !empty($_POST['match_time'])
? $_POST['match_time'] . ':00'
: NULL;
error_log("DEBUG: Raw match_time input = '" . $_POST['match_time'] . "'");
error_log("DEBUG: Processed match_time for DB = '" . $match_time . "'");
$set1_team1 = $_POST['set1_team1_points'] ?? null;
$set1_team2 = $_POST['set1_team2_points'] ?? null;
$set2_team1 = $_POST['set2_team1_points'] ?? null;
$set2_team2 = $_POST['set2_team2_points'] ?? null;
$set3_team1 = $_POST['set3_team1_points'] ?? 0;
$set3_team2 = $_POST['set3_team2_points'] ?? 0;
// Validate required fields
if (empty($tournament_id) || empty($category_id) || empty($stage) || empty($match_date) || empty($match_time)) {
$message = "Please fill in all required fields.";
} else {
// Validate user's access to the tournament and category
$stmt = $conn->prepare("SELECT COUNT(*) FROM tournament_categories tc
INNER JOIN tournaments t ON tc.tournament_id = t.id
WHERE tc.category_id = ? AND t.id = ? AND (t.created_by = ? OR t.id IN (SELECT tournament_id FROM tournament_moderators WHERE user_id = ?))");
$stmt->bind_param("iiii", $category_id, $tournament_id, $userId, $userId);
$stmt->execute();
$stmt->bind_result($isAuthorized);
$stmt->fetch();
$stmt->close();
if (!$isAuthorized) {
$message = "Unauthorized access to this category.";
} else {
// Insert match details into the database
$stmt = $conn->prepare("INSERT INTO matches (
tournament_id, category_id, team1_player1_id, team1_player2_id,
team2_player1_id, team2_player2_id, stage, match_date, match_time,
set1_team1_points, set1_team2_points, set2_team1_points,
set2_team2_points, set3_team1_points, set3_team2_points
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param(
"iiiiissssiiiiii",
$tournament_id, $category_id, $team1_player1_id, $team1_player2_id,
$team2_player1_id, $team2_player2_id, $stage, $match_date, $match_time,
$set1_team1, $set1_team2, $set2_team1, $set2_team2, $set3_team1, $set3_team2
);
if ($stmt->execute()) {
$message = "Match added successfully!";
} else {
$message = "Error adding match: " . $stmt->error;
}
$stmt->close();
}
}
}
}
// Fetch tournaments created or moderated by the logged-in user
$tournaments = $conn->prepare("
SELECT id, name
FROM tournaments
WHERE created_by = ? OR id IN (SELECT tournament_id FROM tournament_moderators WHERE user_id = ?)
");
$tournaments->bind_param("ii", $userId, $userId);
$tournaments->execute();
$tournamentResults = $tournaments->get_result();
if ($lockedTournament) {
$stmt = $conn->prepare("
SELECT c.id, c.name, c.age_group, c.sex
FROM categories c
INNER JOIN tournament_categories tc ON c.id = tc.category_id
WHERE tc.tournament_id = ? AND c.name LIKE '%BD%'
");
$stmt->bind_param("i", $lockedTournament);
$stmt->execute();
$categories = $stmt->get_result();
$stmt->close();
} else {
$categories = [];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Insert Boys Doubles Match</title>
<script>
function loadPlayers(categoryId) {
if (!categoryId) return;
fetch(`get_players.php?category_id=${categoryId}`)
.then(response => response.json())
.then(data => {
const playerSelects = ['team1_player1_id', 'team1_player2_id', 'team2_player1_id', 'team2_player2_id'];
playerSelects.forEach(selectId => {
const select = document.getElementById(selectId);
select.innerHTML = '<option value="">Select Player</option>';
data.forEach(player => {
const option = document.createElement('option');
option.value = player.id;
option.textContent = `${player.name} (${player.age} years, ${player.sex})`;
select.appendChild(option);
});
});
})
.catch(error => console.error('Error fetching players:', error));
}
</script>
</head>
<body>
<div class="container">
<h1>Insert Boys Doubles Match</h1>
<?php if ($message): ?>
<p><?= htmlspecialchars($message) ?></p>
<?php endif; ?>
<?php if (!$lockedTournament): ?>
<form method="post">
<label for="tournament_id">Select Tournament:</label>
<select name="tournament_id" id="tournament_id" required>
<option value="">Select Tournament</option>
<?php while ($row = $tournamentResults->fetch_assoc()): ?>
<option value="<?= $row['id'] ?>"><?= htmlspecialchars($row['name']) ?></option>
<?php endwhile; ?>
</select>
<button type="submit" name="lock_tournament">Lock Tournament</button>
</form>
<?php else: ?>
<form method="post">
<p>Locked Tournament: <?= htmlspecialchars($_SESSION['locked_tournament_name'] ?? 'Unknown') ?></p>
<button type="submit" name="unlock_tournament">Unlock Tournament</button>
</form>
<form method="post">
<label for="category_id">Category:</label>
<select name="category_id" id="category_id" required onchange="loadPlayers(this.value)">
<option value="">Select Category</option>
<?php while ($row = $categories->fetch_assoc()): ?>
<option value="<?= $row['id'] ?>">
<?= htmlspecialchars($row['name']) ?> (<?= htmlspecialchars($row['age_group']) ?>, <?= htmlspecialchars($row['sex']) ?>)
</option>
<?php endwhile; ?>
</select>
<label for="team1_player1_id">Team 1 - Player 1:</label>
<select name="team1_player1_id" id="team1_player1_id" required></select>
<label for="team1_player2_id">Team 1 - Player 2:</label>
<select name="team1_player2_id" id="team1_player2_id" required></select>
<label for="team2_player1_id">Team 2 - Player 1:</label>
<select name="team2_player1_id" id="team2_player1_id" required></select>
<label for="team2_player2_id">Team 2 - Player 2:</label>
<select name="team2_player2_id" id="team2_player2_id" required></select>
<label for="stage">Match Stage:</label>
<select name="stage" id="stage" required>
<option value="">Select Stage</option>
<option value="Pre Quarter Finals">Pre Quarter Finals</option>
<option value="Quarter Finals">Quarter Finals</option>
<option value="Semi Finals">Semi Finals</option>
<option value="Finals">Finals</option>
</select>
<label for="date">Match Date:</label>
<input type="date" name="date" id="date" required>
<label for="match_time">Match Time:</label>
<input type="time" name="match_time" id="match_time" required>
<label for="set1_team1_points">Set 1 Team 1 Points:</label>
<input type="number" name="set1_team1_points" id="set1_team1_points" required>
<label for="set1_team2_points">Set 1 Team 2 Points:</label>
<input type="number" name="set1_team2_points" id="set1_team2_points" required>
<label for="set2_team1_points">Set 2 Team 1 Points:</label>
<input type="number" name="set2_team1_points" id="set2_team1_points" required>
<label for="set2_team2_points">Set 2 Team 2 Points:</label>
<input type="number" name="set2_team2_points" id="set2_team2_points" required>
<label for="set3_team1_points">Set 3 Team 1 Points:</label>
<input type="number" name="set3_team1_points" id="set3_team1_points">
<label for="set3_team2_points">Set 3 Team 2 Points:</label>
<input type="number" name="set3_team2_points" id="set3_team2_points">
<button type="submit">Add Match</button>
</form>
<?php endif; ?>
</div>
</body>
</html>