package RPS::Portal::Command::Label;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use RSApache::Response::Download;
use Common::Assert;
use Common::Debug;

use lib '/app/tools/rps/lib';
use RPS::Portal::Command::Main;

use base 'RPS::Command';

sub area { return "portal"; }
sub cmd  { return "label"; }

use constant kTemplate => '/app/tools/rps/templates/portal/main.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 $name  = $self->getParam('name');
    my $email = $self->getParam('email');
    my $message;
    print STDERR "D: Portal::Command::Label - name($name) email($email)\n";
    my $clientInfo = Common::DB::Item::Client->Lookup( client_id => Common::RSApp::GetClientID() );
    if ($name) {
        print STDERR "D: Portal::Command::Label - setting name\n";
        $clientInfo->display_name($name);
        $clientInfo->save();
        $message = RPS::Message->new( type => "success", code => "name_saved" );
    } else {
        print STDERR "D: Portal::Command::Label - setting email\n";
        $clientInfo->email($email);
        $clientInfo->save();
        $message = RPS::Message->new( type => "success", code => "email_saved" );
    }

    my $command = RPS::Portal::Command::Main->new( msg => $message->toString );
    return RSApache::Response::Redirect->new( $command->getRedirect() );
}

1;
