#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Main::Command::Redirect;
use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use BookPub::Command;
use BookPub::Dashboard::Command::Show;
use BookPub::Analytics::Command::Main;
use base 'BookPub::Command';

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

use lib '/app/tools/appuser/lib';
use AppUser::DB::Item::AccessLevel;

sub cmd      { return 'redirect'; }
sub area     { return 'main'; }
sub pageName { return ''; }

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

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

    # The point of this command is to redirect users to the correct landing
    # page when they go to the default page.
    # Because it can vary by user level, and the landing page for some
    # users is forbidden for others, it's easiest to just have a
    # command like this that manages this for us.
    #

    # Imprint View and Publisher View users don't have access to the dashboard.
    #
    my $accessLevel = $self->_getUser()->AccessLevel();

    if (   AppUser::DB::Item::AccessLevel::kImprintView == $accessLevel
        || AppUser::DB::Item::AccessLevel::kPublisherView == $accessLevel ) {
        my $reportCommand = BookPub::Analytics::Command::Main->new();
        $response = RSApache::Response::Redirect->new( $reportCommand->getRedirect() );
    } else {

        # Everybody else goes to the dashboard.
        #
        my $reportCommand = BookPub::Dashboard::Command::Show->new();
        Common::Log::Print( "REDIRECT= " . $reportCommand->getRedirect() );
        $response = RSApache::Response::Redirect->new( $reportCommand->getRedirect() );
    }

    return $response;
}

1;
