4 * 'Controller' traite les données envoyées par le client.
8 public $message_utilisateur = NULL;
9 public $nouvel_inscrit = FALSE;
14 public function Controller($participant, $config)
16 $this->participant
= $participant;
17 $this->config
= $config;
19 # inscription d'un nouveau participant
20 if (isset($_POST['inscription']) && !Participant
::nombre_participant_max_atteint())
22 if ($this->config
->get("inscription_terminees"))
25 $this->traiter_donnees_inscription();
26 if (!$this->login_deja_pris() && # vérification des données
27 $this->donnees_inscription_valides() &&
28 $_POST['accord'] == "on"
33 // Cherche un id libre.
34 // Pour des questions de sécurité, les ids ne sont pas générés sequentiellement
35 // car ils sont mémorisé dans un cookie et permette l'authentification.
39 $id = rand(1, PHP_INT_MAX
);
40 $row = pg_fetch_row(pg_query_params("SELECT count(*) FROM participants WHERE id = $1", array($id)));
41 } while ($row[0] != 0);
44 INSERT INTO participants
45 (id
, pseudo
, password
, clan_nom
, clan_tag
, nom
, prenom
, age
, e_mail
, remarques
)
46 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
51 sha1($_POST['pass1']),
62 $this->set_periodes($id);
65 $this->participant = new participant($_POST['pseudo'], $_POST['pass1']);
66 setcookie("COOKIE_INFO_PARTICIPANT
", $this->participant->info->id, time() + 31104000);
67 $this->nouvel_inscrit = TRUE;
70 # un participant modifie ses infos
71 else if(isset($_POST['modification_participant']) && $this->participant->existe())
73 if ($this->config->get("inscription_terminees
"))
76 $this->traiter_donnees_inscription();
77 if (!$this->login_deja_pris() && $this->donnees_inscription_valides())
80 pg_query_params("UPDATE participants SET pseudo
= $1 WHERE id
= $2", array($_POST['pseudo'], $this->participant
->info
->id
));
81 pg_query_params("UPDATE participants SET password = $1 WHERE id = $2", array(sha1($_POST['pass1']), $this->participant->info->id));
82 pg_query_params("UPDATE participants SET clan_nom
= $1 WHERE id
= $2", array($_POST['clan_nom'], $this->participant
->info
->id
));
83 pg_query_params("UPDATE participants SET clan_tag = $1 WHERE id = $2", array($_POST['clan_tag'], $this->participant->info->id));
84 pg_query_params("UPDATE participants SET nom
= $1 WHERE id
= $2", array($_POST['nom'], $this->participant
->info
->id
));
85 pg_query_params("UPDATE participants SET prenom = $1 WHERE id = $2", array($_POST['prenom'], $this->participant->info->id));
86 pg_query_params("UPDATE participants SET age
= $1 WHERE id
= $2", array($_POST['age'], $this->participant
->info
->id
));
87 pg_query_params("UPDATE participants SET e_mail = $1 WHERE id = $2", array($_POST['e_mail'], $this->participant->info->id));
88 pg_query_params("UPDATE participants SET remarques
= $1 WHERE id
= $2", array($_POST['remarques'], $this->participant
->info
->id
));
89 $this->set_periodes($this->participant
->info
->id
);
91 //header("Location: /inscrits.html");
92 $this->message_utilisateur
= "Les modifications ont été enregistrées";
94 // Puisque les données du participant ont changés il faut les re-charger.
95 $this->participant
->chargerInfos();
98 # vote pour des jeux (autorisé même lorsque les inscriptions sont terminées)
99 else if (isset($_POST['set_jeux_joues']) && $this->participant
->existe())
101 $votes = $_POST['votes'];
107 # l'utilisateur peut proposer le nom d'un jeu qui ne se trouve pas dans la liste
108 $jeu = trim($_POST['jeu']);
111 if(@pg_query_params("INSERT INTO jeux (nom) VALUES ($1)", array($jeu)))
113 $row = pg_fetch_row(pg_query("SELECT LASTVAL()"));
115 if ($id != 0) # si le jeu se trouve déjà dans la liste alors $id == 0
116 array_unshift($votes, $id);
118 else # Puisque le jeu existe déjà, on le recherche
120 pg_query("ROLLBACK");
122 $res = pg_query_params("SELECT id FROM jeux WHERE nom = $1", array($jeu));
123 if ($id = pg_fetch_object($res))
124 array_unshift($votes, $id->id);
128 # suppression des anciens votes (remplacement par les nouveaux)
129 pg_query_params("DELETE FROM jeux_choisis WHERE participant_id
= $1", array($this->participant
->info
->id
));
131 # traite les trois premiers votes
132 for ($i = 0; $i < count($votes) && $i < $this->config
->get('nb_votes_jeux'); $i++
)
134 pg_query_params("INSERT INTO jeux_choisis (participant_id, jeu_id) VALUES ($1, $2)", array($this->participant
->info
->id
, (int)$votes[$i]));
142 * Traiter les données de l'inscription (trim par exemple).
144 private function traiter_donnees_inscription()
146 $_POST['pseudo'] = trim($_POST['pseudo']);
149 private function login_deja_pris()
151 if ($this->participant
->existe() && strtolower($this->participant
->info
->pseudo
) === strtolower($_POST['pseudo'])) // le pseudo n'a pas changé
154 $loginDejaPris = pg_fetch_array(pg_query_params("SELECT count(*) FROM participants WHERE pseudo = $1", array($_POST['pseudo'])));
155 if ($loginDejaPris[0] > 0)
157 $this->message_utilisateur = "Le pseudo
'".$_POST["pseudo"]."' est déjà pris
";
164 * Renvoie TRUE si les données d'une inscription sont valides (POST).
166 private function donnees_inscription_valides()
169 $_POST['pseudo'] != "" &&
170 $_POST['pass1'] != "" &&
171 $_POST['pass1'] == $_POST['pass2'] &&
172 strlen($_POST['pass1']) >= 3 &&
173 $_POST['nom'] != "" &&
174 $_POST['prenom'] != "" &&
175 $_POST['e_mail'] != "";
179 * Met à jour les periodes du participant dont l'id est donnée
180 * en fonction de $_POST["periodes
"]
181 * Attention, cette fonction doit être appelée dans une transaction.
183 private function set_periodes($id)
185 $periodes = $_POST['periodes'];
189 pg_query_params("DELETE FROM participations WHERE participant_id
= $1", array($id));
190 for ($i = 0; $i < count($periodes); $i++
)
193 INSERT INTO participations (participant_id, periode_id)
194 VALUES ($1, $2)", array($id, $periodes[$i])