<& /Elements/Header, Title => loc("Reported tickets") &>
<& /Elements/Tabs &>

<& /Elements/ListActions, actions => \@results &>

<p>
<% loc('Tickets reported as spam by others without rights to delete tickets.') %>
<% loc('Confirm by clicking "S".') %>
<% loc('If you have rights to delete tickets then they will be deleted.') %>
</p>

<& /Elements/CollectionList, 
    Rows         => 100,
    OrderBy      => 'id',
    Order        => 'DESC',
    %ARGS,
    Class        => 'RT::Tickets',
    Query        => $Query,
    Format       => $Format,
    AllowSorting => 1,
    ShowEmpty    => 1,
&>


<%ARGS>
$Format => undef
</%ARGS>
<%INIT>
my $Query = 'HasAttribute = "SpamReports"';
my @results;

$Format ||= qq{
    '<b><a href="__WebPath__/Ticket/Display.html?id=__id__">__id__</a></b>/TITLE:#',
    '<b><a href="__WebPath__/Ticket/Display.html?id=__id__">__Subject__</a></b>/TITLE:Subject',
    Requestors,
    QueueName,
    ReportSpam
};

if ( $session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser') ) {
    $Format .=
        ', ' . qq{'<a href="__WebPath__/Tools/Spam/Reported.html?NotSpamForAllUsers=__id__">}
      . loc( 'Not Spam For All Users' ) . q{</a>/TITLE:Action'};

    if ( $ARGS{NotSpamForAllUsers} ) {
        my $ticket = RT::Ticket->new($session{CurrentUser});
        $ticket->Load($ARGS{NotSpamForAllUsers});
        if ( $ticket->id ) {
            if ( $ticket->FirstAttribute('SpamReports') ) {
                my ( $status, $msg ) = $ticket->DeleteAttribute( 'SpamReports' );
                if ( $status ) {
                    push @results, loc( "Ticket #[_1] is unmarked as spam for all users", $ARGS{NotSpamForAllUsers} );
                }
                else {
                    push @results, loc( "Failed to unmark ticket #[_1]: [_2]", $ARGS{NotSpamForAllUsers}, $msg );
                }
            }
            else {
                push @results, loc("Ticket #[_1] is not marked as spam", $ARGS{NotSpamForAllUsers} );
            }
        }
        else {
            push @results, loc( "Failed to load ticket #[_1]", $ARGS{NotSpamForAllUsers} );
        }

        MaybeRedirectForResults(
            Actions   => \@results,
            Path      => "/Tools/Spam/Reported.html",
            Anchor    => $ARGS{'Anchor'},
        );
    }
}
</%INIT>
