<& /Admin/Elements/Header, Title => $title &> <& /Elements/Tabs &> <& /Elements/ListActions, actions => \@results &> <&| /Widgets/TitleBox, title => '', class => 'formtools-admin-description ms-auto me-auto' &>
<&| /Elements/LabeledValue, Label => loc("Default Queue") &> <& /Elements/SelectQueue, Name => 'DefaultQueue', Id => 'default-queue', Default => $queue_obj->id &> <& /Elements/Submit, Label => loc('Update Default Queue'), Name => 'ModifyDefault', FullWidth => 1 &>
% my $route_count = 0; % foreach my $route ( @{ $content->{routing} || [] } ) {
<&| /Widgets/TitleBox, title => loc('Update Routing') &>
<% loc('Criteria') %>
<% loc('Route to Queue') %>
 
<% loc('and then') %>
<% loc('and then') %>
% # need to use id="pick-criteria" to get correct styling for the pick cf fields
<& /Search/Elements/RoutingPickTicketCFs, CF_ids => \@cf_ids &>
<&| /Elements/LabeledValue, Label => loc("Queue") &> <& /Elements/SelectQueue, Name => 'Queue-' . $route_count, Id => 'queue' . $route_count, Default => $route->{queue}, ShowNullOption => 0 &>
% $route_count++; % } <&| /Widgets/TitleBox, title => loc('Add new routing') &>
<% loc('Criteria') %>
<% loc('Route to Queue') %>
 
