-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchesFin.php
307 lines (249 loc) · 9.78 KB
/
matchesFin.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
<?php
require_once __DIR__ . '/controllers/ErrorHandler.php';
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: access');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Content-Type: application/json; charset=UTF-8');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
require_once __DIR__ . '/database.php';
require_once __DIR__ . '/sendJson.php';
require_once __DIR__ . '/controllers/authHandler.php';
require_once __DIR__ . '/controllers/pointsHandler.php';
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') :
sendJson(200, 'OK');
endif;
//operazioni admin
$authHandler = new AuthHandler();
if ($_SERVER['REQUEST_METHOD'] == 'GET') :
$id = null;
if(isset($_GET['id'])){
//singola partita
$id = $_GET['id'];
$sql = "SELECT * FROM `matches_fin` WHERE `id`='$id'";
$query = mysqli_query($connection, $sql);
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
if ($row === null) sendJson(404, 'Match not found!');
sendJson(200, '', $row);
}else{
//lista partite
$sql = "SELECT * FROM `matches_fin` ORDER BY `date`";
$query = mysqli_query($connection, $sql);
$teamsDict = $query->fetch_all(MYSQLI_ASSOC);
$response = ["matches" => $teamsDict];
sendJson(200, '', $response);
}
endif;
if ($_SERVER['REQUEST_METHOD'] == 'POST') :
$authHandler->checkIfAdmin($connection);
$data = json_decode(file_get_contents('php://input'));
if (
!isset($data->date) ||
!isset($data->des_1) ||
!isset($data->des_2) ||
!isset($data->fase) ||
!isset($data->n_match) ||
empty(trim($data->date)) ||
empty(trim($data->des_1)) ||
empty(trim($data->des_2)) ||
is_null(trim($data->fase)) ||
is_null(trim($data->n_match))
) :
sendJson(
422,
'Please fill all the required fields & None of the fields should be empty.',
['required_fields' => ['date', 'des_1', 'des_2', 'fase', 'n_match']]
);
endif;
$date = mysqli_real_escape_string($connection, htmlspecialchars(trim($data->date)));
$des_1 = mysqli_real_escape_string($connection, trim($data->des_1));
$des_2 = mysqli_real_escape_string($connection, trim($data->des_2));
$fase = mysqli_real_escape_string($connection, trim($data->fase));
$n_match = mysqli_real_escape_string($connection, trim($data->n_match));
$id_team_1 = null;
if(isset($data->id_team_1)){
$id_team_1 = mysqli_real_escape_string($connection, trim($data->id_team_1));
}
if(is_null($id_team_1)){
$id_team_1 = "NULL";
}
$id_team_2 = null;
if(isset($data->id_team_2)){
$id_team_2 = mysqli_real_escape_string($connection, trim($data->id_team_2));
}
if(is_null($id_team_2)){
$id_team_2 = "NULL";
}
$goal_team_1 = null;
if(isset($data->goal_team_1)){
$goal_team_1 = mysqli_real_escape_string($connection, trim($data->goal_team_1));
}
if(is_null($goal_team_1)){
$goal_team_1 = "NULL";
}
$goal_team_2 = null;
if(isset($data->goal_team_2)){
$goal_team_2 = mysqli_real_escape_string($connection, trim($data->goal_team_2));
}
if(is_null($goal_team_2)){
$goal_team_2 = "NULL";
}
$result = null;
if(isset($data->result)){
$result = mysqli_real_escape_string($connection, trim($data->result));
$result = "'$result'";
}
if(is_null($result)){
$result = "NULL";
}
$final_result = null;
if(isset($data->final_result)){
$final_result = mysqli_real_escape_string($connection, trim($data->final_result));
$final_result = "'$final_result'";
}
if(is_null($final_result)){
$final_result = "NULL";
}
$sql = "INSERT INTO `matches_fin` (`date`,`id_team_1`,`id_team_2`,`fase`,`n_match`,`goal_team_1`,`goal_team_2`,`result`,`final_result`,`des_1`,`des_2`) VALUES('$date',$id_team_1,$id_team_2,$fase,$n_match,$goal_team_1,$goal_team_1,$result,$final_result,'$des_1','$des_2')";
$query = mysqli_query($connection, $sql);
if ($query) sendJson(200, 'You have successfully registered a match fin.');
sendJson(500, 'Something going wrong.');
endif;
if ($_SERVER['REQUEST_METHOD'] == 'PUT') :
$authHandler->checkIfAdmin($connection);
$data = json_decode(file_get_contents('php://input'));
if (
!isset($data->date) ||
!isset($data->des_1) ||
!isset($data->des_2) ||
!isset($data->fase) ||
!isset($data->n_match) ||
empty(trim($data->date)) ||
empty(trim($data->des_1)) ||
empty(trim($data->des_2)) ||
is_null(trim($data->fase)) ||
is_null(trim($data->n_match))
) :
sendJson(
422,
'Please fill all the required fields & None of the fields should be empty.',
['required_fields' => ['date', 'id_team_1', 'id_team_2', 'fase']]
);
endif;
$id = $data->id;
$date = mysqli_real_escape_string($connection, htmlspecialchars(trim($data->date)));
$des_1 = mysqli_real_escape_string($connection, trim($data->des_1));
$des_2 = mysqli_real_escape_string($connection, trim($data->des_2));
$fase = mysqli_real_escape_string($connection, trim($data->fase));
$n_match = mysqli_real_escape_string($connection, trim($data->n_match));
$id_team_1 = null;
if(isset($data->id_team_1)){
$id_team_1 = mysqli_real_escape_string($connection, trim($data->id_team_1));
}
if(is_null($id_team_1)){
$id_team_1 = "NULL";
}
$id_team_2 = null;
if(isset($data->id_team_2)){
$id_team_2 = mysqli_real_escape_string($connection, trim($data->id_team_2));
}
if(is_null($id_team_2)){
$id_team_2 = "NULL";
}
$goal_team_1 = null;
if(isset($data->goal_team_1)){
$goal_team_1 = mysqli_real_escape_string($connection, trim($data->goal_team_1));
}
if(is_null($goal_team_1)){
$goal_team_1 = "NULL";
}
$goal_team_2 = null;
if(isset($data->goal_team_2)){
$goal_team_2 = mysqli_real_escape_string($connection, trim($data->goal_team_2));
}
if(is_null($goal_team_2)){
$goal_team_2 = "NULL";
}
$result = null;
if(isset($data->result)){
$result = mysqli_real_escape_string($connection, trim($data->result));
$result = "'$result'";
}
if(is_null($result)){
$result = "NULL";
}
$final_result = null;
if(isset($data->final_result)){
$final_result = mysqli_real_escape_string($connection, trim($data->final_result));
$final_result = "'$final_result'";
}
if(is_null($final_result)){
$final_result = "NULL";
}
$sql = "SELECT `id` FROM `matches_fin` WHERE `id`= $id";
$query = mysqli_query($connection, $sql);
$row_num = mysqli_num_rows($query);
if ($row_num == 0) sendJson(404, 'This Match fin doesn\'t exists!');
$sql = "UPDATE `matches_fin` SET `date`='$date',`id_team_1`=$id_team_1,`id_team_2`=$id_team_2,`fase`=$fase,`n_match`=$n_match,`goal_team_1`=$goal_team_1,`goal_team_2`=$goal_team_2,`result`=$result,`final_result`=$final_result,`des_1`='$des_1',`des_2`='$des_2' WHERE `id` = $id";
$query = mysqli_query($connection, $sql);
if ($query) {
//updates successfully
//update points of bets
$fase = $data->fase;
$sql = "SELECT
mfb.*,
mf.des_1,
mf.des_2
FROM `matches_fin_bet` as mfb
LEFT JOIN
(SELECT id, des_1, des_2 FROM matches_fin) as mf ON mfb.match_id = mf.id
WHERE
`match_id` IN (SELECT id as match_id FROM matches_fin WHERE fase = '$fase')
ORDER BY match_id";
$query = mysqli_query($connection, $sql);
$matchesFinBetDict = $query->fetch_all(MYSQLI_ASSOC);
$sql = "";
for ($i = 0; $i < count($matchesFinBetDict); $i++) {
$matchFinBet = $matchesFinBetDict[$i];
$matchFinBetId = $matchFinBet["id"];
$pointsHandler = new PointsHandler();
//punti
$matchBetPoints = $pointsHandler->calcMatchesFinBetPoints($data, $matchFinBet);
//echo $matchBetPoints;
if($matchBetPoints === null){
$matchBetPoints = "NULL";
}
//bonus
$matchBetBonus = $pointsHandler->calcMatchesBetBonus($connection, $data, $matchFinBet);
//echo $matchBetBonus;
if($matchBetBonus === null){
$matchBetBonus = "NULL";
}
$sql .= "UPDATE `matches_fin_bet` SET `points`= $matchBetPoints, `bonus`= $matchBetBonus WHERE `id`='$matchFinBetId';";
}
//echo $sql;
$matchFinBetQuery = mysqli_multi_query($connection, $sql);
if($matchFinBetQuery)sendJson(200, 'You have successfully updated a match fin.');
sendJson(500, 'Something going wrong.');
}
sendJson(500, 'Something going wrong.');
endif;
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') :
$authHandler->checkIfAdmin($connection);
$data = json_decode(file_get_contents('php://input'));
if (
!isset($data->id) ||
empty(trim($data->id))
) :
sendJson(
422,
'Please fill all the required fields & None of the fields should be empty.',
['required_fields' => ['id']]
);
endif;
$id = $data->id;
$sql = "DELETE FROM `matches_fin` WHERE `id` = $id";
$query = mysqli_query($connection, $sql);
if ($query) sendJson(200, 'You have successfully deleted a match.');
sendJson(500, 'Something going wrong.');
endif;
sendJson(405, 'Invalid Request Method. HTTP method should be POST');