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 {
209 return ( $self->LoadByCols( Name => $args{'Name'}, Queue => {'Queue'} ) );
219 Takes a paramhash of Content, Queue, Name and Description.
220 Name should be a unique string identifying this Template.
221 Description and Content should be the template's title and content.
222 Queue should be 0 for a global template and the queue # for a queue-specific
225 Returns the Template's id # if the create was successful. Returns undef for
226 unknown database failure.
236 Description => '[no description]',
237 Type => 'Action', #By default, template are 'Action' templates
242 if ( !$args{'Queue'} ) {
243 unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
249 my $QueueObj = new RT::Queue( $self->CurrentUser );
250 $QueueObj->Load( $args{'Queue'} ) || return ( 0, $self->loc('Invalid queue') );
252 unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
255 $args{'Queue'} = $QueueObj->Id;
258 my $result = $self->SUPER::Create(
259 Content => $args{'Content'},
260 Queue => $args{'Queue'},
261 Description => $args{'Description'},
262 Name => $args{'Name'}
275 Delete this template.
282 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
283 return ( 0, $self->loc('Permission Denied') );
286 return ( $self->SUPER::Delete(@_) );
294 return ( $self->{'MIMEObj'} );
303 This routine performs Text::Template parsing on the template and then
304 imports the results into a MIME::Entity so we can really use it
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 = $self->_ParseContent(@_);
316 #Lets build our mime Entity
318 my $parser = MIME::Parser->new();
320 # Setup output directory for files. from RT::EmailParser::_SetupMIMEParser
321 if (my $AttachmentDir = eval { File::Temp::tempdir( TMPDIR => 1, CLEANUP => 1 ) }) {
322 # Set up output directory for files:
323 $parser->output_dir("$AttachmentDir");
326 # On some situations TMPDIR is non-writable. sad but true.
327 $parser->output_to_core(1);
328 $parser->tmp_to_core(1);
330 #If someone includes a message, don't extract it
331 $parser->extract_nested_messages(1);
332 # Set up the prefix for files with auto-generated names:
333 $parser->output_prefix("part");
334 # If content length is <= 50000 bytes, store each msg as in-core scalar;
335 # Else, write to a disk file (the default action):
336 $parser->output_to_core(50000);
339 ### Should we forgive normally-fatal errors?
340 $parser->ignore_errors(1);
341 $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
342 my $error = ( $@ || $parser->last_error );
345 $RT::Logger->error("$error");
346 return ( 0, $error );
350 $self->{'MIMEObj'}->head->unfold();
352 return ( 1, $self->loc("Template parsed") );
359 # {{{ sub _ParseContent
361 # Perform Template substitutions on the template
368 TransactionObj => undef,
373 $T::Ticket = $args{'TicketObj'};
374 $T::Transaction = $args{'TransactionObj'};
375 $T::Argument = $args{'Argument'};
376 $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name };
377 $T::rtname = $RT::rtname;
379 # We need to untaint the content of the template, since we'll be working
381 my $content = $self->Content();
382 $content =~ s/^(.*)$/$1/;
383 my $template = Text::Template->new(
388 my $retval = $template->fill_in( PACKAGE => 'T' );
390 # MIME::Parser has problems dealing with high-bit utf8 data.
391 Encode::_utf8_off($retval);
397 # {{{ sub CurrentUserHasQueueRight
399 =head2 CurrentUserHasQueueRight
401 Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.
405 sub CurrentUserHasQueueRight {
407 return ( $self->QueueObj->CurrentUserHasRight(@_) );