function tjnz_managePlaylist( list_id ) {
$.ajax({
type : 'POST',
data : {
action : 'tjnz-manage-playlist', // name of function to execute in your php file
list_id : list_id
},
dataType: 'json'
}).done(function( json ) {
if( json.success ) {
var playlistItems = JSON.parse( json.message ); // convert string to JSON and save in var
var playlistItem = null;
var playlistHtml = '';
for( var i = 0; i < playlistItems.length; ++i ) { // loop through the parsed JSON objects, each object represents 1 item from the playlist
playlistItem = playlistItems[i]; // saving the current object as a new variable
playlistHtml += '<li>';
playlistHtml += '<small><a class="playlist-manager-remove-item" href="#" title="Remove \'' + playlistItem.title + '\' from playlist" target="_blank">X</a></small> ';
// adding hidden elements to the HTML so that later when we save the new re-ordered playlist we still have all the required playlist information
playlistHtml += '<span class="hidden jp-page-id-' + playlistItem.id + '">' + playlistItem.id + '</span>';
playlistHtml += '<span class="hidden jp-page-url-' + playlistItem.id + '">' + playlistItem.page + '</span>';
playlistHtml += '<span class="hidden jp-genre-' + playlistItem.id + '">' + playlistItem.genre + '</span>';
playlistHtml += '<span class="hidden jp-mp3-' + playlistItem.id + '">' + playlistItem.mp3 + '</span>';
playlistHtml += '<div class="art"><img src="' + playlistItem.thumb + '" class="artwork jp-artwork-' + playlistItem.id + ' wp-post-image" alt="' + playlistItem.title + '"><span id="poster-url-' + playlistItem.id + '" class="hidden">' + playlistItem.poster + '</span></div>';
playlistHtml += '<div class="meta"><span class="jp-title-' + playlistItem.id + '">' + playlistItem.title + '</span> <small> by </small> <span class="jp-artist-' + playlistItem.id + '">' + playlistItem.artist + '</span></div>';
playlistHtml += '</li>';
}
$('#playlist-manager ul').html( playlistHtml ); // after looping through all JSON objects, set our filled HTML variable as the new HTML for the 'sortable UL' element
$('.sortable').sortable(); // call the sortable jQuery plugin on our UL with class 'sortable' to make its elements, well... sortable! =)
} else {
alert( json.message ); // something went wrong, output the error message generated by the PHP code
}
}).fail(function() {
// the AJAX call failed, try again
}).always(function() {
// you can do something here, like stopping a loading animation or changing your mouse cursor back to normal
});
}
$('body').on('click', '.playlist-manager-remove-item', function(e) {
e.preventDefault();
$(this).parents( '#playlist-manager ul.sortable li' ).slideUp( 300, function() { $(this).remove(); } );
});
$('body').on('click', '.manage-playlist', function(e) {
e.preventDefault();
var list_id = $(this).attr( 'data-pid' );
var list_name = $(this).attr( 'title' );
$('#playlist-manager-title').val( list_name ); // sets the text field in the playlist manager div to the mix's name
tjnz_managePlaylist( list_id ); // runs the function to fetch the playlist data for the given ID and fill the UL element
});
$('body').on('click', '.manage-playlist-cancel', function(e) {
e.preventDefault();
$('#playlist-manager ul').html( '' );
});
$('body').on('click', '.manage-playlist-save', function(e) {
e.preventDefault();
$('#playlist-manager-title').val( $.trim( $('#playlist-manager-title').val() ) ); // strip out spaces in front and back
if( $.trim( $('#playlist-manager-title').val() ) == '' ) {
alert( "Please give your playlist a name." );
return false;
}
var json = new Array();
$('#playlist-manager ul.sortable li').each( function() { // this each loop will scrape the required data from the current LI element and save it into the new items array. At the end, all items' elements are joined together and enclosed in {} brackets, then inserted into the json array.
var items = new Array();
items.push('"id":"' + $(this).find( 'span[class^="hidden jp-page-id-"]' ).text() + '"');
items.push('"title":"' + tjnz_addslashes( $(this).find( 'span[class^="jp-title-"]' ).text() ) + '"');
items.push('"artist":"' + tjnz_addslashes( $(this).find( 'span[class^="jp-artist-"]' ).text() ) + '"');
items.push('"genre":"' + $(this).find( 'span[class^="hidden jp-genre-"]' ).text() + '"');
items.push('"mp3":"' + $(this).find( 'span[class^="hidden jp-mp3-"]' ).text() + '"');
items.push('"poster":"' + $(this).find( 'span[id^="poster-url-"]' ).text() + '"');
items.push('"thumb":"' + $(this).find( 'img[class^="artwork jp-artwork-"]' ).attr( 'src' ) + '"');
items.push('"page":"' + $(this).find( 'span[class^="hidden jp-page-url-"]' ).text() + '"');
items.push('"actiontype":"play"');
json.push( '{' + items.join() + '}' );
});
tjnz_playlistSave( $.trim( $('#playlist-manager-title').val() ), '[' + json + ']' );
// this function I have demonstrated earlier in our conversation
// this will save the playlist as a NEW playlist.
// You may require an "update" function instead to update existing playlists in the database.
// I have this in my site's live code, and depending if the user managing the playlist is
// the original owner, I update or save the playlist.
});
/* Function: Get playlist for Playlist Manager
* Since: 8.2
*/
add_action( 'wp_ajax_tjnz-manage-playlist', 'tjnz_manage_playlist' ); // bind the 'action' data POSTed by AJAX to the following PHP function
add_action( 'wp_ajax_nopriv_tjnz-manage-playlist', 'tjnz_manage_playlist' );
function tjnz_manage_playlist() {
// extract POST data
$list_id = intval( $_POST['list_id'] );
// verify data
if( $list_id <= 1 ) { // could be 0 in your case, but I have reserved playlist ID 1 for a tour playlist that is owned by nobody
die( json_encode(array('success'=>false, 'message'=>"Don't try anything funny, dude...")) );
}
// get playlist json from WordPress database
global $wpdb, $current_user;
get_currentuserinfo();
$playlist_owner = ( $current_user->ID == $user_id ? true : false );
$table_name = $wpdb->prefix . 'tjnz_playlists';
$sql = "SELECT `json` FROM $table_name WHERE `list_id` = %u;";
$json = $wpdb->get_var( $wpdb->prepare( $sql, $list_id ) );
if( $json == null ) { die( json_encode(array('success'=>false, 'message'=>'Durr... we couldn\'t find that playlist.')) ); }
else { die( json_encode(array('success'=>true, 'owner'=>$playlist_owner, 'message'=>$json)) ); }
}