%# BEGIN LICENSE BLOCK
%# 
%# Copyright (c) 1996-2002 Jesse Vincent <jesse@bestpractical.com>
%# 
%# (Except where explictly superceded by other copyright notices)
%# 
%# 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.
%# 
%# 
%# Unless otherwise specified, all modifications, corrections or
%# extensions to this work which alter its source code become the
%# property of Best Practical Solutions, LLC when submitted for
%# inclusion in the work.
%# 
%# 
%# END LICENSE BLOCK
<& /RTIR/Search/Elements/ShowResults,
    Query         => $query,
    Rows          => 0,
    Format        => $format,
    DisplayFormat => $dformat,
    AllowSorting  => 0,
    ShowHeader    => 0,
    EmptyLabel    => loc('(no Incidents)'),
&>

% my $wp = RT->Config->Get('WebPath');
% unless ( $action ) {
<b><a href="<% $wp %>/RTIR/Link/ToIncident/?id=<% $id %>">[Link]</a></b>
<b><a href="<% $wp %>/RTIR/Create.html?Child=<% $id %>&Queue=Incidents">[New]</a></b>
% } elsif ( $action ne 'No' ) {
<a href="<% $wp %>/RTIR/Display.html?Action=<% $action %>&id=<% $id %>">[<% loc( $action ) %>]</a>
% }

<%INIT>
my $id = $Ticket->id;

my $action = '';
if ( $Ticket->Owner != $session{'CurrentUser'}->Id ) {
    unless ( $Ticket->CurrentUserHasRight('OwnTicket') ) {
        $action = 'No';
    }
    elsif ( $Ticket->Owner == $RT::Nobody->Id ) {
        $action = 'Take'
            if $Ticket->CurrentUserHasRight('ModifyTicket')
            || $Ticket->CurrentUserHasRight('TakeTicket');
    } else {
        $action = 'Steal'
            if $Ticket->CurrentUserHasRight('ModifyTicket')
            || $Ticket->CurrentUserHasRight('StealTicket');
    }
}

my $config = RT->Config->Get('RTIR_IncidentChildren')
    ->{ RT::IR::TicketType( Ticket => $Ticket ) };

my $has_incidents = RT::IR->Incidents( $Ticket )->Count;

my $show_unlink = 1;
if ( $config->{'Required'} && $has_incidents <= 1 ) {
    $show_unlink = 0;
}

my $query = RT::IR->RelevantIncidentsQuery( $Ticket );

my $format = RT->Config->Get('RTIRSearchResultFormats')->{'ListIncidents'};
my $dformat = $format;
if ( $show_unlink ) {
    $dformat .= ", '<a href=\"__WebPath__/RTIR/Display.html?id=$id&DeleteLink--MemberOf-__id__=1\">". loc('Unlink') ."</a>/TITLE:'";
}
</%INIT>

<%ARGS>
$Ticket => undef
</%ARGS>
