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->chargerInfos($_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(true))
80 pg_query_params("UPDATE participants SET pseudo
= $1 WHERE id
= $2", array($_POST['pseudo'], $this->participant
->info
->id
));
81 if ($_POST['pass1'] != '') // Uniquement si un nouveau password est donné.
82 pg_query_params("UPDATE participants SET password = $1 WHERE id = $2", array(sha1($_POST['pass1']), $this->participant->info->id));
83 pg_query_params("UPDATE participants SET clan_nom
= $1 WHERE id
= $2", array($_POST['clan_nom'], $this->participant
->info
->id
));
84 pg_query_params("UPDATE participants SET clan_tag = $1 WHERE id = $2", array($_POST['clan_tag'], $this->participant->info->id));
85 pg_query_params("UPDATE participants SET nom
= $1 WHERE id
= $2", array($_POST['nom'], $this->participant
->info
->id
));
86 pg_query_params("UPDATE participants SET prenom = $1 WHERE id = $2", array($_POST['prenom'], $this->participant->info->id));
87 pg_query_params("UPDATE participants SET age
= $1 WHERE id
= $2", array($_POST['age'], $this->participant
->info
->id
));
88 pg_query_params("UPDATE participants SET e_mail = $1 WHERE id = $2", array($_POST['e_mail'], $this->participant->info->id));
89 pg_query_params("UPDATE participants SET remarques
= $1 WHERE id
= $2", array($_POST['remarques'], $this->participant
->info
->id
));
90 $this->set_periodes($this->participant
->info
->id
);
92 //header("Location: /inscrits.html");
93 $this->message_utilisateur
= "Les modifications ont été enregistrées";
95 // Puisque les données du participant ont changés il faut les re-charger.
96 $this->participant
->chargerInfos();
99 # vote pour des jeux (autorisé même lorsque les inscriptions sont terminées)
100 else if (isset($_POST['set_jeux_joues']) && $this->participant
->existe() && !$config->get("inscription_terminees"))
102 $votes = $_POST['votes'];
108 # l'utilisateur peut proposer le nom d'un jeu qui ne se trouve pas dans la liste
109 $jeu = trim($_POST['jeu']);
112 if(@pg_query_params("INSERT INTO jeux (participant_id, nom, type, url) VALUES ($1, $2, $3, $4)", array($this->participant
->info
->id
, $jeu, $_POST['type'], $_POST['url'])))
114 $row = pg_fetch_row(pg_query("SELECT LASTVAL()"));
116 if ($id != 0) # si le jeu se trouve déjà dans la liste alors $id == 0
117 array_unshift($votes, $id);
119 else # Puisque le jeu existe déjà, on le recherche
121 pg_query("ROLLBACK");
123 $res = pg_query_params("SELECT id FROM jeux WHERE nom = $1", array($jeu));
124 if ($id = pg_fetch_object($res))
125 if (!in_array($id->id, $votes))
126 array_unshift($votes, $id->id);
130 # suppression des anciens votes (remplacement par les nouveaux)
131 pg_query_params("DELETE FROM jeux_choisis WHERE participant_id
= $1", array($this->participant
->info
->id
));
133 # traite les trois premiers votes
134 for ($i = 0; $i < count($votes) && $i < $this->config
->get('nb_votes_jeux'); $i++
)
136 pg_query_params("INSERT INTO jeux_choisis (participant_id, jeu_id) VALUES ($1, $2)", array($this->participant
->info
->id
, (int)$votes[$i]));
144 * Traiter les données de l'inscription (trim par exemple).
146 private function traiter_donnees_inscription()
148 $_POST['pseudo'] = trim($_POST['pseudo']);
151 private function login_deja_pris()
153 if ($this->participant
->existe() && strtolower($this->participant
->info
->pseudo
) === strtolower($_POST['pseudo'])) // le pseudo n'a pas changé
156 $loginDejaPris = pg_fetch_array(pg_query_params("SELECT count(*) FROM participants WHERE pseudo = $1", array($_POST['pseudo'])));
157 if ($loginDejaPris[0] > 0)
159 $this->message_utilisateur = "Le pseudo
'".$_POST["pseudo"]."' est déjà pris
";
166 * Renvoie TRUE si les données d'une inscription sont valides (POST).
167 * $maj : égal 'TRUE' si c'est une mise à jour, dans ce cas le password est traité différement
169 private function donnees_inscription_valides($maj = FALSE)
172 $_POST['pseudo'] != "" &&
173 ($maj || $_POST['pass1'] != "") &&
174 $_POST['pass1'] == $_POST['pass2'] &&
175 (($maj && $_POST['pass1'] == "") || strlen($_POST['pass1']) >= 3) &&
176 $_POST['nom'] != "" &&
177 $_POST['prenom'] != "" &&
178 $_POST['e_mail'] != "";
182 * Met à jour les periodes du participant dont l'id est donnée
183 * en fonction de $_POST["periodes
"]
184 * Attention, cette fonction doit être appelée dans une transaction.
186 private function set_periodes($id)
188 $periodes = $_POST['periodes'];
192 pg_query_params("DELETE FROM participations WHERE participant_id
= $1", array($id));
193 for ($i = 0; $i < count($periodes); $i++
)
196 INSERT INTO participations (participant_id, periode_id)
197 VALUES ($1, $2)", array($id, $periodes[$i])