<%INIT>
my %documents;

#A default implementation here loops through all transactions and pulls out all their attachments.
# We end up doing an end-run around that to get a bit more performance

# We force the cache of ticket transactions to get populated up front. otherwise, the 
# code that looks at attachments will look at each one in turn.
my $attachments = RT::Attachments->new( $session{'CurrentUser'} );

$attachments->Columns( qw( Id Filename ContentType Headers Subject Parent ContentEncoding ContentType TransactionId Created));

my $transactions = $attachments->NewAlias('Transactions');
$attachments->Join( ALIAS1 => 'main',
		    FIELD1 => 'TransactionId',
		    ALIAS2 => $transactions,
		    FIELD2 => 'id' );
    
my $tickets = $attachments->NewAlias('Tickets');

$attachments->Join( ALIAS1 => $transactions,
		    FIELD1 => 'Ticket',
		    ALIAS2 => $tickets,
		    FIELD2 => 'id' );

if ($Tickets) {
    while ($Ticket = $Tickets->Next) {
	$attachments->Limit( ALIAS => $tickets,
			     FIELD => 'EffectiveId',
			     VALUE => $Ticket->id() );
    }
} else {
    $attachments->Limit( ALIAS => $tickets,
			 FIELD => 'EffectiveId',
			 VALUE => $Ticket->id() );
}


return ($attachments);
</%INIT>
<%ARGS>
$Ticket => undef
$Tickets => undef
</%ARGS>

