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

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

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

use Data::Dumper;

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

sub cmd  { return "show"; }
sub area { return "permission"; }

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 $app     = $self->getParam("App");
    my $area    = $self->getParam("Area");
    my $cmd     = $self->getParam("Command");
    my $levelID = $self->getParam("AccessLevel");
    my $sortBy  = $self->getParam("SortBy");

    my $form = Admin::Permission::Form->new(
        app         => $app,
        area        => $area,
        command     => $cmd,
        accessLevel => $levelID,
        sortBy      => $sortBy,
    );

    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.
            #
            $self->addMessageXML( type => "success", code => Common::FormObject::kSuccessFormSubmit );
        } 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;

