1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # CONTRIBUTION SUBMISSION POLICY:
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
45 # END BPS TAGGED BLOCK }}}
46 # Portions Copyright 2000 Tobias Brox <tobix@cpan.org>
50 RT::Template - RT's template object
63 ok(require RT::Template);
73 no warnings qw(redefine);
78 use File::Temp qw /tempdir/;
88 Description => 'read/write',
89 Type => 'read/write', #Type is one of Action or Message
90 Content => 'read/write',
91 Queue => 'read/write',
92 Creator => 'read/auto',
93 Created => 'read/auto',
94 LastUpdatedBy => 'read/auto',
95 LastUpdated => 'read/auto'
97 return $self->SUPER::_Accessible( @_, %Cols );
107 # use super::value or we get acl blocked
108 if ( ( defined $self->SUPER::_Value('Queue') )
109 && ( $self->SUPER::_Value('Queue') == 0 ) )
111 unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ModifyTemplate') ) {
112 return ( 0, $self->loc('Permission Denied') );
117 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
118 return ( 0, $self->loc('Permission Denied') );
121 return ( $self->SUPER::_Set(@_) );
131 Takes the name of a table column.
132 Returns its value as a string, if the user passes an ACL check
137 my $t = RT::Template->new($RT::SystemUser);
138 $t->Create(Name => "Foo", Queue => 1);
139 my $t2 = RT::Template->new($RT::Nobody);
141 ok($t2->QueueObj->id, "Got the template's queue objet");
155 #If the current user doesn't have ACLs, don't let em at it.
156 #use super::value or we get acl blocked
157 if ( ( !defined $self->__Value('Queue') )
158 || ( $self->__Value('Queue') == 0 ) )
160 unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ShowTemplate') ) {
165 unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
169 return ( $self->__Value($field) );
177 =head2 Load <identifer>
179 Load a template, either by number or by name
185 my $identifier = shift;
187 if ( !$identifier ) {
191 if ( $identifier !~ /\D/ ) {
192 $self->SUPER::LoadById($identifier);
195 $self->LoadByCol( 'Name', $identifier );
202 # {{{ sub LoadGlobalTemplate
204 =head2 LoadGlobalTemplate NAME
206 Load the global tempalte with the name NAME
210 sub LoadGlobalTemplate {
214 return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
219 # {{{ sub LoadQueueTemplate
221 =head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
223 Loads the Queue template named NAME for Queue QUEUE.
227 sub LoadQueueTemplate {
235 return ( $self->LoadByCols( Name => $args{'Name'}, Queue => $args{'Queue'} ) );
245 Takes a paramhash of Content, Queue, Name and Description.
246 Name should be a unique string identifying this Template.
247 Description and Content should be the template's title and content.
248 Queue should be 0 for a global template and the queue # for a queue-specific
251 Returns the Template's id # if the create was successful. Returns undef for
252 unknown database failure.
262 Description => '[no description]',
263 Type => 'Action', #By default, template are 'Action' templates
268 if ( !$args{'Queue'} ) {
269 unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
275 my $QueueObj = new RT::Queue( $self->CurrentUser );
276 $QueueObj->Load( $args{'Queue'} ) || return ( 0, $self->loc('Invalid queue') );
278 unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
281 $args{'Queue'} = $QueueObj->Id;
284 my $result = $self->SUPER::Create(
285 Content => $args{'Content'},
286 Queue => $args{'Queue'},
287 Description => $args{'Description'},
288 Name => $args{'Name'}
301 Delete this template.
308 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
309 return ( 0, $self->loc('Permission Denied') );
312 return ( $self->SUPER::Delete(@_) );
320 return ( $self->{'MIMEObj'} );
329 This routine performs Text::Template parsing on the template and then
330 imports the results into a MIME::Entity so we can really use it
332 Takes a hash containing Argument, TicketObj, and TransactionObj.
334 It returns a tuple of (val, message)
335 If val is 0, the message contains an error message
342 #We're passing in whatever we were passed. it's destined for _ParseContent
343 my $content = $self->_ParseContent(@_);
345 #Lets build our mime Entity
347 my $parser = MIME::Parser->new();
349 # On some situations TMPDIR is non-writable. sad but true.
350 $parser->output_to_core(1);
351 $parser->tmp_to_core(1);
353 #If someone includes a message, don't extract it
354 $parser->extract_nested_messages(1);
356 # Set up the prefix for files with auto-generated names:
357 $parser->output_prefix("part");
359 # If content length is <= 50000 bytes, store each msg as in-core scalar;
360 # Else, write to a disk file (the default action):
361 $parser->output_to_core(50000);
363 ### Should we forgive normally-fatal errors?
364 $parser->ignore_errors(1);
365 $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
366 my $error = ( $@ || $parser->last_error );
369 $RT::Logger->error("$error");
370 return ( 0, $error );
374 $self->{'MIMEObj'}->head->unfold();
376 return ( 1, $self->loc("Template parsed") );
382 # {{{ sub _ParseContent
384 # Perform Template substitutions on the template
391 TransactionObj => undef,
395 no warnings 'redefine';
396 $T::Ticket = $args{'TicketObj'};
397 $T::Transaction = $args{'TransactionObj'};
398 $T::Argument = $args{'Argument'};
399 $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name };
400 $T::rtname = $RT::rtname;
401 *T::loc = sub { $T::Ticket->loc(@_) };
403 # We need to untaint the content of the template, since we'll be working
405 my $content = $self->Content();
406 $content =~ s/^(.*)$/$1/;
407 my $template = Text::Template->new(
413 my $retval = $template->fill_in( PACKAGE => 'T', BROKEN => sub {
415 $RT::Logger->error("Template parsing error: $args{error}")
416 unless $args{error} =~ /^Died at /; # ignore intentional die()
420 return undef if $is_broken;
422 # MIME::Parser has problems dealing with high-bit utf8 data.
423 Encode::_utf8_off($retval);
429 # {{{ sub CurrentUserHasQueueRight
431 =head2 CurrentUserHasQueueRight
433 Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.
437 sub CurrentUserHasQueueRight {
439 return ( $self->QueueObj->CurrentUserHasRight(@_) );