From 8315b79ba12c83669f7af5b7fdf3bfd6a50b142a Mon Sep 17 00:00:00 2001 From: "vivien.dufour" Date: Sun, 4 Feb 2024 14:36:49 +0100 Subject: [PATCH] can now share tactic with account --- front/style/team_panel.css | 10 +- front/views/Home.tsx | 194 +++++++++++++-------- front/views/TeamPanel.tsx | 63 ++++--- front/views/template/Header.tsx | 8 +- public/api/index.php | 6 +- public/index.php | 2 +- src/Api/Controller/APITacticController.php | 38 +++- src/App/Controller/TacticController.php | 78 ++++++--- src/App/Controller/TeamController.php | 2 +- src/Core/Data/TacticInfo.php | 3 +- src/Core/Gateway/AccountGateway.php | 27 ++- src/Core/Gateway/TacticInfoGateway.php | 42 +++-- src/Core/Gateway/TeamGateway.php | 23 +++ src/Core/Model/TacticModel.php | 99 +++++++---- src/Core/Model/TeamModel.php | 41 ++++- 15 files changed, 456 insertions(+), 180 deletions(-) diff --git a/front/style/team_panel.css b/front/style/team_panel.css index 90597ab..28d69ac 100644 --- a/front/style/team_panel.css +++ b/front/style/team_panel.css @@ -15,7 +15,6 @@ body { height: 100vh; } - #content-container { display: flex; flex-direction: row; @@ -57,7 +56,12 @@ header h1 a { font-size: 1.4em; } -html, body, #main-div, #content-container, #right-panel, #tactics { +html, +body, +#main-div, +#content-container, +#right-panel, +#tactics { height: 100%; } @@ -212,7 +216,7 @@ html, body, #main-div, #content-container, #right-panel, #tactics { margin-bottom: 5px; padding: 10px; max-width: 90%; - cursor : pointer; + cursor: pointer; } .tactic button#open-tactic { diff --git a/front/views/Home.tsx b/front/views/Home.tsx index 00091f5..19a4dca 100644 --- a/front/views/Home.tsx +++ b/front/views/Home.tsx @@ -2,14 +2,14 @@ import "../style/home/home.css" import { Header } from "./template/Header" import { BASE } from "../Constants" -import Draggable from "react-draggable"; -import {NULL_POS} from "../components/arrows/Pos"; -import {contains} from "../components/arrows/Box"; -import React, {useRef, useState} from "react"; +import Draggable from "react-draggable" +import { NULL_POS } from "../components/arrows/Pos" +import { contains } from "../components/arrows/Box" +import React, { useRef, useState } from "react" import { fetchAPI } from "../Fetcher" -import {User} from "../model/User"; -import { FaShare } from "react-icons/fa"; -import {SaveStates} from "../components/editor/SavingState"; +import { User } from "../model/User" +import { FaShare } from "react-icons/fa" +import { SaveStates } from "../components/editor/SavingState" interface Tactic { id: number @@ -58,13 +58,18 @@ function Body({ lastTactics: Tactic[] allTactics: Tactic[] teams: Team[] - user : User + user: User }) { const widthPersonalSpace = 78 const widthSideMenu = 100 - widthPersonalSpace return (
- + +
- - + +
) @@ -103,8 +110,8 @@ function PersonalSpace({ }: { width: number allTactics: Tactic[] - teams : Team[] - user : User + teams: Team[] + user: User }) { return (
- +
) } @@ -132,8 +143,8 @@ function TableData({ user, }: { allTactics: Tactic[] - teams : Team[] - user : User + teams: Team[] + user: User }) { const nbRow = Math.floor(allTactics.length / 3) + 1 let listTactic = Array(nbRow) @@ -153,8 +164,12 @@ function TableData({ i = 0 while (i < nbRow) { - listTactic[i] = listTactic[i].map((tactic: Tactic, i) => ( - + listTactic[i] = listTactic[i].map((tactic: Tactic, index: number) => ( + )) i++ } @@ -172,61 +187,71 @@ function TableData({ return data } - function DraggableTableDataElement({ tactic, teams, -} : { - tactic : Tactic - teams : Team[] +}: { + tactic: Tactic + teams: Team[] }) { const ref = useRef(null) const [dragging, setDragging] = useState(false) - const [hovered, setHovered] = useState(false); + const [hovered, setHovered] = useState(false) const handleButtonClick = (event: React.MouseEvent) => { - event.stopPropagation(); + event.stopPropagation() if (!dragging) { - const userEmail = window.prompt("Entrez l'email à qui partager la tactique :"); - if(userEmail != null) { - onShareTactic(userEmail, tactic); + const userEmail = window.prompt( + "Entrez l'email à qui partager la tactique :", + ) + if (userEmail != null) { + onShareTactic(userEmail, tactic) } } else { - setDragging(false); + setDragging(false) } - }; + } return ( - setDragging(true)} - onStop={() => { if(dragging) { - onDropTactic(ref.current.getBoundingClientRect(), tactic, teams) - } - }} - > - { if(!dragging) { - location.pathname = BASE + "/tactic/" + tactic.id + "/edit" - } - else { - setDragging(false) + setDragging(true)} + onStop={() => { + if (dragging) { + if (ref.current) { + onDropTactic( + ref.current.getBoundingClientRect(), + tactic, + teams, + ) + } } + }}> + } + className="data" + onClick={() => { + if (!dragging) { + location.pathname = + BASE + "/tactic/" + tactic.id + "/edit" + } else { + setDragging(false) + } }} onMouseEnter={() => setHovered(true)} - onMouseLeave={() => setHovered(false)} - > - + onMouseLeave={() => setHovered(false)}> {truncateString(tactic.name, 25)} {hovered && (
-
- )} + )}
) @@ -238,14 +263,14 @@ function BodyPersonalSpace({ user, }: { allTactics: Tactic[] - teams : Team[] - user : User + teams: Team[] + user: User }) { let data if (allTactics.length == 0) { data =

Aucune tactique créée !

} else { - data = + data = } return ( @@ -298,7 +323,9 @@ function SetButtonTactic({ tactics }: { tactics: Tactic[] }) { } function SetButtonTeam({ teams }: { teams: Team[] }) { - const listTeam = teams.map((teams, i) => ) + const listTeam = teams.map((teams, i) => ( + + )) return
{listTeam}
} @@ -339,37 +366,62 @@ function truncateString(name: string, limit: number): string { return name } -function onDropTactic(ref : DOMRect, tactic : Tactic, teams : Team[]) { - let shared = false; +function onDropTactic(ref: DOMRect, tactic: Tactic, teams: Team[]) { + let shared = false for (const team of teams) { - if (contains(ref, document.getElementById(`button-team-${team.id}`)!.getBoundingClientRect())) { + if ( + contains( + ref, + document + .getElementById(`button-team-${team.id}`)! + .getBoundingClientRect(), + ) + ) { if (!shared) { - shareTacticToTeam(tactic, team); - shared = true; + shareTacticToTeam(tactic, team) + shared = true } } } } async function onShareTactic(email: string, tactic: Tactic) { - const canShareResponse = await fetchAPI(`tactic/${tactic.id}/can-share`, tactic); + const canShareResponse = await fetchAPI( + `tactic/${tactic.id}/can-share`, + tactic, + ) if (canShareResponse.ok) { - const shareToAccountResponse = await fetchAPI(`tactic/${tactic.id}/share-to-account`, email); + const shareToAccountResponse = await fetchAPI( + `tactic/${tactic.id}/share-to-account`, + { email }, + ) if (!shareToAccountResponse.ok) { - alert("Une erreur s'est produite lors du partage de la tactique avec ce compte"); + alert( + "Une erreur s'est produite lors du partage de la tactique avec ce compte", + ) } } else { - alert("Vous ne pouvez pas partager cette tactique"); + alert("Vous ne pouvez pas partager cette tactique") } } - -async function shareTacticToTeam(tactic : Tactic, team : Team) { - const canShare = await fetchAPI(`tactic/${tactic.id}/can-share-to-team`, team).then((r) => r.ok) - if(canShare && confirm("Etes-vous sûr de vouloir partager la tactique " + tactic.name + " avec l'équipe " + team.name)) { +async function shareTacticToTeam(tactic: Tactic, team: Team) { + const canShare = await fetchAPI( + `tactic/${tactic.id}/can-share-to-team`, + team, + ).then((r) => r.ok) + if ( + canShare && + confirm( + "Etes-vous sûr de vouloir partager la tactique " + + tactic.name + + " avec l'équipe " + + team.name, + ) + ) { fetchAPI(`tactic/${tactic.id}/share-to-team`, team) } - if(!canShare) { + if (!canShare) { alert("Vous ne pouvez pas partager cette tactique à cette équipe") } } diff --git a/front/views/TeamPanel.tsx b/front/views/TeamPanel.tsx index 8ba5aa5..fd9f43b 100644 --- a/front/views/TeamPanel.tsx +++ b/front/views/TeamPanel.tsx @@ -1,9 +1,9 @@ import "../style/team_panel.css" import { BASE } from "../Constants" import { Team, TeamInfo, Member } from "../model/Team" -import {Tactic} from "../model/tactic/Tactic"; -import {fetchAPI} from "../Fetcher"; -import {useState} from "react"; +import { Tactic } from "../model/tactic/Tactic" +import { fetchAPI } from "../Fetcher" +import { useState } from "react" export default function TeamPanel({ isCoach, @@ -14,17 +14,18 @@ export default function TeamPanel({ isCoach: boolean team: Team currentUserId: number - tactics : Tactic[] + tactics: Tactic[] }) { - const [teamTactics, setTeamTactics] = useState(tactics); + const [teamTactics, setTeamTactics] = useState(tactics) - function handleTacticDelete(tacticId) { + function handleTacticDelete(tacticId: number) { fetchAPI(`tactic/${tacticId}/unshare-to-team`, team.info) - const updatedTactics = teamTactics.filter((tactic) => tactic.id !== tacticId); - setTeamTactics(updatedTactics); + const updatedTactics = teamTactics.filter( + (tactic) => tactic.id !== tacticId, + ) + setTeamTactics(updatedTactics) } - return (
@@ -46,7 +47,13 @@ export default function TeamPanel({ />
- handleTacticDelete(tacticId)}/> + + handleTacticDelete(tacticId) + } + />
@@ -193,13 +200,18 @@ function TacticsDisplay({ tactics, isCoach, onTacticDelete, -} : { - tactics : Tactic[] - isCoach : boolean - onTacticDelete : (tacticId: number) => void +}: { + tactics: Tactic[] + isCoach: boolean + onTacticDelete: (tacticId: number) => void }) { const listTactic = tactics.map((tactic) => ( - onTacticDelete(tactic.id)}/> + onTacticDelete(tactic.id)} + /> )) return (
@@ -215,22 +227,27 @@ function TacticDisplay({ tactic, isCoach, onTacticDelete, -} : { - tactic : Tactic - isCoach : boolean +}: { + tactic: Tactic + isCoach: boolean onTacticDelete: () => void }) { return ( -
location.pathname = BASE + "/tactic/" + tactic.id + "/edit"}> +
+ (location.pathname = BASE + "/tactic/" + tactic.id + "/edit") + }>

{tactic.name}

{isCoach && ( )} diff --git a/front/views/template/Header.tsx b/front/views/template/Header.tsx index 3c2c979..105dee7 100644 --- a/front/views/template/Header.tsx +++ b/front/views/template/Header.tsx @@ -1,5 +1,5 @@ import { BASE } from "../../Constants" -import {User} from "../../model/User"; +import { User } from "../../model/User" /** * @@ -11,7 +11,8 @@ export function Header({ user }: { user: User }) { diff --git a/public/api/index.php b/public/api/index.php index 5d63f68..1e8c6d7 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -21,8 +21,10 @@ use IQBall\Core\Model\TeamModel; use IQBall\Core\Gateway\MemberGateway; function getTacticController(): APITacticController { - return new APITacticController(new TacticModel(new TacticInfoGateway(new Connection(get_database())), new AccountGateway(new Connection(get_database()))), - new TeamModel(new TeamGateway(new Connection(get_database())), new MemberGateway(new Connection(get_database())), new AccountGateway(new Connection(get_database())))); + return new APITacticController( + new TacticModel(new TacticInfoGateway(new Connection(get_database())), new AccountGateway(new Connection(get_database()))), + new TeamModel(new TeamGateway(new Connection(get_database())), new MemberGateway(new Connection(get_database())), new AccountGateway(new Connection(get_database()))) + ); } function getAuthController(): APIAuthController { diff --git a/public/index.php b/public/index.php index bde88c8..fcc72a1 100644 --- a/public/index.php +++ b/public/index.php @@ -56,7 +56,7 @@ function getTeamController(): TeamController { } function getTacticController(): TacticController { - return new TacticController(new TacticModel(new TacticInfoGateway(getConnection()), new AccountGateway(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()))); } diff --git a/src/Api/Controller/APITacticController.php b/src/Api/Controller/APITacticController.php index 234e4d7..0b220e5 100644 --- a/src/Api/Controller/APITacticController.php +++ b/src/Api/Controller/APITacticController.php @@ -68,6 +68,11 @@ class APITacticController { } + /** + * @param int $tacticId + * @param Account $account + * @return HttpResponse + */ public function canShareTactic(int $tacticId, Account $account): HttpResponse { if ($this->tacticModel->canShareTactic($tacticId, $account)) { return HttpResponse::fromCode(HttpCodes::OK); @@ -75,14 +80,18 @@ class APITacticController { return new JsonHttpResponse(["message" => "Vous ne pouvez pas partager cette tactique"], HttpCodes::FORBIDDEN); } - + /** + * @param int $tacticId + * @param Account $account + * @return HttpResponse + */ public function canShareTacticToTeam(int $tacticId, Account $account): HttpResponse { return Control::runChecked([ "id" => [], "name" => [], "picture" => [], "main_color" => [], - "second_color" => [] + "second_color" => [], ], function (HttpRequest $request) use ($tacticId, $account) { if ($this->canShareTactic($tacticId, $account)) { if ($this->teamModel->canShareTacticToTeam($request["id"], $account->getUser()->getEmail())) { @@ -93,36 +102,50 @@ class APITacticController { }); } - + /** + * @param int $tacticId + * @param Account $account + * @return HttpResponse + */ public function shareTacticToTeam(int $tacticId, Account $account): HttpResponse { return Control::runChecked([ "id" => [], "name" => [], "picture" => [], "main_color" => [], - "second_color" => [] - ], function (HttpRequest $request) use ($tacticId, $account) { + "second_color" => [], + ], function (HttpRequest $request) use ($tacticId) { $this->teamModel->shareTacticToTeam($request["id"], $tacticId); return HttpResponse::fromCode(HttpCodes::OK); }); } + /** + * @param int $tacticId + * @param Account $account + * @return HttpResponse + */ public function shareTacticToAccount(int $tacticId, Account $account): HttpResponse { return Control::runChecked([ "email" => [], - ], function (HttpRequest $request) use ($tacticId, $account) { + ], function (HttpRequest $request) use ($tacticId) { $this->tacticModel->shareTacticToAccountMail($request["email"], $tacticId); return HttpResponse::fromCode(HttpCodes::OK); }); } + /** + * @param int $tacticId + * @param Account $account + * @return HttpResponse + */ public function unshareTacticToTeam(int $tacticId, Account $account): HttpResponse { return Control::runChecked([ "id" => [], "name" => [], "picture" => [], "mainColor" => [], - "secondColor" => [] + "secondColor" => [], ], function (HttpRequest $request) use ($tacticId, $account) { if ($this->teamModel->canShareTacticToTeam($request["id"], $account->getUser()->getEmail())) { $this->teamModel->unshareTacticToTeam($tacticId, $request["id"]); @@ -132,4 +155,3 @@ class APITacticController { }); } } - diff --git a/src/App/Controller/TacticController.php b/src/App/Controller/TacticController.php index 9df1f6b..3720994 100644 --- a/src/App/Controller/TacticController.php +++ b/src/App/Controller/TacticController.php @@ -6,74 +6,112 @@ use IQBall\App\Session\SessionHandle; use IQBall\App\ViewHttpResponse; use IQBall\Core\Model\TacticModel; use IQBall\Core\Model\TeamModel; -use \IQBall\Core\Http\HttpResponse; +use IQBall\Core\Http\HttpResponse; -class TacticController -{ +class TacticController { private TacticModel $tactics; private ?TeamModel $teams; - public function __construct(TacticModel $tactics, ?TeamModel $teams = NULL) { + public function __construct(TacticModel $tactics, ?TeamModel $teams = null) { $this->tactics = $tactics; $this->teams = $teams; } + /** + * @param bool $toShare + * @param SessionHandle $session + * @return ViewHttpResponse + */ public function displayTactic(bool $toShare, SessionHandle $session): ViewHttpResponse { if($toShare) { $results = $this->tactics->getAll($session->getAccount()->getUser()->getId()); - } - else { + } else { $results = $this->tactics->getAllTacticSharedOwned($session->getAccount()->getUser()->getId()); } return ViewHttpResponse::twig("display_tactic.html.twig", ['tactics' => $results, 'toShare' => $toShare]); } - + /** + * @param bool $toShare + * @param int $tacticId + * @param SessionHandle $session + * @return ViewHttpResponse + */ public function displayTeamAndAccount(bool $toShare, int $tacticId, SessionHandle $session): ViewHttpResponse { if($toShare) { $results = $this->teams->getAllIsCoach($session->getAccount()->getUser()->getId()); - } - else { + } 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 { + /** + * @param int $tacticId + * @param int $teamId + * @param SessionHandle $session + * @return ViewHttpResponse + */ + public function displayShareConfirmation(int $tacticId, int $teamId, SessionHandle $session): ViewHttpResponse { return ViewHttpResponse::twig("display_share_confirmation.html.twig", ['team' => $teamId, 'tactic' => $tacticId]); } - public function shareTacticToTeam(array $confirmation, int $tacticId, int $teamId, SessionHandle $session) : HttpResponse - { + /** + * @param array> $confirmation + * @param int $tacticId + * @param int $teamId + * @param SessionHandle $session + * @return HttpResponse + */ + public function shareTacticToTeam(array $confirmation, int $tacticId, int $teamId, SessionHandle $session): HttpResponse { if($confirmation['confirmation'] == "yes") { $this->teams->shareTacticToTeam($teamId, $tacticId); } return ViewHttpResponse::redirect("/"); } - public function shareTacticToAccount(array $request, int $tacticId, SessionHandle $session) : HttpResponse - { + /** + * @param array $request + * @param int $tacticId + * @param SessionHandle $session + * @return HttpResponse + */ + 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 - { + /** + * @param int $tacticId + * @param SessionHandle $session + * @return HttpResponse + */ + public function unshareTactic(int $tacticId, SessionHandle $session): HttpResponse { $this->tactics->unshareTactic($tacticId); $this->teams->unshareTactic($tacticId); return ViewHttpResponse::redirect("/"); } - public function unshareTacticToTeam(int $tacticId, int $teamId, SessionHandle $session) : HttpResponse - { + /** + * @param int $tacticId + * @param int $teamId + * @param SessionHandle $session + * @return HttpResponse + */ + public function unshareTacticToTeam(int $tacticId, int $teamId, SessionHandle $session): HttpResponse { $this->teams->unshareTacticToTeam($tacticId, $teamId); return ViewHttpResponse::redirect("/"); } - public function unshareTacticToAccount(int $tacticId, int $accountId, SessionHandle $session) : HttpResponse - { + /** + * @param int $tacticId + * @param int $accountId + * @param SessionHandle $session + * @return HttpResponse + */ + public function unshareTacticToAccount(int $tacticId, int $accountId, SessionHandle $session): HttpResponse { $this->tactics->unshareTacticToAccount($tacticId, $accountId); return ViewHttpResponse::redirect("/"); } diff --git a/src/App/Controller/TeamController.php b/src/App/Controller/TeamController.php index 1b526c3..9864ea0 100644 --- a/src/App/Controller/TeamController.php +++ b/src/App/Controller/TeamController.php @@ -140,7 +140,7 @@ class TeamController { ], 'isCoach' => $role, 'currentUserId' => $session->getAccount()->getUser()->getId(), - 'tactics' => $tactics + 'tactics' => $tactics, ] ); } diff --git a/src/Core/Data/TacticInfo.php b/src/Core/Data/TacticInfo.php index 437af15..4a62ada 100644 --- a/src/Core/Data/TacticInfo.php +++ b/src/Core/Data/TacticInfo.php @@ -60,8 +60,7 @@ class TacticInfo implements \JsonSerializable { return $this->creationDate; } - public function jsonSerialize() - { + public function jsonSerialize() { return get_object_vars($this); } } diff --git a/src/Core/Gateway/AccountGateway.php b/src/Core/Gateway/AccountGateway.php index f19158e..48d8ec6 100644 --- a/src/Core/Gateway/AccountGateway.php +++ b/src/Core/Gateway/AccountGateway.php @@ -17,6 +17,14 @@ class AccountGateway { $this->con = $con; } + /** + * @param string $name + * @param string $email + * @param string $token + * @param string $hash + * @param string $profilePicture + * @return int + */ public function insertAccount(string $name, string $email, string $token, string $hash, string $profilePicture): int { $this->con->exec("INSERT INTO Account(username, hash, email, token,profilePicture) VALUES (:username,:hash,:email,:token,:profilePic)", [ ':username' => [$name, PDO::PARAM_STR], @@ -69,6 +77,10 @@ class AccountGateway { return new Account($acc["token"], new User($email, $acc["username"], $acc["id"], $acc["profilePicture"])); } + /** + * @param string $email + * @return array> + */ public function getAccountIdFromMail(string $email): array { return $this->con->fetch( "SELECT id FROM Account WHERE email = :mail", @@ -91,7 +103,11 @@ class AccountGateway { return new Account($acc["token"], new User($acc["email"], $acc["username"], $acc["id"], $acc["profilePicture"])); } - + /** + * @param int $accountId + * @param int $tacticId + * @return int + */ public function shareTacticToAccount(int $accountId, int $tacticId): int { $this->con->exec( "INSERT INTO TacticSharedAccount(id_account, id_tactic) VALUES(:accountId, :tacticId)", @@ -103,6 +119,10 @@ class AccountGateway { return intval($this->con->lastInsertId()); } + /** + * @param int $tacticId + * @return int + */ public function unshareTactic(int $tacticId): int { $this->con->exec( "DELETE FROM TacticSharedAccount WHERE id_tactic = :tacticId", @@ -113,6 +133,11 @@ class AccountGateway { return intval($this->con->lastInsertId()); } + /** + * @param int $tacticId + * @param int $accountId + * @return int + */ public function unshareTacticToAccount(int $tacticId, int $accountId): int { $this->con->exec( "DELETE FROM TacticSharedAccount WHERE id_tactic = :tacticId AND id_account = :accountId", diff --git a/src/Core/Gateway/TacticInfoGateway.php b/src/Core/Gateway/TacticInfoGateway.php index b1d8701..d1f2933 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 { @@ -94,7 +95,7 @@ class TacticInfoGateway { WHERE ts.id_team = m.id_team AND ts.id_tactic = t.id AND m.id_user = :ownerId", [ - ":ownerId" => [$ownerId, PDO::PARAM_INT] + ":ownerId" => [$ownerId, PDO::PARAM_INT], ] ); if (count($res) == 0) { @@ -112,7 +113,7 @@ class TacticInfoGateway { "SELECT t.* FROM Tactic t, TacticSharedAccount ta WHERE t.id = ta.id_tactic AND ta.id_account = :ownerId", [ - ":ownerId" => [$ownerId, PDO::PARAM_INT] + ":ownerId" => [$ownerId, PDO::PARAM_INT], ] ); if (count($res) == 0) { @@ -121,13 +122,17 @@ class TacticInfoGateway { return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res); } + /** + * @param int $ownerId + * @return array|null + */ 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] + ":ownerId" => [$ownerId, PDO::PARAM_INT], ] ); if (count($res) == 0) { @@ -136,12 +141,16 @@ class TacticInfoGateway { return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res); } + /** + * @param int $ownerId + * @return array|null + */ 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] + ":ownerId" => [$ownerId, PDO::PARAM_INT], ] ); if (count($res) == 0) { @@ -150,8 +159,12 @@ class TacticInfoGateway { return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res); } - - public function getOwnerIdTacticSharedTeam(int $tacticId, int $accountId) : ?array { + /** + * @param int $tacticId + * @param int $accountId + * @return array>|null + */ + public function getOwnerIdTacticSharedTeam(int $tacticId, int $accountId): ?array { return $this->con->fetch( "SELECT t.owner FROM Account a @@ -161,12 +174,17 @@ class TacticInfoGateway { WHERE tt.id_tactic = :tacticId AND m.id_user = :accountId", [ ":tacticId" => [$tacticId, PDO::PARAM_INT], - ":accountId" => [$accountId, PDO::PARAM_INT] + ":accountId" => [$accountId, PDO::PARAM_INT], ] ); } - public function getOwnerIdTacticSharedAccount(int $tacticId, int $accountId) : ?array { + /** + * @param int $tacticId + * @param int $accountId + * @return array>|null + */ + public function getOwnerIdTacticSharedAccount(int $tacticId, int $accountId): ?array { return $this->con->fetch( "SELECT t.owner FROM Account a JOIN TacticSharedAccount ta ON a.id = ta.id_account @@ -174,7 +192,7 @@ class TacticInfoGateway { WHERE ta.id_tactic = :tacticId AND ta.id_account = :accountId", [ ":tacticId" => [$tacticId, PDO::PARAM_INT], - ":accountId" => [$accountId, PDO::PARAM_INT] + ":accountId" => [$accountId, PDO::PARAM_INT], ] ); } @@ -227,7 +245,11 @@ class TacticInfoGateway { return $stmnt->rowCount() == 1; } - private function rowToTacticInfo(array $row) : TacticInfo { + /** + * @param array $row + * @return TacticInfo + */ + private function rowToTacticInfo(array $row): TacticInfo { $type = CourtType::fromName($row['court_type']); return new TacticInfo($row['id'], $row["name"], strtotime($row["creation_date"]), $row["owner"], $type, $row['content']); } diff --git a/src/Core/Gateway/TeamGateway.php b/src/Core/Gateway/TeamGateway.php index 2021118..97185bb 100644 --- a/src/Core/Gateway/TeamGateway.php +++ b/src/Core/Gateway/TeamGateway.php @@ -3,6 +3,7 @@ namespace IQBall\Core\Gateway; use IQBall\Core\Connection; +use IQBall\Core\Data\TacticInfo; use IQBall\Core\Data\TeamInfo; use PDO; @@ -33,6 +34,11 @@ class TeamGateway { return intval($this->con->lastInsertId()); } + /** + * @param int $teamId + * @param int $tacticId + * @return int + */ public function shareTacticToTeam(int $teamId, int $tacticId): int { $this->con->exec( "INSERT INTO TacticSharedTeam(id_team, id_tactic) VALUES(:teamId, :tacticId)", @@ -44,6 +50,10 @@ class TeamGateway { return intval($this->con->lastInsertId()); } + /** + * @param int $tacticId + * @return int + */ public function unshareTactic(int $tacticId): int { $this->con->exec( "DELETE FROM TacticSharedTeam WHERE id_tactic = :tacticId", @@ -54,6 +64,11 @@ class TeamGateway { return intval($this->con->lastInsertId()); } + /** + * @param int $tacticId + * @param int $teamId + * @return int + */ public function unshareTacticToTeam(int $tacticId, int $teamId): int { $this->con->exec( "DELETE FROM TacticSharedTeam WHERE id_tactic = :tacticId AND id_team = :teamId", @@ -170,6 +185,10 @@ class TeamGateway { ); } + /** + * @param int $user + * @return array> + */ 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'", @@ -179,6 +198,10 @@ class TeamGateway { ); } + /** + * @param int $team + * @return array> + */ public function getAllTeamTactic(int $team): array { return $this->con->fetch( "SELECT t.* FROM Tactic t, TacticSharedTeam tt WHERE t.id = tt.id_tactic AND tt.id_team = :idTeam", diff --git a/src/Core/Model/TacticModel.php b/src/Core/Model/TacticModel.php index ece9f47..cd77f3e 100644 --- a/src/Core/Model/TacticModel.php +++ b/src/Core/Model/TacticModel.php @@ -55,19 +55,12 @@ class TacticModel { return $this->tactics->get($id); } - /** - * Return the nb last tactics created - * - * @param integer $nb - * @return array> - */ - /** * Return the nb last tactics * * @param integer $nb * @param integer $ownerId - * @return array> + * @return array */ public function getLast(int $nb, int $ownerId): array { return $this->tactics->getLastOwnedBy($nb, $ownerId); @@ -77,22 +70,36 @@ class TacticModel { * Get all the tactics of the owner * * @param integer $ownerId - * @return array|null + * @return array|null */ public function getAll(int $ownerId): ?array { return $this->tactics->getAllOwnedBy($ownerId); } - public function getAllTacticSharedTeam(int $ownerId) : ?array - { + /** + * Get all the tactics shared to a user via team share + * @param int $ownerId + * @return array|null + */ + public function getAllTacticSharedTeam(int $ownerId): ?array { return $this->tactics->getAllTacticSharedTeam($ownerId); } - public function getAllTacticSharedAccount(int $ownerId) : ?array { + /** + * Get all the tactics shared to a user via account share + * @param int $ownerId + * @return array|null + */ + public function getAllTacticSharedAccount(int $ownerId): ?array { return $this->tactics->getAllTacticSharedAccount($ownerId); } - public function getAllTacticShared(int $ownerId) : ?array { + /** + * Get all the tactics shared to a user via team or account share + * @param int $ownerId + * @return array|null + */ + public function getAllTacticShared(int $ownerId): ?array { $allTactics = []; $allTacticsSharedTeam = $this->tactics->getAllTacticSharedTeam($ownerId); if(isset($allTacticsSharedTeam)) { @@ -114,7 +121,12 @@ class TacticModel { return $allTactics; } - public function getAllTacticSharedOwned(int $ownerId) : ?array { + /** + * Get all the tactics a user shared and own + * @param int $ownerId + * @return array|null + */ + public function getAllTacticSharedOwned(int $ownerId): ?array { $allTactics = []; $allTacticsSharedTeamOwned = $this->tactics->getAllTacticSharedTeamOwned($ownerId); if(isset($allTacticsSharedTeamOwned)) { @@ -136,35 +148,55 @@ class TacticModel { return $allTactics; } - public function shareTacticToAccount(int $accountId, int $tacticId): int - { + /** + * Share tactic to an account + * @param int $accountId + * @param int $tacticId + * @return int + */ + public function shareTacticToAccount(int $accountId, int $tacticId): int { return $this->users->shareTacticToAccount($accountId, $tacticId); } - public function shareTacticToAccountMail(string $email, int $tacticId): ?int - { + /** + * Share tactic to an account email + * @param string $email + * @param int $tacticId + * @return int|null + */ + 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; + return $this->shareTacticToAccount($accountId, $tacticId); } - public function unshareTactic(int $tacticId): int - { + /** + * Unshare a tactic to every account + * @param int $tacticId + * @return int + */ + public function unshareTactic(int $tacticId): int { return $this->users->unshareTactic($tacticId); } - public function unshareTacticToAccount(int $tacticId, int $accountId): int - { + /** + * Unshare a tactic to an account + * @param int $tacticId + * @param int $accountId + * @return int + */ + public function unshareTacticToAccount(int $tacticId, int $accountId): int { return $this->users->unshareTacticToAccount($tacticId, $accountId); } - public function getOwnerIdTacticShared(int $tactidId, int $accountId) : ?array { + /** + * Get the owner of a shared tactic + * @param int $tactidId + * @param int $accountId + * @return array|null + */ + public function getOwnerIdTacticShared(int $tactidId, int $accountId): ?array { $ids = []; $idT = $this->tactics->getOwnerIdTacticSharedTeam($tactidId, $accountId); if(isset($idT)) { @@ -215,6 +247,7 @@ class TacticModel { return []; } + public function updateContent(int $id, string $json): ?ValidationFail { if (!$this->tactics->updateContent($id, $json)) { return ValidationFail::error("Could not update content"); @@ -222,7 +255,13 @@ class TacticModel { return null; } - public function canShareTactic(int $id, Account $account) : bool { + /** + * Verify if tactic can be shared by user + * @param int $id + * @param Account $account + * @return bool + */ + public function canShareTactic(int $id, Account $account): bool { if($this->get($id)->getOwnerId() != $account->getUser()->getId()) { return false; } diff --git a/src/Core/Model/TeamModel.php b/src/Core/Model/TeamModel.php index b8bdfbc..9aa896f 100644 --- a/src/Core/Model/TeamModel.php +++ b/src/Core/Model/TeamModel.php @@ -78,21 +78,42 @@ class TeamModel { return new Team($teamInfo, $members); } - - public function shareTacticToTeam(int $teamId, int $tacticId): int - { + /** + * Share tactic to a team + * @param int $teamId + * @param int $tacticId + * @return int + */ + public function shareTacticToTeam(int $teamId, int $tacticId): int { return $this->teams->shareTacticToTeam($teamId, $tacticId); } + /** + * Unshare tactic from every team + * @param int $tacticId + * @return int + */ public function unshareTactic(int $tacticId): int { return $this->teams->unshareTactic($tacticId); } + /** + * Unshare team-shared tactic + * @param int $tacticId + * @param int $teamId + * @return int + */ public function unshareTacticToTeam(int $tacticId, int $teamId): int { return $this->teams->unshareTacticToTeam($tacticId, $teamId); } - public function canShareTacticToTeam(int $teamId, string $email) : bool { + /** + * Verify if user can share tactic to a team + * @param int $teamId + * @param string $email + * @return bool + */ + public function canShareTacticToTeam(int $teamId, string $email): bool { if($this->isCoach($teamId, $email)) { return true; } @@ -161,11 +182,21 @@ class TeamModel { return $this->teams->getAll($user); } + /** + * Get all user's teams where user is coach + * @param int $user + * @return array> + */ public function getAllIsCoach(int $user): array { return $this->teams->getAllIsCoach($user); } - public function getAllTeamTactic(int $team) : array { + /** + * Get all tactic shared with a team + * @param int $team + * @return array> + */ + public function getAllTeamTactic(int $team): array { return $this->teams->getAllTeamTactic($team); } }