Comus Party 1.0.0
Application web de mini-jeux en ligne
Chargement...
Recherche...
Aucune correspondance
article.dao.php
Aller à la documentation de ce fichier.
1<?php
9
10namespace ComusParty\Models;
11
12
13use ComusParty\App\Exceptions\NotFoundException;
14use DateMalformedStringException;
15use DateTime;
16use Exception;
17use PDO;
18
24{
29 private ?PDO $pdo;
30
31
36 public function __construct(?PDO $pdo)
37 {
38 $this->pdo = $pdo;
39 }
40
45 public function getPdo(): ?PDO
46 {
47 return $this->pdo;
48 }
49
54 public function setPdo(?PDO $pdo): void
55 {
56 $this->pdo = $pdo;
57 }
58
66 public function findArticlesByInvoiceId(?int $invoiceId): ?array
67 {
68 $stmt = $this->pdo->prepare(
69 'SELECT *
70 FROM ' . DB_PREFIX . 'invoice i
71 JOIN ' . DB_PREFIX . 'player p ON i.player_uuid = p.uuid
72 WHERE i.id = :id AND p.uuid = :uuid');
73 $stmt->bindParam(':id', $invoiceId);
74 $stmt->bindParam(':uuid', $_SESSION['uuid']);
75 $stmt->execute();
76 $stmt->setFetchMode(PDO::FETCH_ASSOC);
77 $invoice = $stmt->fetch();
78 if ($invoice === false) {
79 throw new NotFoundException('Cette facture n\'existe pas.');
80 }
81
82 $stmt = $this->pdo->prepare(
83 'SELECT a.*
84 FROM ' . DB_PREFIX . 'article a
85 JOIN ' . DB_PREFIX . 'invoice_row ir ON a.id = ir.article_id
86 WHERE ir.invoice_id = :invoice_id');
87 $stmt->bindParam(':invoice_id', $invoiceId);
88 $stmt->execute();
89 $stmt->setFetchMode(PDO::FETCH_ASSOC);
90 $articles = $stmt->fetchAll();
91 if ($articles === false) {
92 return null;
93 }
94 return $this->hydrateMany($articles);
95 }
96
104 public function hydrateMany(array $data): array
105 {
106 $articles = [];
107 foreach ($data as $article) {
108 $articles[] = $this->hydrate($article);
109 }
110 return $articles;
111 }
112
119 public function hydrate(array $data): Article
120 {
121 $article = new Article();
122 $article->setId($data['id']);
123 $article->setName($data['name']);
124
125 if ($data['type'] == 'pfp') {
126 $type = ArticleType::ProfilePicture;
127 } elseif ($data['type'] == 'banner') {
128 $type = ArticleType::Banner;
129 }
130
131 $article->setType($type);
132 $article->setDescription($data['description']);
133 $article->setPricePoint($data['price_point']);
134 $article->setPriceEuro($data['price_euro']);
135
136 $article->setCreatedAt(new DateTime($data['created_at']));
137 $article->setUpdatedAt(new DateTime($data['updated_at']));
138 $article->setFilePath($data['file_path']);
139
140 return $article;
141 }
142
149 public function findAllPfpsOwnedByPlayer(string $uuid): ?array
150 {
151 $stmt = $this->pdo->prepare(
152 'SELECT a.*
153 FROM ' . DB_PREFIX . 'article a
154 JOIN ' . DB_PREFIX . 'invoice_row ir ON a.id = ir.article_id
155 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
156 WHERE i.player_uuid = :uuid AND type = "pfp" ');
157 $stmt->bindParam(':uuid', $uuid);
158 $stmt->execute();
159 $stmt->setFetchMode(PDO::FETCH_ASSOC);
160 $articles = $stmt->fetchAll();
161 return $this->hydrateMany($articles);
162 }
163
170 public function findAllBannersOwnedByPlayer(string $uuid): ?array
171 {
172 $stmt = $this->pdo->prepare(
173 'SELECT a.*
174 FROM ' . DB_PREFIX . 'article a
175 JOIN ' . DB_PREFIX . 'invoice_row ir ON a.id = ir.article_id
176 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
177 WHERE i.player_uuid = :uuid AND type = "banner" ');
178 $stmt->bindParam(':uuid', $uuid);
179 $stmt->execute();
180 $stmt->setFetchMode(PDO::FETCH_ASSOC);
181 $articles = $stmt->fetchAll();
182 return $this->hydrateMany($articles);
183 }
184
185
192
193 public function findAll(): ?array
194 {
195 $stmt = $this->pdo->query(
196 'SELECT *
197 FROM ' . DB_PREFIX . 'article');
198 $stmt->setFetchMode(PDO::FETCH_ASSOC);
199 $tabArticles = $stmt->fetchAll();
200 if ($tabArticles === false) {
201 return null;
202 }
203 return $this->hydrateMany($tabArticles);
204 }
205
213 function findArticlesWithIds(array $ids): ?array
214 {
215
216 if (!empty($ids)) {
217 $idsString = implode(',', $ids);
218
219 $stmt = $this->pdo->query('SELECT * FROM ' . DB_PREFIX . 'article WHERE id IN (' . $idsString . ')');
220 $stmt->setFetchMode(PDO::FETCH_ASSOC);
221
222 $tabArticles = $stmt->fetchAll();
223
224 if ($tabArticles === false) {
225 return null;
226 }
227 return $this->hydrateMany($tabArticles);
228 }
229 return null;
230 }
231
238
239 public function findAllPfps(): ?array
240 {
241 $stmt = $this->pdo->query("SELECT *
242 FROM " . DB_PREFIX . "article
243 WHERE type = 'pfp'");
244
245 $stmt->setFetchMode(PDO::FETCH_ASSOC);
246 $tabPfps = $stmt->fetchAll();
247
248 if ($tabPfps === false) {
249 return null;
250 }
251 return $this->hydrateMany($tabPfps);
252 }
253
260 public function findAllBanners(): ?array
261 {
262 $stmt = $this->pdo->query("SELECT *
263 FROM " . DB_PREFIX . "article
264 WHERE type = 'banner'");
265 $stmt->setFetchMode(PDO::FETCH_ASSOC);
266 $tabBanners = $stmt->fetchAll();
267 if ($tabBanners === false) {
268 return null;
269 }
270 return $this->hydrateMany($tabBanners);
271
272 }
273
282 public function updateActiveArticle(string $uuid, string $idArticle, string $typeArticle)
283 {
284 if ($typeArticle == "ProfilePicture") {
285 $pfpActive = $this->findActivePfpByPlayerUuid($uuid);
286 // Si pfp déjà équipé
287 if ($pfpActive != null) {
288 $idPfpActive = $pfpActive->getId();
289 $stmt = $this->pdo->prepare(
290 'UPDATE ' . DB_PREFIX . 'invoice_row ir
291 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
292 JOIN ' . DB_PREFIX . 'article a ON ir.article_id = a.id
293 SET ir.active = 0
294 WHERE i.player_uuid = :uuid AND ir.article_id = :idArticleActif');
295 $stmt->bindParam(':uuid', $uuid);
296 $stmt->bindParam(':idArticleActif', $idPfpActive);
297 $stmt->execute();
298
299 }
300
301// Mettre en active la pfp choisie
302 $stmt = $this->pdo->prepare(
303 'UPDATE ' . DB_PREFIX . 'invoice_row ir
304 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
305 JOIN ' . DB_PREFIX . 'article a ON ir.article_id = a.id
306 SET ir.active = 1
307 WHERE i.player_uuid = :uuid AND ir.article_id = :idArticle'
308 );
309 $stmt->bindParam(':uuid', $uuid);
310 $stmt->bindParam(':idArticle', $idArticle);
311 $ok = $stmt->execute();
312 $pfp = $this->findById($idArticle);
313 if ($pfp != null) {
314 $_SESSION['pfpPath'] = $pfp->getFilePath();
315 }
316 return $ok;
317 }
318
319 if ($typeArticle == "Banner") {
320 $bannerActive = $this->findActiveBannerByPlayerUuid($uuid);
321
322 //Si banner déjà équipé
323 if ($bannerActive != null) {
324 $idBannerActive = $bannerActive->getId();
325 $stmt = $this->pdo->prepare(
326 'UPDATE ' . DB_PREFIX . 'invoice_row ir
327 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
328 JOIN ' . DB_PREFIX . 'article a ON ir.article_id = a.id
329 SET ir.active = 0
330 WHERE i.player_uuid = :uuid AND ir.article_id = :idArticleActif');
331 $stmt->bindParam(':uuid', $uuid);
332 $stmt->bindParam(':idArticleActif', $idBannerActive);
333 $ok = $stmt->execute();
334
335 }
336
337 //Met en active la bannière choisie
338 $stmt = $this->pdo->prepare(
339 'UPDATE ' . DB_PREFIX . 'invoice_row ir
340 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
341 JOIN ' . DB_PREFIX . 'article a ON ir.article_id = a.id
342 SET ir.active = 1
343 WHERE i.player_uuid = :uuid AND ir.article_id = :idArticle'
344 );
345 $stmt->bindParam(':uuid', $uuid);
346 $stmt->bindParam(':idArticle', $idArticle);
347 $ok = $stmt->execute();
348 $banner = $this->findById($idArticle);
349 if ($banner != null) {
350 $_SESSION['bannerPath'] = $banner->getFilePath();
351 }
352 return $ok;
353
354 }
355 }
356
363 public function findActivePfpByPlayerUuid(string $uuid): ?Article
364 {
365 $stmt = $this->pdo->prepare(
366 'SELECT a.*
367 FROM ' . DB_PREFIX . 'article a
368 JOIN ' . DB_PREFIX . 'invoice_row ir ON a.id = ir.article_id
369 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
370 WHERE i.player_uuid = :uuid AND a.type = "pfp" AND ir.active = 1');
371 $stmt->bindParam(':uuid', $uuid);
372 $stmt->execute();
373 $stmt->setFetchMode(PDO::FETCH_ASSOC);
374 $pfp = $stmt->fetch();
375 if ($pfp === false) {
376 return null;
377 }
378 return $this->hydrate($pfp);
379 }
380
387 public function findById(string $id): ?Article
388 {
389 $stmt = $this->pdo->prepare(
390 'SELECT *
391 FROM ' . DB_PREFIX . 'article
392 WHERE id = :id');
393 $stmt->bindParam(':id', $id);
394 $stmt->execute();
395 $stmt->setFetchMode(PDO::FETCH_ASSOC);
396 $article = $stmt->fetch();
397 if ($article === false) {
398 return null;
399 }
400 return $this->hydrate($article);
401 }
402
409 public function findActiveBannerByPlayerUuid(string $uuid): ?Article
410 {
411 $stmt = $this->pdo->prepare(
412 'SELECT a.*
413 FROM ' . DB_PREFIX . 'article a
414 JOIN ' . DB_PREFIX . 'invoice_row ir ON a.id = ir.article_id
415 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
416 WHERE i.player_uuid = :uuid AND a.type = "banner" AND ir.active = 1');
417 $stmt->bindParam(':uuid', $uuid);
418 $stmt->execute();
419 $stmt->setFetchMode(PDO::FETCH_ASSOC);
420 $banner = $stmt->fetch();
421 if ($banner === false) {
422 return null;
423 }
424 return $this->hydrate($banner);
425 }
426
432 public function deleteActiveArticleForPfp(string $uuid): void
433 {
434 $stmt = $this->pdo->prepare(
435 'SELECT p.*
436 FROM ' . DB_PREFIX . 'player p
437 WHERE p.uuid = :uuid');
438 $stmt->bindParam(':uuid', $uuid);
439 $stmt->execute();
440 if ($stmt->rowCount() === 0) {
441 // Gérer le cas où aucune ligne n'est affectée
442 throw new NotFoundException('No player found for the given UUID');
443 } else {
444 $stmt = $this->pdo->prepare(
445 'UPDATE ' . DB_PREFIX . 'invoice_row ir
446 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
447 JOIN ' . DB_PREFIX . 'article a ON ir.article_id = a.id
448 SET ir.active = 0
449 WHERE i.player_uuid = :uuid AND a.type = "pfp"');
450 $stmt->bindParam(':uuid', $uuid);
451 $stmt->execute();
452 }
453
454
455 }
456
461 public function deleteActiveArticleForBanner(string $uuid): void
462 {
463 $stmt = $this->pdo->prepare(
464 'UPDATE ' . DB_PREFIX . 'invoice_row ir
465 JOIN ' . DB_PREFIX . 'invoice i ON ir.invoice_id = i.id
466 JOIN ' . DB_PREFIX . 'article a ON ir.article_id = a.id
467 SET ir.active = 0
468 WHERE i.player_uuid = :uuid AND a.type = "banner"');
469 $stmt->bindParam(':uuid', $uuid);
470 $stmt->execute();
471 }
472}
findActivePfpByPlayerUuid(string $uuid)
Retourne la photo de profile active que le joueur possède sous forme d'objet Article.
findActiveBannerByPlayerUuid(string $uuid)
Retourne la bannière active que le joueur possède sous forme d'objet Article.
deleteActiveArticleForPfp(string $uuid)
Supprime toutes les pfps pour les mettre à 0 en active.
findAllPfpsOwnedByPlayer(string $uuid)
Retourne un tableau d'objets Article (ou null) à partir de l'UUID du joueur correspondant à l'ensembl...
hydrate(array $data)
Hydrate un objet Article avec les valeurs du tableau associatif passé en paramètre.
findArticlesWithIds(array $ids)
Retourne un tableau d'objets Article recensant l'ensemble des articles correspondant aux id "ids".
deleteActiveArticleForBanner(string $uuid)
Supprime toutes les bannières pour les mettre à 0 en active.
updateActiveArticle(string $uuid, string $idArticle, string $typeArticle)
Met à jour l'article en active dans la base de données.
getPdo()
Retourne la connexion à la base de données.
findAll()
Retourne un tableau d'objets Article recensant l'ensemble des articles enregistrés dans la base de do...
findArticlesByInvoiceId(?int $invoiceId)
Retourne un tableau d'objets Article (ou null) à partir de l'ID de la facture passé en paramètre.
findAllBannersOwnedByPlayer(string $uuid)
Retourne un tableau d'objets Article (ou null) à partir de l'UUID du joueur correspondant à l'ensembl...
findAllPfps()
Retourne un tableau d'objets Article qui ont le type profile_picture dans la base de données.
setPdo(?PDO $pdo)
Modifie la connexion à la base de données.
findAllBanners()
Retourne un tableau d'objets Article qui ont le type banner dans la base de données.
__construct(?PDO $pdo)
Le constructeur de la classe ArticleDAO.
findById(string $id)
Retourne un objet Article (ou null) à partir de l'ID passé en paramètre.
hydrateMany(array $data)
Hydrate un tableau d'objets Article avec les valeurs des tableaux associatifs du tableau passé en par...
@ Banner
Image de profil.
const DB_PREFIX
Préfixe des tables de la base de données.
Definition db.php:52