From 8606ae7b957d42df4f87b51cce3e6fa52ebe6ca9 Mon Sep 17 00:00:00 2001 From: galaxyoyo Date: Sat, 7 Sep 2019 18:08:40 +0200 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20des=20fichiers=20d'ajout=20?= =?UTF-8?q?de=20tournoi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dispatcher.php | 1 + server_files/controllers/ajouter_tournoi.php | 156 +++++++++--------- server_files/controllers/tournoi.php | 8 +- server_files/model.php | 8 + server_files/services/mail.php | 18 +- .../add_organizer_for_tournament.html | 16 ++ server_files/utils.php | 4 +- server_files/views/ajouter_tournoi.php | 11 +- 8 files changed, 129 insertions(+), 93 deletions(-) create mode 100644 server_files/services/mail_templates/add_organizer_for_tournament.html diff --git a/dispatcher.php b/dispatcher.php index ec52e4e..96f405d 100644 --- a/dispatcher.php +++ b/dispatcher.php @@ -42,6 +42,7 @@ $ROUTES["^solutions/?$"] = ["server_files/controllers/solutions.php"]; $ROUTES["^solutions_orga/?$"] = ["server_files/controllers/solutions_orga.php"]; $ROUTES["^syntheses/?$"] = ["server_files/controllers/syntheses.php"]; $ROUTES["^syntheses_orga/?$"] = ["server_files/controllers/syntheses_orga.php"]; +$ROUTES["^tournoi/(.*)/(modifier)/?$"] = ["server_files/controllers/tournoi.php", "name", "modifier"]; $ROUTES["^tournoi/(.*)/?$"] = ["server_files/controllers/tournoi.php", "name"]; $ROUTES["^tournois/?$"] = ["server_files/controllers/tournois.php"]; diff --git a/server_files/controllers/ajouter_tournoi.php b/server_files/controllers/ajouter_tournoi.php index 9670e3f..d6a9998 100644 --- a/server_files/controllers/ajouter_tournoi.php +++ b/server_files/controllers/ajouter_tournoi.php @@ -5,106 +5,100 @@ if (!isset($_SESSION["role"]) || $_SESSION["role"] != Role::ADMIN) $orgas_response = $DB->query("SELECT `id`, `surname`, `first_name` FROM `users` WHERE (`role` = 'ORGANIZER' OR `role` = 'ADMIN') AND `year` = '$YEAR';"); +$has_error = false; +$error_message = null; + if (isset($_POST["submitted"])) { - $error_message = registerTournament(); + $tournament = new NewTournament($_POST); + try { + $tournament->makeVerifications(); + $tournament->register(); + } + catch (AssertionError $e) { + $has_error = true; + $error_message = $e->getMessage(); + } } -function registerTournament() { - global $DB, $YEAR, $MAIL_ADDRESS; +class NewTournament { + public $name; + public $organizers; + public $size; + public $place; + public $price; + public $date_start; + public $date_end; + public $date_inscription; + public $time_inscription; + public $date_solutions; + public $time_solutions; + public $date_syntheses; + public $time_syntheses; + public $description; + public $final; + public $tournament; - $name = htmlspecialchars($_POST["name"]); - - $result = $DB->query("SELECT `id` FROM `tournaments` WHERE `name` = '" . $name . "' AND `year` = '$YEAR';"); - if ($result->fetch()) - return "Un tournoi existe déjà avec ce nom."; - - if (!isset($_POST["organizer"]) || sizeof($_POST["organizer"]) == 0) - return "Aucun organisateur n'a été choisi."; - - $organizers = $_POST["organizer"]; - $orga_mails = []; - - foreach ($organizers as $orga) { - $result = $DB->query("SELECT `role`, `email` FROM `users` WHERE `id` = '" . $orga . "' AND `year` = '$YEAR';"); - $data = $result->fetch(); - if ($data === FALSE) - return "L'organisateur spécifié n'existe pas."; - if ($data["role"] != Role::ORGANIZER && $data["role"] != Role::ADMIN) - return "L'organisateur indiqué ne peut pas organiser de tournoi."; - $orga_mails[] = $data["email"]; + public function __construct($data) + { + foreach ($data as $key => $value) + $this->$key = ($key == "organizers" ? $value : htmlspecialchars($value)); } - try { - $size = intval(htmlspecialchars($_POST["size"])); - } - catch (Exception $ex) { - return "Le nombre d'équipes indiqué n'est pas un entier valide."; - } + public function makeVerifications() + { + global $FINAL; - if ($size < 3 || $size > 12) - return "Un tournoi doit comporter entre 3 et 12 équipes."; + ensure($this->name != null && $this->name != "", "Le nom est invalide."); + ensure(!tournamentExists($this->name), "Un tournoi existe déjà avec ce nom."); + ensure(sizeof($this->organizers) > 0, "Aucun organisateur n'a été choisi."); - $place = htmlspecialchars($_POST["place"]); + $orgas = []; + foreach ($this->organizers as $orga_id) { + $orga = User::fromId($orga_id); + ensure($orga != null, "Un organisateur spécifié n'existe pas."); + ensure($orga->getRole() == Role::ORGANIZER || $orga->getRole() == Role::ADMIN, "Une personne indiquée ne peut pas organiser de tournoi."); + $orgas[] = $orga; + } + $this->organizers = $orgas; - try { - $price = intval(htmlspecialchars($_POST["price"])); - } - catch (Throwable $t) { - return "Le tarif pour les participants n'est pas un nombre valide."; - } + ensure(preg_match("#[0-9]*#", $this->size), "Le nombre d'équipes indiqué n'est pas un nombre valide."); + $this->size = intval($this->size); + ensure($this->size >= 3 && $this->size <= 15, "Un tournoi doit avoir au moins 3 et au plus 15 équipes."); - if ($price < 0) - return "Le TFJM² ne va pas payer les élèves pour venir."; + ensure(preg_match("#[0-9]*#", $this->price), "Le tarif pour les participants n'est pas un entier valide."); + $this->price = intval($this->price); + ensure($this->size >= 0, "Le TFJM² ne va pas payer les élèves pour venir."); + ensure($this->size <= 50, "Soyons raisonnable sur le prix."); - if ($price > 50) - return "Soyons raisonnable sur le prix."; + ensure(dateWellFormed($this->date_start), "La date de début n'est pas valide."); + ensure(dateWellFormed($this->date_end), "La date de fin n'est pas valide."); + ensure(dateWellFormed($this->date_inscription . " " . $this->time_inscription), "La date de clôture des inscriptions n'est pas valide."); + ensure(dateWellFormed($this->date_solutions . " " . $this->time_solutions), "La date limite de remise des solutions n'est pas valide."); + ensure(dateWellFormed($this->date_syntheses . " " . $this->time_syntheses), "La date limite de remise des notes de synthèse n'est pas valide."); - $date_start = htmlspecialchars($_POST["date_start"]); - $date_start_parsed = date_parse_from_format("yyyy-mm-dd", $date_start); + $this->final = $this->final ? 1 : 0; - $date_end = htmlspecialchars($_POST["date_end"]); - $date_end_parsed = date_parse_from_format("yyyy-mm-dd", $date_end); + ensure(!$this->final || $FINAL == NULL, "Une finale nationale est déjà enregistrée."); + } - $date_inscription = htmlspecialchars($_POST["date_inscription"]); - $time_inscription = htmlspecialchars($_POST["time_inscription"]); - $date_inscription_parsed = date_parse_from_format("yyyy-mm-dd", $date_inscription . ' ' . $time_inscription); + public function register() + { + global $DB, $YEAR; - $date_solutions = htmlspecialchars($_POST["date_solutions"]); - $time_solutions = htmlspecialchars($_POST["time_solutions"]); - $date_solutions_parsed = date_parse_from_format("yyyy-mm-dd", $date_solutions . ' ' . $time_solutions); - - $date_syntheses = htmlspecialchars($_POST["date_syntheses"]); - $time_syntheses = htmlspecialchars($_POST["time_syntheses"]); - $date_syntheses_parsed = date_parse_from_format("yyyy-mm-dd", $date_syntheses . ' ' . $time_syntheses); - - if (!$date_start_parsed || !$date_end_parsed || !$date_inscription_parsed || !$date_solutions_parsed || !$date_syntheses_parsed) - return "Une date est mal formée."; - - $description = htmlspecialchars($_POST["description"]); - - $final = isset($_POST["final"]) && $_POST["final"]; - - if ($final && $DB->query("SELECT `id` FROM `tournaments` WHERE `final` = true AND `year` = $YEAR;")->fetch() !== false) - return "Une finale est déjà enregistrée."; - - $req = $DB->prepare("INSERT INTO `tournaments` (`name`, `size`, `place`, `price`, `description`, + $req = $DB->prepare("INSERT INTO `tournaments` (`name`, `size`, `place`, `price`, `description`, `date_start`, `date_end`, `date_inscription`, `date_solutions`, `date_syntheses`, `final`, `year`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); - $req->execute([$name, $size, $place, $price, $description, $date_start, $date_end, - "$date_inscription $time_inscription", "$date_solutions $time_solutions", "$date_syntheses $time_syntheses", $final, $YEAR]); + $req->execute([$this->name, $this->size, $this->place, $this->price, $this->description, $this->date_start, $this->date_end, + "$this->date_inscription $this->time_inscription", "$this->date_solutions $this->time_solutions", "$this->date_syntheses $this->time_syntheses", $this->final ? 1 : 0, $YEAR]); - $req = $DB->query("SELECT `id` FROM `tournaments` WHERE `name` = '$name' AND `year` = $YEAR;"); - $tournament_id = $req->fetch()["id"]; + $this->tournament = Tournament::fromName($this->name); - foreach ($organizers as $orga) { - $req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);"); - $req->execute([$orga, $tournament_id]); - } - - foreach ($orga_mails as $orga_mail) - mail($orga_mail, "Organisateur TFJM² " . $name, "Vous venez d'être promu organisateur du tournoi " . $name . " pour le TFJM² $YEAR !", "From: $MAIL_ADDRESS"); - - return false; + foreach ($this->organizers as $organizer) { + $req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);"); + $req->execute([$organizer->getId(), $this->tournament->getId()]); + sendAddOrganizerForTournamentMail($organizer, $this->tournament); + } + } } require_once "server_files/views/ajouter_tournoi.php"; diff --git a/server_files/controllers/tournoi.php b/server_files/controllers/tournoi.php index 43010f5..84f9fb7 100644 --- a/server_files/controllers/tournoi.php +++ b/server_files/controllers/tournoi.php @@ -30,7 +30,7 @@ else $orgas_response = $DB->query("SELECT `id`, `surname`, `first_name` FROM `users` WHERE (`role` = 'ORGANIZER' OR `role` = 'ADMIN') AND `year` = '$YEAR';"); function updateTournament() { - global $DB, $URL_BASE, $YEAR, $tournament; + global $DB, $URL_BASE, $YEAR, $tournament, $orgas; $name = htmlspecialchars($_POST["name"]); @@ -43,7 +43,7 @@ function updateTournament() { if ($_SESSION["role"] == Role::ADMIN) { $organizers = $_POST["organizer"]; - $orga_mails = []; + $orgas = []; foreach ($organizers as $orga_id) { $orga = User::fromId($orga_id); @@ -51,7 +51,7 @@ function updateTournament() { return "L'organisateur spécifié n'existe pas."; if ($orga->getRole() != Role::ORGANIZER && $orga->getRole() != Role::ADMIN) return "L'organisateur indiqué ne peut pas organiser de tournoi."; - $orga_mails[] = $orga->getEmail(); + $orgas[] = $orga; } } @@ -111,7 +111,7 @@ function updateTournament() { if ($_SESSION["role"] == Role::ADMIN) { $DB->exec("DELETE FROM `organizers` WHERE `tournament` = " . $tournament->getId() . ";"); - foreach ($organizers as $orga) { + foreach ($orgas as $orga) { $req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);"); $req->execute([$orga->getId(), $tournament->getId()]); } diff --git a/server_files/model.php b/server_files/model.php index 61548c6..afe6503 100644 --- a/server_files/model.php +++ b/server_files/model.php @@ -111,4 +111,12 @@ function trigramExists($trigram) { $req = $DB->prepare("SELECT `id` FROM `teams` WHERE `trigram` = ? AND `year` = '$YEAR';"); $req->execute([$trigram]); return $req->fetch(); +} + +function tournamentExists($name) { + global $DB, $YEAR; + + $req = $DB->prepare("SELECT `id` FROM `tournaments` WHERE `name` = ? AND `year` = '$YEAR';"); + $req->execute([$name]); + return $req->fetch(); } \ No newline at end of file diff --git a/server_files/services/mail.php b/server_files/services/mail.php index 32b09f4..cc45bec 100644 --- a/server_files/services/mail.php +++ b/server_files/services/mail.php @@ -61,5 +61,21 @@ function sendAddOrganizerMail($new_orga) $content = preg_replace("#{SURNAME}#", $new_orga->surname, $content); $content = preg_replace("#{PASSWORD}#", $new_orga->password, $content); - sendMail($new_orga->email, "Inscription au TFJM² $YEAR", $content); + sendMail($new_orga->email, "Ajout d'un organisateur -- TFJM² $YEAR", $content); +} + +/** + * @param $organizer User + * @param $tournament Tournament + */ +function sendAddOrganizerForTournamentMail($organizer, $tournament) +{ + global $LOCAL_PATH, $YEAR; + + $content = file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/add_organizer_for_tournament.html"); + $content = preg_replace("#{FIRST_NAME}#", $organizer->getFirstName(), $content); + $content = preg_replace("#{SURNAME}#", $organizer->getSurname(), $content); + $content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content); + + sendMail($organizer->getEmail(), "Ajout d'un organisateur pour le tournoi " . $tournament->getName() . "-- TFJM² $YEAR", $content); } diff --git a/server_files/services/mail_templates/add_organizer_for_tournament.html b/server_files/services/mail_templates/add_organizer_for_tournament.html new file mode 100644 index 0000000..bbc6e03 --- /dev/null +++ b/server_files/services/mail_templates/add_organizer_for_tournament.html @@ -0,0 +1,16 @@ + + + + + Organisateur du tournoi de {TOURNAMENT_NAME} -- TFJM² + + +Bonjour {FIRST_NAME} {SURNAME},
+
+Vous venez d'être promu organisateur du tournoi {TOURNAMENT_NAME} du TFJM2 {YEAR}.
+
+Cordialement,
+
+Le comité national d'organisation du TFJM2 + + \ No newline at end of file diff --git a/server_files/utils.php b/server_files/utils.php index 12671cb..342a268 100644 --- a/server_files/utils.php +++ b/server_files/utils.php @@ -12,6 +12,6 @@ function formatDate($date = NULL, $with_time = false) { return strftime("%d %B %G" . ($with_time ? " %H:%M" : ""), strtotime($date)); } -function dateWellFormed($date, $format = "yyyy-mm-dd") { - return date_parse_from_format($format, $date) !== false; +function dateWellFormed($date, $with_time = false) { + return date_parse_from_format($with_time ? "yyyy-mm-dd HH-MM:ss" : "yy-mm-dd", $date) !== false; } \ No newline at end of file diff --git a/server_files/views/ajouter_tournoi.php b/server_files/views/ajouter_tournoi.php index d62116a..309752e 100644 --- a/server_files/views/ajouter_tournoi.php +++ b/server_files/views/ajouter_tournoi.php @@ -1,10 +1,11 @@ Erreur : " . $error_message . ""; - } else { + } + else { echo "

Tournoi de " . htmlspecialchars($_POST["name"]) . " ajouté avec succès !

"; } }?> @@ -23,10 +24,10 @@ if (isset($error_message)) { - + - fetch()) !== FALSE) { echo "\n";