Skip to content

Commit

Permalink
Abstract ProcessTemplateUpdate to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnavy committed Feb 9, 2024
1 parent 651330c commit a5c9728
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 45 deletions.
38 changes: 38 additions & 0 deletions lib/RT/Interface/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5887,6 +5887,44 @@ sub PreprocessTransactionSearchQuery {
return join ' AND ', @limits;
}


=head2 ProcessTemplateUpdate ( TemplateObj => $Template, ARGSRef => \%ARGS );
Returns an array of results messages.
=cut

sub ProcessTemplateUpdate {
my %args = (
ARGSRef => undef,
TemplateObj => undef,
@_
);

if ( !$args{ARGSRef}{Content} && ( $args{ARGSRef}{Headers} || $args{ARGSRef}{Body} ) ) {

if ( $args{ARGSRef}{Headers} =~ /\S/ ) {
$args{ARGSRef}{Headers} =~ s!^\s+!!;
$args{ARGSRef}{Headers} =~ s!\s+$!!;
$args{ARGSRef}{Content} = join "\n\n", $args{ARGSRef}{Headers}, $args{ARGSRef}{Body} // ();
}
elsif ( $args{ARGSRef}{Body} =~ /\S/ ) {
$args{ARGSRef}{Content} = "\n$args{ARGSRef}{Body}";
}
}

my @attribs = qw( Name Description Queue Type Content );
my @results = UpdateRecordObject(
AttributesRef => \@attribs,
Object => $args{TemplateObj},
ARGSRef => $args{ARGSRef},
);

my ( $ok, $msg ) = $args{TemplateObj}->CompileCheck;
push @results, $msg if !$ok;
return @results;
}

package RT::Interface::Web;
RT::Base->_ImportOverlays();

Expand Down
22 changes: 1 addition & 21 deletions share/html/Admin/Global/Template.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,7 @@
}

if ($TemplateObj->Id()) {
my @attribs = qw( Name Description Queue Type Content );

if ( !$ARGS{Content} && ( $ARGS{Headers} || $ARGS{Body} ) ) {

if ( $ARGS{Headers} =~ /\S/ ) {
$ARGS{Headers} =~ s!^\s+!!;
$ARGS{Headers} =~ s!\s+$!!;
$ARGS{Content} = join "\n\n", $ARGS{Headers}, $ARGS{Body} // ();
}
elsif ( $ARGS{Body} =~ /\S/ ) {
$ARGS{Content} = "\n$ARGS{Body}";
}
}

my @aresults = UpdateRecordObject( AttributesRef => \@attribs,
Object => $TemplateObj,
ARGSRef => \%ARGS);
push @results, @aresults;

my ($ok, $msg) = $TemplateObj->CompileCheck;
push @results, $msg if !$ok;
push @results, ProcessTemplateUpdate( TemplateObj => $TemplateObj, ARGSRef => \%ARGS );
} else {
$Create = 1;
}
Expand Down
25 changes: 1 addition & 24 deletions share/html/Admin/Queues/Template.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,7 @@
}

if ( $TemplateObj->Id() ) {
$Queue = $TemplateObj->Queue;
$QueueObj = $TemplateObj->QueueObj;

if ( !$ARGS{Content} && ( $ARGS{Headers} || $ARGS{Body} ) ) {

if ( $ARGS{Headers} =~ /\S/ ) {
$ARGS{Headers} =~ s!^\s+!!;
$ARGS{Headers} =~ s!\s+$!!;
$ARGS{Content} = join "\n\n", $ARGS{Headers}, $ARGS{Body} // ();
}
elsif ( $ARGS{Body} =~ /\S/ ) {
$ARGS{Content} = "\n$ARGS{Body}";
}
}

my @attribs = qw( Name Description Queue Type Content );
my @aresults = UpdateRecordObject( AttributesRef => \@attribs,
Object => $TemplateObj,
ARGSRef => \%ARGS
);
push @results, @aresults;

my ( $ok, $msg ) = $TemplateObj->CompileCheck;
push @results, $msg if !$ok;
push @results, ProcessTemplateUpdate( TemplateObj => $TemplateObj, ARGSRef => \%ARGS );
} else {
$Create = 1;
$QueueObj = RT::Queue->new( $session{'CurrentUser'} );
Expand Down

0 comments on commit a5c9728

Please sign in to comment.