This commit updates the version code to 4.72.725 and introduces the `fxSettingsApplyChronotrackApi` function to manage ChronoTrack API credentials based on the environment. It also adds relevant UI elements in the sidebar and form sections for ChronoTrack configuration, enhancing the integration of ChronoTrack features within the application.
197 lines
5.8 KiB
PHP
197 lines
5.8 KiB
PHP
<?php
|
|
/**
|
|
* MSIN API ChronoTrack — lecture / filtrage des ressources CT (event, race).
|
|
*/
|
|
|
|
function fxChronotrackApiNormalizeEntityList($mixPayload, $strEntityKey) {
|
|
$arrOut = array();
|
|
|
|
if (!is_array($mixPayload)) {
|
|
return $arrOut;
|
|
}
|
|
|
|
if (isset($mixPayload[$strEntityKey]) && is_array($mixPayload[$strEntityKey])) {
|
|
$mixList = $mixPayload[$strEntityKey];
|
|
if (fxChronotrackApiIsAssocArray($mixList)) {
|
|
$arrOut[] = $mixList;
|
|
} else {
|
|
$arrOut = $mixList;
|
|
}
|
|
return $arrOut;
|
|
}
|
|
|
|
if (fxChronotrackApiIsAssocArray($mixPayload) && (isset($mixPayload['id']) || isset($mixPayload['event_id']) || isset($mixPayload['race_id']))) {
|
|
return array($mixPayload);
|
|
}
|
|
|
|
if (!fxChronotrackApiIsAssocArray($mixPayload)) {
|
|
return $mixPayload;
|
|
}
|
|
|
|
return $arrOut;
|
|
}
|
|
|
|
function fxChronotrackApiIsAssocArray($arr) {
|
|
if (!is_array($arr) || $arr === array()) {
|
|
return false;
|
|
}
|
|
return array_keys($arr) !== range(0, count($arr) - 1);
|
|
}
|
|
|
|
function fxChronotrackApiEntityId($arrEntity) {
|
|
foreach (array('event_id', 'race_id', 'id', 'entry_id') as $strKey) {
|
|
if (isset($arrEntity[$strKey]) && $arrEntity[$strKey] !== '') {
|
|
return (string)$arrEntity[$strKey];
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function fxChronotrackApiEntityName($arrEntity) {
|
|
foreach (array('event_name', 'race_name', 'name', 'title') as $strKey) {
|
|
if (!empty($arrEntity[$strKey])) {
|
|
return trim((string)$arrEntity[$strKey]);
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function fxChronotrackApiEntityStartEpoch($arrEntity) {
|
|
foreach (array('event_start_time', 'race_start_time', 'start_time', 'start_date', 'event_date') as $strKey) {
|
|
if (!isset($arrEntity[$strKey]) || $arrEntity[$strKey] === '' || $arrEntity[$strKey] === null) {
|
|
continue;
|
|
}
|
|
$mixVal = $arrEntity[$strKey];
|
|
if (is_numeric($mixVal)) {
|
|
return intval($mixVal);
|
|
}
|
|
$intTs = strtotime((string)$mixVal);
|
|
if ($intTs !== false) {
|
|
return $intTs;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
function fxChronotrackApiFormatEventRow($arrEntity) {
|
|
$intStart = fxChronotrackApiEntityStartEpoch($arrEntity);
|
|
return array(
|
|
'ct_event_id' => fxChronotrackApiEntityId($arrEntity),
|
|
'name' => fxChronotrackApiEntityName($arrEntity),
|
|
'start_epoch' => $intStart,
|
|
'start_label' => $intStart > 0 ? date('Y-m-d H:i', $intStart) : '',
|
|
'is_past' => ($intStart > 0 && $intStart < strtotime('today')),
|
|
'raw' => $arrEntity,
|
|
);
|
|
}
|
|
|
|
function fxChronotrackApiFetchEventsRaw() {
|
|
$arrApi = fxChronotrackApiOAuthApiGet('event');
|
|
if ($arrApi['state'] !== 'ok') {
|
|
return $arrApi;
|
|
}
|
|
|
|
$arrList = fxChronotrackApiNormalizeEntityList($arrApi['json'], 'event');
|
|
$arrRows = array();
|
|
foreach ($arrList as $arrEntity) {
|
|
if (!is_array($arrEntity)) {
|
|
continue;
|
|
}
|
|
$arrRow = fxChronotrackApiFormatEventRow($arrEntity);
|
|
if ($arrRow['ct_event_id'] === '') {
|
|
continue;
|
|
}
|
|
$arrRows[] = $arrRow;
|
|
}
|
|
|
|
return array('state' => 'ok', 'events' => $arrRows);
|
|
}
|
|
|
|
function fxChronotrackApiFilterEvents($arrEvents, $strSearch = '', $blnShowPast = false) {
|
|
$strSearch = mb_strtolower(trim($strSearch), 'UTF-8');
|
|
$arrFiltered = array();
|
|
|
|
foreach ($arrEvents as $arrEvent) {
|
|
if (!$blnShowPast && !empty($arrEvent['is_past'])) {
|
|
continue;
|
|
}
|
|
if ($strSearch !== '') {
|
|
$strHay = mb_strtolower($arrEvent['name'] . ' ' . $arrEvent['ct_event_id'], 'UTF-8');
|
|
if (mb_strpos($strHay, $strSearch, 0, 'UTF-8') === false) {
|
|
continue;
|
|
}
|
|
}
|
|
$arrFiltered[] = $arrEvent;
|
|
}
|
|
|
|
usort($arrFiltered, function ($a, $b) {
|
|
$intA = intval($a['start_epoch']);
|
|
$intB = intval($b['start_epoch']);
|
|
if ($intA === $intB) {
|
|
return strcasecmp($a['name'], $b['name']);
|
|
}
|
|
if ($intA <= 0) {
|
|
return 1;
|
|
}
|
|
if ($intB <= 0) {
|
|
return -1;
|
|
}
|
|
return ($intA < $intB) ? -1 : 1;
|
|
});
|
|
|
|
return $arrFiltered;
|
|
}
|
|
|
|
function fxChronotrackApiFetchEventsFiltered($strSearch = '', $blnShowPast = false) {
|
|
$arrRaw = fxChronotrackApiFetchEventsRaw();
|
|
if ($arrRaw['state'] !== 'ok') {
|
|
return $arrRaw;
|
|
}
|
|
return array(
|
|
'state' => 'ok',
|
|
'events' => fxChronotrackApiFilterEvents($arrRaw['events'], $strSearch, $blnShowPast),
|
|
'total' => count($arrRaw['events']),
|
|
);
|
|
}
|
|
|
|
function fxChronotrackApiFormatRaceRow($arrEntity) {
|
|
$intStart = fxChronotrackApiEntityStartEpoch($arrEntity);
|
|
return array(
|
|
'ct_race_id' => fxChronotrackApiEntityId($arrEntity),
|
|
'name' => fxChronotrackApiEntityName($arrEntity),
|
|
'start_label' => $intStart > 0 ? date('Y-m-d H:i', $intStart) : '',
|
|
'raw' => $arrEntity,
|
|
);
|
|
}
|
|
|
|
function fxChronotrackApiFetchRacesForEvent($intCtEventId) {
|
|
$intCtEventId = intval($intCtEventId);
|
|
if ($intCtEventId <= 0) {
|
|
return array('state' => 'error', 'message' => 'event_id CT invalide');
|
|
}
|
|
|
|
$arrApi = fxChronotrackApiOAuthApiGet('event/' . $intCtEventId . '/race');
|
|
if ($arrApi['state'] !== 'ok') {
|
|
return $arrApi;
|
|
}
|
|
|
|
$arrList = fxChronotrackApiNormalizeEntityList($arrApi['json'], 'race');
|
|
$arrRows = array();
|
|
foreach ($arrList as $arrEntity) {
|
|
if (!is_array($arrEntity)) {
|
|
continue;
|
|
}
|
|
$arrRow = fxChronotrackApiFormatRaceRow($arrEntity);
|
|
if ($arrRow['ct_race_id'] === '') {
|
|
continue;
|
|
}
|
|
$arrRows[] = $arrRow;
|
|
}
|
|
|
|
usort($arrRows, function ($a, $b) {
|
|
return strcasecmp($a['name'], $b['name']);
|
|
});
|
|
|
|
return array('state' => 'ok', 'races' => $arrRows);
|
|
}
|