diff --git a/CodeIgniter-3.1.13/application/controllers/Playlists.php b/CodeIgniter-3.1.13/application/controllers/Playlists.php index 03d214b..15752d8 100644 --- a/CodeIgniter-3.1.13/application/controllers/Playlists.php +++ b/CodeIgniter-3.1.13/application/controllers/Playlists.php @@ -33,8 +33,7 @@ class Playlists extends CI_Controller { } else { redirect('utilisateur/connexion'); } - } - + } public function verify_playlist_ownership($playlist_id) { // Vérifier si l'utilisateur est connecté @@ -120,7 +119,6 @@ class Playlists extends CI_Controller { public function add_song($playlist_id) { $this->verify_playlist_ownership($playlist_id); - if ($this->input->post()) { $data = array( 'playlist_id' => $playlist_id, @@ -134,10 +132,8 @@ class Playlists extends CI_Controller { } redirect('playlists/view/' . $playlist_id); } else { - // Récupérer toutes les musiques disponibles $data['songs'] = $this->Model_music->get_all_songs(); $data['playlist_id'] = $playlist_id; - $data['title']="Ajouter une Chanson à la Playlist"; $data['css']="assets/css/playlist_add_song"; @@ -149,7 +145,6 @@ class Playlists extends CI_Controller { public function delete($playlist_id) { $this->verify_playlist_ownership($playlist_id); - $this->Model_playlist->delete_playlist($playlist_id); redirect('playlists'); } @@ -163,7 +158,6 @@ class Playlists extends CI_Controller { public function duplicate($playlist_id) { $this->verify_playlist_ownership($playlist_id); - $playlist = $this->Model_playlist->get_playlist_by_id($playlist_id); $songs = $this->Model_playlist->get_songs_by_playlist($playlist_id); @@ -191,27 +185,43 @@ class Playlists extends CI_Controller { if (!$this->session->userdata('user_id')) { redirect('utilisateur/connexion'); } - - $nbrMusiqueAleatoire = 10; - $songs = $this->Model_music->get_random_songs($nbrMusiqueAleatoire); // 10 chansons aléatoires - $new_playlist = array( - 'name' => 'Playlist aléatoire du ' . date('Y-m-d H:i:s'), - 'description' => 'Une playlist avec ' . $nbrMusiqueAleatoire . ' musiques aléatoires.', - 'utilisateur_id' => $this->session->userdata('user_id') - ); - - $this->Model_playlist->create_playlist($new_playlist); - $new_playlist_id = $this->db->insert_id(); - - foreach ($songs as $song) { - $data = array( - 'playlist_id' => $new_playlist_id, - 'song_id' => $song->id + + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $genre = $this->input->post('genre'); + $artist = $this->input->post('artist'); + $nbrMusiqueAleatoire = $this->input->post('nbrMusiqueAleatoire'); + $nbrMusiqueAleatoire = is_numeric($nbrMusiqueAleatoire) ? intval($nbrMusiqueAleatoire) : 10; + $songs = $this->Model_music->get_random_songs($nbrMusiqueAleatoire, $genre, $artist); + + $new_playlist = array( + 'name' => 'Playlist aléatoire du ' . date('Y-m-d H:i:s'), + 'description' => 'Une playlist avec ' . $nbrMusiqueAleatoire . ' musiques aléatoires.', + 'utilisateur_id' => $this->session->userdata('user_id') ); - $this->Model_playlist->add_song_to_playlist($data); - } + + $this->Model_playlist->create_playlist($new_playlist); + $new_playlist_id = $this->db->insert_id(); + + foreach ($songs as $song) { + $data = array( + 'playlist_id' => $new_playlist_id, + 'song_id' => $song->id + ); + $this->Model_playlist->add_song_to_playlist($data); + } + + redirect('playlists'); + } else { + $data['genres'] = $this->Model_music->get_all_genres(); + $data['artists'] = $this->Model_music->get_all_artists(); + $data['title']="Générer une playlist - Onzeur"; + $data['css']="assets/css/generate_playlist"; - redirect('playlists'); + + $this->load->view('layout/header_dark',$data); + $this->load->view('generate_playlist', $data); + $this->load->view('layout/footer_dark'); + } } public function add_album($playlist_id) { diff --git a/CodeIgniter-3.1.13/application/models/Model_music.php b/CodeIgniter-3.1.13/application/models/Model_music.php index 0cbc42e..fe2da6b 100644 --- a/CodeIgniter-3.1.13/application/models/Model_music.php +++ b/CodeIgniter-3.1.13/application/models/Model_music.php @@ -157,10 +157,6 @@ class Model_music extends CI_Model { public function get_all_albums() { return $this->db->get('album')->result(); } - - public function get_all_artists() { - return $this->db->get('artist')->result(); - } public function get_songs_by_album($album_id) { $this->db->select('song.*'); @@ -170,13 +166,42 @@ class Model_music extends CI_Model { return $this->db->get()->result(); } - public function get_random_songs($limit) { + public function get_random_songs($limit, $genre = null, $artist = null) { + $this->db->select('song.id, song.name'); + $this->db->from('song'); + $this->db->join('track', 'track.songId = song.id'); + $this->db->join('album', 'album.id = track.albumId'); + $this->db->join('artist', 'artist.id = album.artistId', 'left'); + $this->db->join('genre', 'genre.id = album.genreId', 'left'); + + if ($genre) { + $this->db->where('genre.name', $genre); + } + if ($artist) { + $this->db->where('artist.name', $artist); + } + $this->db->order_by('RAND()'); $this->db->limit($limit); - return $this->db->get('song')->result(); + + $query = $this->db->get(); + return $query->result(); + } + + public function get_all_genres() { + $this->db->distinct(); + $this->db->select('name'); + $query = $this->db->get('genre'); + return array_column($query->result_array(), 'name'); + } + + public function get_all_artists() { + $this->db->distinct(); + $this->db->select('name'); + $query = $this->db->get('artist'); + return array_column($query->result_array(), 'name'); } - public function get_total_musiques(){ $query = $this->db->query("SELECT COUNT(*) as total_musiques FROM song"); $result = $query->row(); diff --git a/CodeIgniter-3.1.13/application/views/generate_playlist.php b/CodeIgniter-3.1.13/application/views/generate_playlist.php new file mode 100644 index 0000000..a5a961c --- /dev/null +++ b/CodeIgniter-3.1.13/application/views/generate_playlist.php @@ -0,0 +1,25 @@ +
+