153 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			153 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 | 
						|
 | 
						|
class Model_playlist extends CI_Model {
 | 
						|
    public function __construct() {
 | 
						|
        $this->load->database();
 | 
						|
    }
 | 
						|
 | 
						|
    public function getPlaylistsByUser($userEmail) {
 | 
						|
        $this->db->select('*');
 | 
						|
        $this->db->from('playlists');
 | 
						|
        $this->db->where('user_email', $userEmail);
 | 
						|
        $query = $this->db->get();
 | 
						|
        return $query->result();
 | 
						|
    }
 | 
						|
 | 
						|
    public function addPlaylist($userEmail, $name) {
 | 
						|
        $data = array(
 | 
						|
            'user_email' => $userEmail,
 | 
						|
            'name' => $name
 | 
						|
        );
 | 
						|
        $this->db->insert('playlists', $data);
 | 
						|
        return $this->db->insert_id();
 | 
						|
    }
 | 
						|
 | 
						|
    public function addItem($playlistId, $itemId, $itemType) {
 | 
						|
        $data = array(
 | 
						|
            'playlist_id' => $playlistId,
 | 
						|
            'item_id' => $itemId,
 | 
						|
            'item_type' => $itemType
 | 
						|
        );
 | 
						|
        return $this->db->insert('playlist_items', $data);
 | 
						|
    }
 | 
						|
 | 
						|
    public function getPlaylistItems($playlistId) {
 | 
						|
        $this->db->select('playlist_items.item_id, song.name as songName, artist.name as artistName, album.name as albumName, playlist_items.playlist_id');
 | 
						|
        $this->db->from('playlist_items');
 | 
						|
        $this->db->join('track', 'track.id = playlist_items.item_id');
 | 
						|
        $this->db->join('song', 'song.id = track.songid');
 | 
						|
        $this->db->join('album', 'album.id = track.albumid');
 | 
						|
        $this->db->join('artist', 'artist.id = album.artistid');
 | 
						|
        $this->db->where('playlist_items.playlist_id', $playlistId);
 | 
						|
 | 
						|
        $result = $this->db->get();
 | 
						|
        return $result->result();
 | 
						|
    }
 | 
						|
 | 
						|
    public function deleteItem($playlistId, $itemId) {
 | 
						|
        $this->db->where('playlist_id', $playlistId);
 | 
						|
        $this->db->where('item_id', $itemId);
 | 
						|
        return $this->db->delete('playlist_items');
 | 
						|
    }
 | 
						|
 | 
						|
    public function deletePlaylist($playlistId) {
 | 
						|
        $this->db->where('playlist_id', $playlistId);
 | 
						|
        $this->db->delete('playlist_items');
 | 
						|
 | 
						|
        $this->db->where('id', $playlistId);
 | 
						|
        return $this->db->delete('playlists');
 | 
						|
    }
 | 
						|
 | 
						|
    public function duplicatePlaylist($playlistId) {
 | 
						|
        $this->db->select('*');
 | 
						|
        $this->db->from('playlists');
 | 
						|
        $this->db->where('id', $playlistId);
 | 
						|
        $query = $this->db->get();
 | 
						|
        $playlist = $query->row();
 | 
						|
 | 
						|
        if ($playlist) {
 | 
						|
            $newPlaylistData = array(
 | 
						|
                'user_email' => $playlist->user_email,
 | 
						|
                'name' => $playlist->name . ' (Copie)'
 | 
						|
            );
 | 
						|
            $this->db->insert('playlists', $newPlaylistData);
 | 
						|
            $newPlaylistId = $this->db->insert_id();
 | 
						|
 | 
						|
            $this->db->select('*');
 | 
						|
            $this->db->from('playlist_items');
 | 
						|
            $this->db->where('playlist_id', $playlistId);
 | 
						|
            $query = $this->db->get();
 | 
						|
            $items = $query->result();
 | 
						|
 | 
						|
            foreach ($items as $item) {
 | 
						|
                $newItemData = array(
 | 
						|
                    'playlist_id' => $newPlaylistId,
 | 
						|
                    'item_id' => $item->item_id,
 | 
						|
                    'item_type' => $item->item_type
 | 
						|
                );
 | 
						|
                $this->db->insert('playlist_items', $newItemData);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function renamePlaylist($playlistId, $newName) {
 | 
						|
        $data = array('name' => $newName);
 | 
						|
        $this->db->where('id', $playlistId);
 | 
						|
        return $this->db->update('playlists', $data);
 | 
						|
    }
 | 
						|
 | 
						|
    public function createRandomPlaylist($userEmail, $name, $numSongs, $genre) {
 | 
						|
        if ($numSongs <= 0) {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
 | 
						|
        // Création de la nouvelle playlist
 | 
						|
        $data = array(
 | 
						|
            'user_email' => $userEmail,
 | 
						|
            'name' => $name
 | 
						|
        );
 | 
						|
        $this->db->insert('playlists', $data);
 | 
						|
        $playlistId = $this->db->insert_id();
 | 
						|
 | 
						|
        // Filtrage des chansons par genre
 | 
						|
        $this->db->select('track.id');
 | 
						|
        $this->db->from('track');
 | 
						|
        $this->db->join('song', 'track.songid = song.id');
 | 
						|
        $this->db->join('album', 'track.albumid = album.id');
 | 
						|
        $this->db->join('genre', 'album.genreid = genre.id');
 | 
						|
 | 
						|
        if ($genre) {
 | 
						|
            $this->db->where('genre.name', $genre);
 | 
						|
        }
 | 
						|
 | 
						|
        $query = $this->db->get();
 | 
						|
        $songs = $query->result();
 | 
						|
 | 
						|
        if ($numSongs > count($songs)) {
 | 
						|
            $numSongs = count($songs);
 | 
						|
        }
 | 
						|
 | 
						|
        // Sélection aléatoire de chansons
 | 
						|
        $randomKeys = array_rand($songs, $numSongs);
 | 
						|
        foreach ($randomKeys as $key) {
 | 
						|
            $song = $songs[$key];
 | 
						|
            $this->addItem($playlistId, $song->id, 'song');
 | 
						|
        }
 | 
						|
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getGenres() {
 | 
						|
        $this->db->select('name');
 | 
						|
        $this->db->from('genre');
 | 
						|
        $query = $this->db->get();
 | 
						|
        return $query->result();
 | 
						|
    }
 | 
						|
 | 
						|
    public function getPlaylistItemCount($playlistId) {
 | 
						|
        $this->db->where('playlist_id', $playlistId);
 | 
						|
        $this->db->from('playlist_items');
 | 
						|
        return $this->db->count_all_results();
 | 
						|
    }
 | 
						|
}
 |