1 # $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Template.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
2 # Copyright 1996-2002 Jesse Vincent <jesse@bestpractical.com>
3 # Portions Copyright 2000 Tobias Brox <tobix@cpan.org>
4 # Released under the terms of the GNU General Public License
8 RT::Template - RT's template object
22 ok(require RT::TestHarness);
23 ok(require RT::Template);
34 @ISA = qw(RT::Record);
40 $self->{'table'} = "Templates";
41 return ( $self->SUPER::_Init(@_) );
53 Description => 'read/write',
54 Type => 'read/write', #Type is one of Action or Message
55 Content => 'read/write',
56 Queue => 'read/write',
57 Creator => 'read/auto',
58 Created => 'read/auto',
59 LastUpdatedBy => 'read/auto',
60 LastUpdated => 'read/auto'
62 return $self->SUPER::_Accessible( @_, %Cols );
72 # use super::value or we get acl blocked
73 if ( ( defined $self->SUPER::_Value('Queue') )
74 && ( $self->SUPER::_Value('Queue') == 0 ) )
76 unless ( $self->CurrentUser->HasSystemRight('ModifyTemplate') ) {
77 return ( 0, 'Permission Denied' );
82 unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
83 return ( 0, 'Permission Denied' );
86 return ( $self->SUPER::_Set(@_) );
96 Takes the name of a table column.
97 Returns its value as a string, if the user passes an ACL check
106 #If the current user doesn't have ACLs, don't let em at it.
107 #use super::value or we get acl blocked
108 if ( ( !defined $self->__Value('Queue') )
109 || ( $self->__Value('Queue') == 0 ) )
111 unless ( $self->CurrentUser->HasSystemRight('ShowTemplate') ) {
116 unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
120 return ( $self->__Value($field) );
128 =head2 Load <identifer>
130 Load a template, either by number or by name
136 my $identifier = shift;
138 if ( !$identifier ) {
142 if ( $identifier !~ /\D/ ) {
143 $self->SUPER::LoadById($identifier);
146 $self->LoadByCol( 'Name', $identifier );
153 # {{{ sub LoadGlobalTemplate
155 =head2 LoadGlobalTemplate NAME
157 Load the global tempalte with the name NAME
161 sub LoadGlobalTemplate {
165 return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
170 # {{{ sub LoadQueueTemplate
172 =head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
174 Loads the Queue template named NAME for Queue QUEUE.
178 sub LoadQueueTemplate {
185 return ( $self->LoadByCols( Name => $args{'Name'}, Queue => {'Queue'} ) );
195 Takes a paramhash of Content, Queue, Name and Description.
196 Name should be a unique string identifying this Template.
197 Description and Content should be the template's title and content.
198 Queue should be 0 for a global template and the queue # for a queue-specific
201 Returns the Template's id # if the create was successful. Returns undef for
202 unknown database failure.
212 Description => '[no description]',
213 Type => 'Action', #By default, template are 'Action' templates
218 if ( $args{'Queue'} == 0 ) {
219 unless ( $self->CurrentUser->HasSystemRight('ModifyTemplate') ) {
224 my $QueueObj = new RT::Queue( $self->CurrentUser );
225 $QueueObj->Load( $args{'Queue'} ) || return ( 0, 'Invalid queue' );
227 unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
232 my $result = $self->SUPER::Create(
233 Content => $args{'Content'},
234 Queue => $args{'Queue'},
236 Description => $args{'Description'},
237 Name => $args{'Name'}
250 Delete this template.
257 unless ( $self->CurrentUserHasRight('ModifyTemplate') ) {
258 return ( 0, 'Permission Denied' );
261 return ( $self->SUPER::Delete(@_) );
269 return ( $self->{'MIMEObj'} );
278 This routine performs Text::Template parsing on thte template and then imports the
279 results into a MIME::Entity so we can really use it
280 It returns a tuple of (val, message)
281 If val is 0, the message contains an error message
288 #We're passing in whatever we were passed. it's destined for _ParseContent
289 my $content = $self->_ParseContent(@_);
291 #Lets build our mime Entity
293 my $parser = MIME::Parser->new();
295 # Do work on the parsed template in memory, rather than on disk
296 $parser->output_to_core(1);
298 ### Should we forgive normally-fatal errors?
299 $parser->ignore_errors(1);
300 $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
301 $error = ( $@ || $parser->last_error );
304 $RT::Logger->error("$error");
305 return ( 0, $error );
309 $self->{'MIMEObj'}->head->unfold();
311 return ( 1, "Template parsed" );
318 # {{{ sub _ParseContent
320 # Perform Template substitutions on the template
327 TransactionObj => undef,
331 # Might be subject to change
334 $T::Ticket = $args{'TicketObj'};
335 $T::Transaction = $args{'TransactionObj'};
336 $T::Argument = $args{'Argument'};
337 $T::rtname = $RT::rtname;
339 # We need to untaint the content of the template, since we'll be working
341 my $content = $self->Content();
342 $content =~ s/^(.*)$/$1/;
343 $template = Text::Template->new(
348 my $retval = $template->fill_in( PACKAGE => T );
358 Takes nothing. returns this ticket's queue object
364 if ( !defined $self->{'queue'} ) {
366 $self->{'queue'} = RT::Queue->new( $self->CurrentUser );
368 unless ( $self->{'queue'} ) {
370 "RT::Queue->new(" . $self->CurrentUser . ") returned false" );
373 my ($result) = $self->{'queue'}->Load( $self->__Value('Queue') );
376 return ( $self->{'queue'} );
381 # {{{ sub CurrentUserHasQueueRight
383 =head2 CurrentUserHasQueueRight
385 Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.
389 sub CurrentUserHasQueueRight {
391 return ( $self->QueueObj->CurrentUserHasRight(@_) );