From ea4ced044292f20c01a8d5f4420aeddfb13ef561 Mon Sep 17 00:00:00 2001
From: follea <thomas.follea@gmail.com>
Date: Tue, 18 Jun 2024 17:00:04 +0200
Subject: [PATCH] Ajout de la playlist, ajout et suppression de son dans
 l'onglet playlist

---
 .../controllers/ConnexionController.php       |  6 ++
 .../application/controllers/Playlist.php      | 83 +++++++++++++++++++
 .../application/models/Model_music.php        | 48 +++++++++++
 .../application/views/playlist_list.php       | 25 ++++++
 .../application/views/playlist_view.php       | 53 ++++++++++++
 .../application/views/song_search_results.php | 27 ++++++
 codeigniter/assets/style.css                  |  4 +
 7 files changed, 246 insertions(+)
 create mode 100644 codeigniter/application/controllers/Playlist.php
 create mode 100644 codeigniter/application/views/playlist_list.php
 create mode 100644 codeigniter/application/views/playlist_view.php
 create mode 100644 codeigniter/application/views/song_search_results.php

diff --git a/codeigniter/application/controllers/ConnexionController.php b/codeigniter/application/controllers/ConnexionController.php
index f521bbc..044704b 100644
--- a/codeigniter/application/controllers/ConnexionController.php
+++ b/codeigniter/application/controllers/ConnexionController.php
@@ -26,12 +26,18 @@ class ConnexionController extends CI_Controller {
                 $result = $query->row(); // Récupérer la première ligne de résultat
     
                 if($result){ // Vérifier si l'utilisateur existe
+                    $this->session->set_userdata('user_id', $result->id);
                     $this->session->set_userdata('pseudo', $result->pseudo);
                     redirect('../index.php');
                 } else {
                     $data['error_msg'] = "Email ou mot de passe incorrect.";
                 }
             }
+
+            echo "<pre>";
+            print_r($this->session->userdata());
+            echo "</pre>";
+
             $this->load->view('layout/header');
             $this->load->view('connexion', $data);
             $this->load->view('layout/footer');
diff --git a/codeigniter/application/controllers/Playlist.php b/codeigniter/application/controllers/Playlist.php
new file mode 100644
index 0000000..557d191
--- /dev/null
+++ b/codeigniter/application/controllers/Playlist.php
@@ -0,0 +1,83 @@
+<?php
+defined('BASEPATH') OR exit('No direct script access allowed');
+
+class Playlist extends CI_Controller {
+
+    public function __construct(){
+        parent::__construct();
+        $this->load->model('model_music');
+        $this->load->helper('html');
+        $this->load->helper('url');
+        $this->load->helper('form');
+    }
+
+    public function index(){
+
+        $userId = $this->session->userdata('user_id');
+        $playlists = $this->model_music->getPlaylistsByUser($userId);
+        $this->load->view('layout/header');
+        $this->load->view('playlist_list', ['playlists' => $playlists]);
+        $this->load->view('layout/footer');
+    }
+
+    public function create(){
+        $name = $this->input->post('name');
+        $userId = $this->session->userdata('user_id');
+        $this->model_music->createPlaylist($name, $userId);
+        redirect('playlist');
+    }
+
+    public function delete($playlistId){
+        $this->model_music->deletePlaylist($playlistId);
+        redirect('playlist');
+    }
+
+    public function view($id) {
+        $songs = $this->model_music->getSongsByPlaylist($id);
+        $playlist = $this->model_music->getPlaylistById($id);
+        
+        if ($playlist) {
+            $data['playlistName'] = $playlist->name; // Passez le nom de la playlist à la vue
+            $data['songs'] = $songs;
+            $data['playlistId'] = $id;
+    
+            $this->load->view('layout/header');
+            $this->load->view('playlist_view', $data);
+            $this->load->view('layout/footer');
+        } else {
+            echo "Playlist non trouvée.";
+        }
+    }
+
+    public function add_song(){
+        $playlistId = $this->input->post('playlistId');
+        $songId = $this->input->post('songId');
+        $this->model_music->addSongToPlaylist($playlistId, $songId);
+        redirect('playlists/view/' . $playlistId);
+    }
+    
+
+    public function remove_song(){
+        $playlistId = $this->input->post('playlistId');
+        $songId = $this->input->post('songId');
+        $this->model_music->removeSongFromPlaylist($playlistId, $songId);
+        redirect('playlist/view/' . $playlistId);
+    }
+
+    public function search_song(){
+        $playlistId = $this->input->post('playlistId');
+        $songName = $this->input->post('songName');
+    
+        // Recherche la chanson par son nom
+        $song = $this->model_music->findSongByName($songName);
+    
+        if ($song) {
+            // Si la chanson est trouvée, ajoutez-la à la playlist
+            $this->model_music->addSongToPlaylist($playlistId, $song->id);
+        }
+    
+        // Redirige l'utilisateur vers la vue de la playlist mise à jour
+        redirect('playlist/view/' . $playlistId);
+    }
+}
+?>
diff --git a/codeigniter/application/models/Model_music.php b/codeigniter/application/models/Model_music.php
index 8482578..426c8d8 100644
--- a/codeigniter/application/models/Model_music.php
+++ b/codeigniter/application/models/Model_music.php
@@ -132,4 +132,52 @@ class Model_music extends CI_Model {
 		return $query->result();
 	}
 
