[Rt-commit] rt branch 5.0/add-full-scrip-copy-feature created. rt-5.0.4-227-gd3444473de

BPS Git Server git at git.bestpractical.com
Wed Sep 20 20:56:19 UTC 2023


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/add-full-scrip-copy-feature has been created
        at  d3444473ded1da0361b6481b2f5b6d26c50b6ca7 (commit)

- Log -----------------------------------------------------------------
commit d3444473ded1da0361b6481b2f5b6d26c50b6ca7
Author: Ronaldo Richieri <ronaldo at bestpractical.com>
Date:   Wed Sep 20 17:55:54 2023 -0300

    Add ability to copy a scrip to create new
    
    Previously, to copy the parameters from an existing scrip to a new one,
    you have to manually recreate each field.
    
    This commit adds a "Copy" link to the Modify page for a scrip.  By
    clicking the link, RT will clone the scrip and disable the new version.
    It will also copy the applied objects and stages.
    
    This also updates the Stage method of Scrip, allowing it
    to receive queue_id named argument to get the Stage of
    the scrip informing the queue directly and not only
    the ticket.

diff --git a/lib/RT/Scrip.pm b/lib/RT/Scrip.pm
index 0e7bc60ada..ce62a71bcd 100644
--- a/lib/RT/Scrip.pm
+++ b/lib/RT/Scrip.pm
@@ -439,18 +439,20 @@ sub TemplateObj {
 
 =head2 Stage
 
-Takes TicketObj named argument and returns scrip's stage when
-added to ticket's queue.
+Takes TicketObj or QueueId named arguments and returns scrip's stage when
+added to ticket's queue or to the specified queue.
 
 =cut
 
 sub Stage {
     my $self = shift;
-    my %args = ( TicketObj => undef, @_ );
+    my %args = ( TicketObj => undef, QueueId => undef, @_ );
+
+    my $queue_id = defined($args{'QueueId'}) ?
+        $args{'QueueId'} : $args{'TicketObj'}->Queue;
 
-    my $queue = $args{'TicketObj'}->Queue;
     my $rec = RT::ObjectScrip->new( $self->CurrentUser );
-    $rec->LoadByCols( Scrip => $self->id, ObjectId => $queue );
+    $rec->LoadByCols( Scrip => $self->id, ObjectId => $queue_id );
     return $rec->Stage if $rec->id;
 
     $rec->LoadByCols( Scrip => $self->id, ObjectId => 0 );
diff --git a/share/html/Admin/Scrips/Modify.html b/share/html/Admin/Scrips/Modify.html
index de2a9c6d7b..92c4bd5ed0 100644
--- a/share/html/Admin/Scrips/Modify.html
+++ b/share/html/Admin/Scrips/Modify.html
@@ -93,6 +93,11 @@
 
 </&>
 
+  <div class="form-row">
+    <div class="col-12">
+      <& /Elements/Submit, Label => loc('Copy Scrip'), Name => 'Copy' &>
+    </div>
+  </div>
   <div class="form-row">
     <div class="col-12">
       <& /Elements/Submit, Label => loc('Save Changes'), Name => 'Update', Reset => 1 &>
@@ -101,6 +106,11 @@
 
 % if ($session{CurrentUser}->HasRight(Object => $RT::System, Right => 'ExecuteCode')) {
 <& Elements/EditCustomCode, %ARGS, Scrip => $scrip &>
+  <div class="form-row">
+    <div class="col-12">
+      <& /Elements/Submit, Label => loc('Copy Scrip'), Name => 'Copy' &>
+    </div>
+  </div>
   <div class="form-row">
     <div class="col-12">
       <& /Elements/Submit, Label => loc('Save Changes'), Name => 'Update', Reset => 1 &>
@@ -113,6 +123,7 @@
 $id     => undef
 $Update => undef
 $From   => undef
+$Copy => undef
 </%ARGS>
 <%INIT>
 my $scrip = RT::Scrip->new( $session{'CurrentUser'} );
@@ -146,6 +157,73 @@ if ( $Update ) {
         },
     );
 }
+# Scrip copy feature duplicates the scrip to a "Copy of scrip_name",
+# created in a disabled state.
+# We prefer to duplicate rather than just carrie over values to the Create
+# scrip page, because we can then copy all the objects that are applied to
+# the original scrip.
+elsif ($Copy) {
+    my @results;
+    my %NewScripAttributes;
+    for my $item (qw/ScripCondition ScripAction Template /) {
+        $NewScripAttributes{$item} = $scrip->$item;
+    }
+
+    $NewScripAttributes{'Description'}
+        = loc( 'Copy of [_1]', $scrip->Description );
+
+    # Make sure we don't have a scrip with the same name
+    my ($s_ok, $s_msg) = RT::Scrip->new( $session{'CurrentUser'} )
+        ->LoadByCols( Description => $NewScripAttributes{'Description'} );
+
+    if ($s_ok) {
+        push @results, loc( 'Scrip with name [_1] already exists', $NewScripAttributes{'Description'} );
+        MaybeRedirectForResults(
+            Actions   => \@results,
+            Arguments => {
+                id   => $scrip->id,
+                From => $From,
+            },
+        );
+    }
+
+    # Make sure scrip is disabled by default
+    $NewScripAttributes{'Disabled'} = 1;
+
+    my $new_scrip = RT::Scrip->new( $session{'CurrentUser'} );
+    my ($ok, $msg) = $new_scrip->Create(%NewScripAttributes);
+    push @results, $msg;
+
+    # remove from Global to readd correctly if necessary
+    $new_scrip->RemoveFromObject(0);
+
+    # Check if scrip is global
+    my $global = $scrip->IsGlobal;
+    if ($global) {
+        if ( my $Stage = $scrip->Stage( QueueId => 0 ) ) {
+            $new_scrip->AddToObject( 0, Stage => $Stage );
+        }
+    }
+    else {
+        # add to queues
+        my $added_to = $scrip->AddedTo;
+        while ( my $queue = $added_to->Next ) {
+
+            # check Stage
+            if ( my $Stage = $scrip->Stage( QueueId => $queue->id ) ) {
+                $new_scrip->AddToObject( $queue->id, Stage => $Stage );
+            }
+        }
+    }
+    MaybeRedirectForResults(
+        Actions   => \@results,
+        Arguments => {
+            id   => $new_scrip->id,
+            From => $From,
+        },
+        Force => 1,
+    );
+}
 
 my $EnabledChecked = qq[checked="checked"];
 $EnabledChecked = '' if $disabled;

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list