diff --git a/app/Controller/SongsController.php b/app/Controller/SongsController.php index 1f6b6e8..bcf02f5 100755 --- a/app/Controller/SongsController.php +++ b/app/Controller/SongsController.php @@ -249,11 +249,16 @@ public function sync() { $this->viewClass = 'Json'; $this->SortComponent = $this->Components->load('Sort'); - $songs = $this->Song->find("all", array('fields' => array('id', 'album', 'artist', 'band', 'cover', 'title', 'disc', 'track_number', 'playtime'), 'order' => 'title')); + $songs = $this->Song->find("all", array( + 'fields' => array('id', 'album', 'artist', 'band', 'cover', 'title', 'disc', 'track_number', 'playtime'), + 'contain' => 'PlaylistMembership.playlist_id', + 'order' => 'title') + ); $songs = $this->SortComponent->sortByBand($songs); foreach ($songs as $k => &$song) { $song['Song']['url'] = $this->request->base . '/songs/download/' . $song['Song']['id']; $song['Song']['cover'] = $this->request->base.'/'.IMAGES_URL.(empty($song['Song']['cover']) ? "no-cover.png" : THUMBNAILS_DIR.'/'.$song['Song']['cover']); + $song['Song']['playlists'] = array_map(function ($e) { return intval($e['playlist_id']); }, $song['PlaylistMembership']); } $songs = Hash::extract($songs, '{n}.Song'); diff --git a/app/View/Playlists/index.ctp b/app/View/Playlists/index.ctp index 4910f16..cc089cf 100644 --- a/app/View/Playlists/index.ctp +++ b/app/View/Playlists/index.ctp @@ -88,6 +88,7 @@ @@ -179,4 +180,4 @@ - \ No newline at end of file + diff --git a/app/View/Songs/album.ctp b/app/View/Songs/album.ctp index ec238a5..5211746 100644 --- a/app/View/Songs/album.ctp +++ b/app/View/Songs/album.ctp @@ -12,6 +12,7 @@
  • +
  • diff --git a/app/webroot/js/SongsManager.js b/app/webroot/js/SongsManager.js index 8f3e467..da9ca40 100644 --- a/app/webroot/js/SongsManager.js +++ b/app/webroot/js/SongsManager.js @@ -105,6 +105,18 @@ function SongsManager(baseurl, version) { return playlist; }; + this.getPlaylistSongs = function(playlist) { + var playlistSongs = []; + for(var i = 0; i < songs.length; i++) { + for(var j = 0; j < songs[i].playlists.length; j++) { + if(songs[i].playlists[j] == playlist) { + playlistSongs.push(songs[i]); + } + } + } + return playlistSongs; + }; + this.getBandSongs = function(band) { var bands = []; for(var i = 0; i < songs.length; i++) { @@ -159,4 +171,4 @@ function SongsManager(baseurl, version) { this.setPlaylist = function(songs) { playlist = songs; }; -} \ No newline at end of file +} diff --git a/app/webroot/js/player-nav.js b/app/webroot/js/player-nav.js index f16ed41..8dab7c1 100644 --- a/app/webroot/js/player-nav.js +++ b/app/webroot/js/player-nav.js @@ -31,6 +31,8 @@ var playAlbum = '.action-play-album'; var playTitleNext = '.action-play-next'; var playBandNext = '.action-artist-play-next'; var playAlbumNext = '.action-album-play-next'; +var downloadAlbum = '.action-download-album'; +var downloadPlaylist = '.action-download-playlist'; var playPlaylistNext = '.action-playlist-play-next'; var playTitleAfter = '.action-add-to-up-next'; var playBandAfter = '.action-add-artist-to-up-next'; @@ -237,6 +239,21 @@ function init() { player.addAll(songsManager.getAlbumSongs(band, album)); }); + $('#content').on('click', downloadAlbum, function(e) { + e.preventDefault(); + var band = $(this).parents('[data-band]').attr('data-band'); + var album = $(this).parents('[data-album]').attr('data-album'); + var songs = songsManager.getAlbumSongs(band, album); + downloadAll(songs.map(song => baseurl + song.url)); + }); + + $('#content').on('click', downloadPlaylist, function(e) { + e.preventDefault(); + var playlist = $(this).parents('[data-playlist]').attr('data-playlist'); + var songs = songsManager.getPlaylistSongs(playlist); + downloadAll(songs.map(song => baseurl + song.url)); + }); + $('#content').on('click', shuffleAlbum, function(e) { e.preventDefault(); var band = $(this).parents('[data-band]').attr('data-band'); @@ -525,6 +542,28 @@ function toggleQueueList() { } } +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +async function downloadAll(urls) { + var link = document.createElement('a'); + + link.setAttribute('download', null); + link.style.display = 'none'; + link.classList.add("no-ajax"); + + document.body.appendChild(link); + + for (var i = 0; i < urls.length; i++) { + link.setAttribute('href', urls[i]); + link.click(); + await sleep(500); + } + + document.body.removeChild(link); +} + var test = []; for (var i = 0; i < 100; i++) { test.push({title:"element : " + (i+1)});