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 # Setup output directory for files. from RT::EmailParser::_SetupMIMEParser
322 if ( my $AttachmentDir =
323 eval { File::Temp::tempdir( TMPDIR => 1, CLEANUP => 1 ) } )
326 # Set up output directory for files:
327 $parser->output_dir("$AttachmentDir");
330 $RT::Logger->error("Couldn't write attachments to temp dir on disk. using more memory and processor.");
331 # On some situations TMPDIR is non-writable. sad but true.
332 $parser->output_to_core(1);
333 $parser->tmp_to_core(1);
336 #If someone includes a message, don't extract it
337 $parser->extract_nested_messages(1);
339 # Set up the prefix for files with auto-generated names:
340 $parser->output_prefix("part");
342 # If content length is <= 50000 bytes, store each msg as in-core scalar;
343 # Else, write to a disk file (the default action):
344 $parser->output_to_core(50000);
346 ### Should we forgive normally-fatal errors?
347 $parser->ignore_errors(1);
348 $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
349 my $error = ( $@ || $parser->last_error );
352 $RT::Logger->error("$error");
353 return ( 0, $error );
357 $self->{'MIMEObj'}->head->unfold();
359 return ( 1, $self->loc("Template parsed") );
365 # {{{ sub _ParseContent
367 # Perform Template substitutions on the template
374 TransactionObj => undef,
378 no warnings 'redefine';
379 $T::Ticket = $args{'TicketObj'};
380 $T::Transaction = $args{'TransactionObj'};
381 $T::Argument = $args{'Argument'};
382 $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name };
383 $T::rtname = $RT::rtname;
384 *T::loc = sub { $T::Ticket->loc(@_) };
386 # We need to untaint the content of the template, since we'll be working
388 my $content = $self->Content();
389 $content =~ s/^(.*)$/$1/;
390 my $template = Text::Template->new(
395 my $retval = $template->fill_in( PACKAGE => 'T' );
397 # MIME::Parser has problems dealing with high-bit utf8 data.
398 Encode::_utf8_off($retval);
404 # {{{ sub CurrentUserHasQueueRight
406 =head2 CurrentUserHasQueueRight
408 Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.
412 sub CurrentUserHasQueueRight {
414 return ( $self->QueueObj->CurrentUserHasRight(@_) );