Amélioration ; ajout de photo de console et ajout de checkbox physique, demat ou ROM
This commit is contained in:
1 parent
df11d023f0
commit
3235bc50c7
22 files changed
+710
-62
No files matched your search
@@ -18,6 +18,7 @@ final class Config
|
||||
"sessionLifetime" => (int) self::env("SESSION_LIFETIME", "86400"),
|
||||
"dataPath" => dirname(__DIR__) . "/data",
|
||||
"jaquettesPath" => dirname(__DIR__) . "/data/jaquettes",
|
||||
"consolesPhotosPath" => dirname(__DIR__) . "/data/consoles-photos",
|
||||
"cachePath" => dirname(__DIR__) . "/data/cache"
|
||||
];
|
||||
}
|
||||
|
||||
@@ -62,11 +62,15 @@ final class ConsoleController
|
||||
|
||||
try
|
||||
{
|
||||
$console = $this->consoleService->creer($utilisateur["id"], $_POST);
|
||||
$console = $this->consoleService->creer(
|
||||
$utilisateur["id"],
|
||||
$_POST,
|
||||
$_FILES["photo"] ?? null
|
||||
);
|
||||
Flash::definir("success", "Console ajoutée.");
|
||||
Helpers::rediriger("/consoles/" . $console["id"]);
|
||||
}
|
||||
catch (InvalidArgumentException $exception)
|
||||
catch (InvalidArgumentException | RuntimeException $exception)
|
||||
{
|
||||
Flash::definir("danger", $exception->getMessage());
|
||||
}
|
||||
@@ -97,11 +101,16 @@ final class ConsoleController
|
||||
|
||||
try
|
||||
{
|
||||
$console = $this->consoleService->modifier($id, $utilisateur["id"], $_POST);
|
||||
$console = $this->consoleService->modifier(
|
||||
$id,
|
||||
$utilisateur["id"],
|
||||
$_POST,
|
||||
$_FILES["photo"] ?? null
|
||||
);
|
||||
Flash::definir("success", "Console mise à jour.");
|
||||
Helpers::rediriger("/consoles/" . $console["id"]);
|
||||
}
|
||||
catch (InvalidArgumentException $exception)
|
||||
catch (InvalidArgumentException | RuntimeException $exception)
|
||||
{
|
||||
Flash::definir("danger", $exception->getMessage());
|
||||
}
|
||||
@@ -115,6 +124,54 @@ final class ConsoleController
|
||||
]);
|
||||
}
|
||||
|
||||
public function envoyerPhoto(string $id): void
|
||||
{
|
||||
$utilisateur = $this->auth->exigerConnexion();
|
||||
Csrf::exiger();
|
||||
|
||||
$console = $this->consoleService->trouverPourUtilisateur($id, $utilisateur["id"]);
|
||||
|
||||
if ($console === null)
|
||||
{
|
||||
Flash::definir("danger", "Console introuvable.");
|
||||
Helpers::rediriger("/consoles");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$this->consoleService->mettreAJourPhoto(
|
||||
$id,
|
||||
$utilisateur["id"],
|
||||
$_FILES["photo"] ?? []
|
||||
);
|
||||
Flash::definir("success", "Photo de la console enregistrée.");
|
||||
}
|
||||
catch (InvalidArgumentException | RuntimeException $exception)
|
||||
{
|
||||
Flash::definir("danger", $exception->getMessage());
|
||||
}
|
||||
|
||||
Helpers::rediriger("/consoles/" . $id);
|
||||
}
|
||||
|
||||
public function supprimerPhoto(string $id): void
|
||||
{
|
||||
$utilisateur = $this->auth->exigerConnexion();
|
||||
Csrf::exiger();
|
||||
|
||||
try
|
||||
{
|
||||
$this->consoleService->supprimerPhoto($id, $utilisateur["id"]);
|
||||
Flash::definir("success", "Photo supprimée.");
|
||||
}
|
||||
catch (RuntimeException $exception)
|
||||
{
|
||||
Flash::definir("danger", $exception->getMessage());
|
||||
}
|
||||
|
||||
Helpers::rediriger("/consoles/" . $id);
|
||||
}
|
||||
|
||||
public function supprimer(string $id): void
|
||||
{
|
||||
$utilisateur = $this->auth->exigerConnexion();
|
||||
|
||||
@@ -19,12 +19,14 @@ final class JeuController
|
||||
$recherche = Helpers::champ("q");
|
||||
$consoleId = Helpers::champ("console");
|
||||
$etat = Helpers::champ("etat");
|
||||
$format = Helpers::champ("format");
|
||||
|
||||
$jeux = $this->jeuService->listerPourUtilisateur(
|
||||
$utilisateur["id"],
|
||||
$recherche !== "" ? $recherche : null,
|
||||
$consoleId !== "" ? $consoleId : null,
|
||||
$etat !== "" ? $etat : null
|
||||
$etat !== "" ? $etat : null,
|
||||
$format !== "" ? $format : null
|
||||
);
|
||||
|
||||
$consoles = $this->consoleService->listerPourUtilisateur($utilisateur["id"]);
|
||||
@@ -44,7 +46,8 @@ final class JeuController
|
||||
"filtres" => [
|
||||
"q" => $recherche,
|
||||
"console" => $consoleId,
|
||||
"etat" => $etat
|
||||
"etat" => $etat,
|
||||
"format" => $format
|
||||
]
|
||||
]);
|
||||
}
|
||||
@@ -269,6 +272,7 @@ final class JeuController
|
||||
$this->jeuService->modifier($id, $utilisateur["id"], array_merge($jeu, [
|
||||
"jaquette" => $fichier,
|
||||
"genres" => $jeu["genres"] ?? [],
|
||||
"formats" => $jeu["formats"] ?? [],
|
||||
"sourceEnrichissement" => ($jeu["sourceEnrichissement"] ?? "") === "manuel" ? "api" : ($jeu["sourceEnrichissement"] ?? "api")
|
||||
]));
|
||||
|
||||
@@ -309,6 +313,7 @@ final class JeuController
|
||||
"titre" => Helpers::champ("titre"),
|
||||
"consoleId" => Helpers::champ("consoleId"),
|
||||
"etat" => Helpers::champ("etat", "possede"),
|
||||
"formats" => $_POST["formats"] ?? [],
|
||||
"jaquette" => Helpers::champ("jaquette"),
|
||||
"description" => Helpers::champ("description"),
|
||||
"dateSortie" => Helpers::champ("dateSortie"),
|
||||
@@ -328,6 +333,7 @@ final class JeuController
|
||||
"titre" => Helpers::champ("titre"),
|
||||
"consoleId" => Helpers::champ("consoleId"),
|
||||
"etat" => Helpers::champ("etat", "possede"),
|
||||
"formats" => [],
|
||||
"jaquette" => Helpers::champ("jaquette"),
|
||||
"description" => Helpers::champ("description"),
|
||||
"dateSortie" => Helpers::champ("dateSortie"),
|
||||
|
||||
@@ -52,6 +52,48 @@ final class Helpers
|
||||
return self::etatsJeu()[$etat] ?? $etat;
|
||||
}
|
||||
|
||||
public static function formatsJeu(): array
|
||||
{
|
||||
return [
|
||||
"physique" => "Physique",
|
||||
"dematerialise" => "Dématérialisé",
|
||||
"rom" => "ROM"
|
||||
];
|
||||
}
|
||||
|
||||
public static function libelleFormat(string $format): string
|
||||
{
|
||||
return self::formatsJeu()[$format] ?? $format;
|
||||
}
|
||||
|
||||
public static function normaliserFormats(mixed $formats): array
|
||||
{
|
||||
if (!is_array($formats))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
$autorises = array_keys(self::formatsJeu());
|
||||
$resultat = [];
|
||||
|
||||
foreach ($formats as $format)
|
||||
{
|
||||
$format = is_string($format) ? trim($format) : "";
|
||||
|
||||
if ($format !== "" && in_array($format, $autorises, true))
|
||||
{
|
||||
$resultat[] = $format;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($resultat));
|
||||
}
|
||||
|
||||
public static function champCoche(string $nom): bool
|
||||
{
|
||||
return isset($_POST[$nom]) || isset($_GET[$nom]);
|
||||
}
|
||||
|
||||
public static function urlJaquette(?string $chemin): string
|
||||
{
|
||||
if (self::jaquetteAbsente($chemin))
|
||||
@@ -84,6 +126,38 @@ final class Helpers
|
||||
return !is_file($fichier);
|
||||
}
|
||||
|
||||
public static function urlPhotoConsole(?string $chemin): string
|
||||
{
|
||||
if ($chemin === null || trim($chemin) === "")
|
||||
{
|
||||
return "/assets/img/console-manquante.svg";
|
||||
}
|
||||
|
||||
if (str_starts_with($chemin, "http://") || str_starts_with($chemin, "https://"))
|
||||
{
|
||||
return $chemin;
|
||||
}
|
||||
|
||||
return "/consoles-photos/" . ltrim($chemin, "/");
|
||||
}
|
||||
|
||||
public static function photoConsoleAbsente(?string $chemin): bool
|
||||
{
|
||||
if ($chemin === null || trim($chemin) === "")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (str_starts_with($chemin, "http://") || str_starts_with($chemin, "https://"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$fichier = Config::get("consolesPhotosPath") . "/" . basename($chemin);
|
||||
|
||||
return !is_file($fichier);
|
||||
}
|
||||
|
||||
public static function tronquer(string $texte, int $longueur = 140): string
|
||||
{
|
||||
if (mb_strlen($texte) <= $longueur)
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Upload d'images locales (jaquettes, photos de consoles).
|
||||
*/
|
||||
final class ImageUpload
|
||||
{
|
||||
private const EXTENSIONS = [
|
||||
"image/jpeg" => "jpg",
|
||||
"image/png" => "png",
|
||||
"image/webp" => "webp",
|
||||
"image/gif" => "gif"
|
||||
];
|
||||
|
||||
public static function enregistrer(array $fichier, string $dossierCible, int $tailleMaxMo = 5): string
|
||||
{
|
||||
if (($fichier["error"] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE)
|
||||
{
|
||||
throw new InvalidArgumentException("Aucun fichier sélectionné.");
|
||||
}
|
||||
|
||||
if (($fichier["error"] ?? UPLOAD_ERR_OK) !== UPLOAD_ERR_OK)
|
||||
{
|
||||
throw new InvalidArgumentException("Échec de l'envoi du fichier.");
|
||||
}
|
||||
|
||||
$tailleMax = $tailleMaxMo * 1024 * 1024;
|
||||
|
||||
if (($fichier["size"] ?? 0) <= 0 || ($fichier["size"] ?? 0) > $tailleMax)
|
||||
{
|
||||
throw new InvalidArgumentException("La photo doit faire moins de " . $tailleMaxMo . " Mo.");
|
||||
}
|
||||
|
||||
$tmp = (string) ($fichier["tmp_name"] ?? "");
|
||||
|
||||
if ($tmp === "" || !is_uploaded_file($tmp))
|
||||
{
|
||||
throw new InvalidArgumentException("Fichier upload invalide.");
|
||||
}
|
||||
|
||||
$info = @getimagesize($tmp);
|
||||
|
||||
if ($info === false || empty($info["mime"]))
|
||||
{
|
||||
throw new InvalidArgumentException("Le fichier doit être une image (JPG, PNG, WebP ou GIF).");
|
||||
}
|
||||
|
||||
$mime = (string) $info["mime"];
|
||||
|
||||
if (!isset(self::EXTENSIONS[$mime]))
|
||||
{
|
||||
throw new InvalidArgumentException("Format d'image non supporté.");
|
||||
}
|
||||
|
||||
if (!is_dir($dossierCible))
|
||||
{
|
||||
mkdir($dossierCible, 0775, true);
|
||||
}
|
||||
|
||||
$nomFichier = Helpers::genererId() . "." . self::EXTENSIONS[$mime];
|
||||
$destination = rtrim($dossierCible, "/\\") . "/" . $nomFichier;
|
||||
|
||||
if (!move_uploaded_file($tmp, $destination))
|
||||
{
|
||||
throw new RuntimeException("Impossible d'enregistrer la photo.");
|
||||
}
|
||||
|
||||
return $nomFichier;
|
||||
}
|
||||
|
||||
public static function supprimer(?string $nomFichier, string $dossierCible): void
|
||||
{
|
||||
if ($nomFichier === null || $nomFichier === "")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (str_starts_with($nomFichier, "http://") || str_starts_with($nomFichier, "https://"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$chemin = rtrim($dossierCible, "/\\") . "/" . basename($nomFichier);
|
||||
|
||||
if (is_file($chemin))
|
||||
{
|
||||
unlink($chemin);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ final class ConsoleService
|
||||
return $console;
|
||||
}
|
||||
|
||||
public function creer(string $utilisateurId, array $donnees): array
|
||||
public function creer(string $utilisateurId, array $donnees, ?array $fichierPhoto = null): array
|
||||
{
|
||||
$nom = trim((string) ($donnees["nom"] ?? ""));
|
||||
$constructeur = trim((string) ($donnees["constructeur"] ?? ""));
|
||||
@@ -47,7 +47,16 @@ final class ConsoleService
|
||||
throw new InvalidArgumentException("Le nom et le constructeur sont obligatoires.");
|
||||
}
|
||||
|
||||
$annee = $this->normaliserAnnee($donnees["annee"] ?? null);
|
||||
$photo = null;
|
||||
|
||||
if ($this->fichierPresent($fichierPhoto))
|
||||
{
|
||||
$photo = ImageUpload::enregistrer(
|
||||
$fichierPhoto,
|
||||
Config::get("consolesPhotosPath")
|
||||
);
|
||||
}
|
||||
|
||||
$maintenant = Helpers::maintenant();
|
||||
|
||||
$console = [
|
||||
@@ -55,7 +64,8 @@ final class ConsoleService
|
||||
"utilisateurId" => $utilisateurId,
|
||||
"nom" => $nom,
|
||||
"constructeur" => $constructeur,
|
||||
"annee" => $annee,
|
||||
"annee" => $this->normaliserAnnee($donnees["annee"] ?? null),
|
||||
"photo" => $photo,
|
||||
"notes" => trim((string) ($donnees["notes"] ?? "")),
|
||||
"createdAt" => $maintenant,
|
||||
"updatedAt" => $maintenant
|
||||
@@ -64,7 +74,7 @@ final class ConsoleService
|
||||
return $this->store->ajouter($console);
|
||||
}
|
||||
|
||||
public function modifier(string $id, string $utilisateurId, array $donnees): array
|
||||
public function modifier(string $id, string $utilisateurId, array $donnees, ?array $fichierPhoto = null): array
|
||||
{
|
||||
$console = $this->trouverPourUtilisateur($id, $utilisateurId);
|
||||
|
||||
@@ -81,13 +91,25 @@ final class ConsoleService
|
||||
throw new InvalidArgumentException("Le nom et le constructeur sont obligatoires.");
|
||||
}
|
||||
|
||||
$miseAJour = $this->store->mettreAJour($id, [
|
||||
$champs = [
|
||||
"nom" => $nom,
|
||||
"constructeur" => $constructeur,
|
||||
"annee" => $this->normaliserAnnee($donnees["annee"] ?? null),
|
||||
"notes" => trim((string) ($donnees["notes"] ?? "")),
|
||||
"updatedAt" => Helpers::maintenant()
|
||||
]);
|
||||
];
|
||||
|
||||
if ($this->fichierPresent($fichierPhoto))
|
||||
{
|
||||
$nouvellePhoto = ImageUpload::enregistrer(
|
||||
$fichierPhoto,
|
||||
Config::get("consolesPhotosPath")
|
||||
);
|
||||
ImageUpload::supprimer($console["photo"] ?? null, Config::get("consolesPhotosPath"));
|
||||
$champs["photo"] = $nouvellePhoto;
|
||||
}
|
||||
|
||||
$miseAJour = $this->store->mettreAJour($id, $champs);
|
||||
|
||||
if ($miseAJour === null)
|
||||
{
|
||||
@@ -97,6 +119,59 @@ final class ConsoleService
|
||||
return $miseAJour;
|
||||
}
|
||||
|
||||
public function mettreAJourPhoto(string $id, string $utilisateurId, array $fichierPhoto): array
|
||||
{
|
||||
$console = $this->trouverPourUtilisateur($id, $utilisateurId);
|
||||
|
||||
if ($console === null)
|
||||
{
|
||||
throw new RuntimeException("Console introuvable.");
|
||||
}
|
||||
|
||||
$nouvellePhoto = ImageUpload::enregistrer(
|
||||
$fichierPhoto,
|
||||
Config::get("consolesPhotosPath")
|
||||
);
|
||||
|
||||
ImageUpload::supprimer($console["photo"] ?? null, Config::get("consolesPhotosPath"));
|
||||
|
||||
$miseAJour = $this->store->mettreAJour($id, [
|
||||
"photo" => $nouvellePhoto,
|
||||
"updatedAt" => Helpers::maintenant()
|
||||
]);
|
||||
|
||||
if ($miseAJour === null)
|
||||
{
|
||||
throw new RuntimeException("Échec de la mise à jour de la photo.");
|
||||
}
|
||||
|
||||
return $miseAJour;
|
||||
}
|
||||
|
||||
public function supprimerPhoto(string $id, string $utilisateurId): array
|
||||
{
|
||||
$console = $this->trouverPourUtilisateur($id, $utilisateurId);
|
||||
|
||||
if ($console === null)
|
||||
{
|
||||
throw new RuntimeException("Console introuvable.");
|
||||
}
|
||||
|
||||
ImageUpload::supprimer($console["photo"] ?? null, Config::get("consolesPhotosPath"));
|
||||
|
||||
$miseAJour = $this->store->mettreAJour($id, [
|
||||
"photo" => null,
|
||||
"updatedAt" => Helpers::maintenant()
|
||||
]);
|
||||
|
||||
if ($miseAJour === null)
|
||||
{
|
||||
throw new RuntimeException("Échec de la suppression de la photo.");
|
||||
}
|
||||
|
||||
return $miseAJour;
|
||||
}
|
||||
|
||||
public function supprimer(string $id, string $utilisateurId): bool
|
||||
{
|
||||
$console = $this->trouverPourUtilisateur($id, $utilisateurId);
|
||||
@@ -106,6 +181,8 @@ final class ConsoleService
|
||||
return false;
|
||||
}
|
||||
|
||||
ImageUpload::supprimer($console["photo"] ?? null, Config::get("consolesPhotosPath"));
|
||||
|
||||
return $this->store->supprimer($id);
|
||||
}
|
||||
|
||||
@@ -114,6 +191,12 @@ final class ConsoleService
|
||||
return count($this->listerPourUtilisateur($utilisateurId));
|
||||
}
|
||||
|
||||
private function fichierPresent(?array $fichier): bool
|
||||
{
|
||||
return is_array($fichier)
|
||||
&& (($fichier["error"] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_NO_FILE);
|
||||
}
|
||||
|
||||
private function normaliserAnnee(mixed $annee): ?int
|
||||
{
|
||||
if ($annee === null || $annee === "")
|
||||
|
||||
@@ -15,7 +15,8 @@ final class JeuService
|
||||
string $utilisateurId,
|
||||
?string $recherche = null,
|
||||
?string $consoleId = null,
|
||||
?string $etat = null
|
||||
?string $etat = null,
|
||||
?string $format = null
|
||||
): array
|
||||
{
|
||||
$jeux = $this->store->filtrer(
|
||||
@@ -38,6 +39,19 @@ final class JeuService
|
||||
));
|
||||
}
|
||||
|
||||
if ($format !== null && $format !== "")
|
||||
{
|
||||
$jeux = array_values(array_filter(
|
||||
$jeux,
|
||||
static function (array $jeu) use ($format): bool
|
||||
{
|
||||
$formats = $jeu["formats"] ?? [];
|
||||
|
||||
return is_array($formats) && in_array($format, $formats, true);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
if ($recherche !== null && $recherche !== "")
|
||||
{
|
||||
$terme = mb_strtolower($recherche);
|
||||
@@ -50,7 +64,8 @@ final class JeuService
|
||||
$jeu["description"] ?? "",
|
||||
$jeu["editeur"] ?? "",
|
||||
$jeu["developpeur"] ?? "",
|
||||
implode(" ", $jeu["genres"] ?? [])
|
||||
implode(" ", $jeu["genres"] ?? []),
|
||||
implode(" ", $jeu["formats"] ?? [])
|
||||
];
|
||||
|
||||
return str_contains(mb_strtolower(implode(" ", $champs)), $terme);
|
||||
@@ -199,6 +214,7 @@ final class JeuService
|
||||
"titre" => $titre,
|
||||
"consoleId" => $consoleId,
|
||||
"etat" => $etat,
|
||||
"formats" => Helpers::normaliserFormats($donnees["formats"] ?? []),
|
||||
"jaquette" => $jaquette,
|
||||
"description" => trim((string) ($donnees["description"] ?? "")),
|
||||
"dateSortie" => $this->normaliserDate($donnees["dateSortie"] ?? null),
|
||||
|
||||
+4
-1
@@ -7,6 +7,7 @@ require_once __DIR__ . "/JsonStore.php";
|
||||
require_once __DIR__ . "/Csrf.php";
|
||||
require_once __DIR__ . "/Flash.php";
|
||||
require_once __DIR__ . "/Helpers.php";
|
||||
require_once __DIR__ . "/ImageUpload.php";
|
||||
require_once __DIR__ . "/View.php";
|
||||
require_once __DIR__ . "/Auth.php";
|
||||
require_once __DIR__ . "/Router.php";
|
||||
@@ -22,7 +23,7 @@ require_once __DIR__ . "/Controllers/AdminController.php";
|
||||
|
||||
Config::charger();
|
||||
|
||||
foreach (["", "/jaquettes", "/cache"] as $sousDossier)
|
||||
foreach (["", "/jaquettes", "/consoles-photos", "/cache"] as $sousDossier)
|
||||
{
|
||||
$chemin = Config::get("dataPath") . $sousDossier;
|
||||
|
||||
@@ -63,6 +64,8 @@ $router->post("/consoles/creer", [$consoleController, "creer"]);
|
||||
$router->get("/consoles/{id}", [$consoleController, "detail"]);
|
||||
$router->get("/consoles/{id}/modifier", [$consoleController, "modifier"]);
|
||||
$router->post("/consoles/{id}/modifier", [$consoleController, "modifier"]);
|
||||
$router->post("/consoles/{id}/photo", [$consoleController, "envoyerPhoto"]);
|
||||
$router->post("/consoles/{id}/photo/supprimer", [$consoleController, "supprimerPhoto"]);
|
||||
$router->post("/consoles/{id}/supprimer", [$consoleController, "supprimer"]);
|
||||
|
||||
$router->get("/jeux", [$jeuController, "liste"]);
|
||||
|
||||