Better README, write first draft of documentation
[westcastle.git] / scripts / rw_players.php
1 <?php
2
3 if (!isset($_POST["players"]))
4 {
5 if (isset($_GET["restore"]) && $_GET["restore"])
6 {
7 // Restore backup
8 if (!rename("../joueurs.csv.bak", "../joueurs.csv"))
9 exit("[]");
10 }
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],
21 "pdt" => count($data)>=3 ? $data[2] : 0,
22 "session" => count($data)>=4 ? $data[3] : 0,
23 "available" => count($data)>=5 ? $data[4] : 1,
24 );
25 $row++;
26 }
27 fclose($handle);
28 echo json_encode($players);
29 }
30 else
31 {
32 copy("../joueurs.csv", "../joueurs.csv.bak"); //backup current checkpoint
33 // Write header + all players
34 $handle = fopen("../joueurs.csv", "w");
35 fputcsv($handle, ["prenom","nom","pdt","session","present"]);
36 $players = json_decode($_POST["players"]);
37 foreach ($players as $p)
38 fputcsv($handle, (array)$p);
39 fclose($handle);
40 }
41
42 ?>