Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
controller_album.class.php
Aller à la documentation de ce fichier.
1<?php
2
26{
33 public function __construct(\Twig\Environment $twig, \Twig\Loader\FilesystemLoader $loader)
34 {
35 parent::__construct($loader, $twig);
36 }
37
46 public function afficher()
47 {
48 $idAlbum = isset($_GET['idAlbum']) ? (int)$_GET['idAlbum'] : null;
49
50 if (!$idAlbum) {
51 $this->redirectTo('home', 'afficher');
52 }
53
54 // Récupération de l'album
55 $managerAlbum = new AlbumDAO($this->getPdo());
56 $album = $managerAlbum->find($idAlbum);
57
58 if (!$album) {
59 $this->redirectTo('home', 'afficher');
60 }
61
62 // Récupération des chansons de l'album
63 $managerChanson = new ChansonDAO($this->getPdo());
64 $chansons = $managerChanson->rechercherParAlbum($idAlbum);
65
66 // Chargement du template
67 $template = $this->getTwig()->load('chanson_album.html.twig');
68 echo $template->render([
69 'page' => [
70 'title' => $album->getTitreAlbum(),
71 'name' => "album",
72 'description' => "Album dans Paaxio"
73 ],
74 'album' => $album,
75 'chansons' => $chansons,
76 ]);
77 }
78
86 public function lister()
87 {
88 // Récupération des albums
89 $managerAlbum = new AlbumDao($this->getPdo());
90 $albums = $managerAlbum->findAll();
91
92 // Choix du template
93 $template = $this->getTwig()->load('test.html.twig');
94
95 // Affichage de la page
96 echo $template->render(array(
97 'page' => [
98 'title' => "Albums",
99 'name' => "albums",
100 'description' => "Albums dans Paaxio"
101 ],
102 'testing' => $albums,
103 ));
104 }
105
113 public function listerTableau()
114 {
115 $managerAlbum = new AlbumDao($this->getPdo());
116 $albums = $managerAlbum->findAll();
117
118 // Génération de la vue
119 $template = $this->getTwig()->load('test.html.twig');
120 echo $template->render(array(
121 'page' => [
122 'title' => "Albums tableau",
123 'name' => "albumt",
124 'description' => "Albums tableau dans Paaxio"
125 ],
126 'testing' => $albums,
127 ));
128 }
129
139 public function afficherFormulaireAjout()
140 {
141 // Vérifier si l'utilisateur est un artiste connecté
142 // Connexion obligatoire pour ajouter un album (redirige vers connect si non connecté)
143 $this->requireRole(RoleEnum::Artiste);
144
145 $idAlbum = $_GET['idAlbum'] ?? null;
146 $albumExistant = null;
147 $managerAlbum = new AlbumDAO($this->getPdo());
148
149 if ($idAlbum) {
150 $albumExistant = $managerAlbum->find((int)$idAlbum);
151 // Vérifier que l'album appartient bien à l'artiste connecté
152 if (!$albumExistant || $albumExistant->getArtisteAlbum() !== $_SESSION['user_email']) {
153 $this->redirectTo('home', 'afficher');
154 }
155 }
156
157 // Récupérer les albums de l'artiste
158 $managerAlbum = new AlbumDAO($this->getPdo());
159 $albumsArtiste = $managerAlbum->findAllByArtistEmail($_SESSION['user_email']);
160
161 $template = $this->getTwig()->load('album_ajout.html.twig');
162 echo $template->render([
163 'page' => [
164 'title' => "Ajouter un Album/Single",
165 'name' => "album_ajout",
166 'description' => "Téléversez vos chansons pour créer un album ou un single."
167 ],
168 'session' => $_SESSION,
169 'albums_artiste' => $albumsArtiste,
170 'album_existant' => $albumExistant
171 ]);
172 }
173
187 public function ajouterAlbum()
188 {
189 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
190 $this->redirectTo('home', 'afficher');
191 }
192
193 $this->requireRole(RoleEnum::Artiste);
194
195 $managerChanson = new ChansonDAO($this->getPdo());
196 $managerGenre = new GenreDAO($this->getPdo());
197 $managerAlbum = new AlbumDAO($this->getPdo());
198
199 $idAlbumExistant = $_POST['id_album_existant'] ?? null;
200
201 if ($idAlbumExistant) {
202 // Ajout de chansons à un album existant
203 $albumCree = $managerAlbum->find((int)$idAlbumExistant);
204 if (!$albumCree || $albumCree->getArtisteAlbum() !== $_SESSION['user_email']) {
205 // Gérer l'erreur : l'album n'existe pas ou n'appartient pas à l'utilisateur
206 $this->redirectTo('home', 'afficher');
207 }
208 $idAlbum = $albumCree->getIdAlbum();
209 } else {
210 // Création d'un nouvel album
211 $album = new Album();
212 $album->setTitreAlbum($_POST['titre_album'] ?? '');
213 $album->setDateSortieAlbum($_POST['date_sortie'] ?? '');
214 $album->setArtisteAlbum($_SESSION['user_email']);
215
216 // Gérer la pochette de l'album
217 if (isset($_FILES['pochette_album']) && $_FILES['pochette_album']['error'] === UPLOAD_ERR_OK) {
218 $uploadDir = 'assets' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'albums' . DIRECTORY_SEPARATOR;
219 if (!is_dir($uploadDir)) {
220 mkdir($uploadDir, 0777, true);
221 }
222 $nomFichier = basename($_FILES['pochette_album']['name']);
223 $cheminPochette = $uploadDir . uniqid() . '-' . $nomFichier;
224 if (move_uploaded_file($_FILES['pochette_album']['tmp_name'], $cheminPochette)) {
225 $album->seturlPochetteAlbum($cheminPochette);
226 }
227 } else {
228 // Fournir une URL de pochette par défaut si aucune n'est téléchargée
229 $album->seturlPochetteAlbum('assets' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'albums' . DIRECTORY_SEPARATOR . 'default.png');
230 }
231 $idAlbum = $managerAlbum->create($album);
232 $albumCree = $managerAlbum->find($idAlbum);
233 }
234
235 // Gérer les chansons
236 if (isset($_POST['tracks']) && isset($_FILES['tracks'])) {
237 $uploadDirMusique = 'assets' . DIRECTORY_SEPARATOR . 'audio' . DIRECTORY_SEPARATOR;
238 // Inclure getID3 pour analyser les fichiers audio
239 require_once 'vendor/james-heinrich/getid3/getid3/getid3.php';
240 $getID3 = new getID3;
241
242 if (!is_dir($uploadDirMusique)) {
243 mkdir($uploadDirMusique, 0777, true);
244 }
245
246 foreach ($_POST['tracks'] as $index => $chansonData) {
247 // Vérifier si le fichier correspondant a été uploadé
248 if (!isset($_FILES['tracks']['tmp_name'][$index]['file']) || $_FILES['tracks']['error'][$index]['file'] !== UPLOAD_ERR_OK) {
249 continue; // Passer à la chanson suivante si le fichier est manquant ou a une erreur
250 }
251
252 $nomFichierOriginal = basename($_FILES['tracks']['name'][$index]['file']);
253 $cheminCible = $uploadDirMusique . uniqid() . '-' . $nomFichierOriginal;
254
255 if (!move_uploaded_file($_FILES['tracks']['tmp_name'][$index]['file'], $cheminCible)) {
256 continue; // Erreur lors du déplacement du fichier
257 }
258
259 // Analyser le fichier pour obtenir les métadonnées, y compris la durée
260 $infoFichier = $getID3->analyze($cheminCible);
261 $duree = (int)($infoFichier['playtime_seconds'] ?? 0);
262
263 $chanson = new Chanson();
264 $chanson->setTitreChanson($chansonData['title']);
265 $chanson->setDureeChanson($duree);
266 $chanson->setDateTeleversementChanson(new DateTime());
267 $chanson->setAlbumChanson($albumCree);
268 $chanson->setEmailPublicateur($_SESSION['user_email']);
269 $chanson->setUrlAudioChanson($cheminCible);
270
271 $nomGenre = $chansonData['genre'] ?? null;
272 if ($nomGenre) {
273 $genreExistant = $managerGenre->findByName($nomGenre);
274 if ($genreExistant) {
275 $chanson->setGenreChanson($genreExistant);
276 } else {
277 $idNouveauGenre = $managerGenre->create($nomGenre);
278 $chanson->setGenreChanson($managerGenre->find($idNouveauGenre));
279 }
280 }
281
282 $managerChanson->createChanson($chanson);
283 }
284 }
285
286 if ($idAlbumExistant) {
287 // Rediriger vers la page de détails de l'album mis à jour
288 $this->redirectTo('album', 'afficherDetails', ['idAlbum' => $idAlbumExistant, 'success' => 1]);
289 } else {
290 // Rediriger vers le tableau de bord après la création d'un nouvel album
291 $this->redirectTo('home', 'afficher', ['success' => 1]);
292 }
293 }
294
304 public function afficherDetails()
305 {
306
307 $idAlbum = $_GET['idAlbum'] ?? null;
308 $idChanson = $_GET['idChanson'] ?? null;
309
310 if (!$idAlbum) {
311 // Gérer l'erreur, par exemple rediriger
312 $this->redirectTo('home', 'afficher');
313 }
314
315 $albumDAO = new AlbumDAO($this->getPDO());
316 $album = $albumDAO->find((int)$idAlbum);
317
318 $chansonDAO = new ChansonDAO($this->getPDO());
319 $chansons = $chansonDAO->rechercherParAlbum((int)$idAlbum);
320
321 // Déterminer le rôle de l'utilisateur à partir de la session
322 $userRole = $_SESSION['user_role'] ?? null;
323 $template = '';
324
325 // Choisir le template en fonction du rôle
326 // 2 pour artiste, 1 (ou autre) pour auditeur
327 if ($userRole === RoleEnum::Artiste && $album->getArtisteAlbum() === $_SESSION['user_email']) {
328 // Si l'utilisateur est l'artiste propriétaire de l'album, il voit la page d'édition.
329 $template = 'album_details_artiste.html.twig';
330 } else {
331 // Sinon (auditeur, ou artiste regardant l'album d'un autre), il voit la page de lecture.
332 $template = 'album_details_auditeur.html.twig';
333 }
334
335 $template = $this->getTwig()->load($template);
336 echo $template->render([
337 'album' => $album,
338 'chansons' => $chansons,
339 'chansonSelected' => $idChanson ? (int)$idChanson : null,
340 'session' => $_SESSION,
341 ]);
342 }
343
353 public function modifierChanson()
354 {
355 // Sécurité : vérifier la méthode, la session et le rôle
356 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
357 $this->redirectTo('home', 'afficher');
358 }
359
360 $this->requireRole(RoleEnum::Artiste);
361
362 $idChanson = $_GET['idChanson'] ?? null;
363 $idAlbum = $_POST['id_album'] ?? null; // Assurez-vous que ce champ est dans le formulaire de la modale
364
365 if (!$idChanson || !$idAlbum) {
366 // Rediriger si les IDs sont manquants
367 $this->redirectTo('home', 'afficher', ['error' => 1]);
368 }
369
370 $chansonDAO = new ChansonDAO($this->getPDO());
371 $chanson = $chansonDAO->findId((int)$idChanson);
372
373 // Vérifier que la chanson existe et appartient bien à un album de l'artiste
374 if (!$chanson || $chanson->getAlbumChanson()->getArtisteAlbum() !== $_SESSION['user_email']) {
375 $this->redirectTo('home', 'afficher', ['error' => 'unauthorized']);
376 }
377
378 // Mettre à jour les informations
379 $chanson->setTitreChanson($_POST['titre_chanson']);
380
381 $genreDAO = new GenreDAO($this->getPDO());
382 $nomGenre = $_POST['genre_chanson'] ?? '';
383 $genre = $genreDAO->findOrCreateByName($nomGenre);
384 $chanson->setGenreChanson($genre);
385
386 $chansonDAO->updateChanson($chanson);
387
388 // Rediriger vers la page de l'album avec un message de succès
389 $this->redirectTo('album', 'afficherDetails', ['idAlbum' => $idAlbum, 'success_update' => 1]);
390 }
391
402 public function supprimerChanson()
403 {
404 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
405 $this->redirectTo('home', 'afficher');
406 }
407
408 $this->requireRole(RoleEnum::Artiste);
409
410 $idChanson = $_GET['idChanson'] ?? null;
411 $idAlbum = $_POST['id_album'] ?? null;
412
413 if (!$idChanson || !$idAlbum) {
414 $this->redirectTo('home', 'afficher', ['error' => 1]);
415 }
416
417 $chansonDAO = new ChansonDAO($this->getPDO());
418 $chanson = $chansonDAO->findId((int)$idChanson);
419
420 if (!$chanson || !$chanson->getAlbumChanson() || $chanson->getAlbumChanson()->getArtisteAlbum() !== $_SESSION['user_email']) {
421 $this->redirectTo('home', 'afficher', ['error' => 'unauthorized']);
422 }
423
424 // Supprimer le fichier audio local si présent
425 $urlAudio = $chanson->getUrlAudioChanson();
426 if ($urlAudio && strpos($urlAudio, 'http') !== 0) {
427 // Chemin local attendu
428 if (file_exists($urlAudio) && is_writable($urlAudio)) {
429 @unlink($urlAudio);
430 }
431 }
432
433 // Supprimer la chanson en base
434 $chansonDAO->deleteChanson((int)$idChanson);
435
436 // Rediriger vers la page de détails de l'album
437 $this->redirectTo('album', 'afficherDetails', ['idAlbum' => $idAlbum, 'deleted' => 1]);
438 }
439}
Contrôleur dédié à la gestion des albums.
__construct(\Twig\Environment $twig, \Twig\Loader\FilesystemLoader $loader)
Constructeur du contrôleur album.
ajouterAlbum()
Traite l'ajout d'un nouvel album ou l'ajout de chansons à un album existant.
modifierChanson()
Modifie les informations d'une chanson.
afficher()
Affiche les détails d'un album avec ses chansons.
lister()
Liste tous les albums de la plateforme.
afficherFormulaireAjout()
Affiche le formulaire d'ajout d'album.
afficherDetails()
Affiche les détails d'un album avec vue différenciée selon le rôle.
supprimerChanson()
Supprime une chanson (action réservée à l'artiste propriétaire).
listerTableau()
Liste tous les albums sous forme de tableau.
Classe de base pour tous les contrôleurs de l'application.
Twig Environment $twig
requireRole($requiredRole)
Exige que l'utilisateur ait un rôle spécifique.
redirectTo(string $controller, string $method, array $params=[])
Redirige vers un contrôleur et une méthode donnés.
Twig Loader FilesystemLoader $loader
getPDO()
Récupère la connexion PDO.
getTwig()
Récupère l'environnement Twig.
$genreDAO
Definition include.php:307