%# BEGIN BPS TAGGED BLOCK {{{
%# 
%# COPYRIGHT:
%# 
%# This software is Copyright (c) 1996-2014 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 $request_path = $HTML::Mason::Commands::r->path_info;

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


if (
    (
           $request_path =~ m{^/(?:Ticket|Search)/}
        && $request_path !~ m{^/Search/Simple\.html}
    )
    || (   $request_path =~ m{^/Search/Simple\.html}
        && $DECODED_ARGS->{'q'} )
  )
{

    my $search = Menu()->child('search')->child('tickets');
    my $args = '';
    my $has_query = '';
    my $current_search = $session{"CurrentSearchHash"} || {};
    my $search_id = $DECODED_ARGS->{'SavedSearchLoad'} || $DECODED_ARGS->{'SavedSearchId'} || $current_search->{'SearchId'} || '';
    my $chart_id = $DECODED_ARGS->{'SavedChartSearchId'} || $current_search->{SavedChartSearchId};

    $has_query = 1 if ( $DECODED_ARGS->{'Query'} or $current_search->{'Query'} );

    my %query_args;
    my %fallback_query_args = (
        SavedSearchId => ( $search_id eq 'new' ) ? undef : $search_id,
        SavedChartSearchId => $chart_id,
        (
            map {
                my $p = $_;
                $p => $DECODED_ARGS->{$p} || $current_search->{$p}
            } qw(Query Format OrderBy Order Page)
        ),
        RowsPerPage => (
            defined $DECODED_ARGS->{'RowsPerPage'}
            ? $DECODED_ARGS->{'RowsPerPage'}
            : $current_search->{'RowsPerPage'}
        ),
    );

    if ($DECODED_ARGS->{QueryString}) {
        $args = '?' . $DECODED_ARGS->{QueryString};
    }
    else {
        my %final_query_args = ();
        # key => callback to avoid unnecessary work

        for my $param (keys %fallback_query_args) {
            $final_query_args{$param} = defined($DECODED_ARGS->{QueryArgs}->{$param})
                                      ? $DECODED_ARGS->{QueryArgs}->{$param}
                                      : $fallback_query_args{$param};
        }

        for my $field (qw(Order OrderBy)) {
            if ( ref( $final_query_args{$field} ) eq 'ARRAY' ) {
                $final_query_args{$field} = join( "|", @{ $final_query_args{$field} } );
            } elsif (not defined $final_query_args{$field}) {
                delete $final_query_args{$field};
            }
            else {
                $final_query_args{$field} ||= '';
            }
        }

        $args = '?' . $query_string->(%final_query_args);
    }

    my $current_search_menu;
    if ( $request_path =~ m{^/Ticket} ) {
        $current_search_menu = $search->child( 'current_search' );
    } else {
        $current_search_menu = PageMenu();
    }
    if ( $has_query ) {
        my $more = $current_search_menu->child( 'more' );

        $more->child(
            'xls' => title => loc('XLS'),
            path => '/Search/Results.xls?' . $args
        );
    }
}

</%INIT>
