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

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::Admin::Command::ListContributors;
use BookPub::Admin::EditContributorForm;
use RSApache::Message;

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

sub cmd      { return 'edit_contributor'; }
sub area     { return 'admin'; }
sub pageName { return 'Edit 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 $form = BookPub::Admin::EditContributorForm->new( contributorID => $contributorID );

    if ( $self->getParam('formSubmit') ) {
        if (   $form->assignCGIParams( 'Form', $self->getParams() )
            && $form->validate() ) {
            $form->save();

            # 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::kSuccessFormSubmit
            );
            my %showParams = ( msg => $successMessage->toString(), );
            my $command = BookPub::Admin::Command::ListContributors->new(%showParams);
            return RSApache::Response::Redirect->new( $command->getRedirect() );
        } else {
            $form->setError(Common::FormObject::kErrFormSubmit);
            $self->addMessageXML(
                type => "error",
                code => Common::FormObject::kErrFormSubmit
            );
        }
    }

    $self->{xml}{Form} = $form;

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

    return $response;
}

1;