+    public function createPlaylist($name, $userId) {
+        $data = array(
+            'name' => $name,
+            'userId' => $userId 
+        );
+        return $this->db->insert('playlist', $data);
+    }
+    public function deletePlaylist($playlistId) {
+        $this->db->delete('playlist', array('id' => $playlistId));
+        $this->db->delete('playlistsong', array('playlistId' => $playlistId));
+    }
+
+    public function getPlaylistsByUser($userId) {
+        $query = $this->db->get_where('playlist', array('userId' => $userId));
+        return $query->result();
+    }
+
+    public function getPlaylistById($playlistId) {
+        $query = $this->db->get_where('playlist', array('id' => $playlistId), 1);
+        return $query->row(); // Renvoie la première ligne trouvée (la playlist correspondant à l'ID)
+    }
+
+    public function addSongToPlaylist($playlistId, $songId) {
+        $data = array(
+            'playlistId' => $playlistId,
+            'songId' => $songId
+        );
+        return $this->db->insert('playlistsong', $data);
+    }
+
+    public function removeSongFromPlaylist($playlistId, $songId) {
+        $this->db->delete('playlistsong', array('playlistId' => $playlistId, 'songId' => $songId));
+    }
+
+    public function getSongsByPlaylist($playlistId) {
+        $this->db->select('song.*');
+        $this->db->from('playlistsong');
+        $this->db->join('song', 'playlistsong.songId = song.id');
+        $this->db->where('playlistsong.playlistId', $playlistId);
+        $query = $this->db->get();
+        return $query->result();
+    }
+
+    public function findSongByName($songName) {
+        $query = $this->db->get_where('song', array('name' => $songName));
+        return $query->row(); 
+    }
+    
 }
