3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 # (Except where explictly superceded by other copyright notices)
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
24 # Portions Copyright 2000 Tobias Brox <tobix@cpan.org>
28 RT::Template - RT's template object
41 ok(require RT::Template);
48 no warnings qw(redefine);
53 use File::Temp qw /tempdir/;
63 Description => 'read/write',
64 Type => 'read/write', #Type is one of Action or Message
65 Content => 'read/write',
66 Queue => 'read/write',
67 Creator => 'read/auto',
68 Created => 'read/auto',
69 LastUpdatedBy => 'read/auto',
70 LastUpdated => 'read/auto'
72 return $self->SUPER::_Accessible( @_, %Cols );
82 # use super::value or we get acl blocked
83 if ( ( defined $self->SUPER::_Value('Queue') )
84 && ( $self->SUPER::_Value('Queue') == 0 ) )
86 unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ModifyTemplate') ) {
87 return ( 0, $self->loc('Permission Denied') );
92 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
93 return ( 0, $self->loc('Permission Denied') );
96 return ( $self->SUPER::_Set(@_) );
106 Takes the name of a table column.
107 Returns its value as a string, if the user passes an ACL check
112 my $t = RT::Template->new($RT::SystemUser);
113 $t->Create(Name => "Foo", Queue => 1);
114 my $t2 = RT::Template->new($RT::Nobody);
116 ok($t2->QueueObj->id, "Got the template's queue objet");
130 #If the current user doesn't have ACLs, don't let em at it.
131 #use super::value or we get acl blocked
132 if ( ( !defined $self->__Value('Queue') )
133 || ( $self->__Value('Queue') == 0 ) )
135 unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ShowTemplate') ) {
140 unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
144 return ( $self->__Value($field) );
152 =head2 Load <identifer>
154 Load a template, either by number or by name
160 my $identifier = shift;
162 if ( !$identifier ) {
166 if ( $identifier !~ /\D/ ) {
167 $self->SUPER::LoadById($identifier);
170 $self->LoadByCol( 'Name', $identifier );
177 # {{{ sub LoadGlobalTemplate
179 =head2 LoadGlobalTemplate NAME
181 Load the global tempalte with the name NAME
185 sub LoadGlobalTemplate {
189 return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
194 # {{{ sub LoadQueueTemplate
196 =head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
198 Loads the Queue template named NAME for Queue QUEUE.
202 sub LoadQueueTemplate {
210 return ( $self->LoadByCols( Name => $args{'Name'}, Queue => $args{'Queue'} ) );
220 Takes a paramhash of Content, Queue, Name and Description.
221 Name should be a unique string identifying this Template.
222 Description and Content should be the template's title and content.
223 Queue should be 0 for a global template and the queue # for a queue-specific
226 Returns the Template's id # if the create was successful. Returns undef for
227 unknown database failure.
237 Description => '[no description]',
238 Type => 'Action', #By default, template are 'Action' templates
243 if ( !$args{'Queue'} ) {
244 unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
250 my $QueueObj = new RT::Queue( $self->CurrentUser );
251 $QueueObj->Load( $args{'Queue'} ) || return ( 0, $self->loc('Invalid queue') );
253 unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
256 $args{'Queue'} = $QueueObj->Id;
259 my $result = $self->SUPER::Create(
260 Content => $args{'Content'},
261 Queue => $args{'Queue'},
262 Description => $args{'Description'},
263 Name => $args{'Name'}
276 Delete this template.
283 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
284 return ( 0, $self->loc('Permission Denied') );
287 return ( $self->SUPER::Delete(@_) );
295 return ( $self->{'MIMEObj'} );
304 This routine performs Text::Template parsing on the template and then
305 imports the results into a MIME::Entity so we can really use it
306 It returns a tuple of (val, message)
307 If val is 0, the message contains an error message
314 #We're passing in whatever we were passed. it's destined for _ParseContent
315 my $content = $self->_ParseContent(@_);
317 #Lets build our mime Entity
319 my $parser = MIME::Parser->new();
321 # On some situations TMPDIR is non-writable. sad but true.
322 $parser->output_to_core(1);
323 $parser->tmp_to_core(1);
325 #If someone includes a message, don't extract it
326 $parser->extract_nested_messages(1);
328 # Set up the prefix for files with auto-generated names:
329 $parser->output_prefix("part");
331 # If content length is <= 50000 bytes, store each msg as in-core scalar;
332 # Else, write to a disk file (the default action):
333 $parser->output_to_core(50000);
335 ### Should we forgive normally-fatal errors?
336 $parser->ignore_errors(1);
337 $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
338 my $error = ( $@ || $parser->last_error );
341 $RT::Logger->error("$error");
342 return ( 0, $error );
346 $self->{'MIMEObj'}->head->unfold();
348 return ( 1, $self->loc("Template parsed") );
354 # {{{ sub _ParseContent
356 # Perform Template substitutions on the template
363 TransactionObj => undef,
367 no warnings 'redefine';
368 $T::Ticket = $args{'TicketObj'};
369 $T::Transaction = $args{'TransactionObj'};
370 $T::Argument = $args{'Argument'};
371 $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name };
372 $T::rtname = $RT::rtname;
373 *T::loc = sub { $T::Ticket->loc(@_) };
375 # We need to untaint the content of the template, since we'll be working
377 my $content = $self->Content();
378 $content =~ s/^(.*)$/$1/;
379 my $template = Text::Template->new(
384 my $retval = $template->fill_in( PACKAGE => 'T' );
386 # MIME::Parser has problems dealing with high-bit utf8 data.
387 Encode::_utf8_off($retval);
393 # {{{ sub CurrentUserHasQueueRight
395 =head2 CurrentUserHasQueueRight
397 Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.
401 sub CurrentUserHasQueueRight {
403 return ( $self->QueueObj->CurrentUserHasRight(@_) );