149 lines
4.8 KiB
PHP
149 lines
4.8 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 et sélection aléatoire
|
|
$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);
|
|
}
|
|
|
|
$this->db->order_by('RAND()');
|
|
$this->db->limit($numSongs);
|
|
|
|
$query = $this->db->get();
|
|
$songs = $query->result();
|
|
|
|
foreach ($songs as $song) {
|
|
$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();
|
|
}
|
|
}
|