%doc> List the ticket templates associated with a queue, add a new one, or edit an existing one. Template definitions are stored in the "TemplateTicketsDefinition" attribute of tickets, consisting of a hashref containing: Category - category name or empty string Description - description of the template ChildTickets - whether to include child tickets (1 or 0) ShowDerivation - whether to show "derived from template" in new tickets Groups - array ref of group IDs who can use this template Fields - array ref of field names to include in the template ChildFields - array ref of field names to include in child tickets If the "Groups" array ref is empty, the template is unrestricted. %doc> \ <%ARGS> $id $Ticket => undef $CreateTemplate => undef $Store => undef $Delete => undef $ConfirmDelete => undef %ARGS> \ <%INIT> my $QueueObj = RT::Queue->new( $session{'CurrentUser'} ); $QueueObj->Load($id); if ( !$QueueObj->id ) { Abort( loc( 'Queue [_1] not found', $id ) ); } # Check user permissions. # my $CanView = $QueueObj->CurrentUserHasRight('ShowTicketTemplate'); my $CanEdit = $QueueObj->CurrentUserHasRight('ModifyTicketTemplate'); if (not( $CanView || $CanEdit ) ) { Abort( loc('Permission denied') ); } # Mapping of internal field names to displayed name. # my %FieldNames = ( 'Subject' => loc('Subject'), 'Content' => loc('Content'), 'Priority' => loc('Priority'), 'FinalPriority' => loc('Final priority'), 'Requestors' => loc('Requestors'), 'Cc' => loc('CCs'), 'AdminCc' => loc('AdminCCs'), 'Queue' => loc('Queue'), ); # Fields to consider on the main ticket. # my @MainFields = ( 'Subject', 'Content', 'Priority', 'FinalPriority', 'Requestors', 'Cc', 'AdminCc' ); # Fields to consider on child tickets. # my @ChildFields = ( 'Queue', @MainFields ); # Default field selections when none are chosen. # my @DefaultFields = ( 'Subject', 'Content' ); my @DefaultChildFields = ( 'Queue', 'Subject', 'Content' ); my @Results = (); my $Title = loc( 'Template tickets for queue [_1]', $QueueObj->Name ); # If a ticket has been specified, load it and set the page title as # appropriate, clearing the relevant flags if permissions are not # sufficient. # my $TicketObj = undef; if ($Ticket) { $TicketObj = RT::Ticket->new( $session{'CurrentUser'} ); $TicketObj->Load($Ticket); if ( not $TicketObj->id ) { # Return to the ticket list if this ticket was not loaded. # push @Results, loc( 'Ticket [_1] not found', $Ticket ); $Ticket = undef; $TicketObj = undef; $CreateTemplate = 0; } elsif ( not $TicketObj->CurrentUserHasRight('ShowTicket') ) { # Return to the ticket list if the user has no permission to view # this ticket. # push @Results, loc('No permission to view ticket'); $Ticket = undef; $TicketObj = undef; $CreateTemplate = 0; } elsif ( $TicketObj->Queue != $QueueObj->id ) { # Return to the ticket list if the chosen ticket isn't in the # current queue. # push @Results, loc( 'Ticket [_1] is not in queue [_2]', $Ticket, $QueueObj->Name || $id ); $Ticket = undef; $TicketObj = undef; $CreateTemplate = 0; } elsif ( $CreateTemplate && not $CanEdit ) { # Return to the ticket list if we're trying to create a new template # ticket but the user has no permission to modify templates. # push @Results, loc( 'No permission to modify ticket templates in queue [_1]', $QueueObj->Name ); $Ticket = undef; $TicketObj = undef; $CreateTemplate = 0; } elsif ($CreateTemplate) { # Set the title for creating a new template ticket. # $Title = loc( 'Create new template ticket for queue [_1]', $QueueObj->Name ); } elsif ( $Delete && $ConfirmDelete && $CanEdit ) { # Delete a template ticket, given confirmation and given that the # user has the right permissions, and return to the ticket list. # $TicketObj->DeleteAttribute('TemplateTicketsDefinition'); push @Results, loc( 'Template ticket #[_1] deleted.', $Ticket ); $Ticket = undef; $TicketObj = undef; $CreateTemplate = 0; } else { # Set the title for displaying a template ticket. # $Title = loc( 'Queue [_1]: Template ticket #[_2]: [_3]', $QueueObj->Name, $TicketObj->id, $TicketObj->Subject ); # If deletion was attempted, display the appropriate notice. # if ( $Delete && not $ConfirmDelete ) { push @Results, loc('Not deleted - deletion not confirmed.'); } elsif ( $Delete && not $CanEdit ) { push @Results, loc('Not deleted - permission denied.'); } } } # Find all template tickets for this queue. # my $TemplateTickets = RT::Tickets->new( $session{'CurrentUser'} ); $TemplateTickets->FromSQL( 'Queue = ' . $QueueObj->id . ' AND HasAttribute = "TemplateTicketsDefinition"' ); my $TemplateTicketObjects = $TemplateTickets->ItemsArrayRef; # Read the settings of all the template tickets in this queue, and build a # hash of the category names so we can provide them as options in a combobox # when editing. # my %TemplateCategories = (); my %AllTemplateSettings = (); $TemplateTickets->GotoFirstItem(); while ( my $TemplateTicketObj = $TemplateTickets->Next ) { my $Attribute = $TemplateTicketObj->FirstAttribute('TemplateTicketsDefinition'); next if ( not defined $Attribute ); my $Settings = $Attribute->Content; next if ( not defined $Settings ); $AllTemplateSettings{ $TemplateTicketObj->id } = $Settings; $TemplateCategories{ $Settings->{'Category'} }++; } # Sort the template tickets by category and then by subject. # $TemplateTicketObjects = [ sort { ( $AllTemplateSettings{ $a->id }->{'Category'} || '' ) eq ( $AllTemplateSettings{ $b->id }->{'Category'} || '' ) ? ( $a->Subject cmp $b->Subject ) : ( $AllTemplateSettings{ $a->id }->{'Category'} || '' ) cmp( $AllTemplateSettings{ $b->id }->{'Category'} || '' ) } @$TemplateTicketObjects ]; # Settings and ticket content of the currently selected template. # my $TemplateSettings = {}; my $TemplateSummary = ''; my $TemplateContent = ''; # If a template ticket has been loaded into $TicketObj earlier, get ready to # display its details, and perform any appropriate operations on it. # if ($TicketObj) { # Create an object for simulating a read-only version of the ticket # (which the current user has no rights over), so that when we run the # Mason components to display the ticket, they don't include the editing # elements. # my $ReadonlyTicketObj = {%$TicketObj}; bless( $ReadonlyTicketObj, ref $TicketObj ); $ReadonlyTicketObj->{'CurrentUserCanSetOwner'} = sub { return 0; }; $ReadonlyTicketObj->{'CurrentUserHasRight'} = sub { return 0; }; # Limit the ticket history to just the first transaction, since that's # what will provide the "content" for the template. # my $TicketHistory = $ReadonlyTicketObj->SortedTransactions; $TicketHistory->RowsPerPage(1); # Generate the ticket summary display, and then strip out any form # elements and ticket creation links to render it inert. # $TemplateSummary = $m->scomp( '/Ticket/Elements/ShowSummary', 'Ticket' => $ReadonlyTicketObj ); $TemplateSummary =~ s/
<&|/l&>This is just a read-only summary. Open the original ticket to change the details below.&>
<&|/l&>Open the original ticket&>
<% $TemplateSummary |n %> <% $TemplateContent |n %> &> \ % } else { \ <&| /Widgets/TitleBox, title => loc('Template tickets') &> % if (scalar @$TemplateTicketObjects > 0) {# | \<&|/l&>Subject&> | \<&|/l&>Description&> | \<&|/l&>Category&> | \<&|/l&>Groups&> | \
---|---|---|---|---|
<% $TemplateTicketObj->id %> | \<% $TemplateTicketObj->Subject %> | \<% $AllTemplateSettings{$TemplateTicketObj->id}->{'Description'} %> | \<% $AllTemplateSettings{$TemplateTicketObj->id}->{'Category'} %> | \\
% my %DisplayedTemplateGroupNames = ();
% foreach my $GroupId ( @{ $AllTemplateSettings{$TemplateTicketObj->id}->{'Groups'} || [] } ) {
% next if ( not defined $GroupId );
% my $GroupName = $GroupIdToName{$GroupId};
% if (not defined $GroupName) {
% next if ( not $GroupObj->Load($GroupId) );
% next if ( not $GroupObj->id );
% $GroupIdToName{$GroupId} = $GroupObj->Name;
% $GroupName = $GroupIdToName{$GroupId};
% }
% $DisplayedTemplateGroupNames{$GroupName} = $GroupId;
% }
% if (scalar keys %DisplayedTemplateGroupNames == 0) {
<&|/l&>Everyone&>\
% } else {
% my $GroupNum = 0;
% foreach (sort { $a cmp $b } keys %DisplayedTemplateGroupNames) {
% $GroupNum++;
% if ($GroupNum > 1) {
\ % } <% $_ %>\ % } % } % } |
<&|/l&>This queue has no template tickets.&>
% } &> \ % if ( $CanEdit ) { <&| /Widgets/TitleBox, title => loc('Create a new template ticket') &> &> % } \ % }