modifications

This commit is contained in:
stiti 2024-06-06 11:16:03 +02:00
parent 2aba540016
commit fc16b575ed
5 changed files with 133 additions and 34 deletions

View File

@ -33,8 +33,7 @@ class Playlists extends CI_Controller {
} else { } else {
redirect('utilisateur/connexion'); redirect('utilisateur/connexion');
} }
} }
public function verify_playlist_ownership($playlist_id) { public function verify_playlist_ownership($playlist_id) {
// Vérifier si l'utilisateur est connecté // Vérifier si l'utilisateur est connecté
@ -120,7 +119,6 @@ class Playlists extends CI_Controller {
public function add_song($playlist_id) { public function add_song($playlist_id) {
$this->verify_playlist_ownership($playlist_id); $this->verify_playlist_ownership($playlist_id);
if ($this->input->post()) { if ($this->input->post()) {
$data = array( $data = array(
'playlist_id' => $playlist_id, 'playlist_id' => $playlist_id,
@ -134,10 +132,8 @@ class Playlists extends CI_Controller {
} }
redirect('playlists/view/' . $playlist_id); redirect('playlists/view/' . $playlist_id);
} else { } else {
// Récupérer toutes les musiques disponibles
$data['songs'] = $this->Model_music->get_all_songs(); $data['songs'] = $this->Model_music->get_all_songs();
$data['playlist_id'] = $playlist_id; $data['playlist_id'] = $playlist_id;
$data['title']="Ajouter une Chanson à la Playlist"; $data['title']="Ajouter une Chanson à la Playlist";
$data['css']="assets/css/playlist_add_song"; $data['css']="assets/css/playlist_add_song";
@ -149,7 +145,6 @@ class Playlists extends CI_Controller {
public function delete($playlist_id) { public function delete($playlist_id) {
$this->verify_playlist_ownership($playlist_id); $this->verify_playlist_ownership($playlist_id);
$this->Model_playlist->delete_playlist($playlist_id); $this->Model_playlist->delete_playlist($playlist_id);
redirect('playlists'); redirect('playlists');
} }
@ -163,7 +158,6 @@ class Playlists extends CI_Controller {
public function duplicate($playlist_id) { public function duplicate($playlist_id) {
$this->verify_playlist_ownership($playlist_id); $this->verify_playlist_ownership($playlist_id);
$playlist = $this->Model_playlist->get_playlist_by_id($playlist_id); $playlist = $this->Model_playlist->get_playlist_by_id($playlist_id);
$songs = $this->Model_playlist->get_songs_by_playlist($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')) { if (!$this->session->userdata('user_id')) {
redirect('utilisateur/connexion'); redirect('utilisateur/connexion');
} }
$nbrMusiqueAleatoire = 10; if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$songs = $this->Model_music->get_random_songs($nbrMusiqueAleatoire); // 10 chansons aléatoires $genre = $this->input->post('genre');
$new_playlist = array( $artist = $this->input->post('artist');
'name' => 'Playlist aléatoire du ' . date('Y-m-d H:i:s'), $nbrMusiqueAleatoire = $this->input->post('nbrMusiqueAleatoire');
'description' => 'Une playlist avec ' . $nbrMusiqueAleatoire . ' musiques aléatoires.', $nbrMusiqueAleatoire = is_numeric($nbrMusiqueAleatoire) ? intval($nbrMusiqueAleatoire) : 10;
'utilisateur_id' => $this->session->userdata('user_id') $songs = $this->Model_music->get_random_songs($nbrMusiqueAleatoire, $genre, $artist);
);
$new_playlist = array(
$this->Model_playlist->create_playlist($new_playlist); 'name' => 'Playlist aléatoire du ' . date('Y-m-d H:i:s'),
$new_playlist_id = $this->db->insert_id(); 'description' => 'Une playlist avec ' . $nbrMusiqueAleatoire . ' musiques aléatoires.',
'utilisateur_id' => $this->session->userdata('user_id')
foreach ($songs as $song) {
$data = array(
'playlist_id' => $new_playlist_id,
'song_id' => $song->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) { public function add_album($playlist_id) {

View File

@ -157,10 +157,6 @@ class Model_music extends CI_Model {
public function get_all_albums() { public function get_all_albums() {
return $this->db->get('album')->result(); 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) { public function get_songs_by_album($album_id) {
$this->db->select('song.*'); $this->db->select('song.*');
@ -170,13 +166,42 @@ class Model_music extends CI_Model {
return $this->db->get()->result(); 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->order_by('RAND()');
$this->db->limit($limit); $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(){ public function get_total_musiques(){
$query = $this->db->query("SELECT COUNT(*) as total_musiques FROM song"); $query = $this->db->query("SELECT COUNT(*) as total_musiques FROM song");
$result = $query->row(); $result = $query->row();

View File

@ -0,0 +1,25 @@
<body>
<h1>Générer une playlist</h1>
<form action="<?php echo site_url('playlists/generate_random'); ?>" method="post" class="formulaire">
<label for="nbrMusiqueAleatoire">Nombre de musiques :</label>
<input type="number" name="nbrMusiqueAleatoire" id="nbrMusiqueAleatoire" value="5" min="1" max="50"><br><br>
<label for="genre">Genre :</label>
<select name="genre" id="genre">
<option value="">Tous les genres</option>
<?php foreach ($genres as $genre): ?>
<option value="<?php echo $genre; ?>"><?php echo $genre; ?></option>
<?php endforeach; ?>
</select><br><br>
<label for="artist">Artiste :</label>
<select name="artist" id="artist">
<option value="">Tous les artistes</option>
<?php foreach ($artists as $artist): ?>
<option value="<?php echo $artist; ?>"><?php echo $artist; ?></option>
<?php endforeach; ?>
</select><br><br>
<input type="submit" value="Générer la playlist">
</form>
</body>

View File

@ -0,0 +1,40 @@
.formulaire {
background-color: #fff;
border-radius: 5px;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 500px;
margin: 0 auto;
}
h1 {
color: #333;
text-align: center;
}
label {
font-weight: bold;
}
input[type="number"],
select {
width: calc(100% - 10px);
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
input[type="submit"] {
background-color: #5a0993;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
input[type="submit"]:hover {
background-color: #2d0648;
}

View File

@ -1,4 +1,3 @@
/* Styles généraux */
body { body {
font-family: Arial, sans-serif; font-family: Arial, sans-serif;
margin: 0; margin: 0;