2019-09-05 17:07:41 +00:00
|
|
|
<?php
|
|
|
|
|
2019-09-06 23:33:05 +00:00
|
|
|
if (!isset($_SESSION["role"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT))
|
2019-09-07 11:42:36 +00:00
|
|
|
require_once "server_files/403.php";
|
2019-09-06 23:33:05 +00:00
|
|
|
|
2019-12-19 12:02:01 +00:00
|
|
|
$tournaments_response = $DB->query("SELECT `id`, `name` FROM `tournaments` WHERE `date_inscription` > CURRENT_TIMESTAMP AND `year` = $YEAR;");
|
2019-09-05 17:07:41 +00:00
|
|
|
|
2019-09-07 14:58:23 +00:00
|
|
|
$has_error = false;
|
|
|
|
$error_message = null;
|
|
|
|
|
2019-12-19 12:02:01 +00:00
|
|
|
if (isset($_POST["add_team"])) {
|
2019-09-07 14:58:23 +00:00
|
|
|
$new_team = new NewTeam($_POST);
|
|
|
|
try {
|
|
|
|
$new_team->makeVerifications();
|
|
|
|
$new_team->register();
|
|
|
|
}
|
|
|
|
catch (AssertionError $e) {
|
|
|
|
$has_error = true;
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
2019-09-05 17:07:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 14:58:23 +00:00
|
|
|
class NewTeam {
|
|
|
|
public $name;
|
|
|
|
public $trigram;
|
|
|
|
public $tournament_id;
|
|
|
|
public $tournament;
|
|
|
|
public $access_code;
|
|
|
|
|
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeVerifications() {
|
|
|
|
ensure($_SESSION["team"] == null, "Vous êtes déjà dans une équipe.");
|
|
|
|
ensure($this->name != null && $this->name != "", "Vous devez spécifier un nom d'équipe.");
|
|
|
|
ensure(preg_match("#^[A-Z]{3}$#", $this->trigram), "Le trigramme entré n'est pas valide.");
|
|
|
|
ensure(!teamExists($this->name), "Une équipe existe déjà avec ce nom.");
|
|
|
|
ensure(!trigramExists($this->trigram), "Une équipe a déjà choisi ce trigramme.");
|
|
|
|
$this->tournament = Tournament::fromId($this->tournament_id);
|
|
|
|
ensure($this->tournament != null, "Le tournoi spécifié n'existe pas.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function register() {
|
|
|
|
global $DB, $YEAR;
|
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
$this->access_code = genRandomPhrase(6);
|
2019-09-07 14:58:23 +00:00
|
|
|
|
|
|
|
$req = $DB->prepare("INSERT INTO `teams` (`name`, `trigram`, `tournament`, `encadrant_1`, `participant_1`, `validation_status`, `access_code`, `year`)
|
2019-09-05 17:07:41 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
|
2019-09-07 14:58:23 +00:00
|
|
|
$req->execute([$this->name, $this->trigram, $this->tournament_id, $_SESSION["role"] == Role::ENCADRANT ? $_SESSION["user_id"] : NULL,
|
2020-01-16 22:00:31 +00:00
|
|
|
$_SESSION["role"] == Role::PARTICIPANT ? $_SESSION["user_id"] : NULL, ValidationStatus::getName(ValidationStatus::NOT_READY), $this->access_code, $YEAR]);
|
2019-09-05 17:07:41 +00:00
|
|
|
|
2019-09-07 14:58:23 +00:00
|
|
|
$_SESSION["team"] = Team::fromTrigram($this->trigram);
|
|
|
|
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
|
2019-09-05 17:07:41 +00:00
|
|
|
|
2019-09-08 10:45:48 +00:00
|
|
|
Mailer::sendAddTeamMail($_SESSION["user"], $_SESSION["team"], $this->tournament);
|
2019-09-07 14:58:23 +00:00
|
|
|
}
|
2019-09-05 17:07:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 11:42:36 +00:00
|
|
|
require_once "server_files/views/ajouter_equipe.php";
|