last requirements implemented; still a 'restore' bug to fix
[westcastle.git] / scripts / rw_players.php
CommitLineData
7a00c409
BA
1<?php
2
3if (!isset($_POST["players"]))
4{
1369a09d
BA
5 if (isset($_GET["restore"]) && $_GET["restore"])
6 {
7 // Restore backup
8 if (!rename("../joueurs.csv.bak", "../joueurs.csv"))
9 exit("[]");
10 }
7a00c409
BA
11 // Retrieve all players
12 $handle = fopen("../joueurs.csv", "r");
13 $players = [];
14 $row = 0;
15 $data = fgetcsv($handle); //skip header
16 while (($data = fgetcsv($handle)) !== FALSE)
17 {
18 $players[$row] = array(
19 "prenom" => $data[0],
20 "nom" => $data[1],
ade10194
BA
21 "pdt" => count($data)>=3 ? $data[2] : 0,
22 "session" => count($data)>=4 ? $data[3] : 0,
7a00c409
BA
23 "available" => count($data)>=5 ? $data[4] : 1,
24 );
25 $row++;
26 }
27 fclose($handle);
28 echo json_encode($players);
29}
30else
31{
1369a09d 32 copy("../joueurs.csv", "../joueurs.csv.bak"); //backup current checkpoint
7a00c409
BA
33 // Write header + all players
34 $handle = fopen("../joueurs.csv", "w");
ade10194 35 fputcsv($handle, ["prenom","nom","pdt","session","present"]);
7a00c409
BA
36 $players = json_decode($_POST["players"]);
37 foreach ($players as $p)
38 fputcsv($handle, (array)$p);
39 fclose($handle);
40}
41
42?>