#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Product::Command::EditDigital;

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

use lib '/app/tools/rps/lib';
use RPS::Product::EntryFormDigital;
use RPS::Product::Command::ShowDigital;
use RPS::Catalog::Command::Show;

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

use constant kTemplate => '/app/tools/rps/templates/product/index.xsl';

sub cmd  { return "edit_digital"; }
sub area { return "product"; }

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

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    my $productID = $self->getParam("ProductID");
    if ( !$productID ) {
        $productID = $self->getParam("Form_ProductDigital_ProductID");
    }

    my $form = RPS::Product::EntryFormDigital->new( productID => $productID );

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

    if ( $self->getParam('formSubmit') ) {

        # Apply cgi parameters, then save changes if all went well.
        #
        if ( $form->assignCGIParams( 'Form', $self->getParams() ) && $form->validate() ) {
            $form->save();

            RPS::CommandLog::Log( $self, $form->productID() );

            my $command = RPS::Catalog::Command::Show->new(
                AlbumID => $form->albumID(),
                tab     => RPS::Catalog::Command::Show::kTabDistribution,
            );

            my $redirectResponse = RSApache::Response::Redirect->new( $command->getRedirect() );
            return $redirectResponse;
        } else {

            # We stay on the same page, and display our errors.
            #
            $form->setError(Common::FormObject::kErrFormSubmit);
            $self->addMessageXML( type => "error", code => Common::FormObject::kErrFormSubmit );
        }
    }

    my $template = kTemplate;

    # JPK - This is a hack for development purposes...
    #
    if ( $self->getParam('hack') ) {
        $template = '/app/tools/rps/templates/product/index_hack.xsl';
    }

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

1;

