<%init>
# Only show the Public nav to the public user
return unless RT::BugTracker::Public->IsPublicUser;

# Save the About menu, if any (provided by RT::Extension::rt_cpan_org, for
# example)
my $about = Menu->child("about");

# Save the Preferences menu ("Logged in as..") for Bitcard/OpenID users
my $preferences = Menu->child("preferences");

# Clear the decks
RT::Interface::Web::InitializeMenu();

PageWidgets()->child( simple_search => raw_html => $m->scomp(
    '/Elements/SimpleSearch', SendTo => '/Public/Search/Simple.html' ));

PageWidgets()->child( create_ticket => raw_html => $m->scomp(
    '/Elements/CreateTicket', SendTo => '/Public/Bug/Report.html' ));

Menu()->child(
    search_dist => title => loc('Search Distributions'),
    path        => '/Public/',
);

Menu()->child(
    browse_dist => title => loc('Browse Distributions'),
    path        => '/Public/Dist/Browse.html',
);

Menu->child( "about", menu => $about );

if ($session{CurrentUser}->Name eq RT->Config->Get("WebPublicUser")) {
    Menu->child(
        'preferences' => title => loc( 'Welcome [_1]anonymous guest[_2].', '<span class="current-user">', '</span>' ),
        escape_title  => 0,
        sort_order    => 1000,
    );

    # Public user must logout to login
    Menu->child(
        "login",
        title       => loc('Login as another user'),
        path        => '/NoAuth/Logout.html',
        sort_order  => 1001,
    );
} else {
    # Preserve core RT generated "Logged in as" menu for other authenticated
    # users from Bitcard and OpenID which also get the Public view
    Menu->child("preferences", menu => $preferences);
}

my ($queue, $ticket);
my $request_path = $HTML::Mason::Commands::r->path_info;
   $request_path =~ s!^/{2,}!/!;

if ( $request_path =~ m{^/Public/Bug/(?:Display|Update)\.html}
     and my $id = $DECODED_ARGS->{id} )
{
    $ticket = RT::Ticket->new( $session{CurrentUser} );
    $ticket->Load($id);
    $queue = $ticket->QueueObj if $ticket->id;
}
elsif ( $request_path =~ m{^/Public/(?:Bug/Report|Dist/Display)\.html}
        and my $name = ($DECODED_ARGS->{Name} || $DECODED_ARGS->{Queue}) )
{
    $queue = RT::Queue->new( $session{CurrentUser} );
    $queue->Load($name);
}

if ( $queue and $queue->id ) {
    my $escaped = $m->interp->apply_escapes($queue->Name, 'u');
    PageMenu()->child(
        active_bugs => title => loc("Active bugs"),
        path => "/Public/Dist/Display.html?Status=Active;Name=" . $escaped,
    );

    PageMenu()->child( resolved_bugs =>
            title => loc("Resolved bugs"),
            path  => "/Public/Dist/Display.html?Status=Resolved;Name=". $escaped,
    );

    PageMenu()->child( rejected_bugs => 
            title => loc("Rejected bugs"),
            path  => "/Public/Dist/Display.html?Status=Rejected;Name=". $escaped,
    );

    PageMenu()->child( report => 
            title => loc("Report a new bug"),
            path  => '/Public/Bug/Report.html?Queue='. $escaped,
    );

    if ($ticket and $ticket->id
        and $queue->Lifecycle->IsInactive($ticket->Status)
        and $ticket->CurrentUserHasRight("OpenTicket")) {

        PageMenu->child(
            "reopen",
            title       => loc("Re-open this bug"),
            path        => "/Public/Bug/Display.html?Status=open;id=".$ticket->id,
            sort_order  => -2,
        );
        PageMenu->child(
            "ticket-queue-separator",
            raw_html    => "<span style='display: block; padding: 0.75em 1em'>&mdash;</span>",
            sort_order  => -1,
        );
    }
}
</%init>
