Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
controller_chanson.class.php
Aller à la documentation de ce fichier.
1<?php
2
27{
34 public function __construct(\Twig\Environment $twig, \Twig\Loader\FilesystemLoader $loader)
35 {
36 parent::__construct($loader, $twig);
37 }
38
46 public function afficher()
47 {
48 $idChanson = isset($_GET['idChanson']) ? $_GET['idChanson'] : null;
49
50 // Récupération de la chanson
51 $managerChanson = new ChansonDao($this->getPdo());
52 $chanson = $managerChanson->findId($idChanson);
53
54 $template = $this->getTwig()->load('test.html.twig');
55 echo $template->render(array(
56 'page' => [
57 'title' => "Chanson",
58 'name' => "chanson",
59 'description' => "Chanson dans Paaxio"
60 ],
61 'testing' => $chanson,
62 ));
63 }
64
72 public function rechercherParTitre()
73 {
74 $titreChanson = isset($_GET['titreChanson']) ? $_GET['titreChanson'] : null;
75
76 // Récupération des chansons par titre
77 $managerChanson = new ChansonDao($this->getPdo());
78 $chanson = $managerChanson->rechercherParTitre($titreChanson);
79
80 $template = $this->getTwig()->load('test.html.twig');
81 echo $template->render(array(
82 'page' => [
83 'title' => "Chanson",
84 'name' => "chanson",
85 'description' => "Chanson dans Paaxio"
86 ],
87 'testing' => $chanson,
88 ));
89 }
90
98 public function rechercherParAlbum()
99 {
100 $idAlbum = isset($_GET['idAlbum']) ? $_GET['idAlbum'] : null;
101
102 // Récupération des chansons de l'album
103 $managerChanson = new ChansonDao($this->getPdo());
104 $chanson = $managerChanson->rechercherParAlbum($idAlbum);
105
106 $template = $this->getTwig()->load('test.html.twig');
107 echo $template->render(array(
108 'page' => [
109 'title' => "Chanson",
110 'name' => "chanson",
111 'description' => "Chanson dans Paaxio"
112 ],
113 'testing' => $chanson,
114 ));
115 }
116
124 public function lister()
125 {
126 // Récupération des chansons
127 $managerChanson = new ChansonDao($this->getPdo());
128 $chansons = $managerChanson->findAll();
129
130 // Choix du template
131 $template = $this->getTwig()->load('test.html.twig');
132
133 // Affichage de la page
134 echo $template->render(array(
135 'page' => [
136 'title' => "Chansons",
137 'name' => "chansons",
138 'description' => "Chansons dans Paaxio"
139 ],
140 'testing' => $chansons,
141 ));
142 }
143
151 public function listerTableau()
152 {
153 $managerChanson = new ChansonDao($this->getPdo());
154 $chansons = $managerChanson->findAll();
155
156 // Génération de la vue
157 $template = $this->getTwig()->load('test.html.twig');
158 echo $template->render(array(
159 'page' => [
160 'title' => "Chansons tableau",
161 'name' => "chansont",
162 'description' => "Chansons tableau dans Paaxio"
163 ],
164 'testing' => $chansons,
165 ));
166 }
167
179 public function filtrerChanson()
180 {
181 // Récupération des filtres depuis l'URL
182 $idGenre = $_GET['idGenre'] ?? null;
183 $idAlbum = $_GET['idAlbum'] ?? null;
184
185 // Récupération de l'ordre (asc ou desc) et de la colonne de tri
186 $ordre = isset($_GET['ordre']) && in_array(strtolower($_GET['ordre']), ['asc', 'desc'])
187 ? strtoupper($_GET['ordre'])
188 : 'ASC';
189
190 $tri = $_GET['tri'] ?? 'titreChanson';
191 $colonnesValides = ['titreChanson', 'dateTeleversementChanson', 'nbEcouteChanson'];
192 $colonne = in_array($tri, $colonnesValides) ? $tri : 'titreChanson';
193
194 // Récupération des chansons filtrées via le DAO
195 $managerChanson = new ChansonDao($this->getPdo());
196 $chansons = $managerChanson->filtrerChanson($idGenre, $idAlbum, $colonne, $ordre);
197
198 // Génération de la vue
199 $template = $this->getTwig()->load('test.html.twig');
200 echo $template->render([
201 'page' => [
202 'title' => "Chansons filtrées",
203 'name' => "chansons_filtrees",
204 'description' => "Chansons filtrées dans Paaxio"
205 ],
206 'testing' => $chansons,
207 ]);
208 }
209
218 public function toggleLike()
219 {
220 // Vérifie la connexion
221 $this->requireAuth();
222
223 $emailUtilisateur = $_SESSION['user_email'] ?? null;
224
225 // Récupère l'ID de la chanson depuis POST
226 $idChanson = $_POST['idChanson'] ?? null;
227 if (!$idChanson) {
228 http_response_code(400);
229 echo json_encode(['error' => 'ID de chanson manquant']);
230 exit;
231 }
232
233 $chansonDAO = new ChansonDAO($this->getPdo());
234
235 // Vérifie l'état actuel du like avant de basculer
236 $chansonsLikees = $chansonDAO->findChansonsLikees($emailUtilisateur);
237 $estLikee = false;
238 foreach ($chansonsLikees as $chanson) {
239 if ($chanson->getIdChanson() == $idChanson) {
240 $estLikee = true;
241 break;
242 }
243 }
244
245 // Bascule le like
246 $chansonDAO->toggleLike($emailUtilisateur, $idChanson);
247
248 // Renvoie le nouvel état du like
249 header('Content-Type: application/json');
250 echo json_encode(['liked' => !$estLikee]);
251 exit;
252 }
253
263 public function incrementEcoute()
264 {
265 // Vérification de l'authentification
266 $emailUtilisateur = $_SESSION['user_email'] ?? null;
267 if (!$emailUtilisateur) {
268 http_response_code(401);
269 echo json_encode(['error' => 'Utilisateur non connecté']);
270 exit;
271 }
272
273 $idChanson = $_POST['idChanson'] ?? null;
274 if (!$idChanson) {
275 http_response_code(400);
276 echo json_encode(['error' => 'ID de chanson manquant']);
277 exit;
278 }
279
280 $chansonDAO = new ChansonDAO($this->getPdo());
281 $nouveau = $chansonDAO->incrementNbEcoute((int)$idChanson);
282
283 if ($nouveau === null) {
284 http_response_code(500);
285 echo json_encode(['error' => 'Impossible d\'incrémenter']);
286 exit;
287 }
288
289 header('Content-Type: application/json');
290 echo json_encode(['success' => true, 'nbEcoute' => $nouveau]);
291 exit;
292 }
293}
Contrôleur dédié à la gestion des chansons.
rechercherParTitre()
Recherche des chansons par leur titre.
rechercherParAlbum()
Recherche des chansons par album.
afficher()
Affiche les détails d'une chanson spécifique.
toggleLike()
Bascule l'état "j'aime" d'une chanson pour l'utilisateur connecté.
lister()
Liste toutes les chansons de la plateforme.
__construct(\Twig\Environment $twig, \Twig\Loader\FilesystemLoader $loader)
Constructeur du contrôleur chanson.
listerTableau()
Liste toutes les chansons sous forme de tableau.
incrementEcoute()
Incrémente le compteur d'écoutes d'une chanson.
filtrerChanson()
Filtre les chansons selon différents critères.
Classe de base pour tous les contrôleurs de l'application.
Twig Environment $twig
requireAuth(string $controller='', string $method='', array $params=[])
Exige que l'utilisateur soit authentifié.
Twig Loader FilesystemLoader $loader
getTwig()
Récupère l'environnement Twig.