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

use lib '/app/tools/common/lib';

use lib '/app/tools/rps/lib';
use RPS::Command;
use RPS::AlphabeticalPagination;
use RPS::Publisher::US::Publisher;
use base 'RPS::Command';

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

sub cmd  { return 'list'; }


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

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

    my %getArgs;

    # Get the pagination parameters
    #
    my $page = $self->getParam("page");
    $page = 'A' unless defined $page;
    if ('ALL' ne $page)
    {
        $getArgs{alpha} = $page;
    }

    # Could be direct, admin, agent (or just not there)
    #
    my $filter = $self->getParam('filter');
    if ('direct' eq $filter)
    {
        $getArgs{directOnly} = 1;
    }
    elsif ('admin' eq $filter)
    {
        $getArgs{adminOnly} = 1;
    }
    elsif ('agent' eq $filter)
    {
        $getArgs{agentOnly} = 1;
    }
    elsif ('hasAgent' eq $filter)
    {
        $getArgs{withAgent} = 1;
    }
    elsif ('hasAdmin' eq $filter)
    {
        $getArgs{withAdmin} = 1;
    }



	my $searchCollection = $self->_publisherDB()->Get(%getArgs);

    my $pagination = RPS::AlphabeticalPagination->new(currentPage => $page);

	while ($searchCollection->hasNext())
	{
		my $publisherObj = $searchCollection->next();
        push @{$self->{xml}{Publisher}}, $self->_publisherObj()->new(_dbItem => $publisherObj, loadSubs => 0);
	}

	$self->{xml}{Pagination} = $pagination if $pagination;
	
	
    # Save area as preference for this command
    #    
    Common::Preference::SetPersistentCookie( 'list_publisher_area' => $self->area());
    Common::Preference::StoreCookies();	
	

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



1;