<% loc('and then') %>
<% loc('and then') %>
% # need to use id="pick-criteria" to get correct styling for the pick cf fields
<& /Search/Elements/RoutingPickTicketCFs, CF_ids => \@cf_ids &>
<&| /Elements/LabeledValue, Label => loc("Queue") &> <& /Elements/SelectQueue, Name => 'Queue', Id => 'queue', Default => $ARGS{Queue} &>
<& /Elements/Submit, Label => loc('Add Routing'), Name => 'AddNew' &>
<%INIT> Abort("No form id found") unless $id; my $form_attribute = RT::Attribute->new($session{'CurrentUser'}); my ( $ok, $msg ) = $form_attribute->Load($id); unless ( $ok ) { Abort("Unable to load form with id $id"); } my ( $title, @results, $maybe_redirect ); $title = loc("Routing for form [_1]", $form_attribute->Description); my $content = $form_attribute->Content; my @add_new_routing; if ( $ModifyDefault ) { if ( $ARGS{DefaultQueue} ne $content->{queue} ) { $content->{queue} = $ARGS{DefaultQueue}; ( $ok, $msg ) = $form_attribute->SetContent($content); if ($ok) { push @results, loc('Updated default queue'); $maybe_redirect = 1; } else { push @results, loc( "Could not update default queue: [_1]", $msg ); } } } elsif ( $AddNew ) { Abort( loc('Permission Denied') ) unless $session{'CurrentUser'}->HasRight( Object => $RT::System, Right => 'AdminForm' ); my %count_for_field; foreach my $cf ( @{ $ARGS{SelectCustomFieldField} || [] } ) { if ( $cf ) { $count_for_field{$cf} ||= 0; my $cf_name = $cf; $cf_name =~ s/^CF.\{//; $cf_name =~ s/\}$//; # the first value in the op and val arrays is not a valid value # so increment the count by 1 to get the actual op and value my $cf_ops = $ARGS{ $cf . 'Op' } || []; my $cf_op = $cf_ops->[ $count_for_field{$cf} + 1] || ''; my $cf_vals = $ARGS{ 'ValueOf' . $cf } || []; my ( $cf_val ) = $cf_vals->[ $count_for_field{$cf} + 1 ] || ''; push @add_new_routing, [ $cf_name, $cf_op, $cf_val ]; $count_for_field{$cf}++; } } my $add_new_queue = $ARGS{Queue}; if ( $add_new_queue ) { if ( @add_new_routing ) { my $new_route = { queue => $add_new_queue, criteria => \@add_new_routing, }; $content->{routing} ||= []; push @{ $content->{routing} }, $new_route; ( $ok, $msg ) = $form_attribute->SetContent($content); if ($ok) { push @results, loc('Added new routing'); $maybe_redirect = 1; } else { push @results, loc( "Cannot add new routing: [_1]", $msg ); } } else { push @results, loc('Cannot add new routing: No criteria selected'); } } else { push @results, loc('Cannot add new routing: No queue selected'); } } elsif ( my ( $modify ) = grep { $_ =~ /^Update-\d+/ } keys %ARGS ) { $modify =~ s/^Update-//; if ( my $new_queue = $ARGS{ 'Queue-' . $modify } ) { $content->{routing}[$modify]{queue} = $new_queue; # new criteria my @new_routing; my %count_for_field; foreach my $cf ( @{ $ARGS{SelectCustomFieldField} || [] } ) { if ( $cf ) { $count_for_field{$cf} ||= 0; my $cf_name = $cf; $cf_name =~ s/^CF.\{//; $cf_name =~ s/\}$//; # the first value in the op and val arrays is not a valid value # so increment the count by 1 to get the actual op and value my $cf_ops = $ARGS{ $cf . 'Op' } || []; my $cf_op = $cf_ops->[ $count_for_field{$cf} + 1] || ''; my $cf_vals = $ARGS{ 'ValueOf' . $cf } || []; my ( $cf_val ) = $cf_vals->[ $count_for_field{$cf} + 1 ] || ''; push @new_routing, [ $cf_name, $cf_op, $cf_val ]; $count_for_field{$cf}++; } } $content->{routing}[$modify]{criteria} = \@new_routing; ( $ok, $msg ) = $form_attribute->SetContent($content); if ($ok) { push @results, loc('Updated routing'); $maybe_redirect = 1; } else { push @results, loc( "Could not update routing: [_1]", $msg ); } } } elsif ( my ( $delete ) = grep { $_ =~ /^Delete-\d/ } keys %ARGS ) { $delete =~ s/^Delete-//; splice @{ $content->{routing} }, $delete, 1; ( $ok, $msg ) = $form_attribute->SetContent($content); if ($ok) { push @results, loc('Deleted routing'); $maybe_redirect = 1; } else { push @results, loc( "Could not delete routing: [_1]", $msg ); } } MaybeRedirectForResults( Actions => \@results, Path => '/Admin/FormTools/Routing.html', Arguments => { id => $id }, ) if $maybe_redirect; my $queue_obj = RT::Queue->new( $session{CurrentUser} ); ( $ok, $msg ) = $queue_obj->Load( $content->{queue} ); unless ( $ok ) { RT->Logger->error( 'FormTools unable to load queue: ' . $content->{queue} ); Abort('Unable to load form, invalid queue'); } # find all custom fields that are on the form my @cf_ids; foreach my $page ( keys %{ $content->{'formtools-pages'} || {} } ) { my @fields = grep { $_->{comp_name} ? $_->{comp_name} eq 'Field' : 0 } @{ $content->{'formtools-pages'}{$page}{content} || [] }; foreach my $field ( @fields ) { unless ( RT::Extension::FormTools::is_core_field( $field->{arguments}{name} ) ) { my $cf_name = $field->{arguments}{name}; my $cf = RT::CustomField->new( $session{'CurrentUser'} ); # try loading CFs for this Queue, followed by Global, followed by any CF of given $cf_name $cf->LoadByName( Name => $cf_name, Queue => $queue_obj->id ) if (defined $queue_obj); $cf->LoadByName( Name => $cf_name, Queue => 0 ) unless ( $cf->id ); unless ( $cf->Id ) { $cf->LoadByName( Name => $cf_name ); } unless ( $cf->id ) { my $msg = "Could not find a custom field called $cf_name"; $msg .= " for the queue " . $queue_obj->Name if (defined $queue_obj); RT->Logger->error('FormTools unable to load custom field: ' . $msg); next; } push @cf_ids, $cf->id; } } } <%ARGS> $id => undef $AddNew => undef $ModifyDefault => undef