From 51f150a16a746c8401585b48ea296a0a46171e54 Mon Sep 17 00:00:00 2001 From: "vivien.dufour" Date: Wed, 10 Jan 2024 14:57:56 +0100 Subject: [PATCH] can share tactic to team or account and can unshare tactic to everyone (build will fail because need to modify home page) --- front/views/Home.tsx | 1 + public/index.php | 19 +++-- src/App/Controller/TacticController.php | 38 ++++++++-- src/App/Controller/TeamController.php | 2 +- src/App/Controller/UserController.php | 9 ++- src/App/Views/display_tactic.html.twig | 18 +++-- src/App/Views/display_user_teams.html.twig | 21 ------ .../display_user_teams_accounts.html.twig | 47 ++++++++++++ src/Core/Gateway/AccountGateway.php | 30 ++++++++ src/Core/Gateway/TacticInfoGateway.php | 34 ++++++++- src/Core/Gateway/TeamGateway.php | 19 +++++ src/Core/Model/TacticModel.php | 74 ++++++++++++++++++- src/Core/Model/TeamModel.php | 8 ++ 13 files changed, 271 insertions(+), 49 deletions(-) delete mode 100644 src/App/Views/display_user_teams.html.twig create mode 100644 src/App/Views/display_user_teams_accounts.html.twig diff --git a/front/views/Home.tsx b/front/views/Home.tsx index 132457c..6f22f01 100644 --- a/front/views/Home.tsx +++ b/front/views/Home.tsx @@ -29,6 +29,7 @@ export default function Home({ teams: Team[] username: string }) { + console.log(allTactics) return (
diff --git a/public/index.php b/public/index.php index 3e1a9d3..fb075e7 100644 --- a/public/index.php +++ b/public/index.php @@ -39,15 +39,15 @@ function getConnection(): Connection { } function getUserController(): UserController { - return new UserController(new TacticModel(new TacticInfoGateway(getConnection())), new TeamModel(new TeamGateway(getConnection()), new MemberGateway(getConnection()), new AccountGateway(getConnection()))); + return new UserController(new TacticModel(new TacticInfoGateway(getConnection()), new AccountGateway(getConnection())), new TeamModel(new TeamGateway(getConnection()), new MemberGateway(getConnection()), new AccountGateway(getConnection()))); } function getVisualizerController(): VisualizerController { - return new VisualizerController(new TacticModel(new TacticInfoGateway(getConnection()))); + return new VisualizerController(new TacticModel(new TacticInfoGateway(getConnection()), new AccountGateway(getConnection()))); } function getEditorController(): EditorController { - return new EditorController(new TacticModel(new TacticInfoGateway(getConnection()))); + return new EditorController(new TacticModel(new TacticInfoGateway(getConnection()), new AccountGateway(getConnection()))); } function getTeamController(): TeamController { @@ -56,7 +56,7 @@ function getTeamController(): TeamController { } function getTacticController(): TacticController { - return new TacticController(new TacticModel(new TacticInfoGateway(getConnection())), new TeamModel( new TeamGateway(getConnection()), new MemberGateway(getConnection()), new AccountGateway(getConnection()))); + return new TacticController(new TacticModel(new TacticInfoGateway(getConnection()), new AccountGateway(getConnection())), new TeamModel( new TeamGateway(getConnection()), new MemberGateway(getConnection()), new AccountGateway(getConnection()))); } @@ -92,10 +92,16 @@ function getRoutes(): AltoRouter { $ar->map("GET", "/settings", Action::auth(fn(SessionHandle $s) => getUserController()->settings($s))); $ar->map("GET", "/disconnect", Action::auth(fn(MutableSessionHandle $s) => getUserController()->disconnect($s))); - $ar->map("GET", "/shareTactic", Action::auth(fn(SessionHandle $s) => getTacticController()->displayTactic($s))); - $ar->map("GET", "/shareTactic/[i:id]", Action::auth(fn(int $id, SessionHandle $s) => getTacticController()->displayTeam($id, $s))); + + $ar->map("GET", "/shareTactic", Action::auth(fn(SessionHandle $s) => getTacticController()->displayTactic(true, $s))); + $ar->map("GET", "/shareTactic/[i:id]", Action::auth(fn(int $id, SessionHandle $s) => getTacticController()->displayTeamAndAccount(true, $id, $s))); $ar->map("GET", "/shareTactic/[i:id]/team/[i:idTeam]", Action::auth(fn(int $tacticId, int $teamId, SessionHandle $s) => getTacticController()->displayShareConfirmation($tacticId, $teamId, $s))); $ar->map("POST", "/shareTactic/[i:id]/team/[i:idTeam]", Action::auth(fn(int $tacticId, int $teamId, SessionHandle $s) => getTacticController()->shareTacticToTeam($_POST, $tacticId, $teamId, $s))); + $ar->map("POST", "/shareTactic/[i:id]/account", Action::auth(fn(int $tacticId, SessionHandle $s) => getTacticController()->shareTacticToAccount($_POST, $tacticId, $s))); + + $ar->map("GET", "/unshareTactic", Action::auth(fn(SessionHandle $s) => getTacticController()->displayTactic(false, $s))); + $ar->map("GET", "/unshareTactic/[i:id]", Action::auth(fn(int $id, SessionHandle $s) => getTacticController()->displayTeamAndAccount(false, $id, $s))); + $ar->map("POST", "/unshareTactic/[i:id]", Action::auth(fn(int $id, SessionHandle $s) => getTacticController()->unshareTactic($id, $s))); //tactic-related @@ -120,7 +126,6 @@ function getRoutes(): AltoRouter { $ar->map("GET", "/team/[i:id]/edit", Action::auth(fn(int $idTeam, SessionHandle $s) => getTeamController()->displayEditTeam($idTeam, $s))); $ar->map("POST", "/team/[i:id]/edit", Action::auth(fn(int $idTeam, SessionHandle $s) => getTeamController()->editTeam($idTeam, $_POST, $s))); - return $ar; } diff --git a/src/App/Controller/TacticController.php b/src/App/Controller/TacticController.php index 8e7f2b6..f2ee6a1 100644 --- a/src/App/Controller/TacticController.php +++ b/src/App/Controller/TacticController.php @@ -18,14 +18,26 @@ class TacticController $this->teams = $teams; } - public function displayTactic(SessionHandle $session): ViewHttpResponse { - $results = $this->tactics->getAll($session->getAccount()->getUser()->getId()); - return ViewHttpResponse::twig("display_tactic.html.twig", ['tactics' => $results]); + public function displayTactic(bool $toShare, SessionHandle $session): ViewHttpResponse { + if($toShare) { + $results = $this->tactics->getAll($session->getAccount()->getUser()->getId()); + } + else { + $results = $this->tactics->getAllTacticSharedOwned($session->getAccount()->getUser()->getId()); + } + + return ViewHttpResponse::twig("display_tactic.html.twig", ['tactics' => $results, 'toShare' => $toShare]); } - public function displayTeam(int $tacticId, SessionHandle $session): ViewHttpResponse { - $results = $this->teams->getAll($session->getAccount()->getUser()->getId()); - return ViewHttpResponse::twig("display_user_teams.html.twig", ['teams' => $results, 'tactic' => $tacticId]); + + public function displayTeamAndAccount(bool $toShare, int $tacticId, SessionHandle $session): ViewHttpResponse { + if($toShare) { + $results = $this->teams->getAllIsCoach($session->getAccount()->getUser()->getId()); + } + else { + $results = $this->teams->getAllIsCoach($session->getAccount()->getUser()->getId()); + } + return ViewHttpResponse::twig("display_user_teams_accounts.html.twig", ['teams' => $results, 'tactic' => $tacticId, 'toShare' => $toShare]); } public function displayShareConfirmation(int $tacticId, int $teamId, SessionHandle $session) : ViewHttpResponse { @@ -39,4 +51,18 @@ class TacticController } return ViewHttpResponse::redirect("/"); } + + public function shareTacticToAccount(array $request, int $tacticId, SessionHandle $session) : HttpResponse + { + $email = $request["email"]; + $this->tactics->shareTacticToAccountMail($email, $tacticId); + return ViewHttpResponse::redirect("/"); + } + + public function unshareTactic(int $tacticId, SessionHandle $session) : HttpResponse + { + $this->tactics->unshareTactic($tacticId); + $this->teams->unshareTactic($tacticId); + return ViewHttpResponse::redirect("/"); + } } diff --git a/src/App/Controller/TeamController.php b/src/App/Controller/TeamController.php index 174c971..f2fc690 100644 --- a/src/App/Controller/TeamController.php +++ b/src/App/Controller/TeamController.php @@ -246,7 +246,7 @@ class TeamController { public function shareTactic(int $teamId, int $tacticId, SessionHandle $session): ViewHttpResponse { - $result = $this->model->shareTacticToTeam($teamId, $tacticId); + $this->model->shareTacticToTeam($teamId, $tacticId); return $this->displayTeam($teamId, $session); } } diff --git a/src/App/Controller/UserController.php b/src/App/Controller/UserController.php index 8755115..308a525 100644 --- a/src/App/Controller/UserController.php +++ b/src/App/Controller/UserController.php @@ -39,15 +39,17 @@ class UserController { if ($this->teams != null) { $teams = $this->teams->getAll($user->getId()); - $allTacticsSharedTeam = $this->tactics->getAllTacticSharedTeam($user->getId()); - if(isset($allTacticsSharedTeam)) { - foreach ($allTacticsSharedTeam as $tactic) { + $allTacticsShared = $this->tactics->getAllTacticShared($user->getId()); + if(isset($allTacticsShared)) { + foreach ($allTacticsShared as $tactic) { if(!in_array($tactic, $allTactics)) { array_push($allTactics, $tactic); } } } + var_dump($allTactics); + var_dump($teams); } else { $teams = []; } @@ -71,5 +73,4 @@ class UserController { $session->destroy(); return HttpResponse::redirect("/"); } - } diff --git a/src/App/Views/display_tactic.html.twig b/src/App/Views/display_tactic.html.twig index 0877bd5..ee48d47 100644 --- a/src/App/Views/display_tactic.html.twig +++ b/src/App/Views/display_tactic.html.twig @@ -9,11 +9,19 @@

IQBall

- {% for t in tactics %} -
-

{{ t.name }}

-
- {% endfor %} + {% if toShare %} + {% for t in tactics %} +
+

{{ t.name }}

+
+ {% endfor %} + {% else %} + {% for t in tactics %} +
+

{{ t.name }}

+
+ {% endfor %} + {% endif %} \ No newline at end of file diff --git a/src/App/Views/display_user_teams.html.twig b/src/App/Views/display_user_teams.html.twig deleted file mode 100644 index e801af5..0000000 --- a/src/App/Views/display_user_teams.html.twig +++ /dev/null @@ -1,21 +0,0 @@ - - - - - Twig view - - - -{% if teams is empty %} -

Vous n'êtes dans aucune équipe

-{% else %} - {% for team in teams %} -
-

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

- logo de l'équipe -
- {% endfor %} -{% endif %} - - - \ No newline at end of file diff --git a/src/App/Views/display_user_teams_accounts.html.twig b/src/App/Views/display_user_teams_accounts.html.twig new file mode 100644 index 0000000..21e60e0 --- /dev/null +++ b/src/App/Views/display_user_teams_accounts.html.twig @@ -0,0 +1,47 @@ + + + + + Twig view + + + +{% if toShare %} + {% if teams is empty %} +

Vous n'êtes dans aucune équipe

+ {% else %} + {% for team in teams %} +
+

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

+ logo de l'équipe +
+ {% endfor %} + {% endif %} + +

Partager à un compte

+
+
+ + + {% if badEmail %} +

Email invalide

+ {% endif %} + {%if notFound %} +

Cette personne n'a pas été trouvé

+ {% endif %} + +
+
+ +
+
+{% else %} +
+ +
+{% endif %} + + + + + \ No newline at end of file diff --git a/src/Core/Gateway/AccountGateway.php b/src/Core/Gateway/AccountGateway.php index a9c3e18..fa098e7 100644 --- a/src/Core/Gateway/AccountGateway.php +++ b/src/Core/Gateway/AccountGateway.php @@ -69,6 +69,15 @@ class AccountGateway { return new Account($acc["token"], new User($email, $acc["username"], $acc["id"], $acc["profilePicture"])); } + public function getAccountIdFromMail(string $email): array { + return $this->con->fetch( + "SELECT id FROM Account WHERE email = :mail", + [ + ":mail" => [$email, PDO::PARAM_STR], + ] + ); + } + /** * @param string $token get an account from given token * @return Account|null @@ -83,4 +92,25 @@ class AccountGateway { } + public function shareTacticToAccount(int $accountId, int $tacticId): int { + $this->con->exec( + "INSERT INTO TacticSharedAccount(id_account, id_tactic) VALUES(:accountId, :tacticId)", + [ + ":accountId" => [$accountId, PDO::PARAM_INT], + ":tacticId" => [$tacticId, PDO::PARAM_INT], + ] + ); + return intval($this->con->lastInsertId()); + } + + public function unshareTactic(int $tacticId): int { + $this->con->exec( + "DELETE FROM TacticSharedAccount WHERE id_tactic = :tacticId", + [ + ":tacticId" => [$tacticId, PDO::PARAM_INT], + ] + ); + return intval($this->con->lastInsertId()); + } + } diff --git a/src/Core/Gateway/TacticInfoGateway.php b/src/Core/Gateway/TacticInfoGateway.php index a534bb7..961045f 100644 --- a/src/Core/Gateway/TacticInfoGateway.php +++ b/src/Core/Gateway/TacticInfoGateway.php @@ -6,6 +6,7 @@ use IQBall\Core\Connection; use IQBall\Core\Data\CourtType; use IQBall\Core\Data\TacticInfo; use PDO; +use function PHPStan\dumpType; class TacticInfoGateway { private Connection $con; @@ -108,8 +109,37 @@ class TacticInfoGateway { */ public function getAllTacticSharedAccount(int $ownerId): ?array { $res = $this->con->fetch( - "SELECT * FROM TacticSharedAccount - WHERE id_account = :ownerId", + "SELECT t.* FROM Tactic t, TacticSharedAccount ta + WHERE t.id = ta.id_tactic AND ta.id_account = :ownerId", + [ + ":ownerId" => [$ownerId, PDO::PARAM_INT] + ] + ); + if (count($res) == 0) { + return []; + } + return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res); + } + + public function getAllTacticSharedTeamOwned(int $ownerId): ?array { + $res = $this->con->fetch( + "SELECT t.* FROM Tactic t, TacticSharedTeam ts, Member m + WHERE ts.id_team = m.id_team AND ts.id_tactic = t.id + AND m.id_user = :ownerId AND t.owner = :ownerId", + [ + ":ownerId" => [$ownerId, PDO::PARAM_INT] + ] + ); + if (count($res) == 0) { + return []; + } + return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res); + } + + public function getAllTacticSharedAccountOwned(int $ownerId): ?array { + $res = $this->con->fetch( + "SELECT t.* FROM Tactic t, TacticSharedAccount ta + WHERE t.id = ta.id_tactic AND t.owner = :ownerId", [ ":ownerId" => [$ownerId, PDO::PARAM_INT] ] diff --git a/src/Core/Gateway/TeamGateway.php b/src/Core/Gateway/TeamGateway.php index bb96d34..46bcfbb 100644 --- a/src/Core/Gateway/TeamGateway.php +++ b/src/Core/Gateway/TeamGateway.php @@ -44,6 +44,17 @@ class TeamGateway { return intval($this->con->lastInsertId()); } + public function unshareTactic(int $tacticId): int { + $this->con->exec( + "DELETE FROM TacticSharedTeam WHERE id_tactic = :tacticId", + [ + ":tacticId" => [$tacticId, PDO::PARAM_INT], + ] + ); + return intval($this->con->lastInsertId()); + } + + /** * @param string $name @@ -149,5 +160,13 @@ class TeamGateway { ); } + public function getAllIsCoach(int $user): array { + return $this->con->fetch( + "SELECT t.* FROM team t,Member m WHERE m.id_team = t.id AND m.id_user= :idUser AND m.role = 'COACH'", + [ + "idUser" => [$user, PDO::PARAM_INT], + ] + ); + } } diff --git a/src/Core/Model/TacticModel.php b/src/Core/Model/TacticModel.php index dfcfb9c..d02486c 100644 --- a/src/Core/Model/TacticModel.php +++ b/src/Core/Model/TacticModel.php @@ -5,20 +5,22 @@ namespace IQBall\Core\Model; use IQBall\Core\Data\CourtType; use IQBall\App\Session\SessionHandle; use IQBall\Core\Data\TacticInfo; +use IQBall\Core\Gateway\AccountGateway; use IQBall\Core\Gateway\TacticInfoGateway; use IQBall\Core\Validation\ValidationFail; class TacticModel { public const TACTIC_DEFAULT_NAME = "Nouvelle tactique"; - private TacticInfoGateway $tactics; + private AccountGateway $users; /** * @param TacticInfoGateway $tactics */ - public function __construct(TacticInfoGateway $tactics) { + public function __construct(TacticInfoGateway $tactics, AccountGateway $users) { $this->tactics = $tactics; + $this->users = $users; } /** @@ -74,7 +76,7 @@ class TacticModel { * Get all the tactics of the owner * * @param integer $ownerId - * @return array> + * @return array|null */ public function getAll(int $ownerId): ?array { return $this->tactics->getAllOwnedBy($ownerId); @@ -89,6 +91,50 @@ class TacticModel { return $this->tactics->getAllTacticSharedAccount($ownerId); } + public function getAllTacticShared(int $ownerId) : ?array { + $allTactics = []; + $allTacticsSharedTeam = $this->tactics->getAllTacticSharedTeam($ownerId); + if(isset($allTacticsSharedTeam)) { + foreach ($allTacticsSharedTeam as $tactic) { + if(!in_array($tactic, $allTactics)) { + array_push($allTactics, $tactic); + } + } + } + + $allTacticsSharedAccount = $this->tactics->getAllTacticSharedAccount($ownerId); + if(isset($allTacticsSharedAccount)) { + foreach ($allTacticsSharedAccount as $tactic) { + if(!in_array($tactic, $allTactics)) { + array_push($allTactics, $tactic); + } + } + } + return $allTactics; + } + + public function getAllTacticSharedOwned(int $ownerId) : ?array { + $allTactics = []; + $allTacticsSharedTeamOwned = $this->tactics->getAllTacticSharedTeamOwned($ownerId); + if(isset($allTacticsSharedTeamOwned)) { + foreach ($allTacticsSharedTeamOwned as $tactic) { + if(!in_array($tactic, $allTactics)) { + array_push($allTactics, $tactic); + } + } + } + + $allTacticsSharedAccountOwned = $this->tactics->getAllTacticSharedAccountOwned($ownerId); + if(isset($allTacticsSharedAccountOwned)) { + foreach ($allTacticsSharedAccountOwned as $tactic) { + if(!in_array($tactic, $allTactics)) { + array_push($allTactics, $tactic); + } + } + } + return $allTactics; + } + /** * Update the name of a tactic * @param int $id the tactic identifier @@ -120,4 +166,26 @@ class TacticModel { return null; } + public function shareTacticToAccount(int $accountId, int $tacticId): int + { + return $this->users->shareTacticToAccount($accountId, $tacticId); + } + + public function shareTacticToAccountMail(string $email, int $tacticId): ?int + { + $account = $this->users->getAccountFromMail($email); + var_dump($account); + $accountId = $account->getUser()->getId(); + var_dump($accountId); + if(isset($accountId)) { + return $this->shareTacticToAccount($accountId, $tacticId); + } + return null; + } + + + public function unshareTactic(int $tacticId): int + { + return $this->users->unshareTactic($tacticId); + } } diff --git a/src/Core/Model/TeamModel.php b/src/Core/Model/TeamModel.php index fd8bea4..4373a45 100644 --- a/src/Core/Model/TeamModel.php +++ b/src/Core/Model/TeamModel.php @@ -84,6 +84,10 @@ class TeamModel { return $this->teams->shareTacticToTeam($teamId, $tacticId); } + public function unshareTactic(int $tacticId): int { + return $this->teams->unshareTactic($tacticId); + } + /** * delete a member from given team identifier @@ -146,4 +150,8 @@ class TeamModel { public function getAll(int $user): array { return $this->teams->getAll($user); } + + public function getAllIsCoach(int $user): array { + return $this->teams->getAllIsCoach($user); + } }