Ajout des playlists

This commit is contained in:
stiti 2024-05-25 15:44:57 +02:00
parent ce612f2b55
commit dfb10221de
13 changed files with 740 additions and 1 deletions

View File

@ -0,0 +1,180 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Playlists extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Model_playlist');
$this->load->model('Model_music');
$this->load->helper('url');
$this->load->helper('html');
$this->load->library('session'); // Charger la bibliothèque de sessions
}
public function index(){
// Récupérer l'ID de l'utilisateur connecté depuis la session
$user_id = $this->session->userdata('user_id');
// Vérifier si l'utilisateur est connecté
if ($user_id) {
$data['playlists'] = $this->Model_playlist->get_user_playlists($user_id);
$this->load->view('layout/header_dark');
$this->load->view('playlists_list', $data);
$this->load->view('layout/footer_dark');
} else {
redirect('utilisateur/connexion');
}
}
public function create() {
if ($this->input->post()) {
// Récupérer l'ID de l'utilisateur depuis la session
$user_id = $this->session->userdata('user_id');
// Vérifier si l'ID de l'utilisateur est présent
if($user_id) {
// Si l'ID de l'utilisateur est disponible, créer la playlist avec cet ID
$data = array(
'name' => $this->input->post('name'),
'description' => $this->input->post('description'),
'utilisateur_id' => $user_id // Ajoutez l'ID de l'utilisateur
);
$this->Model_playlist->create_playlist($data);
redirect('playlists');
} else {
// Gérer le cas où l'ID de l'utilisateur est manquant
// Peut-être rediriger vers la page de connexion ou afficher un message d'erreur
redirect('utilisateur/connexion');
}
} else {
$this->load->view('layout/header_dark');
$this->load->view('playlist_create');
$this->load->view('layout/footer_dark');
}
}
public function update($playlist_id) {
if ($this->input->post()) {
$data = array(
'name' => $this->input->post('name'),
'description' => $this->input->post('description')
);
$this->Model_playlist->update_playlist($playlist_id, $data);
redirect('playlists/view/' . $playlist_id);
} else {
// Gérer le cas où les données POST ne sont pas disponibles
redirect('playlists/view/' . $playlist_id);
}
}
public function add_song($playlist_id) {
if ($this->input->post()) {
$data = array(
'playlist_id' => $playlist_id,
'song_id' => $this->input->post('song_id')
);
$this->Model_playlist->add_song_to_playlist($data);
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;
$this->load->view('layout/header_dark');
$this->load->view('playlist_add_song', $data);
$this->load->view('layout/footer_dark');
}
}
public function delete($playlist_id) {
$this->Model_playlist->delete_playlist($playlist_id);
redirect('playlists');
}
public function remove_song($playlist_id, $song_id) {
$this->Model_playlist->remove_song_from_playlist($playlist_id, $song_id);
redirect('playlists/view/' . $playlist_id);
}
public function duplicate($playlist_id) {
$playlist = $this->Model_playlist->get_playlist_by_id($playlist_id);
$songs = $this->Model_playlist->get_songs_by_playlist($playlist_id);
$new_playlist = array(
'name' => $playlist->name . ' (Duplicate)',
'description' => $playlist->description,
'utilisateur_id' => $playlist->utilisateur_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
);
$this->Model_playlist->add_song_to_playlist($data);
}
redirect('playlists');
}
public function generate_random() {
$songs = $this->Model_music->get_random_songs(10); // 10 chansons aléatoires
$new_playlist = array(
'name' => 'Random Playlist ' . date('Y-m-d H:i:s'),
'description' => 'A randomly generated playlist',
'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
);
$this->Model_playlist->add_song_to_playlist($data);
}
redirect('playlists');
}
public function add_album($playlist_id) {
if ($this->input->post()) {
// Récupérer l'ID de l'album à partir du formulaire
$album_id = $this->input->post('album_id');
// Récupérer toutes les chansons de l'album
$songs = $this->Model_music->get_songs_by_album($album_id);
// Ajouter chaque chanson à la playlist
foreach ($songs as $song) {
$data = array(
'playlist_id' => $playlist_id,
'song_id' => $song->id
);
$this->Model_playlist->add_song_to_playlist($data);
}
redirect('playlists/view/' . $playlist_id);
} else {
// Récupérer tous les albums disponibles
$data['albums'] = $this->Model_music->get_all_albums();
$data['playlist_id'] = $playlist_id;
$this->load->view('layout/header_dark');
$this->load->view('playlist_add_album', $data);
$this->load->view('layout/footer_dark');
}
}
}
?>

