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 abf5049..b13712f 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -22,6 +22,9 @@ CREATE TABLE Tactic FOREIGN KEY (owner) REFERENCES Account ); +CREATE TABLE FormEntries(name varchar, description varchar); + + CREATE TABLE Team ( id integer PRIMARY KEY AUTOINCREMENT, @@ -31,14 +34,9 @@ CREATE TABLE Team secondColor varchar ); -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..adefa41 100644 --- a/src/Controller/Sub/TeamController.php +++ b/src/Controller/Sub/TeamController.php @@ -23,11 +23,17 @@ class TeamController { 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 +86,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/Controller/UserController.php b/src/Controller/UserController.php index 674f81e..46f4992 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -62,4 +62,23 @@ class UserController extends VisitorController { $ctrl = new Sub\TeamController($model); return $ctrl->getTeam($id); } + + public function addMember(): HttpResponse { + $model = new TeamModel(new TeamGateway(new Connexion(get_database()))); + $ctrl = new Sub\TeamController($model); + if ($_SERVER['REQUEST_METHOD'] === 'GET') { + return $ctrl->displayAddMember($_POST); + } + return $ctrl->addMember($_POST); + } + + public function deleteMember(): HttpResponse { + $model = new TeamModel(new TeamGateway(new Connexion(get_database()))); + $ctrl = new Sub\TeamController($model); + if ($_SERVER['REQUEST_METHOD'] === 'GET') { + return $ctrl->displayDeleteMember($_POST); + } + return $ctrl->deleteMember($_POST); + } + } 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..3049fde 100755 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -7,10 +7,15 @@ namespace App\Data; */ class Member { /** - * @var int The member's user id + * @var AccountUser The member's user account */ private int $userId; + /** + * @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) { + public function __construct(int $userId, int $teamId, MemberRole $role) { $this->userId = $userId; + $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..c98d803 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 a.id,m.role,a.email,a.username FROM Account a,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = a.id", + [ + ":id" => [$id, PDO::PARAM_INT] ] ); } + public function getMemberIdByMail($mail) : array { + return $this->con->fetch( + "SELECT id FROM Account 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..c936d30 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); @@ -42,13 +46,22 @@ class TeamModel { $result = $this->gateway->getTeamById($id)[0]; $resultMembers = $this->gateway->getMembersById($id); foreach ($resultMembers as $row) { + var_dump($row['role']); if ($row['role'] == 'C') { $role = MemberRole::coach(); } 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..ef74da2 --- /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..a478284 --- /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..474397e 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().isCoach() %} +

: Coach

+ {% else %} +

: Joueur

+ {% endif %} {% endfor %} diff --git a/src/Views/display_teams.html.twig b/src/Views/display_teams.html.twig index 47b3cbc..bf89909 100644 --- a/src/Views/display_teams.html.twig +++ b/src/Views/display_teams.html.twig @@ -10,7 +10,7 @@

Aucune équipe n'a été trouvée

Chercher une équipe

-
+
@@ -22,7 +22,7 @@
{% else %} {% for t in teams %} -
+

Nom de l'équipe : {{ t.name }}

logo de l'équipe
diff --git a/src/Views/list_team_by_name.html.twig b/src/Views/list_team_by_name.html.twig index 1165da3..0ea1905 100644 --- a/src/Views/list_team_by_name.html.twig +++ b/src/Views/list_team_by_name.html.twig @@ -62,7 +62,7 @@

Chercher une équipe

- +