Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
playlist.dao.php
Aller à la documentation de ce fichier.
1<?php
2
9{
13 private ?PDO $pdo;
14
19 public function __construct(?PDO $pdo = null)
20 {
21 $this->pdo = $pdo;
22 }
23
28 public function findAll(): array
29 {
30 $sql = "SELECT * FROM playlist";
31 $pdoStatement = $this->pdo->prepare($sql);
32 $pdoStatement->execute();
33 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
34 $tableau = $pdoStatement->fetchAll();
35 $playlist = $this->hydrateMany($tableau);
36 return $playlist;
37 }
38
39 public function findFromUser(int $id, ?string $email): ?playlist
40 {
41 $sql = "SELECT * FROM playlist WHERE idPlaylist = :id
42 AND emailProprietaire = :email";
43 $pdoStatement = $this->pdo->prepare($sql);
44 $pdoStatement->execute(array(
45 ':id' => $id,
46 ':email' => $email
47 ));
48
49 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
50 $tableau = $pdoStatement->fetch();
51 if (!$tableau) {
52 return null;
53 }
54 $playlist = $this->hydrate($tableau);
55 return $playlist;
56 }
57
58 public function findAllFromUser(?string $email = null): array
59 {
60 if ($email) {
61 $sql = "SELECT * FROM playlist WHERE emailProprietaire = :email";
62 $stmt = $this->pdo->prepare($sql);
63 $stmt->execute([':email' => $email]);
64 $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
65 return $this->hydrateMany($results);
66 } else {
67 return [];
68 }
69 }
70
71 public function hydrate(array $tableaAssoc): ?playlist
72 {
73 if (empty($tableaAssoc)) {
74 return null;
75 }
76
77 $playlist = new Playlist();
78 $playlist->setIdPlaylist(isset($tableaAssoc['idPlaylist']) ? (int)$tableaAssoc['idPlaylist'] : null);
79 $playlist->setNomPlaylist($tableaAssoc['nomPlaylist'] ?? null);
80 $playlist->setEstPubliquePlaylist($tableaAssoc['estPubliquePlaylist'] ?? null);
81
82 // Conversion sécurisée des dates SQL → objets DateTime
83 $playlist->setDateCreationPlaylist(
84 !empty($tableaAssoc['dateCreationPlaylist']) ? new DateTime($tableaAssoc['dateCreationPlaylist']) : null
85 );
86
87 $playlist->setDateDerniereModification(
88 !empty($tableaAssoc['dateDerniereModification']) ? new DateTime($tableaAssoc['dateDerniereModification']) : null
89 );
90
91 $playlist->setEmailProprietaire($tableaAssoc['emailProprietaire'] ?? null);
92 return $playlist;
93 }
94
95 public function hydrateMany(array $tableauxAssoc): array
96 {
97 $playlists = [];
98 foreach ($tableauxAssoc as $tableauAssoc) {
99 $playlist = $this->hydrate($tableauAssoc);
100 if ($playlist !== null) {
101 $pochette = $this->recupererPochetteAuto($playlist->getIdPlaylist());
102 $playlist->setUrlPochetteAuto($pochette);
103 $playlists[] = $playlist;
104 }
105 }
106 return $playlists;
107 }
108
109 public function getChansonsByPlaylist(int $idPlaylist, ?string $emailUtilisateur = null): array
110 {
111 $sql = "
112 SELECT c.*
113 FROM chanson c
114 JOIN chansonPlaylist cp ON c.idChanson = cp.idChanson
115 WHERE cp.idPlaylist = :idPlaylist
116 ORDER BY cp.positionChanson ASC
117 ";
118
119 $stmt = $this->pdo->prepare($sql);
120 $stmt->execute([':idPlaylist' => $idPlaylist]);
121 $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
122
123 $chansons = [];
124 foreach ($results as $row) {
125 $chansonDAO = new ChansonDAO($this->pdo);
126 $chanson = $chansonDAO->hydrate($row);
127 // Vérifier si la chanson est likée par l'utilisateur connecté
128 $isLiked = false;
129 if ($emailUtilisateur) {
130 $sqlLike = "SELECT 1 FROM likeChanson WHERE idChanson = :idChanson AND emailUtilisateur = :emailUtilisateur LIMIT 1";
131 $stmtLike = $this->pdo->prepare($sqlLike);
132 $stmtLike->execute([
133 ':idChanson' => $chanson->getIdChanson(),
134 ':emailUtilisateur' => $emailUtilisateur
135 ]);
136 $isLiked = $stmtLike->fetchColumn() ? true : false;
137 }
138 $chanson->setIsLiked($isLiked);
139 $chansons[] = $chanson;
140 }
141
142 return $chansons;
143 }
144
148 public function getPdo(): ?PDO
149 {
150 return $this->pdo;
151 }
156 public function setPdo(?PDO $pdo): void
157 {
158 $this->pdo = $pdo;
159 }
160
169 public function creerPlaylist(string $nom, string $emailProprietaire, bool $estPublique = false): int
170 {
171 $sql = "INSERT INTO playlist (nomPlaylist, estPubliquePlaylist, emailProprietaire) VALUES (:nom, :estPublique, :email)";
172 $stmt = $this->pdo->prepare($sql);
173 $stmt->execute([
174 ':nom' => $nom,
175 ':estPublique' => $estPublique ? 1 : 0,
176 ':email' => $emailProprietaire
177 ]);
178 return (int)$this->pdo->lastInsertId();
179 }
180
190 public function getPlaylistIdsForChansons(array $chansonIds, string $emailUtilisateur): array
191 {
192 if (empty($chansonIds)) {
193 return [];
194 }
195
196 $placeholders = implode(',', array_fill(0, count($chansonIds), '?'));
197 $sql = "
198 SELECT cp.idChanson, cp.idPlaylist
199 FROM chansonPlaylist cp
200 JOIN playlist p ON cp.idPlaylist = p.idPlaylist
201 WHERE p.emailProprietaire = ?
202 AND cp.idChanson IN ($placeholders)
203 ";
204
205 $params = array_merge([$emailUtilisateur], array_map('intval', $chansonIds));
206 $stmt = $this->pdo->prepare($sql);
207 $stmt->execute($params);
208 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
209
210 $map = [];
211 foreach ($rows as $row) {
212 $map[(int)$row['idChanson']][] = (int)$row['idPlaylist'];
213 }
214 return $map;
215 }
216
224 public function supprimerChansonPlaylist(int $idPlaylist, int $idChanson): bool
225 {
226 $sql = "DELETE FROM chansonPlaylist WHERE idPlaylist = :idPlaylist AND idChanson = :idChanson";
227 $stmt = $this->pdo->prepare($sql);
228 $stmt->execute([':idPlaylist' => $idPlaylist, ':idChanson' => $idChanson]);
229
230 if ($stmt->rowCount() === 0) {
231 return false;
232 }
233
234 $sqlUpdate = "UPDATE playlist SET dateDerniereModification = NOW() WHERE idPlaylist = :idPlaylist";
235 $stmtUpdate = $this->pdo->prepare($sqlUpdate);
236 $stmtUpdate->execute([':idPlaylist' => $idPlaylist]);
237
238 return true;
239 }
240
249 public function ajouterChansonPlaylist(int $idPlaylist, int $idChanson): bool
250 {
251 $sqlCheck = "SELECT 1 FROM chansonPlaylist WHERE idPlaylist = :idPlaylist AND idChanson = :idChanson LIMIT 1";
252 $stmtCheck = $this->pdo->prepare($sqlCheck);
253 $stmtCheck->execute([':idPlaylist' => $idPlaylist, ':idChanson' => $idChanson]);
254 if ($stmtCheck->fetchColumn()) {
255 return false;
256 }
257
258 $sqlPos = "SELECT COALESCE(MAX(positionChanson), 0) + 1 AS nextPos FROM chansonPlaylist WHERE idPlaylist = :idPlaylist";
259 $stmtPos = $this->pdo->prepare($sqlPos);
260 $stmtPos->execute([':idPlaylist' => $idPlaylist]);
261 $nextPos = (int)$stmtPos->fetchColumn();
262
263 $sql = "INSERT INTO chansonPlaylist (idPlaylist, idChanson, positionChanson) VALUES (:idPlaylist, :idChanson, :position)";
264 $stmt = $this->pdo->prepare($sql);
265 $stmt->execute([
266 ':idPlaylist' => $idPlaylist,
267 ':idChanson' => $idChanson,
268 ':position' => $nextPos
269 ]);
270
271 $sqlUpdate = "UPDATE playlist SET dateDerniereModification = NOW() WHERE idPlaylist = :idPlaylist";
272 $stmtUpdate = $this->pdo->prepare($sqlUpdate);
273 $stmtUpdate->execute([':idPlaylist' => $idPlaylist]);
274
275 return true;
276 }
277
285 public function supprimerPlaylist(int $idPlaylist, string $emailProprietaire): bool
286 {
287 $sql = "DELETE FROM playlist WHERE idPlaylist = :idPlaylist AND emailProprietaire = :email";
288 $stmt = $this->pdo->prepare($sql);
289 $stmt->execute([
290 ':idPlaylist' => $idPlaylist,
291 ':email' => $emailProprietaire
292 ]);
293 return $stmt->rowCount() > 0;
294 }
295
300 public function recupererPochetteAuto(int $idPlaylist): ?string
301 {
302 $sql = "
303 SELECT a.urlPochetteAlbum
304 FROM chansonPlaylist cp
305 JOIN chanson c ON cp.idChanson = c.idChanson
306 JOIN album a ON c.albumChanson = a.idAlbum
307 WHERE cp.idPlaylist = :idPlaylist
308 ORDER BY cp.positionChanson ASC
309 LIMIT 1
310 ";
311
312 $stmt = $this->pdo->prepare($sql);
313 $stmt->execute(['idPlaylist' => $idPlaylist]);
314
315 $result = $stmt->fetch(PDO::FETCH_ASSOC);
316
317 return $result ? $result['urlPochetteAlbum'] : null;
318 }
319}
creerPlaylist(string $nom, string $emailProprietaire, bool $estPublique=false)
Crée une nouvelle playlist pour un utilisateur.
hydrate(array $tableaAssoc)
getPdo()
Get the value of pdo.
supprimerChansonPlaylist(int $idPlaylist, int $idChanson)
Supprime une chanson d'une playlist.
findAllFromUser(?string $email=null)
__construct(?PDO $pdo=null)
Constructeur de la classe PlaylistDAO.
findAll()
Récupère toutes les playlists de la base de données.
getPlaylistIdsForChansons(array $chansonIds, string $emailUtilisateur)
Pour un ensemble de chansons et un utilisateur, retourne un tableau chansonId => [playlistId,...
recupererPochetteAuto(int $idPlaylist)
getChansonsByPlaylist(int $idPlaylist, ?string $emailUtilisateur=null)
supprimerPlaylist(int $idPlaylist, string $emailProprietaire)
Supprime une playlist et toutes ses associations chansonPlaylist (CASCADE).
setPdo(?PDO $pdo)
Set the value of pdo.
ajouterChansonPlaylist(int $idPlaylist, int $idChanson)
Ajoute une chanson à une playlist.
hydrateMany(array $tableauxAssoc)
findFromUser(int $id, ?string $email)