Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.0/add create tickets template gui #381

Open
wants to merge 14 commits into
base: stable
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions etc/RT_Config.pm.in
Original file line number Diff line number Diff line change
Expand Up @@ -3992,8 +3992,8 @@ Set(%AdminSearchResultFormat,
.q{,__Condition__, __Action__, __Template__, __Disabled__,__HasLogs__},

Templates =>
q{'<a href="__WebPath__/__WebRequestPathDir__/Template.html?Queue=__QueueId__&Template=__id__">__id__</a>/TITLE:#'}
.q{,'<a href="__WebPath__/__WebRequestPathDir__/Template.html?Queue=__QueueId__&Template=__id__">__Name__</a>/TITLE:Name'}
q{'<a href="__WebPath__/Admin/Templates/Basics.html?Queue=__QueueId__&Template=__id__">__id__</a>/TITLE:#'}
.q{,'<a href="__WebPath__/Admin/Templates/Basics.html?Queue=__QueueId__&Template=__id__">__Name__</a>/TITLE:Name'}
.q{,'__Description__','__UsedBy__','__IsEmpty__'},
Classes =>
q{ '<a href="__WebPath__/Admin/Articles/Classes/Modify.html?id=__id__">__id__</a>/TITLE:#'}
Expand Down
135 changes: 133 additions & 2 deletions lib/RT/Interface/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3171,8 +3171,6 @@ sub ProcessCustomFieldUpdates {
return (@results);
}



=head2 ProcessTicketBasics ( TicketObj => $Ticket, ARGSRef => \%ARGS );

Returns an array of results messages.
Expand Down Expand Up @@ -5321,6 +5319,139 @@ sub ProcessAuthToken {
return @results;
}

=head2 ProcessTemplateUpdate ( TemplateObj => $Template, ARGSRef => \%ARGS );

Accepts a Template Object and a ref to %ARGS and processes any updates
to the Template Object in %ARGS.

Returns an array of results messages.

=cut

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

my @results;
my @FIELDS = qw(
PerlCode
Queue
Subject
Status
SLA
Due
Starts
Started
Resolved
Owner
Requestor
Cc
AdminCc
RequestorGroup
CcGroup
AdminCcGroup
TimeWorked
TimeEstimated
TimeLeft
InitialPriority
FinalPriority
Type
DependsOn
DependedOnBy
RefersTo
ReferredToBy
Members
MemberOf
CustomFields
Content
ContentType
UpdateType
SkipCreate
);

# ensure $args{ARGSRef}{CreateSectionName} is an array ref
# could be undefined (new template), scalar (single section), or an array ref (multiple sections)
if ( $args{ARGSRef}{CreateSectionName} ) {
$args{ARGSRef}{CreateSectionName} = [ $args{ARGSRef}{CreateSectionName} ]
unless ref $args{ARGSRef}{CreateSectionName};
}
else {
$args{ARGSRef}{CreateSectionName} = [];
}

# check if the user submitted a new section without filling in section name
# if so then set a section name, making sure to set it to a unique value
unless ( $args{ARGSRef}{AddNewSectionName} ) {
for my $field ( @FIELDS ) {
if ( defined( $args{ARGSRef}{"ADD-NEW-SECTION-$field"} ) && ( $args{ARGSRef}{"ADD-NEW-SECTION-$field"} ne '' ) ) {
my $new_section_name = 'new-section';
my $counter = 1;
while ( grep { $_ eq $new_section_name } @{ $args{ARGSRef}{CreateSectionName} } ) {
$new_section_name = 'new-section-' . $counter++;
}
$args{ARGSRef}{AddNewSectionName} = $new_section_name;
push @results, "Section Name was not filled in for the new Section. Set Section Name to '$new_section_name'";
last;
}
}
}

push @{ $args{ARGSRef}{CreateSectionName} }, 'ADD-NEW-SECTION'
if $args{ARGSRef}{AddNewSectionName};

if ( @{ $args{ARGSRef}{CreateSectionName} } ) {
my $new_content = '';
for my $name ( @{ $args{ARGSRef}{CreateSectionName} } ) {
my $section_name = $name eq 'ADD-NEW-SECTION' ? $args{ARGSRef}{AddNewSectionName} : $name;
if ( my $new_name = $args{ARGSRef}{"$name-NewSectionName"} ) {
$section_name = $new_name;
}
$new_content .= "===Create-Ticket: $section_name\n";
for my $field ( @FIELDS ) {
if ( $field eq "CustomFields" ) {
while ( my $cf_id = shift @{ $args{ARGSRef}{ $name . "-CustomField-id" } || [] } ) {
my $cf_val = shift @{ $args{ARGSRef}{ $name . "-CustomField-val" } || [] };
$new_content .= "CustomField-$cf_id: $cf_val\n";
}
}
else {
if ( my $val = $args{ARGSRef}{"$name-$field"} ) {
if ( $field eq 'PerlCode' ) {
# trim leading and trailing whitespace
$val =~ s/^\s+//;
$val =~ s/\s+$//;
$new_content .= "{\n$val\n}\n";
}
elsif ( $field eq 'Content' ) {
$new_content .= "Content: $val\nENDOFCONTENT\n"
}
else {
$new_content .= "$field: $val\n";
}
}
}
}
}
$args{ARGSRef}{Content} = $new_content;
}

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

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

return ( @results );
}

