SAE_2.02/CodeIgniter-3.1.13/application/controllers/Playlists.php

303 lines
12 KiB
PHP
Raw Normal View History

2024-05-25 15:44:57 +02:00
<?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');
}
}
2024-05-26 15:15:35 +02:00
public function verify_playlist_ownership($playlist_id) {
// Vérifier si l'utilisateur est connecté
if (!$this->session->userdata('user_id')) {
redirect('utilisateur/connexion');
}
// Récupérer l'ID de l'utilisateur connecté
$user_id = $this->session->userdata('user_id');
// Vérifier si l'utilisateur est le propriétaire de la playlist
$playlist = $this->Model_playlist->get_playlist_by_id($playlist_id);
if (!$playlist || $playlist->utilisateur_id !== $user_id) {
// Rediriger vers une page d'erreur ou une page appropriée
redirect('utilisateur/non_autorisee');
}
}
2024-05-25 15:44:57 +02:00
public function create() {
2024-05-26 15:15:35 +02:00
if (!$this->session->userdata('user_id')) {
redirect('utilisateur/connexion');
}
2024-05-25 15:44:57 +02:00
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) {
2024-05-26 15:15:35 +02:00
// Vérifier si l'utilisateur est connecté
$this->verify_playlist_ownership($playlist_id);
// Récupérer l'ID de l'utilisateur connecté
$user_id = $this->session->userdata('user_id');
// Vérifier si l'utilisateur est le propriétaire de la playlist
$playlist = $this->Model_playlist->get_playlist_by_id($playlist_id);
if (!$playlist || $playlist->utilisateur_id !== $user_id) {
// Rediriger vers une page d'erreur ou une page appropriée
redirect('erreur/page_non_autorisee');
}
// Si les vérifications sont réussies, procéder à la mise à jour de la playlist
2024-05-25 15:44:57 +02:00
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);
}
}
2024-05-26 15:15:35 +02:00
2024-05-25 15:44:57 +02:00
public function add_song($playlist_id) {
2024-05-26 15:15:35 +02:00
$this->verify_playlist_ownership($playlist_id);
2024-05-25 15:44:57 +02:00
if ($this->input->post()) {
$data = array(
'playlist_id' => $playlist_id,
'song_id' => $this->input->post('song_id')
);
2024-05-25 19:13:36 +02:00
$result = $this->Model_playlist->add_song_to_playlist($data);
if ($result) {
$this->session->set_flashdata('success', 'La chanson a été ajoutée à la playlist.');
} else {
$this->session->set_flashdata('error', 'La chanson existe déjà dans la playlist.');
}
2024-05-25 15:44:57 +02:00
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');
}
2024-05-25 19:13:36 +02:00
}
2024-05-25 15:44:57 +02:00
public function delete($playlist_id) {
2024-05-26 15:15:35 +02:00
$this->verify_playlist_ownership($playlist_id);
2024-05-25 15:44:57 +02:00
$this->Model_playlist->delete_playlist($playlist_id);
redirect('playlists');
}
public function remove_song($playlist_id, $song_id) {
2024-05-26 15:15:35 +02:00
$this->verify_playlist_ownership($playlist_id);
2024-05-25 15:44:57 +02:00
$this->Model_playlist->remove_song_from_playlist($playlist_id, $song_id);
redirect('playlists/view/' . $playlist_id);
}
public function duplicate($playlist_id) {
2024-05-26 15:15:35 +02:00
$this->verify_playlist_ownership($playlist_id);
2024-05-25 15:44:57 +02:00
$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() {
2024-05-26 15:15:35 +02:00
if (!$this->session->userdata('user_id')) {
redirect('utilisateur/connexion');
}
2024-05-25 16:18:50 +02:00
$nbrMusiqueAleatoire = 10;
$songs = $this->Model_music->get_random_songs($nbrMusiqueAleatoire); // 10 chansons aléatoires
2024-05-25 15:44:57 +02:00
$new_playlist = array(
2024-05-25 16:18:50 +02:00
'name' => 'Playlist aléatoire du ' . date('Y-m-d H:i:s'),
'description' => 'Une playlist avec ' . $nbrMusiqueAleatoire . ' musiques aléatoires.',
2024-05-25 15:44:57 +02:00
'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) {
2024-05-26 15:15:35 +02:00
$this->verify_playlist_ownership($playlist_id);
2024-05-25 15:44:57 +02:00
if ($this->input->post()) {
$album_id = $this->input->post('album_id');
2024-05-25 22:51:53 +02:00
$songs = $this->Model_music->get_songs_by_album($album_id);
$all_songs_exist = true; // Variable pour vérifier si toutes les chansons existent déjà dans la playlist
foreach ($songs as $song) {
if (!$this->Model_playlist->song_exists_in_playlist($playlist_id, $song->id)) {
$all_songs_exist = false;
break;
}
}
2024-05-25 15:44:57 +02:00
2024-05-25 22:51:53 +02:00
if ($all_songs_exist) {
$this->session->set_flashdata('error', 'Toutes les chansons de cet album existent déjà dans la playlist.');
} else {
2024-05-25 19:13:36 +02:00
foreach ($songs as $song) {
2024-05-25 22:51:53 +02:00
if (!$this->Model_playlist->song_exists_in_playlist($playlist_id, $song->id)) {
$data = array(
'playlist_id' => $playlist_id,
'song_id' => $song->id
);
$this->Model_playlist->add_song_to_playlist($data);
}
2024-05-25 19:13:36 +02:00
}
2024-05-25 22:51:53 +02:00
$this->session->set_flashdata('success', 'Album ajouté avec succès à la playlist, les chansons manquantes ont été ajoutées.');
2024-05-25 15:44:57 +02:00
}
2024-05-25 19:13:36 +02:00
2024-05-25 15:44:57 +02:00
redirect('playlists/view/' . $playlist_id);
} else {
$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');
}
}
2024-05-25 15:58:09 +02:00
public function view($playlist_id) {
2024-05-26 15:15:35 +02:00
$this->verify_playlist_ownership($playlist_id);
2024-05-25 15:58:09 +02:00
// Charger les détails de la playlist spécifique en fonction de son ID
$data['playlist'] = $this->Model_playlist->get_playlist_by_id($playlist_id);
// Charger les chansons de la playlist spécifique
$data['songs'] = $this->Model_playlist->get_songs_by_playlist($playlist_id);
// Charger la vue pour afficher les détails de la playlist
$this->load->view('layout/header_dark');
$this->load->view('playlist_view', $data);
$this->load->view('layout/footer_dark');
}
2024-05-25 19:13:36 +02:00
public function add_artist($playlist_id) {
2024-05-26 15:15:35 +02:00
$this->verify_playlist_ownership($playlist_id);
2024-05-25 19:13:36 +02:00
if ($this->input->post()) {
// Récupérer l'ID de l'artiste à partir du formulaire
$artist_id = $this->input->post('artist_id');
2024-05-25 22:51:53 +02:00
$songs = $this->Model_music->get_songs_by_artist($artist_id);
$all_songs_exist = true; // Variable pour vérifier si toutes les chansons existent déjà dans la playlist
2024-05-25 19:13:36 +02:00
2024-05-25 22:51:53 +02:00
// Vérifier si toutes les chansons de l'artiste existent déjà dans la playlist
foreach ($songs as $song) {
if (!$this->Model_playlist->song_exists_in_playlist($playlist_id, $song->id)) {
$all_songs_exist = false;
break;
}
}
if ($all_songs_exist) {
$this->session->set_flashdata('error', 'Toutes les chansons de cet artiste existent déjà dans la playlist.');
} else {
2024-05-25 19:13:36 +02:00
foreach ($songs as $song) {
2024-05-25 22:51:53 +02:00
if (!$this->Model_playlist->song_exists_in_playlist($playlist_id, $song->id)) {
$data = array(
'playlist_id' => $playlist_id,
'song_id' => $song->id
);
$this->Model_playlist->add_song_to_playlist($data);
}
2024-05-25 19:13:36 +02:00
}
2024-05-26 15:15:35 +02:00
$this->session->set_flashdata('success', 'Les chansons de l\'artiste ont été ajoutées avec succès à la playlist.');
2024-05-25 19:13:36 +02:00
}
2024-05-25 15:58:09 +02:00
2024-05-25 19:13:36 +02:00
redirect('playlists/view/' . $playlist_id);
} else {
// Récupérer tous les artistes disponibles
$data['artists'] = $this->Model_music->get_all_artists();
$data['playlist_id'] = $playlist_id;
$this->load->view('layout/header_dark');
$this->load->view('playlist_add_artist', $data);
$this->load->view('layout/footer_dark');
}
2024-05-25 22:51:53 +02:00
}
2024-05-25 15:44:57 +02:00
}
?>