#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Admin::Command::DeleteContributor;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::Admin::Command::ListContributors;
use RSApache::Message;
use BookPub::Catalog::Contributor::WithBookCount;
use BookPub::DB::Item::Contributor;

use BookPub::Command;
use base 'BookPub::Command';

sub cmd      { return 'delete_contributor'; }
sub area     { return 'admin'; }
sub pageName { return 'Delete Contributor'; }

#use constant kTemplate => "/app/tools/bookpub/templates/admin/edit_contributor.xsl";

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

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

    my $contributorID = $self->getParam('ContributorID');
    my $contributor = BookPub::Catalog::Contributor::WithBookCount->new( contributorID => $contributorID );
    die "ERROR - Cannot delete contributors with non-0 book count" if $contributor->BookCount() > 0;

    my $dbItem = BookPub::DB::Item::Contributor->Lookup( contributor_id => $contributorID );
    $dbItem->delete();

    # We want to change views, so we want to redirect to the
    # Show command now.
    my $successMessage = RSApache::Message->new(
        type => "success",
        code => Common::FormObject::kSuccessDelete,
    );
    my %showParams = ( msg => $successMessage->toString(), );
    my $command = BookPub::Admin::Command::ListContributors->new(%showParams);
    return RSApache::Response::Redirect->new( $command->getRedirect() );
}

1;
