<%INIT>

$Query = join ' AND ', map "( $_ )", grep $_, $BaseQuery, $Query;

$BaseURL = RT->Config->Get('WebPath')."/$BaseURL" unless $BaseURL =~ m{^/};
$BaseURL .= ( $BaseURL =~ /\?/ )? '&': '?' unless $BaseURL =~ m{[&?]$};

my $collection = RT::Tickets->new( $session{'CurrentUser'} );
$session{'tickets'} = $collection if $UpdateSession;

$collection->FromSQL( $Query );

my $result = $m->scomp( '/Elements/TicketList',
    %ARGS,
    Collection     => $collection,
    Query          => $Query,
    OrderBy        => $OrderBy,
    Order          => $Order,
    Rows           => $Rows,
    Page           => $Page,
    BaseURL        => $BaseURL,
    ShowNavigation => $ShowNavigation,
    AllowSorting   => $AllowSorting,
);

$session{'CurrentSearchHash'} = {
    Format      => $Format,
    Query       => $Query,
    Page        => $Page,
    Order       => $Order,
    OrderBy     => $OrderBy,
    RowsPerPage => $Rows
};

my $found = $collection->Count;
if ( $found || $ShowEmpty ) {
    $m->out( $result );
}
else {
    $m->out( $m->interp->apply_escapes($EmptyLabel, 'h') )
        if $EmptyLabel;
}
return $found;
</%INIT>
<%ARGS>
$Queue           => ''

$BaseQuery       => ''
$Query           => ''
$Format          => ''
$Rows            => undef
$Page            => 1
$OrderBy         => 'id'
$Order           => 'ASC'

$BaseURL         => "RTIR/Search/Results.html?"

$ShowEmpty       => 0
$EmptyLabel      => ''
$ShowNavigation  => 0
$AllowSorting    => 1
$UpdateSession   => 0
</%ARGS>
