1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
48 # Portions Copyright 2000 Tobias Brox <tobix@cpan.org>
52 RT::Template - RT's template object
65 ok(require RT::Template);
75 no warnings qw(redefine);
80 use File::Temp qw /tempdir/;
90 Description => 'read/write',
91 Type => 'read/write', #Type is one of Action or Message
92 Content => 'read/write',
93 Queue => 'read/write',
94 Creator => 'read/auto',
95 Created => 'read/auto',
96 LastUpdatedBy => 'read/auto',
97 LastUpdated => 'read/auto'
99 return $self->SUPER::_Accessible( @_, %Cols );
109 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
110 return ( 0, $self->loc('Permission Denied') );
112 return $self->SUPER::_Set( @_ );
121 Takes the name of a table column.
122 Returns its value as a string, if the user passes an ACL check
127 my $t = RT::Template->new($RT::SystemUser);
128 $t->Create(Name => "Foo", Queue => 1);
129 my $t2 = RT::Template->new($RT::Nobody);
131 ok($t2->QueueObj->id, "Got the template's queue objet");
142 unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
145 return $self->__Value( @_ );
153 =head2 Load <identifer>
155 Load a template, either by number or by name
161 my $identifier = shift;
162 return undef unless $identifier;
164 if ( $identifier =~ /\D/ ) {
165 return $self->LoadByCol( 'Name', $identifier );
167 return $self->LoadById( $identifier );
172 # {{{ sub LoadGlobalTemplate
174 =head2 LoadGlobalTemplate NAME
176 Load the global template with the name NAME
180 sub LoadGlobalTemplate {
184 return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
189 # {{{ sub LoadQueueTemplate
191 =head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
193 Loads the Queue template named NAME for Queue QUEUE.
197 sub LoadQueueTemplate {
205 return ( $self->LoadByCols( Name => $args{'Name'}, Queue => $args{'Queue'} ) );
215 Takes a paramhash of Content, Queue, Name and Description.
216 Name should be a unique string identifying this Template.
217 Description and Content should be the template's title and content.
218 Queue should be 0 for a global template and the queue # for a queue-specific
221 Returns the Template's id # if the create was successful. Returns undef for
222 unknown database failure.
232 Description => '[no description]',
233 Type => 'Action', #By default, template are 'Action' templates
238 unless ( $args{'Queue'} ) {
239 unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
240 return ( undef, $self->loc('Permission denied') );
245 my $QueueObj = new RT::Queue( $self->CurrentUser );
246 $QueueObj->Load( $args{'Queue'} ) || return ( undef, $self->loc('Invalid queue') );
248 unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
249 return ( undef, $self->loc('Permission denied') );
251 $args{'Queue'} = $QueueObj->Id;
254 my $result = $self->SUPER::Create(
255 Content => $args{'Content'},
256 Queue => $args{'Queue'},
257 Description => $args{'Description'},
258 Name => $args{'Name'},
271 Delete this template.
278 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
279 return ( 0, $self->loc('Permission Denied') );
282 return ( $self->SUPER::Delete(@_) );
290 return ( $self->{'MIMEObj'} );
299 This routine performs Text::Template parsing on the template and then
300 imports the results into a MIME::Entity so we can really use it
302 Takes a hash containing Argument, TicketObj, and TransactionObj. TicketObj
303 and TransactionObj are not mandatory, but highly recommended.
305 It returns a tuple of (val, message)
306 If val is 0, the message contains an error message
313 #We're passing in whatever we were passed. it's destined for _ParseContent
314 my ($content, $msg) = $self->_ParseContent(@_);
315 return ( 0, $msg ) unless defined $content;
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 if ( my $error = $@ || $parser->last_error ) {
339 $RT::Logger->error( "$error" );
340 return ( 0, $error );
344 $self->{'MIMEObj'}->head->unfold;
346 return ( 1, $self->loc("Template parsed") );
352 # {{{ sub _ParseContent
354 # Perform Template substitutions on the template
361 TransactionObj => undef,
365 no warnings 'redefine';
366 local $T::Ticket = $args{'TicketObj'};
367 local $T::Transaction = $args{'TransactionObj'};
368 local $T::Argument = $args{'Argument'};
369 local $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name } if $T::Ticket;
370 local $T::rtname = $RT::rtname;
372 local *T::loc = sub {
373 $T::Ticket ? $T::Ticket->loc(@_)
374 : $self->CurrentUser->loc(@_)
377 my $content = $self->Content;
378 unless ( defined $content ) {
379 return ( undef, $self->loc("Permissions denied") );
382 # We need to untaint the content of the template, since we'll be working
384 $content =~ s/^(.*)$/$1/;
385 my $template = Text::Template->new(
391 my $retval = $template->fill_in( PACKAGE => 'T', BROKEN => sub {
393 $RT::Logger->error("Template parsing error: $args{error}")
394 unless $args{error} =~ /^Died at /; # ignore intentional die()
398 return ( undef, $self->loc('Template parsing error') ) if $is_broken;
400 # MIME::Parser has problems dealing with high-bit utf8 data.
401 Encode::_utf8_off($retval);
407 # {{{ sub CurrentUserHasQueueRight
409 =head2 CurrentUserHasQueueRight
411 Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.
415 sub CurrentUserHasQueueRight {
417 return ( $self->QueueObj->CurrentUserHasRight(@_) );