-
Notifications
You must be signed in to change notification settings - Fork 2
/
Fun_TourDelete.php
457 lines (431 loc) · 13.7 KB
/
Fun_TourDelete.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
function tour_delete($TourId) {
require_once('Common/CheckPictures.php');
$TourCode=getCodeFromId($TourId);
$TableArray=array(
"AccColors" => "AcTournament",
"AccEntries" => "AETournament",
"AccPrice" => "APTournament",
"ACL" => "AclTournament",
'AclDetails' => 'AclDtTournament',
"AvailableTarget" => "AtTournament",
"Awards" => "AwTournament",
"Awarded" => "AwTournament",
"BackNumber" => "BnTournament",
"BoinxSchedule" => "BsTournament",
"CasScore"=>"CaSTournament",
"CasTeam"=>"CaTournament",
"CasTeamFinal"=>"CTFTournament",
"CasTeamTarget"=>"CTTTournament",
"Classes" => "ClTournament",
"Countries" => "CoTournament",
"DistanceInformation" => "DiTournament",
"Divisions" => "DivTournament",
"Eliminations" => "ElTournament",
"Emails" => "EmTournament",
"Entries" => "EnTournament",
"EventClass" => "EcTournament",
"Events" => "EvTournament",
"Individuals" => "IndTournament",
"F2FGrid"=>"F2FTournament",
"F2FEntries" => "F2FTournament",
"F2FFinal" => "F2FTournament",
"FinalReportA" => "FraTournament",
"Finals" => "FinTournament",
"FinSchedule" => "FSTournament",
"FinTraining" => 'FtTournament',
"FinTrainingEvent" => 'FteTournament',
"Flags" => 'FlTournament',
"GuessWho" => 'GwTournament',
"GuessWhoData" => 'GwdTournament',
"HhtData" => "HdTournament",
"HhtEvents" => "HeTournament",
"HhtSetup" => "HsTournament",
"IdCardElements" => "IceTournament",
"IdCards" => "IcTournament",
"Images" => "ImTournament",
"IskDevices" => "IskDvTournament",
'ModulesParameters' => 'MpTournament',
'OnLineIds' => 'OliTournament', // This table gets deleted and recreated everytime so no need to import/export it but needs to be deleted for housekeeping!
'PrintOutsRules' => 'PorTournament',
'Rankings' => 'RankTournament',
'RecTournament' => 'RtTournament',
"Reviews" => "RevTournament",
"Scheduler" => "SchTournament",
"Session" => "SesTournament",
"SubClass" => "ScTournament",
"TargetFaces" => "TfTournament",
"TargetGroups" => "TgTournament",
"TeamComponent" => "TcTournament",
"TeamFinals" => "TfTournament",
"TeamFinComponent" => "TfcTournament",
"Teams" => "TeTournament",
"Tournament" => "ToId",
"TournamentDistances" => "TdTournament",
"TournamentInvolved" => "TiTournament",
"TourRecords" => "TrTournament",
"TVContents" => "TVCTournament",
"TVParams" => "TVPTournament",
"TVRules" => "TVRTournament",
"TVSequence" => "TVSTournament",
"VegasAwards" => "VaTournament",
);
foreach($TableArray as $Key=>$Value)
{
$Sql = "DELETE FROM " . $Key . " WHERE " . $Value . " = " . StrSafe_DB($TourId);
safe_w_sql($Sql);
}
$Sql = "DELETE FROM Qualifications WHERE QuId NOT IN (SELECT EnId From Entries) ";
safe_w_sql($Sql);
$Sql = "DELETE FROM ElabQualifications WHERE EqId NOT IN (SELECT EnId From Entries) ";
safe_w_sql($Sql);
// $Sql = "DELETE FROM Eliminations WHERE ElId NOT IN (SELECT EnId From Entries) ";
// safe_w_sql($Sql);
$Sql = "DELETE FROM Individuals WHERE IndId NOT IN (SELECT EnId From Entries) ";
safe_w_sql($Sql);
$Sql = "DELETE FROM Photos WHERE PhEnId NOT IN (SELECT EnId From Entries) ";
safe_w_sql($Sql);
$Sql = "DELETE FROM Vegas WHERE VeId NOT IN (SELECT EnId From Entries) ";
safe_w_sql($Sql);
$Sql = "DELETE FROM ExtraData WHERE EdId NOT IN (SELECT EnId From Entries) ";
safe_w_sql($Sql);
$Sql = "DELETE FROM ExtraDataCountries WHERE EdcId NOT IN (SELECT CoId From Countries) ";
safe_w_sql($Sql);
// removea all media
RemoveMedia($TourCode);
}
function tour_import($filename, $isString=false) {
require_once('Common/CheckPictures.php');
// Tabelle che hanno il codice Tournament
$tabs_on_tour=array(
'AccColors' => 'Ac',
'AccEntries' => 'AE',
'AccPrice' => 'AP',
'ACL' => 'Acl',
'AclDetails' => 'AclDt',
'AvailableTarget' => 'At',
'Awards' => 'Aw',
'Awarded' => 'Aw',
'BackNumber' => 'Bn',
'CasScore' => 'CaS',
'CasTeam' => 'Ca',
'CasTeamFinal' => 'CTF',
'CasTeamTarget' => 'CTT',
'Classes' => 'Cl',
'ClubTeam' => 'CT',
'ClubTeamScore' => 'CTS',
'Countries' => 'Co',
'Divisions' => 'Div',
'DistanceInformation' => 'Di',
'DocumentVersions' => 'Dv',
'Emails' => 'Em',
'Entries' => 'En',
'EventClass' => 'Ec',
'Events' => 'Ev',
'Individuals' => 'Ind',
"F2FGrid"=>'F2F',
"F2FEntries" => "F2F",
"F2FFinal" => "F2F",
//"F2FScore" => "F2F",
//"F2FTarget" => "F2F",
//"F2FTargetElim" => "F2F",
'FinalReportA' => 'Fra',
'Finals' => 'Fin',
'FinSchedule' => 'FS',
'FinTraining' => 'Ft',
'FinTrainingEvent' => 'Fte',
'FinWarmup' => 'Fw',
//'Flags' => 'Fl',
'HhtData' => 'Hd',
'HhtEvents' => 'He',
'HhtSetup' => 'Hs',
'IdCardElements' => 'Ice',
'IdCards' => 'Ic',
'Images' => 'Im',
'IskDevices' => 'IskDv',
'ModulesParameters' => 'Mp',
// 'OnLineIds' => 'Oli', // this table gets deleted/inserted if local TourId changes so no need to import/export it
'Rankings' => 'Rank',
'RecTournament' => 'Rt',
'Reviews' => 'Rev',
'SubClass' => 'Sc',
'TargetFaces' => 'Tf',
'TeamComponent' => 'Tc',
'TeamFinals' => 'Tf',
'TeamFinComponent' => 'Tfc',
'Teams' => 'Te',
'TournamentDistances' => 'Td',
'TournamentInvolved' => 'Ti',
'TourRecords' => 'Tr',
'TVContents' => 'TVC',
'TVParams' => 'TVP',
'TVRules' => 'TVR',
'TVSequence' => 'TVS',
'Session'=>'Ses',
'Scheduler'=>'Sch',
'Eliminations'=>'El',
'VegasAwards'=>'Va',
);
// Tabelle che hanno il codice Countries
$tab_to_country=array(
'CasTeam' => 'CaTeam',
'ClubTeam' => 'CTTeam',
'Entries' => array('EnCountry','EnCountry2','EnCountry3'),
'ExtraDataCountries' => 'EdcId',
'TeamComponent' => 'TcCoId',
'TeamFinals' => 'TfTeam',
'TeamFinComponent' => 'TfcCoId',
'Teams' => 'TeCoId',
);
// Tabelle che hanno il codice Entries
$tab_to_entry=array(
'AccEntries' => 'AEId',
'Awarded' => 'AwEntry',
'ElabQualifications' => 'EqId',
'Eliminations' => 'ElId',
'ExtraData' => 'EdId',
'Individuals' => 'IndId',
"F2FEntries" => "F2FEnId",
"F2FFinal" => "F2FEnId",
'Finals' => 'FinAthlete',
'HhtData' => 'HdEnId',
'Photos' => 'PhEnId',
'Qualifications' => 'QuId',
'TeamComponent' => 'TcId',
'TeamFinComponent' => 'TfcId',
'Vegas' => 'VeId',
);
// Tabelle che dipendono SOLO da Tournament
$tabs_only_tour=array(
'AccColors',
'AccPrice',
'ACL',
'AclDetails',
'AvailableTarget',
'Awards',
'BackNumber',
'CasScore',
'CasTeamFinal',
'CasTeamTarget',
'Classes',
'ClubTeamScore',
'DistanceInformation',
'DocumentVersions',
'Divisions',
'Emails',
'EventClass',
'Events',
'F2FGrid',
//"F2FScore",
//"F2FTarget",
//"F2FTargetElim",
'FinalReportA',
'FinSchedule',
'FinTraining',
'FinTrainingEvent',
'FinWarmup',
//'Flags',
'HhtEvents',
'HhtSetup',
'IdCardElements',
'IdCards',
'Images',
'ModulesParameters',
// 'OnLineIds', see comments above
'Rankings',
'RecTournament' ,
'Reviews',
'SubClass',
'TargetFaces',
'TournamentDistances',
'TournamentInvolved',
'TourRecords' ,
'TVContents',
'TVParams',
'TVRules',
'TVSequence',
'Session',
'Scheduler',
'Eliminations',
'VegasAwards',
);
if($isString) {
$Gara=unserialize(gzuncompress($filename));
} else {
$Gara=unserialize(gzuncompress(file_get_contents($filename)));
}
// If is not compatible, exits
if($Gara['Tournament']['ToDbVersion'] > GetParameter('DBUpdate')) {
return false;
}
require_once('UpdateTournament.inc.php');
$Gara=UpdateTournament($Gara);
// CONTROLLA SE C'E' UN TORNEO CON LO STESSO CODICE E LO SEGA!
$q=safe_r_sql("select ToId from Tournament where ToCode=" . strsafe_db($Gara['Tournament']['ToCode']));
if($r=safe_fetch($q) ){
// esiste un tournament con lo stesso codice... ranzo tutto!
tour_delete($r->ToId);
}
// elimina i media di questa gara
RemoveMedia($Gara['Tournament']['ToCode']);
// Insert competition
$quer=array();
foreach($Gara['Tournament'] as $key=>$val) {
if($key!='ToId'){
$quer[]="$key=" . strsafe_db($val);
}
}
safe_w_sql("Insert into Tournament set ".implode(', ', $quer));
$TourId=safe_w_last_id();
// adjust ToId in all arrays...
foreach($tabs_on_tour as $tab=>$code) {
if(isset($Gara[$tab])) {
foreach($Gara[$tab] as $key=>$val) {
$Gara[$tab][$key][$code.'Tournament']=$TourId;
}
}
}
// inserisce le tabelle che hanno SOLO il tournament
foreach($tabs_only_tour as $tab) {
if(isset($Gara[$tab])) {
foreach($Gara[$tab] as $record) {
$query=array();
foreach($record as $key=>$val) {
$query[]="$key = " . strsafe_db($val) ;
}
safe_w_sql("insert into $tab set ". implode(', ', $query). " on duplicate key update ". implode(', ', $query));
}
}
}
// inserisce i paesi e mantieni l'array per il cambio country
$Countries=array();
if(array_key_exists('Countries',$Gara) && is_array($Gara['Countries']) && count($Gara['Countries'])>0)
{
foreach($Gara['Countries'] as $record) {
$query=array();
foreach($record as $key=>$val) {
if($key!='CoId' and $key!='CoFlag' and $key!='CoMail' and $key!='CoNoPrint'){
$query[]="$key = " . strsafe_db($val) ;
}
}
safe_w_sql("insert into Countries set ". implode(', ', $query));
$Countries[$record['CoId']]=safe_w_last_id();
}
}
//aggiorna CoParent1 della Countries stessa
$tmpSql = "SELECT DISTINCT CoParent1 FROM Countries WHERE CoTournament=" . $TourId . " AND CoParent1!=0";
$tmpRs = safe_r_sql($tmpSql);
if(safe_num_rows($tmpRs)!=0)
{
while($tmpRow=safe_fetch($tmpRs))
safe_w_sql("UPDATE Countries SET CoParent1=". $Countries[$tmpRow->CoParent1] . " WHERE CoParent1=" . $tmpRow->CoParent1 . " AND CoTournament=" . $TourId);
safe_free_result($tmpRs);
}
//aggiorna CoParent2 della Countries stessa
$tmpSql = "SELECT DISTINCT CoParent2 FROM Countries WHERE CoTournament=" . $TourId . " AND CoParent2!=0";
$tmpRs = safe_r_sql($tmpSql);
if(safe_num_rows($tmpRs)!=0)
{
while($tmpRow=safe_fetch($tmpRs))
safe_w_sql("UPDATE Countries SET CoParent2=". $Countries[$tmpRow->CoParent2] . " WHERE CoParent2=" . $tmpRow->CoParent2 . " AND CoTournament=" . $TourId);
safe_free_result($tmpRs);
}
// aggiorna i paesi nelle tabelle che ne fanno uso
foreach($tab_to_country as $tab=>$field) {
if(array_key_exists($tab, $Gara))
{
foreach($Gara[$tab] as $key=>$record) {
if(is_array($field)) {
foreach($field as $ff) {
if(array_key_exists($ff,$record) && $record[$ff])
$Gara[$tab][$key][$ff]=$Countries[$record[$ff]];
}
} else {
if($record[$field] && array_key_exists($record[$field],$Countries))
$Gara[$tab][$key][$field]=$Countries[$record[$field]];
}
}
}
}
// inserisce le Entries e mantieni l'array per il cambio Entry
$Entries=array();
if(array_key_exists('Entries',$Gara) && is_array($Gara['Entries']) && count($Gara['Entries'])>0)
{
foreach($Gara['Entries'] as $record) {
$query=array();
foreach($record as $key=>$val) {
if($key!='EnId' and $key!='EnWorldRank'){
$query[]="$key = " . strsafe_db($val) ;
}
}
safe_w_sql("insert into Entries set ". implode(', ', $query));
$Entries[$record['EnId']]=safe_w_last_id();
}
}
// aggiorna le rimanenti tabelle con le Entries corrette
foreach($tab_to_entry as $tab=>$field) {
if(array_key_exists($tab, $Gara))
{
foreach($Gara[$tab] as $key=>$record) {
if(array_key_exists($record[$field], $Entries) && $record[$field]) $Gara[$tab][$key][$field]=$Entries[$record[$field]];
}
}
}
// inserisce le tabelle restanti
unset($tab_to_country['Entries']);
$final_tabs = array_unique(array_merge(array_keys($tab_to_country), array_keys($tab_to_entry)));
foreach($final_tabs as $tab) {
if(array_key_exists($tab, $Gara))
{
foreach($Gara[$tab] as $record) {
$query=array();
foreach($record as $key=>$val) {
$query[]="$key = " . strsafe_db($val) ;
}
// attenzione: questa query era "commentata" per non far apparirer errori:
// se questo comportamento è desiderato, chiamare la funzione safe_w_sql passando
// come terzo parametro un array con gli errori numerici ammessi
safe_w_sql("REPLACE INTO $tab set ". implode(', ', $query));
}
}
}
//Gestisce la tabella Flags
if(array_key_exists('Flags',$Gara) && is_array($Gara['Flags']) && count($Gara['Flags'])>0)
{
foreach($Gara['Flags'] as $record) {
if($record['FlTournament']!=-1)
$record['FlTournament']=$TourId;
$query=array();
foreach($record as $key=>$val) {
$query[]="$key = " . strsafe_db($val) ;
}
safe_w_sql("insert into Flags set ". implode(', ', $query). " on duplicate key update ". implode(', ', $query));
}
}
// // Manage OnLineIds table
// foreach($Entries as $Org => $New) {
// safe_w_sql("update OnLineIds set OliId=$New where OliTournament=$TourId and OliType='E' and OliId=$Org");
// }
// foreach($Countries as $Org => $New) {
// safe_w_sql("update OnLineIds set OliId=$New where OliTournament=$TourId and OliType='C' and OliId=$Org");
// }
// safe_w_sql("update OnLineIds set OliId=$TourId where OliTournament=$TourId and OliType='T'");
// if for accreditation, inserts all the LocCodes!
if(!empty($_REQUEST['Accreditation'])) {
safe_w_sql("insert ignore into ExtraData (select EnId, 'Z', '', concat(EnCode, '-', EnIocCode, '-', EnDivision) from Entries where EnTournament=$TourId)");
}
// RECREATES ALL MEDIA
CheckPictures($Gara['Tournament']['ToCode']);
// check if this is an active Accreditation Booth
if(GetParameter('AccBoothActive') and in_array($Gara['Tournament']['ToCode'], explode(', ', GetParameter('AccBoothCodes')))) {
require_once('Modules/AccreditationBooth/Lib.php');
// delete all the log files of this competition...
deleteAccLogs($Gara['Tournament']['ToCode']);
// track info for this competition in all servers ready to track
$q=safe_r_sql("select distinct SsApi, SsName from ianseo_Accreditation.SyncServers where SsStatus=1");
while ($r=safe_fetch($q)) {
startAccTrack($Gara['Tournament']['ToCode'], $r->SsName, $r->SsApi);
}
}
return ($TourId);
}