push
This commit is contained in:
		
							
								
								
									
										98
									
								
								application/models/Model_playlist.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								application/models/Model_playlist.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,98 @@
 | 
			
		||||
<?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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user