-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconnectors.php
More file actions
401 lines (341 loc) · 12 KB
/
Copy pathconnectors.php
File metadata and controls
401 lines (341 loc) · 12 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
<?php
if (!isset($g_external_is_connector))
{
require_once('./cfg/_cfg.php');
}
// Define a class to represent the race data
class RaceInfo {
public $ext_id;
public $datum;
public $datum2;
public $nazev;
public $misto;
public $oblasti;
public $typ;
public $zebricek2;
public $ranking;
public $odkaz;
public $prihlasky;
public $prihlasky1;
public $prihlasky2;
public $prihlasky3;
public $prihlasky4;
public $prihlasky5;
public $koeficient1;
public $koeficient2;
public $etap;
public $poznamka;
public $vicedenni;
public $oddil;
public $modify_flag;
public $kategorie;
public $startovne;
public $cancelled;
// Constructor to initialize the object with key-value pairs
public function __construct($data) {
$this->ext_id = $data['ext_id'] ?? null;
$this->datum = $data['datum'] ?? null;
$this->datum2 = $data['datum2'] ?? null;
$this->nazev = $data['nazev'] ?? null;
$this->misto = $data['misto'] ?? null;
$this->oblasti = $data['oblasti'] ?? null;
$this->typ = $data['typ'] ?? null;
$this->zebricek2 = $data['zebricek2'] ?? null;
$this->ranking = $data['ranking'] ?? null;
$this->odkaz = $data['odkaz'] ?? null;
$this->prihlasky = $data['prihlasky'] ?? null;
$this->prihlasky1 = $data['prihlasky1'] ?? null;
$this->prihlasky2 = $data['prihlasky2'] ?? null;
$this->prihlasky3 = $data['prihlasky3'] ?? null;
$this->prihlasky4 = $data['prihlasky4'] ?? null;
$this->prihlasky5 = $data['prihlasky5'] ?? null;
$this->koeficient1 = $data['koeficient1'] ?? null;
$this->koeficient2 = $data['koeficient2'] ?? null;
$this->etap = $data['etap'] ?? null;
$this->poznamka = $data['poznamka'] ?? null;
$this->vicedenni = $data['vicedenni'] ?? null;
$this->oddil = $data['oddil'] ?? null;
$this->modify_flag = $data['modify_flag'] ?? null;
$this->kategorie = $data['kategorie'] ?? null;
$this->startovne = $data['startovne'] ?? null;
$this->cancelled = $data['cancelled'] ?? null;
}
}
class RacePayement {
public int $raceId;
public RaceOverview $overview;
/** @var array<int, RaceParticipant> RegNo => User */
public array $participants = [];
public function __construct(int $raceId) {
$this->raceId = $raceId;
$this->overview = new RaceOverview();
}
public function addPatricipant(RaceParticipant $user): void {
$this->participants[$user->regNo] = $user;
}
public function addCategory(string $name, int $feeTier, int $fee ): void {
$this->overview->addCategory($name,$feeTier,$fee);
}
public function addService(string $name, int $fee, int $count ): void {
$this->overview->addService( $name, $fee, $count );
}
}
class RaceOverview {
/** @var array<string, array<int, float>> Category => [EntryStop => Fee] */
public array $categories = [];
/** @var array<string, array<float,int>> ServiceName => [ Fee => Count ] */
public array $services = [];
/** @var array<int, bool> feeTier => exist */
public array $feeTiers = [];
public function addCategory(string $name, int $feeTier, int $fee ): void {
if (!isset($this->categories[$name])) {
$this->categories[$name] = [];
}
$this->categories[$name][$feeTier] = $fee;
// Store only unique sorted feeTier values
$this->feeTiers[$feeTier] = true;
}
public function addService(string $name, int $fee, int $count ): void {
if ( isset ( $this->services[$name][$fee] ) ) {
$this->services[$name][$fee] += $count;
} else {
$this->services[$name][$fee] = $count;
}
}
}
class RaceParticipant {
public string $regNo;
public string $classDesc;
public string $name;
public bool $rentSI;
public string $licence;
public $fee;
public int $feeTier;
public function __construct(string $regNo, string $classDesc, string $name, bool $rentSI, string|null $licence, int $fee, int $feeTier) {
$this->regNo = $regNo;
$this->classDesc = $classDesc;
$this->name = $name;
$this->rentSI = $rentSI;
$this->licence = $licence ?? '';
$this->fee = $fee;
$this->feeTier = $feeTier;
}
}
interface ConnectorInterface {
public function getSystemName(): string;
public function getRaceURL(string $id): string;
public function getRaceInfo(string $id) : ?RaceInfo;
public function getRacesList($fromDate, $toDate);
public function getRacePayement(string $id) : ?RacePayement;
}
class OrisCZConnector implements ConnectorInterface {
private $sourceUrl = 'https://oris.orientacnisporty.cz/';
private $apiUrl;
public function __construct() {
$this->apiUrl = $this->sourceUrl . 'API/';
}
// Method to get the system name
public function getSystemName(): string {
return "Oris";
}
// RaceInfo URL
public function getRaceURL($raceId) : string {
return $this->sourceUrl . 'Zavod?id=' . $raceId;
}
private function mapLevelToZebricek2($levelId) {
$map = [
1 => 17, // MCR
3 => 6, // ZB
4 => 8, // OZ
7 => 33, // CPS
8 => 1, // CP
11 => 24, // OM
17 => 17 // VET
];
return $map[$levelId] ?? 0x0080; // Default to 0x80 if not found
}
private function mapSport($sportId) {
//sport ID from ORIS : 1=OB, 2=LOB, 3=MTBO, 4=TRAIL
$map = [
1 => 1, // OB
2 => 4, // LOB
3 => 2, // MTBO
4 => 8, // TRAIL
];
return $map[$sportId] ?? 1; // Default is OB
}
private function getClubs(&$raceData) {
$oddily = [];
if (isset($raceData['Org1']['Abbr'])) {
$oddily[] = $raceData['Org1']['Abbr'];
}
if (isset($raceData['Org2']['Abbr'])) {
$oddily[] = $raceData['Org2']['Abbr'];
}
return implode('+', $oddily);
}
// Method to get race date based on race ID or provided response
public function getRaceDate($raceId, $response = null) {
// If no response provided, fetch it from API
if ($response === null) {
$url = $this->apiUrl . '?format=json&method=getEvent&id=' . $raceId;
$response = $this->makeRequest($url);
}
if ($response && isset($response['Status']) && $response['Status'] === "OK") {
$raceData = $response['Data'];
return String2DateDMY(formatDate($raceData['Date']));
}
return ''; // Return empty string if race not found or error
}
// Method to get detailed race information based on race ID
public function getRaceInfo($raceId) : RaceInfo {
$url = $this->apiUrl . '?format=json&method=getEvent&id=' . $raceId;
$response = $this->makeRequest($url);
if ($response && $response['Status'] == "OK") {
$raceData = $response['Data'];
$classFees = [];
if (isset($raceData['Classes'])) {
foreach ($raceData['Classes'] as $class) {
if (isset($class['Name'])) {
$name = $class['Name'];
$fee = $class['Fee'] ?? null; // default to null if missing
$classFees[$name] = $fee;
}
}
}
ksort ( $classFees );
$oddily = $this->getClubs($raceData);
$oblasti = [];
if (isset($raceData['Regions'])) {
foreach ($raceData['Regions'] as $tag => $region) {
$oblasti[] = $region['ID'];
}
}
// Get last Stage date if multistage event
$date2 = ($raceData['Stages'] > 1) ? $this->getRaceDate($raceData['Stage'.$raceData['Stages']], $response) : 0;
// Use associative array to pass data to constructor
return new RaceInfo([
'ext_id' => $raceData['ID'],
'datum' => String2DateDMY(formatDate($raceData['Date'])),
'datum2' => $date2,
'nazev' => $raceData['Name'],
'misto' => $raceData['Place'],
// 'category' => $raceData['Category'],
//typ0 => Typ akce
'oblasti' => $oblasti,
'typ0' => 'Z',
'typ' => $this->mapSport($raceData['Sport']['ID']),
'zebricek2' => $this->mapLevelToZebricek2($raceData['Level']['ID']),
'ranking' => $raceData['Ranking'],
'odkaz' => $this->getRaceURL($raceData['ID']),
'prihlasky' => strtotime($raceData['EntryDate1']),
'prihlasky1' => strtotime($raceData['EntryDate2']),
'prihlasky2' => strtotime($raceData['EntryDate3']),
// 'prihlasky3' => '',
// 'prihlasky4' => '',
// 'prihlasky5' => '',
'koeficient1' => $raceData['EntryKoef2'],
'koeficient2' => $raceData['EntryKoef3'],
'etap' => $raceData['Stages'],
// 'poznamka' => $poznamka,
'vicedenni' => ($raceData['Stages']>1?1:0),
'oddil' => $oddily,
'modify_flag' => 0,
'kategorie' => implode(';', array_keys ( $classFees ) ),
'startovne' => $classFees,
'cancelled' => (!empty($raceData['Cancelled']) || !empty($raceData['Canceled']) || !empty($raceData['cancelled']) || !empty($raceData['canceled'])) ? 1 : 0
]);
} else {
return null; // Return null if race not found or error
}
}
// Helper method to make HTTP requests
private function makeRequest($url) {
$response = file_get_contents($url);
// Decode JSON response
return json_decode($response, true);
}
private function makeRequestCurl($url) {
$ch = curl_init($url);
// Set curl options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
return null;
}
curl_close($ch);
// Decode JSON response
return json_decode($response, true);
}
function getRacesList($fromDate, $toDate) {
$url = $this->apiUrl.'?format=json&method=getEventList&all=1&datefrom='.$fromDate.'&dateto='.$toDate;
// echo($url.'<BR/>');
$response = $this->makeRequest($url);
if ($response && $response['Status'] == "OK") {
$racesData = $response['Data'];
$rows = array();
foreach($racesData as $oneRace) {
$oddily = $this->getClubs($oneRace);
$row = array();
$row[] = $oneRace['ID'];
$row[] = $oneRace['Date'];
$row[] = $oneRace['Name'];
$row[] = $oddily;
$rows[] = $row;
}
return $rows;
} else {
return null; // Return null if race not found or error
}
}
// Method to get detailed race information based on race ID
public function getRacePayement($raceId) : ?RacePayement {
global $g_external_is_club_id;
if ( !IsSet ($g_external_is_club_id) || $g_external_is_club_id === '' ) return null;
$url = $this->apiUrl . '?format=json&method=getEventEntries&clubid=' . $g_external_is_club_id . '&eventid=' . $raceId;
$response = $this->makeRequest($url);
$racePayement = null;
if ($response && $response['Status'] == "OK") {
$racePayement = new RacePayement($raceId);
foreach ($response['Data'] as $entry) {
if (isset($entry['Fee'])) {
if (isset($entry['RegNo']) ) {
$racePayement->addPatricipant(
new RaceParticipant($entry['RegNo'], $entry['ClassDesc'], $entry['Name'],
$entry['RentSI'], $entry['Licence'], (int)$entry['Fee'], $entry['EntryStop']));
}
if (isset($entry['ClassDesc'])&&isset($entry['ClassDesc'])) {
$racePayement->addCategory($entry['ClassDesc'], $entry['EntryStop'], (int)$entry['Fee']);
}
}
}
}
$url = $this->apiUrl . '?format=json&method=getEventServiceEntries&clubid=' . $g_external_is_club_id . '&eventid=' . $raceId;
$response = $this->makeRequest($url);
if ($response && $response['Status'] == "OK") {
if ( $racePayement === null ) $racePayement = new RacePayement($raceId);
foreach ($response['Data'] as $entry) {
if ( isset ( $entry['Service'] ) ) {
$racePayement->addService($entry['Service']['NameCZ'] ?? 'Name?', $entry['Service']['UnitPrice'] , $entry['Quantity'] );
} else {
if ( isset ( $entry['Quantity'] ) && isset ( $entry['TotalFee'] ) ) {
$racePayement->addService('Name?', $entry['TotalFee'] / $entry['Quantity'], $entry['Quantity'] );
}
}
}
}
//print_r ($racePayement);
return $racePayement;
}
}
class ConnectorFactory {
public static function create(): ?ConnectorInterface {
global $g_external_is_connector;
if ( $g_external_is_connector && class_exists( $g_external_is_connector)) {
return new $g_external_is_connector();
}
return null; // Return null explicitly if no valid connector is found
}
}