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

use lib '/app/tools/admin/lib';
use Admin::Permission::Command::Show;
use Admin::Command;
use Admin::Permission::AddForm;
use base 'Admin::Command';

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

sub cmd  { return 'add'; }
sub area { return 'permission'; }

use constant kTemplate => '/app/tools/admin/templates/permission/index.xsl';

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

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

    my $form = Admin::Permission::AddForm->new();

    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 = Admin::Permission::Command::Show->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;
