forked from radepee/cot2html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.php
More file actions
576 lines (500 loc) · 18.8 KB
/
Copy pathtools.php
File metadata and controls
576 lines (500 loc) · 18.8 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
define( "STATUT_PRESENT", "Q" );
define( "STATUT_ABSENT", "F" );
define( "STATUT_ELIMINE", "N" );
define( "POULE_STATUT_VICTOIRE", "V" );
define( "POULE_STATUT_DEFAITE", "D" );
define( "POULE_STATUT_ABANDON", "A" );
define( "POULE_STATUT_EXPULSION", "E" );
define( "SEXE_MALE", "M" );
define( "SEXE_FEMALE", "F" );
function endsWith( $str, $sub )
{
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub );
}
function explorer($dir, $extension = '') {
$files = array();
// Vérifier si le dossier existe
if (!is_dir($dir)) {
error_log("Dossier non trouvé: $dir");
return $files;
}
try {
// Scan le dossier
$items = scandir($dir);
foreach ($items as $item) {
if ($item != "." && $item != "..") {
$fullpath = $dir . DIRECTORY_SEPARATOR . $item;
// Vérifier si c'est un fichier et si l'extension correspond
if (is_file($fullpath) &&
(empty($extension) || strtolower(substr($item, -strlen($extension))) === strtolower($extension))) {
$files[] = $fullpath;
}
}
}
} catch (Exception $e) {
error_log("Erreur lors du scan du dossier $dir: " . $e->getMessage());
}
return $files;
}
function selectedCompetition()
{
if( isset( $_GET[ 'idCompetition' ] ) )
return $_GET[ 'idCompetition' ];
return -1;
}
function selectedPhase()
{
if( isset( $_GET[ 'phaseId' ] ) )
return $_GET[ 'phaseId' ];
return -1;
}
function selectedPhaseName( $domXml )
{
if( isset( $_GET[ 'phaseId' ] ) )
{
$phase = getPhaseXmlElement( $domXml, $_GET[ 'phaseId' ] );
if( $phase != null )
return $phase->tagName;
}
return 'ListeInscrit';
}
function getPhaseXmlElement( $docXml, $idPhase )
{
$phasesXml = $docXml->getElementsByTagName( 'Phases' );
foreach( $phasesXml as $phases )
{
foreach($phases->childNodes as $phase)
{
if( isset( $phase->tagName ) && getAttribut( $phase, 'PhaseID' ) == $idPhase )
return $phase;
}
}
return null;
}
function makeUrl($paramsOverlay, $clear=0)
{
$url = 'index.php?action=affichage';
if ($clear == 0)
{
if( isset( $_GET[ 'refresh' ] ) )
unset( $_GET[ 'refresh' ] );
$params = array_merge($_GET, $paramsOverlay);
}
else
$params = $paramsOverlay;
foreach ($params as $k=>$v) if (! is_null($v))
{
$url.= '&'.$k.'='.$v;
}
return $url;
}
/*************
XML
*************/
if (!function_exists('getAttribut')) {
function getAttribut($node, $attr) {
if ($node->hasAttribute($attr)) {
return $node->getAttribute($attr);
}
// For team competitions, if 'RangFinal' is not set, try 'Classement'
if ($attr === 'RangFinal' && $node->hasAttribute('Classement')) {
return $node->getAttribute('Classement');
}
return '';
}
}
function normalizeRegionKey($region)
{
$region = trim(str_replace("\xc2\xa0", ' ', $region));
$region = html_entity_decode($region, ENT_QUOTES | ENT_HTML5, 'UTF-8');
if (function_exists('iconv')) {
$ascii = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $region);
if ($ascii !== false) {
$region = $ascii;
}
}
$region = strtoupper($region);
$region = preg_replace('/[^A-Z0-9]+/', ' ', $region);
return trim(preg_replace('/\s+/', ' ', $region));
}
function regionShortCode($region)
{
$key = normalizeRegionKey($region);
if ($key === '') {
return '';
}
$map = [
'AUVERGNE RHONE ALPES' => 'ARA',
'BOURGOGNE FRANCHE COMTE' => 'BFC',
'BRETAGNE' => 'BRE',
'CENTRE VAL DE LOIRE' => 'CVL',
'CORSE' => 'COR',
'SUD CORSE' => 'COR',
'NORD CORSE' => 'COR',
'GRAND EST' => 'GES',
'HAUTS DE FRANCE' => 'HDF',
'ILE DE FRANCE' => 'IDF',
'NORMANDIE' => 'NOR',
'NOUVELLE AQUITAINE' => 'NAQ',
'OCCITANIE' => 'OCC',
'PAYS DE LA LOIRE' => 'PDL',
'PROVENCE ALPES COTE D AZUR' => 'PACA',
'GUADELOUPE' => 'GUA',
'MARTINIQUE' => 'MTQ',
'GUYANE' => 'GUY',
'REUNION' => 'REU',
'LA REUNION' => 'REU',
'MAYOTTE' => 'MAY',
'NOUVELLE CALEDONIE' => 'NCL',
'WALLIS ET FUTUNA' => 'WLF',
'POLYNESIE FRANCAISE' => 'PF',
'SAINT PIERRE ET MIQUELON' => 'SPM',
];
if (isset($map[$key])) {
return $map[$key];
}
$tokens = explode(' ', $key);
$abbr = '';
foreach ($tokens as $token) {
if ($token === '') {
continue;
}
$abbr .= substr($token, 0, 1);
if (strlen($abbr) >= 4) {
break;
}
}
if ($abbr === '') {
$abbr = substr(preg_replace('/[^A-Z0-9]/', '', $key), 0, 4);
}
return $abbr;
}
function regionLogoPath($region)
{
$abbr = regionShortCode($region);
if ($abbr === '') {
return '';
}
$safeAbbr = preg_replace('/[^A-Z0-9]/', '', strtoupper($abbr));
if ($safeAbbr === '') {
return '';
}
$fileFs = __DIR__ . '/svg/regions/' . $safeAbbr . '.svg';
if (!file_exists($fileFs)) {
return '';
}
return 'svg/regions/' . $safeAbbr . '.svg';
}
function region_icon($region, $size = 'small')
{
$path = regionLogoPath($region);
if ($path === '') {
return '';
}
$abbr = regionShortCode($region);
$alt = htmlspecialchars($abbr, ENT_QUOTES, 'UTF-8');
return "<img src='{$path}' class='region_icon{$size}' alt='{$alt}' title='" . htmlspecialchars($region, ENT_QUOTES, 'UTF-8') . "' />";
}
function region_with_icon($region, $size = 'small')
{
if (trim($region) === '') {
return '';
}
$icon = region_icon($region, $size);
if ($icon !== '') {
return $icon;
}
return htmlspecialchars($region, ENT_QUOTES, 'UTF-8');
}
/**
* Checks if the given XML represents a team competition
* @param DOMDocument $xml The XML document to check
* @return boolean True if this is a team competition
*/
function isTeamCompetition($xml) {
return $xml->documentElement->nodeName === 'CompetitionParEquipes';
}
/**
* Translates team (Equipe) nodes to fencer (Tireur) nodes for compatibility
* @param DOMElement $equipe Th<?php
$competitors = getCompetitors($xml);
foreach ($competitors as $id => $competitor) {
$displayName = getCompetitorDisplayName($competitor);
// ... rest of your code ...
}e team node to translate
* @return DOMElement A new Tireur node with equivalent data
*/
function translateTeamToTireur($equipe) {
$doc = $equipe->ownerDocument;
$tireur = $doc->createElement('Tireur');
// Map direct attribute translations
$attributeMap = [
'ID' => 'ID',
'Nom' => 'Nom',
'Sexe' => 'Sexe',
'Nation' => 'Nation',
'Region' => 'Region',
'Ligue' => 'Ligue',
'Club' => 'Club',
'Ranking' => 'Ranking',
'Exporte' => 'Exporte',
'Statut' => 'Statut',
'Departement' => 'Departement'
];
foreach ($attributeMap as $sourceAttr => $targetAttr) {
if ($equipe->hasAttribute($sourceAttr)) {
$tireur->setAttribute($targetAttr, $equipe->getAttribute($sourceAttr));
}
}
// Add required attributes that might not exist in Equipe
if (!$equipe->hasAttribute('Prenom')) {
$tireur->setAttribute('Prenom', ''); // Empty string for team competitions
}
if (!$equipe->hasAttribute('RangFinal') && $equipe->hasAttribute('Classement')) {
$tireur->setAttribute('RangFinal', $equipe->getAttribute('Classement'));
}
if (!$equipe->hasAttribute('Present')) {
$tireur->setAttribute('Present', '1'); // Default to present
}
return $tireur;
}
/**
* Gets a list of competitors (either individual fencers or teams)
* @param DOMDocument $xml The competition XML
* @return array Array of competitors as Tireur nodes
*/
function getCompetitors($xml) {
$competitors = [];
if (isTeamCompetition($xml)) {
// For team competitions, convert Equipe nodes to Tireur nodes
$teams = $xml->getElementsByTagName('Equipe');
foreach ($teams as $team) {
$competitors[$team->getAttribute('ID')] = translateTeamToTireur($team);
}
} else {
// For individual competitions, use existing Tireur nodes
$fencers = $xml->getElementsByTagName('Tireur');
foreach ($fencers as $fencer) {
$competitors[$fencer->getAttribute('ID')] = $fencer;
}
}
return $competitors;
}
/**
* Gets the display name for a competitor (team or individual)
* @param DOMElement $competitor The competitor node
* @return string The display name
*/
function getCompetitorDisplayName($competitor) {
if ($competitor->nodeName === 'Equipe') {
return $competitor->getAttribute('Nom'); // For teams, just use team name
} else {
// For individuals, combine name and first name
return trim($competitor->getAttribute('Nom') . ' ' . $competitor->getAttribute('Prenom'));
}
}
/**
* Render a competition from its XML data
* Works with both individual and team competitions
*/
function renderCompetition($xml) {
// Determine competition type
$isTeamCompetition = $xml->documentElement->nodeName === 'CompetitionParEquipes';
// Get basic competition info from XML
$info = getCompetitionInfo($xml);
// Output begins with the competition header
$html = "<div class='competition-container'>";
$html .= "<h1 class='competition-title'>{$info['titre']}</h1>";
$html .= "<div class='competition-details'>";
$html .= "<p><strong>Date:</strong> {$info['date']}</p>";
$html .= "<p><strong>Organisateur:</strong> {$info['organisateur']}</p>";
$html .= "<p><strong>Arme:</strong> {$info['arme']}</p>";
$html .= "<p><strong>Catégorie:</strong> {$info['categorie']}</p>";
$html .= "</div>";
// Display participants
if ($isTeamCompetition) {
$html .= renderTeams($xml);
} else {
$html .= renderFencers($xml);
}
// Display results
$html .= renderResults($xml, $isTeamCompetition);
$html .= "</div>";
return $html;
}
/**
* Render teams list for team competitions
*/
function renderTeams($xml) {
$html = "<div class='tblhd_top' onclick='mickey()'>";
$html .= "<div class='tblhd'><div class='tblhd-page-label'>" . t('page.teams_list') . "</div>";
$html .= "<table id='scrollme' class='listeTireur teams-table'>";
$html .= "<thead class='monocol'><tr>";
$html .= "<th class=''>" . t('col.team') . "</th><th class='VR'>" . t('col.region') . "</th><th class='VR'>" . t('col.club') . "</th><th class='B RIG VR'>" . t('col.standing') . "</th><th class='VR'>" . t('col.members') . "</th>";
$html .= "</tr></thead>\n<tbody>";
$pair = 'impair';
foreach ($xml->getElementsByTagName('Equipes') as $equipes) {
foreach ($equipes->getElementsByTagName('Equipe') as $team) {
$teamName = htmlspecialchars($team->getAttribute('Nom'));
$region = $team->getAttribute('Region');
$club = htmlspecialchars($team->getAttribute('Club'));
$ranking = htmlspecialchars($team->getAttribute('Classement'));
// Alternance de couleurs
$rowClass = ($pair === 'impair') ? 'impairQL' : 'pairQL';
$pair = ($pair === 'impair') ? 'pair' : 'impair';
$members = [];
foreach ($team->getElementsByTagName('Tireur') as $fencer) {
$members[] = htmlspecialchars(trim(
$fencer->getAttribute('Nom') . ' ' . $fencer->getAttribute('Prenom')
));
}
$html .= "<tr class='{$rowClass}'>";
$html .= "<td class='team-name'>{$teamName}</td>";
$html .= "<td class='VR'>" . region_with_icon($region, '') . "</td>";
$html .= "<td class='VR'>{$club}</td>";
$html .= "<td class='B RIG VR'>{$ranking}</td>";
$html .= "<td class='team-members-cell VR'>" . implode(' • ', $members) . "</td>";
$html .= "</tr>\n";
}
}
$html .= "</tbody></table></div></div>";
return $html;
}
/**
* Render individual fencers list
*/
function renderFencers($xml) {
$html = "<div class='fencers-section'>";
$html .= "<h2>Tireurs</h2>";
$html .= "<table class='fencers-table'>";
$html .= "<thead><tr><th>Nom</th><th>Prénom</th><th>Club</th><th>Classement</th></tr></thead>";
$html .= "<tbody>";
$fencers = $xml->getElementsByTagName('Tireur');
foreach ($fencers as $fencer) {
// Only include top-level fencers (not part of a team)
if (!$fencer->parentNode->nodeName === 'Equipe') {
$name = $fencer->getAttribute('Nom');
$firstName = $fencer->getAttribute('Prenom');
$club = $fencer->getAttribute('Club');
$ranking = $fencer->getAttribute('Ranking');
$html .= "<tr>";
$html .= "<td>$name</td>";
$html .= "<td>$firstName</td>";
$html .= "<td>$club</td>";
$html .= "<td>$ranking</td>";
$html .= "</tr>";
}
}
$html .= "</tbody></table></div>";
return $html;
}
/**
* Render competition results
*/
function renderResults($xml, $isTeamCompetition) {
$html = "<div class='results-section'>";
$html .= "<h2>Résultats</h2>";
// Get the phase information
$phaseTableaux = $xml->getElementsByTagName('PhaseDeTableaux');
if ($phaseTableaux->length > 0) {
$html .= "<div class='tableaux-results'>";
foreach ($phaseTableaux as $phase) {
$suiteTableaux = $phase->getElementsByTagName('SuiteDeTableaux');
foreach ($suiteTableaux as $suite) {
$title = $suite->getAttribute('Titre');
$html .= "<h3>$title</h3>";
$tableaux = $suite->getElementsByTagName('Tableau');
foreach ($tableaux as $tableau) {
$tabTitle = $tableau->getAttribute('Titre');
$html .= "<h4>$tabTitle</h4>";
$matches = $tableau->getElementsByTagName('Match');
if ($matches->length > 0) {
$html .= "<table class='match-table'>";
$html .= "<thead><tr><th>Équipe/Tireur 1</th><th>Score</th><th>Équipe/Tireur 2</th><th>Score</th></tr></thead>";
$html .= "<tbody>";
foreach ($matches as $match) {
$competitors = $isTeamCompetition ?
$match->getElementsByTagName('Equipe') :
$match->getElementsByTagName('Tireur');
if ($competitors->length >= 2) {
$comp1Ref = $competitors->item(0)->getAttribute('REF');
$comp1Score = $competitors->item(0)->getAttribute('Score');
$comp1Status = $competitors->item(0)->getAttribute('Statut');
$comp2Ref = $competitors->item(1)->getAttribute('REF');
$comp2Score = $competitors->item(1)->getAttribute('Score');
$comp2Status = $competitors->item(1)->getAttribute('Statut');
// Find names for the references
$comp1Name = getNameByRef($xml, $comp1Ref, $isTeamCompetition);
$comp2Name = getNameByRef($xml, $comp2Ref, $isTeamCompetition);
$html .= "<tr>";
$html .= "<td>" . ($comp1Status === 'V' ? "<strong>$comp1Name</strong>" : $comp1Name) . "</td>";
$html .= "<td>$comp1Score</td>";
$html .= "<td>" . ($comp2Status === 'V' ? "<strong>$comp2Name</strong>" : $comp2Name) . "</td>";
$html .= "<td>$comp2Score</td>";
$html .= "</tr>";
}
}
$html .= "</tbody></table>";
}
}
}
}
$html .= "</div>";
}
$html .= "</div>";
return $html;
}
/**
* Helper function to get name by reference
*/
function getNameByRef($xml, $ref, $isTeamCompetition) {
if ($isTeamCompetition) {
$nodes = $xml->getElementsByTagName('Equipe');
foreach ($nodes as $node) {
if ($node->getAttribute('ID') === $ref) {
return $node->getAttribute('Nom');
}
}
} else {
$nodes = $xml->getElementsByTagName('Tireur');
foreach ($nodes as $node) {
if ($node->getAttribute('ID') === $ref) {
return $node->getAttribute('Nom') . ' ' . $node->getAttribute('Prenom');
}
}
}
return "Unknown";
}
/**
* Translate team competition XML into individual competitor structure
* by converting Equipe elements into Tireur elements.
*
* @param \DOMDocument $domXml
*/
function translateEquipeToTireur(\DOMDocument &$domXml) {
$root = $domXml->documentElement;
if (!$root || $root->nodeName !== 'CompetitionParEquipes') {
return;
}
// Create a Tireurs container
$tireursContainer = $domXml->createElement('Tireurs');
// Collect all Equipe elements
$equipes = $domXml->getElementsByTagName('Equipe');
foreach ($equipes as $equipe) {
$tireur = $domXml->createElement('Tireur');
// Map Equipe ID to Tireur ID
if ($equipe->hasAttribute('ID')) {
$tireur->setAttribute('ID', $equipe->getAttribute('ID'));
}
// Use Equipe Nom as full name
if ($equipe->hasAttribute('Nom')) {
$tireur->setAttribute('Nom', $equipe->getAttribute('Nom'));
}
// No surname available in team, set empty Prenom
$tireur->setAttribute('Prenom', '');
$tireursContainer->appendChild($tireur);
}
// Append the new Tireurs node under root
$root->appendChild($tireursContainer);
}