Audio-DB

 view release on metacpan or  search on metacpan

DB/Util/Playlists.pm  view on Meta::CPAN

  return $this;
}


sub process_requests {
  my $self    = shift;
  my $coderef = url_param('playlist');

  # Some coderefs are actually NOT url_params, things
  # like adding songs to playlists, etc
  $coderef = param('todo') if param('todo');
  $coderef =~ s/\s/_/g;
  $self->$coderef;

  # Print out the appropriate title...
  # Better to do it here so that I can jump in later
  # and print forms on the fly without the intervening table
  # IN an ideal world, that is how it would work

  # Print out the various playlist options
  $self->display_options(1) unless ($coderef eq 'display_options');
  print end_div;
}

sub display_options {
  my ($self,$suppress) = @_;
  $self->print_navigation() unless ($suppress);
  print start_div({-class=>'configoptions'});
  print h4($self->build_nav_link(-class=>'playlist',-action=>'browse_user_playlists',
				 -text=>'Browse Your Playlists'));
  print h4($self->build_nav_link(-class=>'playlist',-action=>'browse_all_playlists',
				 -text=>'Browse All Playlists'));
  print h4($self->build_nav_link(-class=>'playlist',-action=>'create_new_playlist',
				 -text=>'Create New Playlist'));
  print h4($self->build_nav_link(-class=>'playlist',-action=>'delete_all_playlists',
				 -text=>'Delete All Of Your Playlists'));
}


sub create_new_playlist {
  my ($self,$suppress) = @_;
  my $user_id = $self->{user_id};
  if (param('submit')) {
    $self->print_navigation('Create A New Playlist (Results)');
    $self->add_entry(-table       => 'playlists',
		     -success_msg => 'The playlist ' . span({-class=>'name'},param('playlist'))
		     . ' has been successfully added.',
		     -user_id=>$user_id);
  } else {
    unless ($suppress) {
      $self->print_navigation($self->build_nav_link(-class=>'playlist',-action=>'create_new_playlist',
						    -text=>'Create New Playlist'));
    }
    print start_div({-class=>'actioncontent'});
    print "Use this form to add a new playlist.";
    Delete('playlist');
    print startform(),
      start_table(),
	TR(td('Playlist Name'),td(textfield({-name=>'playlist'}))),
	  TR(td('Description'),td(textfield({-name=>'description'}))),
	    TR(td('Publically viewable?'),td(popup_menu({-name=>'is_shared',-values=>[qw/yes no/]}))),
	      TR(td({-colspan=>2,-align=>'right'},
		    submit(-name=>'submit',-label=>'Add New Playlist'))),
		      end_table;
    # Fetch all the user playlists
    print end_div;
  }
}





# Currently, this just displays a flat listing of the playlist?
sub display_playlist {
  my ($self,$id) = @_;
  $id ||= url_param('id');
  my $tracks = $self->fetch_playlists(-type=>'filled',-id=>$id);
  # I need some blanket description of the playlist here

  # Also need all of my typical checkboxes...
  my @cols = qw/Select stream song artist album genre/;
  print div({-class=>'actionbyline'},"Browsing Playlist");
  print start_div({-class=>'actioncontent'});
  print  start_table(),TR(td(\@cols));
  
  my $count;
  my $total;
  foreach my $song (@$tracks) {
    # Build up some links for each item.
    # can't use the normal mechanism since the individual songs are not blessed.
    my $song_link   = $self->build_url('song',$song);
    my $album_link  = $self->build_url('album',$song);
    my $artist_link = $self->build_url('artist',$song);
    my $genre_link  = $self->build_url('genre',$song);
    
    $total++;
    $count = ($count eq 'one' ) ? 'two' : 'one';
    print start_TR({-class=>"shade$count"});
    #    $self->print_checkboxes($song,$total);
    my $track;
    
    # I should only print the artist column if this is a compilation CD
    print
      td($song_link),
	td($artist_link),
	  td($album_link),
	    td($song->{duration}),
	      td($song->{bitrate}),
		td($genre_link),
		  end_TR;
  }
# line 500
print end_table,end_div;
}



#######################################################
#  BROWSING PLAYLISTS
#######################################################

DB/Util/Playlists.pm  view on Meta::CPAN

    foreach (@to_add) {
      my $id = ($_ =~ /.*_(.*)/) ? $1 : '';
      $sth->execute($id);
      while (my ($song_id) = $sth->fetchrow_array) {
	my $result = $self->insert_songs($playlist_id,$song_id);
        $count += $result;
      }
    }
  }
  
  # Now display a message and the number of tracks add ed to the playlist
  print "$count tracks were added to: ";
  $self->display_playlist($playlist_id);
}


sub insert_songs {
  my ($self,$playlist_id,$song_id) = @_;
  my $dbh = $self->{dbh};
  $dbh->do("insert into playlist_songs (playlist_id,song_id) VALUES ($playlist_id" 
			. ',' . $dbh->quote($song_id) . ')');
  my $result = abs $dbh->rows;
  return $result;
}




