#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package AppUser::User::Command::Profile;

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

use lib '/app/tools/appuser/lib';
use AppUser::User::ProfileForm;

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

sub cmd  { return "profile"; }
sub area { return "user"; }

sub _template { return '/app/tools/appuser/templates/user/index.xsl'; }

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 $form = AppUser::User::ProfileForm->new();

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

        # Apply cgi parameters, then save changes if all went well.
        #
        if ( $form->assignCGIParams( 'Form', $self->getParams() ) && $form->validate() ) {
            $form->save();
            $self->addMessageXML( type => "success", code => Common::FormObject::kSuccessFormSubmit );
        } 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}{PageName} = $self->pageName() if ( $self->can('pageName') );
    $self->{xml}{Form} = $form;

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

1;

