#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Song::Command::List;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::XML;

use lib '/app/tools/rps/lib';
use Common::FormObject;
use RPS::Song::Song;

use RPS::Command;
use base 'RPS::Command';

sub cmd  { return 'list'; }
sub area { return 'song'; }

use constant kTemplate => "/app/tools/rps/templates/song/index.xsl";

sub execute
{
	my ($self) = @_;

	my $response = $self->SUPER::execute();
	return $response if $response;

	my $searchCollection;
	$searchCollection = RPS::DB::Item::Song->GetAll(order => $self->getParam("order"));

	my $page = $self->getParam("page") || 1;
	my $perPage = 50;

	my $count = 0;
	while ($searchCollection->hasNext())
	{
		my $songObj = $searchCollection->next();

		if($count >= ($page-1)*$perPage && $count < $page*$perPage)
		{
			push @{$self->{xml}{Song}}, RPS::Song::Song->new(_dbItem => $songObj, loadSubs => 0);
		}

		$count++;
	}

	# TODO: we probably need to add pagination xml so the template knows which links to display

	$response = RSApache::Response::XSLT->new($self->{xml}, kTemplate);
	return $response;
}



1;
