Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
controller_search.class.php
Aller à la documentation de ce fichier.
1<?php
2
8{
15 public function __construct(\Twig\Environment $twig, \Twig\Loader\FilesystemLoader $loader)
16 {
17 parent::__construct($loader, $twig);
18 }
19
23 public function json_search()
24 {
25 if (ob_get_length()) ob_clean();
26 header('Content-Type: application/json');
27
28 $term = $_GET['term'] ?? '';
29 $term = trim($term);
30
31 $results = [
32 'artistes' => [],
33 'albums' => [],
34 'chansons' => []
35 ];
36
37 try {
38 if (!empty($term)) {
39 $pdo = $this->getPDO();
40
41 // --- ARTISTES ---
42 $utilisateurDAO = new UtilisateurDAO($pdo);
43 $artistes = $utilisateurDAO->rechercher($term);
44 if (is_array($artistes)) {
45 foreach ($artistes as $artiste) {
46 $results['artistes'][] = [
47 'pseudo' => $artiste->getPseudoUtilisateur(),
48 'image' => $artiste->geturlPhotoUtilisateur() ?: "assets/images/profile_pictures/default.png"
49 ];
50 }
51 }
52
53 // --- ALBUMS ---
54 $albumDAO = new AlbumDAO($pdo);
55 $listaAlbums = $albumDAO->rechercher($term);
56 if (is_array($listaAlbums)) {
57 foreach ($listaAlbums as $album) {
58 $img = $album->geturlPochetteAlbum();
59 $path = $img ? ltrim($img, '/') : "assets/images/albums/default.png";
60
61 $results['albums'][] = [
62 'id' => $album->getIdAlbum(),
63 'titre' => $album->getTitreAlbum(),
64 'image' => $path,
65 'artiste' => $album->getPseudoArtiste()
66 ];
67 }
68 }
69
70 // --- CHANSONS ---
71 $chansonDAO = new ChansonDAO($pdo);
72 $listaChansons = $chansonDAO->rechercherParTitre($term);
73 if (is_array($listaChansons)) {
74 foreach ($listaChansons as $chanson) {
75 $albumObj = $chanson->getAlbumChanson();
76 $idAlbum = ($albumObj && method_exists($albumObj, 'getIdAlbum')) ? $albumObj->getIdAlbum() : null;
77
78 $results['chansons'][] = [
79 'id' => $chanson->getIdChanson(),
80 'titre' => $chanson->getTitrechanson(),
81 'ecoutes' => $chanson->getNbecoutechanson(),
82 'idAlbum' => $idAlbum,
83 ];
84 }
85 }
86 }
87
88 ob_clean();
89 echo json_encode($results);
90
91 } catch (Exception $e) {
92 ob_clean();
93 echo json_encode(['error' => $e->getMessage()]);
94 }
95
96 exit;
97 }
98}
json_search()
Retourne les résultats de la recherche au format JSON.
__construct(\Twig\Environment $twig, \Twig\Loader\FilesystemLoader $loader)
Constructeur du contrôleur search.
Classe de base pour tous les contrôleurs de l'application.
Twig Environment $twig
Twig Loader FilesystemLoader $loader
getPDO()
Récupère la connexion PDO.