%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2026 Best Practical Solutions, LLC
%#                                          <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
<%INIT>

my $query_string = sub {
    my %args = @_;
    my $u    = URI->new();
    $u->query_form(map { $_ => $args{$_} } sort keys %args);
    return $u->query || '';
};

if ( my $admin = Menu->child('admin') ) {
    if ( my $global = $admin->child('global') ) {
        $global->child(
            'my-rtir' => title => 'RTIR at a glance',
            path      => '/Admin/Global/MyRT.html?'
                . $query_string->(
                    Title                => loc('Customize Global RTIR at a glance'),
                    WidgetTitle          => loc('Set RTIR Homepage'),
                    AttributeName        => 'RTIRDefaultDashboard',
                    AttributeDescription => 'RTIR Default Dashboard',
                ),
            sort_order => 8.1,
        );
    }
}

my $args = $DECODED_ARGS;

my @query_fields = qw(Query Format RowsPerPage Page OrderBy Order ExtraQueryParams);

my $search_arguments = sub {
    my %res = ();
    my $caller_args = $m->caller_args(1);
    if ( $caller_args->{'SearchArgs'} ) {
        @res{ @query_fields } = @{ $caller_args->{'SearchArgs'} }{ @query_fields };
        if ( $res{ExtraQueryParams} ) {
            $res{$_} = $caller_args->{'SearchArgs'}{$_}
              for ref $res{ExtraQueryParams} ? @{ $res{ExtraQueryParams} } : $res{ExtraQueryParams};
        }
    } else {
        @res{ @query_fields } = @{ $args }{ @query_fields };
        if ( $res{ExtraQueryParams} ) {
            $res{$_} = $args->{$_}
              for ref $res{ExtraQueryParams} ? @{ $res{ExtraQueryParams} } : $res{ExtraQueryParams};
        }
    }

    delete $res{$_} foreach grep !defined $res{$_}, keys %res;
    return %res;
};

my $root = Menu->child( rtir => title => loc('RTIR'), path => '/RTIR/', sort_order => '1.5' );

my $search = $root->child( search => title => loc('Search'), path => RT::IR->HREFTo('', IncludeWebPath => 0) );
$search->child( new => title => loc('New Search'), path => '/Search/Build.html?NewQuery=1&ExtraQueryParams=RTIR&RTIR=1' );

$root->child(
    incidents => title => loc('Incidents'),
    path => '/Search/Results.html?ExtraQueryParams=RTIR&RTIR=1&Lifecycle='.RT::IR->lifecycle_incident,
)->child(
    create => title => loc('Create'),
    path => RT::IR->HREFTo('Incident/Create.html?Lifecycle='.RT::IR->lifecycle_incident, IncludeWebPath => 0),
);
$root->child(
    reports => title => loc('Incident Reports'),
    path => '/Search/Results.html?ExtraQueryParams=RTIR&RTIR=1&Lifecycle='. RT::IR->lifecycle_report,
)->child(
    create => title => loc('Create'),
    path => RT::IR->HREFTo('Create.html?Lifecycle='.RT::IR->lifecycle_report, IncludeWebPath => 0),
);
$root->child(
    investigations => title => loc('Investigations'),
    path => '/Search/Results.html?ExtraQueryParams=RTIR&RTIR=1&Lifecycle='.RT::IR->lifecycle_investigation,
)->child(
    launch => title => loc('Launch'),
    path => RT::IR->HREFTo('Create.html?Lifecycle='.RT::IR->lifecycle_investigation, IncludeWebPath => 0),
);
unless ( RT->Config->Get('RTIR_DisableCountermeasures') ) {
    $root->child(
        countermeasures => title => loc('Countermeasures'),
        path => '/Search/Results.html?ExtraQueryParams=RTIR&RTIR=1&Lifecycle='.RT::IR->lifecycle_countermeasure,
    )->child(
        create => title => loc('Create'),
        path => RT::IR->HREFTo('Create.html?Lifecycle='.RT::IR->lifecycle_countermeasure, IncludeWebPath => 0),
    );
}

my $tools = $root->child( tools => title => loc('Tools'), path => RT::IR->HREFTo('Tools/', IncludeWebPath => 0) );
$tools->child( lookup => title => loc('Lookup'), path => RT::IR->HREFTo('Tools/Lookup.html', IncludeWebPath => 0));
$tools->child( reporting => title => loc('Reporting'), path => RT::IR->HREFTo('Reporting/', IncludeWebPath => 0) );
my $scripted_actions = $tools->child( scripted_actions => title => loc('Scripted Action') );
$scripted_actions->child( email => title => loc('By Email address'), path => RT::IR->HREFTo('Tools/ScriptedAction.html', IncludeWebPath => 0) );
$scripted_actions->child( ip => title => loc('By IP address'), path => RT::IR->HREFTo('Tools/ScriptedAction.html?loop=IP', IncludeWebPath => 0) );
my $external_feeds = $tools->child( 'external_feeds', title => loc('External Feeds'), path => RT::IR->HREFTo('Tools/ExternalFeeds.html', IncludeWebPath => 0) );

my $request_path = $HTML::Mason::Commands::r->path_info;
$request_path =~ s!/{2,}!/!g;

my $re_rtir_path = qr'^/RTIR/(?:c/[^/]*/?)?';
my $re_rtir_types = '(?:'. join( '|', map "\Q$_\E", RT::IR->Types ) .')';

if ( $session{'CurrentUser'}->HasRight(Right => 'ModifySelf', Object => $RT::System) ) {
    Menu->child('preferences')->child('settings')->child(
        rtir_home_page => title => loc('RTIR at a glance'),
        path => RT::IR->HREFTo('Prefs/Home.html', IncludeWebPath => 0),
    );
}

</%INIT>
