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

use lib '/app/tools/rps/lib';
use RPS::Track::TrackContract;
use RPS::Track::Command::Show;
use RPS::Catalog::Command::Show;
use RPS::Command;
use RPS::DB::Item::Track;

use lib '/app/tools/common/lib';
use Common::FormObject;
use RSApache::Response::XML;
use RSApache::Response::XSLT;
use RSApache::Response::Redirect;

use base 'RPS::Command';

sub cmd  { return 'attach'; }
sub area { return 'track'; }

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

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

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

    my $trackID          = $self->getParam('TrackID');
    my $artistContractID = $self->getParam('ContractID');

    my $trackContract = RPS::Track::TrackContract->new(
        trackID          => $trackID,
        artistContractID => $artistContractID
    );
    if ( $trackContract && $trackContract->validate() ) {
        $trackContract->save();

        # !!! Not sure what, if any, messages are appropriate here.
        # None, perhaps.
        #
    }

    # Going to need to know the album id so we can redirect properly.
    #
    my $trackData = RPS::DB::Item::Track->Lookup( track_id => $trackID );
    my $albumID = $trackData->album_id;

    # We've moved away from the page, so we'll have to do a redirect to get the
    # page to refresh.   We'll pass in a hint to the command to ask it to select
    # the contracts tab.
    #
    my $command = RPS::Catalog::Command::Show->new(
        AlbumID => $albumID,
        tab     => RPS::Catalog::Command::Show::kTabContracts,
    );

    return RSApache::Response::Redirect->new( $command->getRedirect() );
}

1;