View File

@ -128,6 +128,30 @@ class Model_music extends CI_Model {
}
public function get_all_songs() {
return $this->db->get('song')->result();
}
public function get_all_albums() {
return $this->db->get('album')->result();
}
public function get_songs_by_album($album_id) {
$this->db->select('song.*');
$this->db->from('track');
$this->db->join('song', 'track.songid = song.id');
$this->db->where('track.albumid', $album_id);
return $this->db->get()->result();
}
public function get_random_songs($limit) {
$this->db->order_by('RAND()');
$this->db->limit($limit);
return $this->db->get('song')->result();
}
public function get_total_musiques(){
$query = $this->db->query("SELECT COUNT(*) as total_musiques FROM song");
$result = $query->row();

View File

@ -0,0 +1,71 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_playlist extends CI_Model {
public function __construct() {
parent::__construct();
}
public function create_playlist($data) {
// Récupérer l'ID de l'utilisateur à partir de la session
$user_id = $this->session->userdata('user_id');
if ($user_id !== null) {
$data['utilisateur_id'] = $user_id;
return $this->db->insert('playlist', $data);
} else {
return false;
}
}
// Récupérer une playlist par ID
public function get_playlist_by_id($playlist_id) {
return $this->db->get_where('playlist', array('id' => $playlist_id))->row();
}
// Récupérer toutes les playlists d'un utilisateur spécifique
public function get_user_playlists($user_id) {
$this->db->where('utilisateur_id', $user_id);
return $this->db->get('playlist')->result();
}
// Mettre à jour une playlist
public function update_playlist($playlist_id, $data) {
$this->db->where('id', $playlist_id);
return $this->db->update('playlist', $data);
}
// Supprimer une playlist
public function delete_playlist($playlist_id) {
$this->db->where('id', $playlist_id);
return $this->db->delete('playlist');
}
// Ajouter une chanson à une playlist
public function add_song_to_playlist($data) {
return $this->db->insert('playlist_song', $data);
}
// Supprimer une chanson d'une playlist
public function remove_song_from_playlist($playlist_id, $song_id) {
$this->db->where('playlist_id', $playlist_id);
$this->db->where('song_id', $song_id);
return $this->db->delete('playlist_song');
}
// Récupérer les chansons d'une playlist
public function get_songs_by_playlist($playlist_id) {
$this->db->select('song.*');
$this->db->from('playlist_song');
$this->db->join('song', 'song.id = playlist_song.song_id');
$this->db->where('playlist_song.playlist_id', $playlist_id);
return $this->db->get()->result();
}
public function add_album_to_playlist($data) {
return $this->db->insert('playlist_album', $data);
}
}
?>

View File

@ -19,7 +19,7 @@
<a href="<?php echo site_url('albums'); ?>" class="btn-albums">Albums</a>
<a href="<?php echo site_url('artiste/list_artists'); ?>" class="btn-artistes">Artistes</a>
<a href="<?php echo site_url('musiques'); ?>" class="btn-musiques">Musiques</a>
<a href="#PlaylistBIENTOT" class="btn-playlist">Mes Playlists</a>
<a href="<?php echo site_url('playlists'); ?>" class="btn-playlist">Mes Playlists</a>
<a href="<?php echo site_url('utilisateur/dashboard'); ?>" class="btn-MonCompte">Mon compte</a>
<a href="<?php echo site_url('utilisateur/deconnexion'); ?>" class="btn-deconnexion">Déconnexion</a>
</div>

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Ajouter un Album à la Playlist</title>
<link rel="stylesheet" href="<?php echo base_url('assets/css/style.css'); ?>">
</head>
<body>
<div class="container">
<h1>Ajouter un Album à la Playlist</h1>
<?php echo form_open('playlists/add_album/'.$playlist_id); ?>
<div class="form-group">
<label for="album_id">Sélectionner un Album :</label>
<select name="album_id" class="form-control" required>
<?php foreach ($albums as $album): ?>
<option value="<?php echo $album->id; ?>"><?php echo $album->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Ajouter</button>
<?php echo form_close(); ?>
</div>
</body>
</html>

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Ajouter une Chanson à la Playlist</title>
<link rel="stylesheet" href="<?php echo base_url('assets/css/playlist_add_song'); ?>">
</head>
<body>
<div class="container">
<h1>Ajouter une Chanson à la Playlist</h1>
<?php echo form_open('playlists/add_song/'.$playlist_id); ?>
<div class="form-group">
<label for="song_id">Sélectionner une Chanson :</label>
<select name="song_id" class="form-control" required>
<?php foreach ($songs as $song): ?>
<option value="<?php echo $song->id; ?>"><?php echo $song->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Ajouter</button>
<?php echo form_close(); ?>
</div>
</body>
</html>

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Créer une Nouvelle Playlist</title>
<link rel="stylesheet" href="<?php echo base_url('assets/css/playlist_create'); ?>">
</head>
<body>
<div class="container">
<h1>Créer une Nouvelle Playlist</h1>
<?php echo form_open('playlists/create'); ?>
<div class="form-group">
<label for="name">Nom de la Playlist :</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="form-group">
<label for="description">Description :</label>
<textarea name="description" class="form-control" rows="4" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Créer</button>
<?php echo form_close(); ?>
</div>
</body>
</html>

View File

@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Détails de la Playlist</title>
<link rel="stylesheet" href="<?php echo base_url('assets/css/playlist_view.css'); ?>">
</head>
<body>
<div class="container">
<h1><?php echo htmlspecialchars($playlist->name, ENT_QUOTES, 'UTF-8'); ?></h1>
<form action="<?php echo site_url('playlists/update/' . $playlist->id); ?>" method="post">
<div class="form-group">
<label for="name">Nom de la Playlist:</label>
<input type="text" name="name" value="<?php echo htmlspecialchars($playlist->name, ENT_QUOTES, 'UTF-8'); ?>" class="form-control" required>
</div>
<div class="form-group">
<label for="description">Description de la Playlist:</label>
<textarea name="description" class="form-control" required><?php echo htmlspecialchars($playlist->description, ENT_QUOTES, 'UTF-8'); ?></textarea>
</div>
<button type="submit" class="btn btn-primary">Enregistrer</button>
</form>
<h2>Chansons</h2>
<table class="table">
<thead>
<tr>
<th>Titre</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (!empty($songs)) : ?>
<?php foreach ($songs as $song) : ?>
<tr>
<td><?php echo htmlspecialchars($song->name, ENT_QUOTES, 'UTF-8'); ?></td>
<td>
<a href="<?php echo site_url('playlists/remove_song/' . $playlist->id . '/' . $song->id); ?>" class="btn btn-danger" onclick="return confirm('Êtes-vous sûr de vouloir supprimer cette chanson de la playlist ?');">Supprimer</a>
</td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="2">Aucune chanson trouvée dans cette playlist.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<a href="<?php echo site_url('playlists/add_song/' . $playlist->id); ?>" class="btn btn-primary">Ajouter une musique</a>
<a href="<?php echo site_url('playlists/add_album/' . $playlist->id); ?>" class="btn btn-primary">Ajouter un album</a>
<a href="<?php echo site_url('playlists'); ?>" class="btn btn-secondary">Retour à la Liste des Playlists</a>
</div>
</body>
</html>

View File

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Liste des Playlists</title>
<link rel="stylesheet" href="<?php echo base_url('assets/css/playlists_list'); ?>">
</head>
<body>
<div class="container">
<h1>Liste des Playlists</h1>
<a href="<?php echo site_url('playlists/create'); ?>" class="btn btn-primary">Créer une Nouvelle Playlist</a>
<a href="<?php echo site_url('playlists/generate_random'); ?>" class="btn btn-primary">Générer une playlist aléatoire</a>
<table class="table">
<thead>
<tr>
<th>Nom</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (!empty($playlists)) : ?>
<?php foreach ($playlists as $playlist) : ?>
<tr>
<td><?php echo htmlspecialchars($playlist->name, ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlspecialchars($playlist->description, ENT_QUOTES, 'UTF-8'); ?></td>
<td>
<a href="<?php echo site_url('playlists/view/' . $playlist->id); ?>" class="btn btn-info">Voir</a>
<a href="<?php echo site_url('playlists/delete/' . $playlist->id); ?>" class="btn btn-danger" onclick="return confirm('Êtes-vous sûr de vouloir supprimer cette playlist ?');">Supprimer</a>
<a href="<?php echo site_url('playlists/duplicate/' . $playlist->id); ?>" class="btn btn-warning">Dupliquer</a>
</td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="3">Aucune playlist trouvée.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</body>
</html>

View File

@ -0,0 +1,60 @@
/* Reset CSS */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
margin-bottom: 20px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
font-weight: bold;
}
.form-control {
width: 100%;
padding: 8px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
button[type="submit"] {
display: block;
width: 100%;
padding: 10px;
border: none;
border-radius: 4px;
background-color: #5c10d6;
color: #fff;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button[type="submit"]:hover {
background-color: #330c72;
}

View File

@ -0,0 +1,48 @@
/* style.css */
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #f9f9f9;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 20px;
}
label {
font-weight: bold;
}
input[type="text"],
textarea {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
button[type="submit"] {
display: block;
width: 100%;
padding: 10px;
background-color: #5c10d6;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button[type="submit"]:hover {
background-color: #330c72;
}

View File

@ -0,0 +1,69 @@
/* Reset CSS */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1, h2 {
margin-bottom: 10px;
}
p {
margin-bottom: 20px;
}
.table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
.table th, .table td {
padding: 8px;
border: 1px solid #ddd;
text-align: left;
}
.table th {
background-color: #f2f2f2;
font-weight: bold;
}
.btn {
display: inline-block;
padding: 8px 16px;
border: none;
border-radius: 4px;
background-color: #7700ff;
color: #fff;
text-decoration: none;
cursor: pointer;
transition: background-color 0.3s ease;
}
.btn:hover {
background-color: #3c0c72;
}
.btn-secondary {
background-color: #6c757d;
}
.btn-secondary:hover {
background-color: #545b62;
}

View File

@ -0,0 +1,118 @@
/* Fichier: assets/css/style.css */
/* Réinitialisation de certains styles par défaut */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Styles pour le conteneur principal */
.container {
width: 80%;
margin: 20px auto;
padding: 20px;
background-color: #f9f9f9;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
/* Styles pour le titre principal */
h1 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
/* Styles pour les boutons */
.btn {
display: inline-block;
padding: 10px 20px;
margin: 5px 0;
color: #fff;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s ease;
}
.btn-primary {
background-color: #6c0980;
}
.btn-primary:hover {
background-color: #551063;
}
.btn-info {
background-color: #17a2b8;
}
.btn-info:hover {
background-color: #117a8b;
}
.btn-danger {
background-color: #dc3545;
}
.btn-danger:hover {
background-color: #c82333;
}
.btn-warning {
background-color: #ffc107;
color: #212529;
}
.btn-warning:hover {
background-color: #e0a800;
}
/* Styles pour la table */
.table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.table th, .table td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
.table th {
background-color: #f2f2f2;
color: #333;
}
.table tbody tr:hover {
background-color: #f1f1f1;
}
/* Styles pour les messages d'erreur ou d'information */
.alert {
padding: 20px;
background-color: #f44336; /* Red */
color: white;
margin-bottom: 15px;
}
.alert.success {background-color: #4CAF50;} /* Green */
.alert.info {background-color: #2196F3;} /* Blue */
.alert.warning {background-color: #ff9800;} /* Orange */
.closebtn {
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}
.closebtn:hover {
color: black;
}