can share tactic to team or account and can unshare tactic to everyone (build will fail because need to modify home page)
continuous-integration/drone/push Build is failing Details

shareTactic
Vivien DUFOUR 1 year ago
parent 2beda1c7f5
commit 51f150a16a

@ -29,6 +29,7 @@ export default function Home({
teams: Team[]
username: string
}) {
console.log(allTactics)
return (
<div id="main">
<Header username={username} />

@ -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;
}

@ -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("/");
}
}

@ -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);
}
}

@ -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("/");
}
}

@ -9,11 +9,19 @@
<h1><a href="{{ path('/') }}">IQBall</a></h1>
</header>
{% for t in tactics %}
<div onclick="window.location.href = '{{ path("/shareTactic/#{t.id}") }}'">
<p> {{ t.name }} </p>
</div>
{% endfor %}
{% if toShare %}
{% for t in tactics %}
<div onclick="window.location.href = '{{ path("/shareTactic/#{t.id}") }}'">
<p> {{ t.name }} </p>
</div>
{% endfor %}
{% else %}
{% for t in tactics %}
<div onclick="window.location.href = '{{ path("/unshareTactic/#{t.id}") }}'">
<p> {{ t.name }} </p>
</div>
{% endfor %}
{% endif %}
</body>
</html>

@ -1,21 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twig view</title>
</head>
<body>
{% if teams is empty %}
<p>Vous n'êtes dans aucune équipe</p>
{% else %}
{% for team in teams %}
<div class="team" onclick="window.location.href = '{{ path("/shareTactic/#{tactic}/team/#{team.id}") }}'">
<p>Nom de l'équipe : {{ team.name }}</p>
<img src="{{ team.picture }}" alt="logo de l'équipe">
</div>
{% endfor %}
{% endif %}
</body>
</html>

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twig view</title>
</head>
<body>
{% if toShare %}
{% if teams is empty %}
<p>Vous n'êtes dans aucune équipe</p>
{% else %}
{% for team in teams %}
<div class="team" onclick="window.location.href = '{{ path("/shareTactic/#{tactic}/team/#{team.id}") }}'">
<p>Nom de l'équipe : {{ team.name }}</p>
<img src="{{ team.picture }}" alt="logo de l'équipe">
</div>
{% endfor %}
{% endif %}
<h2>Partager à un compte</h2>
<form action="{{ path("/shareTactic/#{tactic}/account") }}" method="POST">
<div class="form-group">
<label for="email">Email du membre :</label>
{% if badEmail %}
<p class="failed">Email invalide</p>
{% endif %}
{%if notFound %}
<p class="failed">Cette personne n'a pas été trouvé</p>
{% endif %}
<input type="text" id="email" name="email" required>
</div>
<div class="form-group">
<input type="submit" value="Confirmer">
</div>
</form>
{% else %}
<form action="{{ path("/unshareTactic/#{tactic}") }}" method="POST">
<button name="unshareAll" value="unshareAll">Supprimer le partage de cette tactique à tout le monde</button>
</form>
{% endif %}
</body>
</html>

@ -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());
}
}

@ -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]
]

@ -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],
]
);
}
}

@ -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<array<string,mixed>>
* @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);
}
}

@ -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);
}
}

Loading…
Cancel
Save