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 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
108 return ( 0, $self->loc('Permission Denied') );
110 return $self->SUPER::_Set( @_ );
119 Takes the name of a table column.
120 Returns its value as a string, if the user passes an ACL check
125 my $t = RT::Template->new($RT::SystemUser);
126 $t->Create(Name => "Foo", Queue => 1);
127 my $t2 = RT::Template->new($RT::Nobody);
129 ok($t2->QueueObj->id, "Got the template's queue objet");
140 unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
143 return $self->__Value( @_ );
151 =head2 Load <identifer>
153 Load a template, either by number or by name
159 my $identifier = shift;
160 return undef unless $identifier;
162 if ( $identifier =~ /\D/ ) {
163 return $self->LoadByCol( 'Name', $identifier );
165 return $self->LoadById( $identifier );
170 # {{{ sub LoadGlobalTemplate
172 =head2 LoadGlobalTemplate NAME
174 Load the global tempalte with the name NAME
178 sub LoadGlobalTemplate {
182 return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
187 # {{{ sub LoadQueueTemplate
189 =head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
191 Loads the Queue template named NAME for Queue QUEUE.
195 sub LoadQueueTemplate {
203 return ( $self->LoadByCols( Name => $args{'Name'}, Queue => $args{'Queue'} ) );
213 Takes a paramhash of Content, Queue, Name and Description.
214 Name should be a unique string identifying this Template.
215 Description and Content should be the template's title and content.
216 Queue should be 0 for a global template and the queue # for a queue-specific
219 Returns the Template's id # if the create was successful. Returns undef for
220 unknown database failure.
230 Description => '[no description]',
231 Type => 'Action', #By default, template are 'Action' templates
236 unless ( $args{'Queue'} ) {
237 unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
238 return ( undef, $self->loc('Permission denied') );
243 my $QueueObj = new RT::Queue( $self->CurrentUser );
244 $QueueObj->Load( $args{'Queue'} ) || return ( undef, $self->loc('Invalid queue') );
246 unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
247 return ( undef, $self->loc('Permission denied') );
249 $args{'Queue'} = $QueueObj->Id;
252 my $result = $self->SUPER::Create(
253 Content => $args{'Content'},
254 Queue => $args{'Queue'},
255 Description => $args{'Description'},
256 Name => $args{'Name'},
269 Delete this template.
276 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
277 return ( 0, $self->loc('Permission Denied') );
280 return ( $self->SUPER::Delete(@_) );
288 return ( $self->{'MIMEObj'} );
297 This routine performs Text::Template parsing on the template and then
298 imports the results into a MIME::Entity so we can really use it
300 Takes a hash containing Argument, TicketObj, and TransactionObj.
302 It returns a tuple of (val, message)
303 If val is 0, the message contains an error message
310 #We're passing in whatever we were passed. it's destined for _ParseContent
311 my ($content, $msg) = $self->_ParseContent(@_);
312 return ( 0, $msg ) unless defined $content;
314 #Lets build our mime Entity
316 my $parser = MIME::Parser->new();
318 # On some situations TMPDIR is non-writable. sad but true.
319 $parser->output_to_core(1);
320 $parser->tmp_to_core(1);
322 #If someone includes a message, don't extract it
323 $parser->extract_nested_messages(1);
325 # Set up the prefix for files with auto-generated names:
326 $parser->output_prefix("part");
328 # If content length is <= 50000 bytes, store each msg as in-core scalar;
329 # Else, write to a disk file (the default action):
330 $parser->output_to_core(50000);
332 ### Should we forgive normally-fatal errors?
333 $parser->ignore_errors(1);
334 $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
335 if ( my $error = $@ || $parser->last_error ) {
336 $RT::Logger->error( "$error" );
337 return ( 0, $error );
341 $self->{'MIMEObj'}->head->unfold;
343 return ( 1, $self->loc("Template parsed") );
349 # {{{ sub _ParseContent
351 # Perform Template substitutions on the template
358 TransactionObj => undef,
362 no warnings 'redefine';
363 $T::Ticket = $args{'TicketObj'};
364 $T::Transaction = $args{'TransactionObj'};
365 $T::Argument = $args{'Argument'};
366 $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name };
367 $T::rtname = $RT::rtname;
368 *T::loc = sub { $T::Ticket->loc(@_) };
370 my $content = $self->Content;
371 unless ( defined $content ) {
372 return ( undef, $self->loc("Permissions denied") );
375 # We need to untaint the content of the template, since we'll be working
377 $content =~ s/^(.*)$/$1/;
378 my $template = Text::Template->new(
384 my $retval = $template->fill_in( PACKAGE => 'T', BROKEN => sub {
386 $RT::Logger->error("Template parsing error: $args{error}")
387 unless $args{error} =~ /^Died at /; # ignore intentional die()
391 return ( undef, $self->loc('Template parsing error') ) if $is_broken;
393 # MIME::Parser has problems dealing with high-bit utf8 data.
394 Encode::_utf8_off($retval);
400 # {{{ sub CurrentUserHasQueueRight
402 =head2 CurrentUserHasQueueRight
404 Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.
408 sub CurrentUserHasQueueRight {
410 return ( $self->QueueObj->CurrentUserHasRight(@_) );