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

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

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

sub cmd { return "edit"; }



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 $id = $self->getParam("PublisherID");
    my $form = $self->_editForm()->new( publisherID => $id );

    my $openRuns = $openRuns = ( $self->_openRunCount($id) || 0 ) if $id;

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

            # I believe we want to remain on this form after this operation.
            $self->addMessageXML( type => "success", code => Common::FormObject::kSuccessFormMakeDefault );
        } else {
            $form->setError(Common::FormObject::kErrFormMakeDefault);
            $self->addMessageXML( type => "error", code => Common::FormObject::kErrFormMakeDefault );
        }
    } elsif ( $self->getParam('formSubmit') && $openRuns == 0 ) {

        # IsAgency is a checkbox - If that is not checked, the form does
        # not submit it.  We need to hack this form logic to give a virtual
        # parameter value to it so we can change it's value...
        if ( !$self->getParam('Form_Publisher_IsAgency') ) {
            $self->setParam( 'Form_Publisher_IsAgency', 0 );
        }

        # Apply cgi parameters, then save changes if all went well.
        if ( $self->_assignCGIParams($form) && $self->_validateForm($form) ) {
            $form->save();
            $id = $form->getPublisherID();
            RPS::CommandLog::Log( $self, $id );

            # recipients can be added only when statement distribution is set to Online
            if ( $form->getPublisherDistribution() ) {
                $self->_deletePendingStatementRecipients($form);
                $self->_saveStatementRecipients($form);
            }

            # on a 'regular' save, we will refresh back to the Detail page.
            # on a 'save and new', we refresh back to a blank Edit screen (with a success message).

            # We want to change views, so we want to execute the Detail command now.
            my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
            my $command;

            if ( $self->getParam('addAnother') ) {
                $command = $self->_editCommand()->new( msg => $successMessage->toString() );
            } else {
                $command = $self->_showCommand()->new( msg => $successMessage->toString, PublisherID => $id );
            }

            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 );
        }
    }

    $self->{xml}{Form} = $form;
    $self->{xml}{Form}{OpenRuns} = $openRuns;
    $self->{xml}{Form}{Publisher}{CanBeDeleted} = $self->_canBeDeleted($id);

    # Save area as preference for this command
    #
    Common::Preference::SetPersistentCookie( 'edit_publisher_area' => $self->area() );
    Common::Preference::StoreCookies();

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

sub _canBeDeleted {
    my ($self, $id) = @_;
    # should be overwritten
    return 0;
}

sub _assignCGIParams {
    my ($self, $form) = @_;
    # should be overwritten if additional statements recipients are used

    return $form->assignCGIParams( 'Form', $self->getParams() );
}

sub _validateForm {
    my ($self, $form) = @_;
    # should be overwritten if additional statements recipients are used

   return $form->validate();
}

sub _assignCGIParamsRecipients {
    # should be overwritten if additional statements recipients are used
    return 1;
}

sub _saveStatementRecipients {
    # should be overwritten if additional statements recipients are used
    return 1;
}

sub _deletePendingStatementRecipients {
    # should be overwritten if additional statements recipients are used
    return 1;
}

1;