# MAYBE EDITS SHOULD JUST BE SUPERSEDED WITH A SINGLE CHECBOX ON EACH
# listing?
# THIS NEEDS TO BE FLESHED OUT...
# I will be editing a single playlist at a time...
### IT"S NOT DONE YET

# THIS WILL BE A LINK IN
sub edit_playlist {
  my $self = shift;
  $self->print_navigation($self->build_nav_link(-class=>'playlist',
						-action=>'edit_playlist',
						-text=>'Edit Playlists'));
  
  my $id     = url_param('id');
  my $tracks = $self->fetch_playlists(-type=>'filled',-id=>$id);

  my @cols = qw/select Song Artist Album Genre/;
  print div({-class=>'actionbyline'},
	    "Use this form to edit the tracks of the current playlists");
  print start_div({-class=>'actioncontent'});
  print startform(),
    start_table();
  print TR(td(\@cols));
  
  # First I need to print a header for the playlist, letting user edit the name
  # and shared properties.
#    print
#      hidden(-name=>'playlist_id',-value=>$h->{playlist_id});
#    print
#      TR(td(textfield({-name=>'playlist',-value=>$h->{playlist},-size=>15})),
##	 td(textfield({-name=>'description',-value=>$h->{description},-size=>15})),
#	 td(textfield({-name=>'email',-value=>$h->{created},-size=>20})),
#	 td(popup_menu({-name=>'is_shared',-values=>[qw/yes no/]})));
#
#  $sth->execute();
#  while (my $h = $sth->fetchrow_hashref) {
#
#  }
  print end_table;
  print submit(-name=>'submit',-label=>'Update Users');
  print end_div;
}


sub delete_playlist {

}


sub delete_all_playlists {

}



sub playlist_popup {
  my $self = shift;
  # Flow
  # 1. Create the popup window
  # 2. Fetch user playlists
  # 3. Display form
 
  # Processing...
  # Should songs be added to new playlist?
  #       Creat playlist, then add songs.
  # Should songs be added to existing playlist?
  # Popupmen presents: view playlist or return...

  # Example code for creating a javapopup window
  # <script language="javascript" type="text/javascript">
  # function OpenComments (c) {
  #    window.open('<$MTCGIPath$>mt-comments.cgi?' +
  #                    'entry_id=' + c,
  #                    'comments',
  #                    'width=480,height=480,scrollbars=yes,status=yes');
  #}
  #</script>
  #
  #}
}


#######################################################
#  SUPPORT: Fetching, streaming, linking, navigating
#######################################################

# Fetch all the current playlists associated with
# this user and return 
# This could be generalized to get lists of all playlists
# from all users
# Or perhaps that should just be a seperate sub

# Could also be modified to return a filled list
# This might actually be usefull, because then I can build the page
# from that

# Should this be a factory churning out Playlistlist objects?

sub fetch_playlists {
  my ($self,@p) = @_;
  my ($type,$id) = rearrange([qw/TYPE ID/],@p);

  # Cases:
  # 1. fetch playlists for a specific user, 
  # 2. Browse all playlists
  # 3. fetch an individual playlist
  
  # UI could present a popupmenu for each user
  # Or alternatively, could expand them all
  my $user_id = $self->{user_id};
  my $dbh = $self->dbh;

  # This is the flat query that simply returns all the playlists
  # but does not actually list the song ids
  my %queries = ( by_user => qq{select * from playlists where user_id=?},
		  all      => qq{select * from playlists,users where
				 playlists.user_id=users.user_id
				 ORDER BY last_name},
		  expanded => qq{select *,count(song_id) from playlists,playlist_songs 
				 where
				 playlists.user_id=? 
				 and playlists.playlist_id=playlist_songs.playlist_id
				 GROUP BY playlist_id},
		  filled => qq{select * from playlists,playlist_songs,songs,artists,albums,genres
			       where
			       playlists.playlist_id=? 
			       and playlists.playlist_id=playlist_songs.playlist_id
			       and playlist_songs.song_id=songs.song_id
			       and songs.artist_id=artists.artist_id
			       and songs.genre_id=genres.genre_id
			       and songs.album_id=albums.album_id});
  my $results = [];
  my $sth = $dbh->prepare($queries{$type});
  if ($type eq 'by_user') {
    $sth->execute($user_id);
  } elsif ($type eq 'filled') {
    # Argh! What should this be and where should it come from?
    $sth->execute($id);
  } elsif ($type eq 'all') {
    $sth->execute();
  } else {
    $sth->execute();
  }
  
  my $c;
  while (my $h = $sth->fetchrow_hashref) {
    push (@$results,$h);
    $c++;
  }
  return '' if $c < 1;
  return $results;
}


# This will be a generic link in, and not for streaming
sub link_playlist {
  my ($self,$playlist,$id) = @_;
  my $url = url();
  return a({-href=>$url . '?playlist=' . 'display_playlist&id=' . $id },$playlist);
}

sub stream_link {

}


# THIS CONTROLS NAVIGATIONAL FORMATTING
sub print_navigation { 



( run in 1.055 second using v1.01-cache-2.11-cpan-364913b4093 )