Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
controller_playlist.class.php
Aller à la documentation de ce fichier.
1<?php
2
24{
31 public function __construct(\Twig\Environment $twig, \Twig\Loader\FilesystemLoader $loader)
32 {
33 parent::__construct($loader, $twig);
34 }
35
45 public function afficher()
46 {
47
48 $idPlaylist = isset($_GET['idPlaylist']) ? (int)$_GET['idPlaylist'] : null;
49
50 if (!$idPlaylist) {
51 $this->redirectTo('home', 'afficher');
52 }
53
54 $this->requireAuth();
55
56 // Récupération de la playlist
57 $managerPlaylist = new PlaylistDAO($this->getPdo());
58 $playlist = $managerPlaylist->findFromUser($idPlaylist, $_SESSION['user_email'] ?? null);
59
60 if (!$playlist) {
61 $this->redirectTo('home', 'afficher');
62 }
63
64 // Récupération des chansons de la playlist
65 $chansons = $managerPlaylist->getChansonsByPlaylist($idPlaylist, $_SESSION['user_email'] ?? null);
66
67 $nomPlaylist = $playlist->getNomPlaylist();
68 $imagePlaylist = $managerPlaylist->recupererPochetteAuto($idPlaylist);
69 $playlistObj = new class($nomPlaylist, $imagePlaylist) {
70 private string $nom;
71 private ?string $image;
72 public function __construct(string $nom, ?string $image)
73 {
74 $this->nom = $nom;
75 $this->image = $image;
76 }
77 public function getTitreAlbum(): string
78 {
79 return $this->nom;
80 }
81 public function getUrlImageAlbum(): ?string
82 {
83 return $this->image;
84 }
85 public function getArtisteAlbum(): string
86 {
87 return '';
88 }
89 public function getDateSortieAlbum(): ?string
90 {
91 return null;
92 }
93 };
94
95 // Chargement du template
96 $template = $this->getTwig()->load('chanson_album.html.twig');
97 echo $template->render([
98 'page' => [
99 'title' => $playlist->getNomPlaylist(),
100 'name' => "playlist",
101 'description' => "Playlist dans Paaxio"
102 ],
103 'album' => $playlistObj,
104 'chansons' => $chansons,
105 'idPlaylist' => $idPlaylist
106 ]);
107 }
108
117 public function ajouterChanson()
118 {
119 header('Content-Type: application/json');
120
121 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
122 http_response_code(405);
123 echo json_encode(['success' => false, 'message' => 'Méthode non autorisée']);
124 exit;
125 }
126
127 if (!isset($_SESSION['user_logged_in']) || !$_SESSION['user_logged_in']) {
128 http_response_code(401);
129 echo json_encode(['success' => false, 'message' => 'Non authentifié']);
130 exit;
131 }
132
133 $idPlaylist = isset($_POST['idPlaylist']) ? (int)$_POST['idPlaylist'] : 0;
134 $idChanson = isset($_POST['idChanson']) ? (int)$_POST['idChanson'] : 0;
135
136 if (!$idPlaylist || !$idChanson) {
137 http_response_code(400);
138 echo json_encode(['success' => false, 'message' => 'Paramètres manquants']);
139 exit;
140 }
141
142 $managerPlaylist = new PlaylistDAO($this->getPdo());
143 $playlist = $managerPlaylist->findFromUser($idPlaylist, $_SESSION['user_email'] ?? null);
144
145 if (!$playlist) {
146 http_response_code(403);
147 echo json_encode(['success' => false, 'message' => 'Playlist introuvable ou accès refusé']);
148 exit;
149 }
150
151 $added = $managerPlaylist->ajouterChansonPlaylist($idPlaylist, $idChanson);
152
153 if ($added) {
154 echo json_encode(['success' => true, 'message' => 'Chanson ajoutée à la playlist']);
155 } else {
156 echo json_encode(['success' => false, 'message' => 'Cette chanson est déjà dans la playlist']);
157 }
158 exit;
159 }
160
169 public function supprimerChanson()
170 {
171 header('Content-Type: application/json');
172
173 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
174 http_response_code(405);
175 echo json_encode(['success' => false, 'message' => 'Méthode non autorisée']);
176 exit;
177 }
178
179 if (!isset($_SESSION['user_logged_in']) || !$_SESSION['user_logged_in']) {
180 http_response_code(401);
181 echo json_encode(['success' => false, 'message' => 'Non authentifié']);
182 exit;
183 }
184
185 $idPlaylist = isset($_POST['idPlaylist']) ? (int)$_POST['idPlaylist'] : 0;
186 $idChanson = isset($_POST['idChanson']) ? (int)$_POST['idChanson'] : 0;
187
188 if (!$idPlaylist || !$idChanson) {
189 http_response_code(400);
190 echo json_encode(['success' => false, 'message' => 'Paramètres manquants']);
191 exit;
192 }
193
194 $managerPlaylist = new PlaylistDAO($this->getPdo());
195 $playlist = $managerPlaylist->findFromUser($idPlaylist, $_SESSION['user_email'] ?? null);
196
197 if (!$playlist) {
198 http_response_code(403);
199 echo json_encode(['success' => false, 'message' => 'Playlist introuvable ou accès refusé']);
200 exit;
201 }
202
203 $removed = $managerPlaylist->supprimerChansonPlaylist($idPlaylist, $idChanson);
204
205 if ($removed) {
206 echo json_encode(['success' => true, 'message' => 'Chanson retirée de la playlist']);
207 } else {
208 echo json_encode(['success' => false, 'message' => 'Cette chanson n\'est pas dans la playlist']);
209 }
210 exit;
211 }
212
220 public function creerPlaylist()
221 {
222 header('Content-Type: application/json');
223
224 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
225 http_response_code(405);
226 echo json_encode(['success' => false, 'message' => 'Méthode non autorisée']);
227 exit;
228 }
229
230 if (!isset($_SESSION['user_logged_in']) || !$_SESSION['user_logged_in']) {
231 http_response_code(401);
232 echo json_encode(['success' => false, 'message' => 'Non authentifié']);
233 exit;
234 }
235
236 $nom = trim($_POST['nomPlaylist'] ?? '');
237
238 if ($nom === '') {
239 http_response_code(400);
240 echo json_encode(['success' => false, 'message' => 'Le nom de la playlist est requis']);
241 exit;
242 }
243
244 if (mb_strlen($nom) > 255) {
245 http_response_code(400);
246 echo json_encode(['success' => false, 'message' => 'Le nom est trop long (255 caractères max)']);
247 exit;
248 }
249
250 $managerPlaylist = new PlaylistDAO($this->getPdo());
251 $idPlaylist = $managerPlaylist->creerPlaylist($nom, $_SESSION['user_email']);
252
253 echo json_encode([
254 'success' => true,
255 'message' => 'Playlist créée',
256 'idPlaylist' => $idPlaylist,
257 'nomPlaylist' => $nom
258 ]);
259 exit;
260 }
261
270 public function supprimerPlaylist()
271 {
272 header('Content-Type: application/json');
273
274 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
275 http_response_code(405);
276 echo json_encode(['success' => false, 'message' => 'Méthode non autorisée']);
277 exit;
278 }
279
280 if (!isset($_SESSION['user_logged_in']) || !$_SESSION['user_logged_in']) {
281 http_response_code(401);
282 echo json_encode(['success' => false, 'message' => 'Non authentifié']);
283 exit;
284 }
285
286 $idPlaylist = isset($_POST['idPlaylist']) ? (int)$_POST['idPlaylist'] : 0;
287
288 if (!$idPlaylist) {
289 http_response_code(400);
290 echo json_encode(['success' => false, 'message' => 'Paramètre manquant']);
291 exit;
292 }
293
294 $managerPlaylist = new PlaylistDAO($this->getPdo());
295 $playlist = $managerPlaylist->findFromUser($idPlaylist, $_SESSION['user_email'] ?? null);
296
297 if (!$playlist) {
298 http_response_code(403);
299 echo json_encode(['success' => false, 'message' => 'Playlist introuvable ou accès refusé']);
300 exit;
301 }
302
303 $deleted = $managerPlaylist->supprimerPlaylist($idPlaylist, $_SESSION['user_email']);
304
305 if ($deleted) {
306 echo json_encode(['success' => true, 'message' => 'Playlist supprimée']);
307 } else {
308 echo json_encode(['success' => false, 'message' => 'Erreur lors de la suppression']);
309 }
310 exit;
311 }
312
320 public function lister()
321 {
322 // Récupération des playlists
323 $managerPlaylist = new PlaylistDao($this->getPdo());
324 $playlists = $managerPlaylist->findAll();
325
326 // Choix du template
327 $template = $this->getTwig()->load('test.html.twig');
328
329 // Affichage de la page
330 echo $template->render(array(
331 'page' => [
332 'title' => "Playlists",
333 'name' => "playlists",
334 'description' => "Playlists dans Paaxio"
335 ],
336 'testing' => $playlists,
337 ));
338 }
339
347 public function listerTableau()
348 {
349 $managerPlaylist = new PlaylistDao($this->getPdo());
350 $playlists = $managerPlaylist->findAll();
351
352 // Génération de la vue
353 $template = $this->getTwig()->load('test.html.twig');
354 echo $template->render(array(
355 'page' => [
356 'title' => "Playlists tableau",
357 'name' => "playlistt",
358 'description' => "Playlists tableau dans Paaxio"
359 ],
360 'testing' => $playlists,
361 ));
362 }
363}
Contrôleur dédié à la gestion des playlists.
creerPlaylist()
Liste toutes les playlists de la plateforme.
lister()
Liste toutes les playlists de la plateforme.
listerTableau()
Liste toutes les playlists sous forme de tableau.
supprimerChanson()
Supprime une chanson d'une playlist de l'utilisateur connecté (AJAX).
__construct(\Twig\Environment $twig, \Twig\Loader\FilesystemLoader $loader)
Constructeur du contrôleur playlist.
supprimerPlaylist()
Supprime une playlist de l'utilisateur connecté (AJAX).
ajouterChanson()
Ajoute une chanson à une playlist de l'utilisateur connecté (AJAX).
afficher()
Affiche une playlist avec ses chansons.
Classe de base pour tous les contrôleurs de l'application.
Twig Environment $twig
redirectTo(string $controller, string $method, array $params=[])
Redirige vers un contrôleur et une méthode donnés.
requireAuth(string $controller='', string $method='', array $params=[])
Exige que l'utilisateur soit authentifié.
Twig Loader FilesystemLoader $loader
getTwig()
Récupère l'environnement Twig.