diff --git a/public/index.php b/public/index.php index cd4868d..d8dd098 100644 --- a/public/index.php +++ b/public/index.php @@ -11,6 +11,7 @@ use App\Controller\FrontController; use App\Session\PhpSessionHandle; $basePath = get_public_path(); + $frontController = new FrontController($basePath); $frontController->run(PhpSessionHandle::init()); diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index e105a1e..06bb32b 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -22,6 +22,15 @@ CREATE TABLE Tactic FOREIGN KEY (owner) REFERENCES Account ); +CREATE TABLE FormEntries(name varchar, description varchar); + +CREATE TABLE AccountUser( + id integer PRIMARY KEY AUTOINCREMENT, + username varchar, + hash varchar, + email varchar unique +); + CREATE TABLE Team ( id integer PRIMARY KEY AUTOINCREMENT, @@ -36,9 +45,9 @@ CREATE TABLE User id integer PRIMARY KEY AUTOINCREMENT ); -CREATE TABLE Member -( - idTeam integer, + +CREATE TABLE Member( + idTeam integer, idMember integer, role char(1) CHECK (role IN ('C', 'P')), FOREIGN KEY (idTeam) REFERENCES Team (id), diff --git a/src/Controller/Sub/TeamController.php b/src/Controller/Sub/TeamController.php index c3991a3..d85075a 100644 --- a/src/Controller/Sub/TeamController.php +++ b/src/Controller/Sub/TeamController.php @@ -11,23 +11,32 @@ use App\Validation\Validators; class TeamController { private TeamModel $model; + private Environment $twig; /** * @param TeamModel $model + * @param Environment $twig */ - public function __construct(TeamModel $model) { + public function __construct(TeamModel $model, Environment $twig) { $this->model = $model; + $this->twig = $twig; } public function displaySubmitTeam(): HttpResponse { return ViewHttpResponse::twig("insert_team.html.twig", []); } - /** - * @param array $request - * @return HttpResponse - */ + + public function displayAddMember() : HttpResponse { + return ViewHttpResponse::twig("add_member.html.twig", []); + } + + public function displayDeleteMember() : HttpResponse { + return ViewHttpResponse::twig("delete_member.html.twig", []); + } + public function submitTeam(array $request): HttpResponse { + $errors = []; $request = HttpRequest::from($request, $errors, [ @@ -80,4 +89,26 @@ class TeamController { $result = $this->model->displayTeam($id); return ViewHttpResponse::twig('display_team.html.twig', ['team' => $result]); } + + public function addMember(array $request) : HttpResponse { + $errors = []; + + $request = HttpRequest::from($request, $errors, [ + "team" => [Validators::isInteger()], + "mail" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] + ]); + + return $this->getTeam($this->model->addMember($request['mail'], intval($request['team']), $request['role'])); + } + + public function deleteMember(array $request) : HttpResponse { + $errors = []; + + $request = HttpRequest::from($request, $errors, [ + "team" => [Validators::isInteger()], + "mail" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] + ]); + + return $this->getTeam($this->model->deleteMember($request['mail'], intval($request['team']))); + } } diff --git a/src/Data/Account.php b/src/Data/Account.php index 0ed4339..6a61563 100755 --- a/src/Data/Account.php +++ b/src/Data/Account.php @@ -2,8 +2,6 @@ namespace App\Data; -use http\Exception\InvalidArgumentException; - /** * Base class of a user account. * Contains the private information that we don't want diff --git a/src/Data/Member.php b/src/Data/Member.php index b415f65..64beb51 100755 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -7,9 +7,14 @@ namespace App\Data; */ class Member { /** - * @var int The member's user id + * @var AccountUser The member's user account */ - private int $userId; + private AccountUser $user; + + /** + * @var int The member's team id + */ + private int $teamId; /** * @var MemberRole the member's role @@ -20,8 +25,9 @@ class Member { * @param int $userId * @param MemberRole $role */ - public function __construct(int $userId, MemberRole $role) { - $this->userId = $userId; + public function __construct(Account $user, int $teamId, MemberRole $role) { + $this->user = $user; + $this->teamId = $teamId; $this->role = $role; } @@ -39,4 +45,12 @@ class Member { public function getRole(): MemberRole { return $this->role; } + + /** + * @return int + */ + public function getTeamId(): int + { + return $this->teamId; + } } diff --git a/src/Data/Team.php b/src/Data/Team.php index e50ad40..a359722 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -72,4 +72,8 @@ class Team { return $this->members; } + public function addMember(Member $m) { + $this->members[] = $m; + } + } diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php new file mode 100644 index 0000000..3370d26 --- /dev/null +++ b/src/Gateway/AuthGateway.php @@ -0,0 +1,47 @@ +con = $con; + } + + + public function mailExist(string $email): bool { + return $this->getUserFields($email) != null; + } + + + public function insertAccount(string $username, string $hash, string $email): void { + $this->con->exec("INSERT INTO AccountUser(username, hash, email) VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash' => [$hash, PDO::PARAM_STR],':email' => [$email, PDO::PARAM_STR]]); + } + + public function getUserHash(string $email): string { + $results = $this->con->fetch("SELECT hash FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]); + return $results[0]['hash']; + } + + + /** + * @param string $email + * @return array|null + */ + public function getUserFields(string $email): ?array { + $results = $this->con->fetch("SELECT username,email FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]); + $firstRow = $results[0] ?? null; + return $firstRow; + } + + + + +} diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index c3d22dc..6c13571 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -24,10 +24,19 @@ class TeamGateway { ); } - /** - * @param string $name - * @return array[] - */ + + public function insertMember(int $idTeam, int $idMember, string $role) { + $this->con->exec( + "INSERT INTO Member(idTeam, idMember, role) VALUES (:idTeam , :idMember, :role)", + [ + ":idTeam" => [$idTeam, PDO::PARAM_INT], + ":idMember" => [$idMember, PDO::PARAM_INT], + ":role" => [$role, PDO::PARAM_STR] + ] + ); + } + + public function listByName(string $name): array { return $this->con->fetch( "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'", @@ -69,11 +78,30 @@ class TeamGateway { */ public function getMembersById(int $id): array { return $this->con->fetch( - "SELECT m.role,u.id FROM User u,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = u.id", - [ - ":id" => [$id, PDO::PARAM_INT], + "SELECT u.id,m.role,u.email,u.username FROM AccountUser u,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = u.id", + [ + ":id" => [$id, PDO::PARAM_INT] ] ); } + public function getMemberIdByMail($mail) : array { + return $this->con->fetch( + "SELECT id FROM AccountUser WHERE email = :mail", + [ + ":mail" => [$mail, PDO::PARAM_STR] + ] + ); + } + + public function deleteMember(int $idTeam, int $idMember) { + $this->con->exec( + "DELETE FROM Member WHERE idTeam = :idTeam AND idMember = :idMember", + [ + ":idTeam" => [$idTeam, PDO::PARAM_INT], + ":idMember" => [$idMember, PDO::PARAM_INT], + ] + ); + } + } diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 65f22a7..5a3ce5f 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -24,10 +24,14 @@ class TeamModel { return intval($result[0]['id']); } - /** - * @param string $name - * @return Team[] - */ + + public function addMember(string $mail, int $teamId, string $role) : int { + $result = $this->gateway->getMemberIdByMail($mail)[0]; + $memberId = intval($result['id']); + $this->gateway->insertMember($teamId, $memberId, $role); + return $teamId; + } + public function listByName(string $name): array { $teams = []; $results = $this->gateway->listByName($name); @@ -47,8 +51,16 @@ class TeamModel { } else { $role = MemberRole::player(); } - $members[] = new Member($row['id'], $role); + $members[] = new Member($row['id'], $id, $role); } return new Team(intval($result['id']), $result['name'], $result['picture'], Color::from($result['mainColor']), Color::from($result['secondColor']), $members); } + + public function deleteMember(string $mail, int $teamId) : int { + $result = $this->gateway->getMemberIdByMail($mail)[0]; + $memberId = intval($result['id']); + $this->gateway->deleteMember($teamId, $memberId); + return $teamId; + } + } diff --git a/src/Views/add_member.html.twig b/src/Views/add_member.html.twig new file mode 100644 index 0000000..7faef23 --- /dev/null +++ b/src/Views/add_member.html.twig @@ -0,0 +1,103 @@ + + + + + Ajouter un membre + + + + +
+

Ajouter un membre à votre équipe

+
+
+ + + + + +
+ Rôle du membre dans l'équipe : +
+ + +
+
+ + +
+
+ +
+
+ +
+
+
+ + + \ No newline at end of file diff --git a/src/Views/delete_member.html.twig b/src/Views/delete_member.html.twig new file mode 100644 index 0000000..9be5803 --- /dev/null +++ b/src/Views/delete_member.html.twig @@ -0,0 +1,73 @@ + + + + + Ajouter un membre + + + + +
+

Supprimez un membre de votre équipe

+
+
+ + + + +
+
+ +
+
+
+ + + \ No newline at end of file diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index ada8566..f1207db 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -54,6 +54,7 @@ height: 80px; width: 80px; } + @@ -72,8 +73,14 @@

Couleur principale :

Couleur secondaire :

- {% for m in team.members %} -

m.id

+ + {% for m in team.listMembers() %} +

{{ m.getUserId() }}

+ {% if m.getRole() == 'C' %} +

: Coach

+ {% else %} +

: Joueur

+ {% endif %} {% endfor %}