forked from SimonezYT/fantacalcio-4h
192 lines
5.6 KiB
PHP
Executable File
192 lines
5.6 KiB
PHP
Executable File
<?php
|
|
|
|
require_once 'logic.php';
|
|
|
|
/**
|
|
* IMPORTA GIOCATORI
|
|
* CSV → JSON { "numero": { "nome": "...", "cognome": "...", ... } }
|
|
*/
|
|
function importaGiocatori(string $csvContent): string|false
|
|
{
|
|
$separators = detectSeparators($csvContent);
|
|
$delimiter = $separators['delimiter'];
|
|
|
|
$result = [];
|
|
$lines = preg_split('/\r\n|\n|\r/', trim($csvContent));
|
|
$headers = null;
|
|
|
|
foreach ($lines as $index => $line) {
|
|
$row = str_getcsv($line, $delimiter);
|
|
if (empty($row) || trim(implode('', $row)) === '') {
|
|
continue;
|
|
}
|
|
|
|
if ($headers === null) {
|
|
$headers = array_map('trim', $row);
|
|
continue;
|
|
}
|
|
|
|
$data = [];
|
|
foreach ($headers as $i => $header) {
|
|
$value = isset($row[$i]) ? trim($row[$i]) : '';
|
|
if ($value !== '') {
|
|
$data[$header] = $value;
|
|
}
|
|
}
|
|
|
|
$key = $data['numero'] ?? '';
|
|
if ($key === '') continue;
|
|
unset($data['numero']);
|
|
|
|
// conversioni tipi speciali (isDestro rimane booleano)
|
|
if (isset($data['isDestro'])) {
|
|
$lower = strtolower($data['isDestro']);
|
|
$data['isDestro'] = in_array($lower, ['true', '1', 'si', 'yes', 'vero', 't']) ? true : false;
|
|
}
|
|
|
|
// Conversione esplicita per i campi che devono essere numeri interi
|
|
foreach (['partiteGiocate', 'assist', 'goal'] as $field) {
|
|
if (isset($data[$field]) && is_numeric($data[$field])) {
|
|
$data[$field] = (int)$data[$field];
|
|
}
|
|
}
|
|
|
|
$result[$key] = $data;
|
|
}
|
|
|
|
ksort($result, SORT_NATURAL);
|
|
return json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
/**
|
|
* ESPORTA GIOCATORI
|
|
* JSON → CSV
|
|
*/
|
|
function esportaGiocatori(string|array $giocatoriJson, string $delimiter = ';'): string
|
|
{
|
|
$data = is_string($giocatoriJson) ? json_decode($giocatoriJson, true) : $giocatoriJson;
|
|
if (!is_array($data) || empty($data)) {
|
|
return '';
|
|
}
|
|
|
|
// raccogliamo tutte le chiavi possibili (per gestire giocatori con campi extra)
|
|
$allKeys = ['numero'];
|
|
foreach ($data as $obj) {
|
|
$allKeys = array_merge($allKeys, array_keys($obj));
|
|
}
|
|
$allKeys = array_unique($allKeys);
|
|
|
|
$csvLines = [];
|
|
$csvLines[] = implode($delimiter, array_map(fn($k) => csvEscape($k), $allKeys));
|
|
|
|
foreach ($data as $numero => $giocatore) {
|
|
$row = ['numero' => $numero];
|
|
foreach ($allKeys as $key) {
|
|
if ($key === 'numero') continue;
|
|
|
|
$value = $giocatore[$key] ?? '';
|
|
|
|
// Gestione speciale per i booleani
|
|
if ($key === 'isDestro' && is_bool($value)) {
|
|
$row[$key] = $value ? 'true' : 'false';
|
|
} else {
|
|
$row[$key] = $value;
|
|
}
|
|
}
|
|
|
|
$csvLines[] = implode($delimiter, array_map('csvEscape', $row));
|
|
}
|
|
|
|
return implode("\n", $csvLines);
|
|
}
|
|
|
|
/**
|
|
* IMPORTA VOTI
|
|
* CSV → JSON { "numero": [voto1, voto2, ...] }
|
|
*/
|
|
function importaVoti(string $csvContent): string|false
|
|
{
|
|
$separators = detectSeparators($csvContent);
|
|
$delimiter = $separators['delimiter'];
|
|
|
|
$result = [];
|
|
$lines = preg_split('/\r\n|\n|\r/', trim($csvContent));
|
|
|
|
foreach ($lines as $index => $line) {
|
|
if ($index === 0) continue; // skip header
|
|
|
|
$row = str_getcsv($line, $delimiter);
|
|
if (empty($row) || trim($row[0] ?? '') === '') continue;
|
|
|
|
$key = trim($row[0]);
|
|
if ($key === '') continue;
|
|
|
|
$voti = [];
|
|
// I voti sono tutti gli elementi dalla seconda colonna in poi
|
|
for ($i = 1; $i < count($row); $i++) {
|
|
$val = trim($row[$i]);
|
|
|
|
// Normalizza SEMPRE la virgola in punto per gestire contenuti misti.
|
|
// Questo è sicuro per i voti, che non usano separatori per le migliaia.
|
|
$normalizedVal = str_replace(',', '.', $val);
|
|
|
|
if ($val !== '' && is_numeric($normalizedVal)) {
|
|
$voti[] = (float)$normalizedVal;
|
|
}
|
|
}
|
|
|
|
if (!empty($voti)) {
|
|
$result[(string)$key] = $voti;
|
|
}
|
|
}
|
|
|
|
ksort($result, SORT_NATURAL);
|
|
return json_encode($result, JSON_PRETTY_PRINT);
|
|
}
|
|
|
|
/**
|
|
* ESPORTA VOTI
|
|
* JSON → CSV
|
|
*/
|
|
function esportaVoti(string|array $votiJson, string $delimiter = ';', string $decimal = '.'): string
|
|
{
|
|
$data = is_string($votiJson) ? json_decode($votiJson, true) : $votiJson;
|
|
if (!is_array($data) || empty($data)) {
|
|
return '';
|
|
}
|
|
|
|
$csvLines = [];
|
|
$csvLines[] = 'numero' . $delimiter . 'voti'; // Intestazione
|
|
|
|
foreach ($data as $numero => $votiArray) {
|
|
if (!is_array($votiArray)) {
|
|
continue;
|
|
}
|
|
|
|
// Converti i voti nel formato decimale richiesto
|
|
$formattedVoti = array_map(function($voto) use ($decimal) {
|
|
return str_replace('.', $decimal, (string)$voto);
|
|
}, $votiArray);
|
|
|
|
// Crea una riga con il numero del giocatore seguito dai suoi voti
|
|
$row = array_merge([$numero], $formattedVoti);
|
|
|
|
// Implode della riga con il delimitatore
|
|
$csvLines[] = implode($delimiter, $row);
|
|
}
|
|
|
|
return implode("\n", $csvLines);
|
|
}
|
|
|
|
|
|
/**
|
|
* Utility: escaping CSV
|
|
*/
|
|
function csvEscape($value): string
|
|
{
|
|
$value = (string)$value;
|
|
if (str_contains($value, ',') || str_contains($value, '"') || str_contains($value, "\n")) {
|
|
return '"' . str_replace('"', '""', $value) . '"';
|
|
}
|
|
return $value;
|
|
} |