1 # {{{ BEGIN BPS TAGGED BLOCK
5 # This software is Copyright (c) 1996-2004 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);
70 no warnings qw(redefine);
75 use File::Temp qw /tempdir/;
85 Description => 'read/write',
86 Type => 'read/write', #Type is one of Action or Message
87 Content => 'read/write',
88 Queue => 'read/write',
89 Creator => 'read/auto',
90 Created => 'read/auto',
91 LastUpdatedBy => 'read/auto',
92 LastUpdated => 'read/auto'
94 return $self->SUPER::_Accessible( @_, %Cols );
104 # use super::value or we get acl blocked
105 if ( ( defined $self->SUPER::_Value('Queue') )
106 && ( $self->SUPER::_Value('Queue') == 0 ) )
108 unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ModifyTemplate') ) {
109 return ( 0, $self->loc('Permission Denied') );
114 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
115 return ( 0, $self->loc('Permission Denied') );
118 return ( $self->SUPER::_Set(@_) );
128 Takes the name of a table column.
129 Returns its value as a string, if the user passes an ACL check
134 my $t = RT::Template->new($RT::SystemUser);
135 $t->Create(Name => "Foo", Queue => 1);
136 my $t2 = RT::Template->new($RT::Nobody);
138 ok($t2->QueueObj->id, "Got the template's queue objet");
152 #If the current user doesn't have ACLs, don't let em at it.
153 #use super::value or we get acl blocked
154 if ( ( !defined $self->__Value('Queue') )
155 || ( $self->__Value('Queue') == 0 ) )
157 unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ShowTemplate') ) {
162 unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
166 return ( $self->__Value($field) );
174 =head2 Load <identifer>
176 Load a template, either by number or by name
182 my $identifier = shift;
184 if ( !$identifier ) {
188 if ( $identifier !~ /\D/ ) {
189 $self->SUPER::LoadById($identifier);
192 $self->LoadByCol( 'Name', $identifier );
199 # {{{ sub LoadGlobalTemplate
201 =head2 LoadGlobalTemplate NAME
203 Load the global tempalte with the name NAME
207 sub LoadGlobalTemplate {
211 return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
216 # {{{ sub LoadQueueTemplate
218 =head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
220 Loads the Queue template named NAME for Queue QUEUE.
224 sub LoadQueueTemplate {
232 return ( $self->LoadByCols( Name => $args{'Name'}, Queue => $args{'Queue'} ) );
242 Takes a paramhash of Content, Queue, Name and Description.
243 Name should be a unique string identifying this Template.
244 Description and Content should be the template's title and content.
245 Queue should be 0 for a global template and the queue # for a queue-specific
248 Returns the Template's id # if the create was successful. Returns undef for
249 unknown database failure.
259 Description => '[no description]',
260 Type => 'Action', #By default, template are 'Action' templates
265 if ( !$args{'Queue'} ) {
266 unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
272 my $QueueObj = new RT::Queue( $self->CurrentUser );
273 $QueueObj->Load( $args{'Queue'} ) || return ( 0, $self->loc('Invalid queue') );
275 unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
278 $args{'Queue'} = $QueueObj->Id;
281 my $result = $self->SUPER::Create(
282 Content => $args{'Content'},
283 Queue => $args{'Queue'},
284 Description => $args{'Description'},
285 Name => $args{'Name'}
298 Delete this template.
305 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
306 return ( 0, $self->loc('Permission Denied') );
309 return ( $self->SUPER::Delete(@_) );
317 return ( $self->{'MIMEObj'} );
326 This routine performs Text::Template parsing on the template and then
327 imports the results into a MIME::Entity so we can really use it
328 It returns a tuple of (val, message)
329 If val is 0, the message contains an error message
336 #We're passing in whatever we were passed. it's destined for _ParseContent
337 my $content = $self->_ParseContent(@_);
339 #Lets build our mime Entity
341 my $parser = MIME::Parser->new();
343 # On some situations TMPDIR is non-writable. sad but true.
344 $parser->output_to_core(1);
345 $parser->tmp_to_core(1);
347 #If someone includes a message, don't extract it
348 $parser->extract_nested_messages(1);
350 # Set up the prefix for files with auto-generated names:
351 $parser->output_prefix("part");
353 # If content length is <= 50000 bytes, store each msg as in-core scalar;
354 # Else, write to a disk file (the default action):
355 $parser->output_to_core(50000);
357 ### Should we forgive normally-fatal errors?
358 $parser->ignore_errors(1);
359 $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
360 my $error = ( $@ || $parser->last_error );
363 $RT::Logger->error("$error");
364 return ( 0, $error );
368 $self->{'MIMEObj'}->head->unfold();
370 return ( 1, $self->loc("Template parsed") );
376 # {{{ sub _ParseContent
378 # Perform Template substitutions on the template
385 TransactionObj => undef,
389 no warnings 'redefine';
390 $T::Ticket = $args{'TicketObj'};
391 $T::Transaction = $args{'TransactionObj'};
392 $T::Argument = $args{'Argument'};
393 $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name };
394 $T::rtname = $RT::rtname;
395 *T::loc = sub { $T::Ticket->loc(@_) };
397 # We need to untaint the content of the template, since we'll be working
399 my $content = $self->Content();
400 $content =~ s/^(.*)$/$1/;
401 my $template = Text::Template->new(
406 my $retval = $template->fill_in( PACKAGE => 'T' );
408 # MIME::Parser has problems dealing with high-bit utf8 data.
409 Encode::_utf8_off($retval);
415 # {{{ sub CurrentUserHasQueueRight
417 =head2 CurrentUserHasQueueRight
419 Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.
423 sub CurrentUserHasQueueRight {
425 return ( $self->QueueObj->CurrentUserHasRight(@_) );