\ No newline at end of file
diff --git a/codeigniter/application/views/playlist_list.php b/codeigniter/application/views/playlist_list.php
new file mode 100644
index 0000000..f1cea33
--- /dev/null
+++ b/codeigniter/application/views/playlist_list.php
@@ -0,0 +1,25 @@
+<h5>Mes Playlists</h5>
+
+<!-- Formulaire pour créer la playlist avec le nom voulu -->
+<form action="<?= site_url('playlist/create'); ?>" method="post" class="create-playlist-form">
+    <input type="text" name="name" placeholder="Nom de la playlist" required>
+    <button type="submit">Créer</button>
+</form>
+
+<!-- Affichez les playlist que nous avons -->
+<section class="playlists">
+    <?php foreach($playlists as $playlist): ?>
+        <div>
+            <article>
+                <header class="short-text">
+                    <?= anchor("playlist/view/{$playlist->id}", "{$playlist->name}"); ?>
+                </header>
+                <!-- Bouton pour supprimer la playlist -->
+                <form action="<?= site_url('playlist/delete/' . $playlist->id); ?>" method="post" style="display:inline;">
+                    <button type="submit">Supprimer</button>
+                </form>
+            </article>
+        </div>
+    <?php endforeach; ?>
+</section>
+
diff --git a/codeigniter/application/views/playlist_view.php b/codeigniter/application/views/playlist_view.php
new file mode 100644
index 0000000..ed7c2f8
--- /dev/null
+++ b/codeigniter/application/views/playlist_view.php
@@ -0,0 +1,53 @@
+<!-- En-tête de la playlist -->
+<h5>Playlist : <?= $playlistName; ?></h5>
+
+<!-- Formulaire pour rechercher et ajouter une chanson -->
+<form action="<?= site_url('playlist/search_song'); ?>" method="post" class="add-song-form">
+    <input type="hidden" name="playlistId" value="<?= $playlistId; ?>">
+    <input type="text" name="songName" placeholder="Nom de la chanson" required>
+    <button type="submit">Rechercher et Ajouter</button>
+</form>
+
+<!-- Section pour afficher les résultats de la recherche -->
+<?php if (!empty($searchResults)): ?>
+    <section class="search-results">
+        <h5>Résultats de la recherche :</h5>
+        <ul>
+            <?php foreach($searchResults as $song): ?>
+                <li>
+                    <?= $song->name; ?>
+                    <!-- Formulaire pour ajouter la chanson à la playlist -->
+                    <form action="<?= site_url('playlist/add_song'); ?>" method="post" style="display:inline;">
+                        <input type="hidden" name="playlistId" value="<?= $playlistId; ?>">
+                        <input type="hidden" name="songId" value="<?= $song->id; ?>">
+                        <button type="submit">Ajouter</button>
+                    </form>
+                </li>
+            <?php endforeach; ?>
+        </ul>
+    </section>
+    <p>Test</p>
+<?php endif; ?>
+
+<!-- Section pour afficher les chansons de son playlist -->
+<?php if (!empty($songs)): ?>
+    <section class="current-songs">
+        <h5>Chansons actuelles :</h5>
+        <ul>
+            <?php foreach($songs as $song): ?>
+                <li>
+                    <?= $song->name; ?>
+                    <!-- Formulaire pour supprimer la chanson de la playlist -->
+                    <form action="<?= site_url('playlist/remove_song'); ?>" method="post" style="display:inline;">
+                        <input type="hidden" name="playlistId" value="<?= $playlistId; ?>">
+                        <input type="hidden" name="songId" value="<?= $song->id; ?>">
+                        <button type="submit">Supprimer</button>
+                    </form>
+                </li>
+            <?php endforeach; ?>
+        </ul>
+    </section>
+<?php endif; ?>
+
+<a href="<?= site_url('playlist'); ?>" class="btn btn-secondary">Retour à toutes les playlists</a>
+
diff --git a/codeigniter/application/views/song_search_results.php b/codeigniter/application/views/song_search_results.php
new file mode 100644
index 0000000..6e51a29
--- /dev/null
+++ b/codeigniter/application/views/song_search_results.php
@@ -0,0 +1,27 @@
+<h5>Résultats de la recherche pour : <?= $this->input->get('query'); ?></h5>
+
+<form action="<?= site_url('playlist/search_song'); ?>" method="get" class="search-song-form">
+    <input type="text" name="query" placeholder="Nom de la chanson" required>
+    <button type="submit">Rechercher</button>
+</form>
+
+<section class="songs">
+<?php foreach($songs as $song): ?>
+    <div>
+        <article>
+            <header class="short-text">
+                <?= $song->name; ?>
+                <form action="<?= site_url('playlists/add_song'); ?>" method="post" style="display:inline;">
+                    <input type="hidden" name="songId" value="<?= $song->id; ?>">
+                    <select name="playlistId" required>
+                        <?php foreach($playlists as $playlist): ?>
+                            <option value="<?= $playlist->id; ?>"><?= $playlist->name; ?></option>
+                        <?php endforeach; ?>
+                    </select>
+                    <button type="submit">Ajouter à la playlist</button>
+                </form>
+            </header>
+        </article>
+    </div>
+<?php endforeach; ?>
+</section>
diff --git a/codeigniter/assets/style.css b/codeigniter/assets/style.css
index 725f574..0bda3f5 100644
--- a/codeigniter/assets/style.css
+++ b/codeigniter/assets/style.css
@@ -195,3 +195,7 @@ div.new {
 .tri-button {
     width: 55%!important;
 }
+
+form.search-form {
+    margin-left: -200px;
+}