%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2026 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 }}}
<& /Elements/ListActions, actions => \@actions &>

<form action="Scrips.html" method="post">
<input type="hidden" class="hidden" name="id" value="<% $id %>" />
<input type="hidden" class="hidden" name="LookupType" value="<% $LookupType %>" />

<h2><&|/l&>Scrips</&></h2>
<div class="admin-hint"><&|/l&>Scrips normally run after each individual change.</&></div>
% my $scrips = $find_scrips->(Stage => 'TransactionCreate');
<& /Elements/CollectionList, %common_applied_args, Collection => $scrips &>
% unless ( $scrips->Count ) {
<p><i><&|/l&>(No scrips)</&></i></p>
% }

<h2><&|/l&>Batch scrips</&></h2>
<div class="admin-hint"><&|/l&>Batch scrips run after a set of related changes.</&></div>
% $scrips = $find_scrips->(Stage => 'TransactionBatch');
<& /Elements/CollectionList, %common_applied_args, Collection => $scrips &>
% unless ( $scrips->Count ) {
<p><i><&|/l&>(No scrips)</&></i></p>
% }


<& /Elements/Submit,
    Name => 'RemoveScrips',
    Caption => loc("Un-apply selected scrips"),
    Label => loc("Update"),
    FullWidth => 1
    &>

<h2><&|/l&>Not applied scrips</&></h2>
% $scrips = $find_scrips->(Added => 0);
<& /Elements/CollectionList,
    Rows => $Rows,
    Page => 1,
    %ARGS,
    Collection => $scrips,
    Format     => $Format,
    DisplayFormat => "__CheckBox.{AddScrip}__, $Format",
    AllowSorting => 1,
    PassArguments => [ qw(Format Rows Page Order OrderBy id) ],
&>
% unless ( $scrips->Count ) {
<p><i><&|/l&>(No scrips)</&></i></p>
% }

<& SelectStageForAdded &>

<& /Elements/Submit,
    Name => 'AddScrips',
    Caption => loc("Apply selected scrips"),
    Label => loc("Update"),
    FullWidth => 1
&>

</form>

<%init>
my (@actions);

if ( $id ) {
    my $class = RT::Scrip->RecordClassFromLookupType( $LookupType );
    my $object = $class->new($session{'CurrentUser'});
    $object->Load( $id );
    Abort( loc( "Couldn't load [_1] #[_2]", $class, $id ) ) unless $object->id;
}
$id ||= 0;

my $find_scrips = sub {
    my %args = (Added => 1, @_);
    my $scrips = RT::Scrips->new($session{'CurrentUser'});
    $scrips->LimitToLookupType( $LookupType );
    $scrips->LimitByStage( $args{'Stage'} )
        if $args{'Stage'};
    my $method = $args{'Added'}? 'LimitToAdded' : 'LimitToNotAdded';
    $scrips->$method(0, $id);
    $scrips->ApplySortOrder if $args{'Added'};
    $scrips->FindAllRows;
    return $scrips;
};

$Format ||= RT->Config->Get('AdminSearchResultFormat')->{'Scrips'};
my $Rows = RT->Config->Get('AdminSearchResultRows')->{'Scrips'} || 50;
my $DisplayFormat = $Format;
if ( $id ) {
    $DisplayFormat = "__RemoveCheckBox__, $DisplayFormat";
} else {
    $DisplayFormat = "__CheckBox.{RemoveScrip}__, $DisplayFormat";
}
$DisplayFormat .= ", __Move.{$id}__";

my %common_applied_args = (
    %ARGS,
    Format => $Format,
    DisplayFormat => $DisplayFormat,
    Rows => 0,
    Page => 1,
    AllowSorting => 0,
    PassArguments => [ qw(Format id) ],
);

if ( $RemoveScrips ) {
    foreach my $sid ( @RemoveScrip ) {
        my $scrip = RT::Scrip->new( $session{'CurrentUser'} );
        $scrip->Load( $sid );
        next unless $scrip->id;

        my ($status, $msg) = $scrip->RemoveFromObject( $id );
        push @actions, $msg;
    }
}

if ( $AddScrips ) {
    foreach my $sid ( @AddScrip ) {
        my $scrip = RT::Scrip->new( $session{'CurrentUser'} );
        $scrip->Load( $sid );
        next unless $scrip->id;

        my ($status, $msg) = $scrip->AddToObject( $id, Stage => $Stage );
        push @actions, $msg;
    }
}

if ( $MoveScripUp ) {
    my $scrip = RT::ObjectScrip->new( $session{'CurrentUser'} );
    $scrip->LoadByCols( Scrip => $MoveScripUp, ObjectId => $id );
    if ( $scrip->id ) {
        my ($status, $msg) = $scrip->MoveUp;
        push @actions, $msg;
    }
}

if ( $MoveScripDown ) {
    my $scrip = RT::ObjectScrip->new( $session{'CurrentUser'} );
    $scrip->LoadByCols( Scrip => $MoveScripDown, ObjectId => $id );
    if ( $scrip->id ) {
        my ($status, $msg) = $scrip->MoveDown;
        push @actions, $msg;
    }
}

</%init>

<%ARGS>
$id => undef
$LookupType => 'RT::Queue-RT::Ticket'

$title => undef
$Format => undef

@RemoveScrip => ()
$RemoveScrips => undef

@AddScrip => ()
$AddScrips => undef
$Stage     => 'TransactionCreate'

$MoveScripUp => undef
$MoveScripDown => undef

</%ARGS>