=head3 CachedCustomFieldValues FIELD

Similar to FIELD->Values, but caches the return value of FIELD->Values
Expand Down
29 changes: 21 additions & 8 deletions lib/RT/Interface/Web/MenuBuilder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1204,10 +1204,10 @@ sub _BuildAdminMenu {
my $templates = $admin_global->child( templates =>
title => loc('Templates'),
description => loc('Edit system templates'),
path => '/Admin/Global/Templates.html',
path => '/Admin/Templates/',
);
$templates->child( select => title => loc('Select'), path => "/Admin/Global/Templates.html" );
$templates->child( create => title => loc('Create'), path => "/Admin/Global/Template.html?Create=1" );
$templates->child( select => title => loc('Select'), path => "/Admin/Templates/" );
$templates->child( create => title => loc('Create'), path => "/Admin/Templates/Create.html" );

my $cfadmin = $admin_global->child( 'custom-fields' =>
title => loc('Custom Fields'),
Expand Down Expand Up @@ -1427,8 +1427,8 @@ sub _BuildAdminMenu {
$queue->child( people => title => loc('Watchers'), path => "/Admin/Queues/People.html?id=" . $id );

my $templates = $queue->child(templates => title => loc('Templates'), path => "/Admin/Queues/Templates.html?id=" . $id);
$templates->child( select => title => loc('Select'), path => "/Admin/Queues/Templates.html?id=".$id);
$templates->child( create => title => loc('Create'), path => "/Admin/Queues/Template.html?Create=1;Queue=".$id);
$templates->child( select => title => loc('Select'), path => "/Admin/Queues/Templates.html?id=" . $id);
$templates->child( create => title => loc('Create'), path => "/Admin/Templates/Create.html?Queue=".$id);

my $scrips = $queue->child( scrips => title => loc('Scrips'), path => "/Admin/Queues/Scrips.html?id=" . $id);
$scrips->child( select => title => loc('Select'), path => "/Admin/Queues/Scrips.html?id=" . $id );
Expand Down Expand Up @@ -1654,9 +1654,22 @@ sub _BuildAdminMenu {
$page->child( create => title => loc('Create'), path => "/Admin/Actions/Create.html" );
}

if ( $request_path =~ m{^/Admin/Global/Templates?\.html} ) {
$page->child( select => title => loc('Select'), path => "/Admin/Global/Templates.html" );
$page->child( create => title => loc('Create'), path => "/Admin/Global/Template.html?Create=1" );
if ( $request_path =~ m{^/Admin/Templates/} ) {
if ( $HTML::Mason::Commands::m->request_args->{'Template'} && $HTML::Mason::Commands::m->request_args->{'Template'} =~ /^\d+$/ ) {
my $id = $HTML::Mason::Commands::m->request_args->{'Template'};
my $queue = $HTML::Mason::Commands::m->request_args->{'Queue'} || 0;
my $templates = $page->child( select => title => loc('Templates'),
path => "/Admin/Templates/" );
$templates->child( select => title => loc('Select'), path => "/Admin/Templates/" );
$templates->child( create => title => loc('Create'), path => "/Admin/Templates/Create.html" );
$page->child( basics => title => loc('Basics'), path => "/Admin/Templates/Basics.html?Queue=$queue&Template=$id" );
$page->child( content => title => loc('Content'), path => "/Admin/Templates/Content.html?Queue=$queue&Template=$id" );
$page->child( advanced => title => loc('Advanced'), path => "/Admin/Templates/Advanced.html?Queue=$queue&Template=$id" );
}
else {
$page->child( select => title => loc('Select'), path => "/Admin/Templates/" );
$page->child( create => title => loc('Create'), path => "/Admin/Templates/Create.html" );
}
}

if ( $request_path =~ m{^/Admin/Articles/Classes/} ) {
Expand Down
102 changes: 102 additions & 0 deletions lib/RT/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,108 @@ sub PreInflate {
return 1;
}

=head2 my ( $parsed, $error ) = ParseContentAsCreateTicket;

Attempts to parse the value of the Content as a Create Ticket template.

If successful it returns a list of an arrayref and an empty error
message. The arrayref contains a hashref for each section in the
template.

If it cannot parse the Content it returns a list of undef and an error
message.

=cut

sub ParseContentAsCreateTicket {
my $self = shift;

my $content = $self->Content || '';

# allow blank content for new templates
unless ( length($content) > 0 ) {
return ( [], '' );
}
unless ( $self->Type eq 'Create' ) {
return ( undef, 'Template is not Type Create Ticket' );
}
unless ( substr( $content, 0, 3 ) eq '===' ) {
return ( undef, 'Template content is not valid Create Ticket format' );
}

my @parsed = ();
my ( $current_ticket, %current_section );
my @lines = split /\n/, $content;
while ( my $line = shift @lines ) {
if ( $line =~ /^===(.*)-Ticket: (.*)$/ ) {
# TODO: do we need to do anything different for different ticket action?
# Create vs Update vs Base
my $ticket_action = $1;
my $ticket_name = $2;

if ( ! $current_ticket ) {
# first ticket template
$current_ticket = $ticket_name;
$current_section{name} = $ticket_name;
$current_section{action} = $ticket_action;
$current_section{active} = 1;
}
elsif ( $ticket_name ne $current_ticket ) {
# new ticket template
push @parsed, { %current_section };
%current_section = ();
$current_ticket = $ticket_name;
$current_section{name} = $ticket_name;
$current_section{action} = $ticket_action;
}
}
elsif ( $line =~ /^===#.*$/ ) { # a comment
next;
}
elsif ( $line =~ /^(.*?):(?:\s+)(.*?)(?:\s*)$/ ) {
my $field = $1;
my $val = $2;

# fields can have dashes and mixed case so normalize to no dash and lowercase
$field =~ s/-//g;
$field = lc $field;

if ( $field eq 'content' ) {
while ( defined( my $l = shift @lines ) ) {
last if ( $l =~ /^ENDOFCONTENT\s*$/ );
$val .= "\n" . $l;
}
$current_section{$field} = $val;
}
elsif ( $field =~ /(CustomField|CF)(.*)/i ) {
$current_section{CustomFields} = []
unless $current_section{CustomFields};
push @{ $current_section{CustomFields} }, [ $2, $val ];
}
else {
$current_section{$field} = $val;
}
}
else {
# if there is a line of perl code after the initial perl code block we cannot parse it correctly
# in that case we need to return an error that the template is "too advanced" for the GUI and
# they can edit it on the Advanced tab
$line =~ s/^\s+//g;
if ( exists( $current_section{perlcode} ) && ( $line =~ /^{/ ) ) {
# we have a line opening a perl code block but we already have our perlcode block
return ( undef, 'Template content is too advanced to parse correctly' );
}
else {
$current_section{perlcode} .= "$line\n";
}
}
}
# push the last template on to array
push @parsed, { %current_section };

return ( \@parsed, '' );
}

RT::Base->_ImportOverlays();

1;
Loading
Loading