diff options
Diffstat (limited to 'rt/lib/RT/Template.pm')
| -rwxr-xr-x | rt/lib/RT/Template.pm | 512 | 
1 files changed, 272 insertions, 240 deletions
| diff --git a/rt/lib/RT/Template.pm b/rt/lib/RT/Template.pm index f73ea3ed6..3ef96c7df 100755 --- a/rt/lib/RT/Template.pm +++ b/rt/lib/RT/Template.pm @@ -1,363 +1,395 @@ -# BEGIN LICENSE BLOCK -#  -# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com> -#  -# (Except where explictly superceded by other copyright notices) -#  -# This work is made available to you under the terms of Version 2 of -# the GNU General Public License. A copy of that license should have -# been provided with this software, but in any event can be snarfed -# from www.gnu.org. -#  -# This work is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU -# General Public License for more details. -#  -# Unless otherwise specified, all modifications, corrections or -# extensions to this work which alter its source code become the -# property of Best Practical Solutions, LLC when submitted for -# inclusion in the work. -#  -#  -# END LICENSE BLOCK -# Autogenerated by DBIx::SearchBuilder factory (by <jesse@bestpractical.com>) -# WARNING: THIS FILE IS AUTOGENERATED. ALL CHANGES TO THIS FILE WILL BE LOST.   -#  -# !! DO NOT EDIT THIS FILE !! -# - -use strict; - +# $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Template.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $ +# Copyright 1996-2002 Jesse Vincent <jesse@bestpractical.com> +# Portions Copyright 2000 Tobias Brox <tobix@cpan.org>  +# Released under the terms of the GNU General Public License  =head1 NAME -RT::Template - +  RT::Template - RT's template object  =head1 SYNOPSIS -=head1 DESCRIPTION - -=head1 METHODS - -=cut - -package RT::Template; -use RT::Record;  -use RT::Queue; - - -use vars qw( @ISA ); -@ISA= qw( RT::Record ); - -sub _Init { -  my $self = shift;  - -  $self->Table('Templates'); -  $self->SUPER::_Init(@_); -} +  use RT::Template; +=head1 DESCRIPTION +=head1 METHODS -=item Create PARAMHASH +=begin testing -Create takes a hash of values and creates a row in the database: +ok(require RT::TestHarness); +ok(require RT::Template); -  int(11) 'Queue'. -  varchar(200) 'Name'. -  varchar(255) 'Description'. -  varchar(16) 'Type'. -  varchar(16) 'Language'. -  int(11) 'TranslationOf'. -  blob 'Content'. +=end testing  =cut +package RT::Template; +use RT::Record; +use MIME::Entity; +use MIME::Parser; +@ISA = qw(RT::Record); +# {{{ sub _Init -sub Create { +sub _Init {      my $self = shift; -    my %args = (  -                Queue => '0', -                Name => '', -                Description => '', -                Type => '', -                Language => '', -                TranslationOf => '0', -                Content => '', - -		  @_); -    $self->SUPER::Create( -                         Queue => $args{'Queue'}, -                         Name => $args{'Name'}, -                         Description => $args{'Description'}, -                         Type => $args{'Type'}, -                         Language => $args{'Language'}, -                         TranslationOf => $args{'TranslationOf'}, -                         Content => $args{'Content'}, -); - +    $self->{'table'} = "Templates"; +    return ( $self->SUPER::_Init(@_) );  } +# }}} +# {{{ sub _Accessible  -=item id - -Returns the current value of id.  -(In the database, id is stored as int(11).) - - -=cut +sub _Accessible { +    my $self = shift; +    my %Cols = ( +        id            => 'read', +        Name          => 'read/write', +        Description   => 'read/write', +        Type          => 'read/write',    #Type is one of Action or Message +        Content       => 'read/write', +        Queue         => 'read/write', +        Creator       => 'read/auto', +        Created       => 'read/auto', +        LastUpdatedBy => 'read/auto', +        LastUpdated   => 'read/auto' +    ); +    return $self->SUPER::_Accessible( @_, %Cols ); +} +# }}} -=item Queue +# {{{ sub _Set -Returns the current value of Queue.  -(In the database, Queue is stored as int(11).) +sub _Set { +    my $self = shift; +    # use super::value or we get acl blocked +    if ( ( defined $self->SUPER::_Value('Queue') ) +        && ( $self->SUPER::_Value('Queue') == 0 ) ) +    { +        unless ( $self->CurrentUser->HasSystemRight('ModifyTemplate') ) { +            return ( 0, 'Permission Denied' ); +        } +    } +    else { + +        unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) { +            return ( 0, 'Permission Denied' ); +        } +    } +    return ( $self->SUPER::_Set(@_) ); +} -=item SetQueue VALUE +# }}} +# {{{ sub _Value  -Set Queue to VALUE.  -Returns (1, 'Status message') on success and (0, 'Error Message') on failure. -(In the database, Queue will be stored as a int(11).) +=head2 _Value +Takes the name of a table column. +Returns its value as a string, if the user passes an ACL check  =cut +sub _Value { -=item QueueObj - -Returns the Queue Object which has the id returned by Queue +    my $self  = shift; +    my $field = shift; +    #If the current user doesn't have ACLs, don't let em at it.   +    #use super::value or we get acl blocked +    if ( ( !defined $self->__Value('Queue') ) +        || ( $self->__Value('Queue') == 0 ) ) +    { +        unless ( $self->CurrentUser->HasSystemRight('ShowTemplate') ) { +            return (undef); +        } +    } +    else { +        unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) { +            return (undef); +        } +    } +    return ( $self->__Value($field) ); -=cut - -sub QueueObj { -	my $self = shift; -	my $Queue =  RT::Queue->new($self->CurrentUser); -	$Queue->Load($self->__Value('Queue')); -	return($Queue);  } -=item Name - -Returns the current value of Name.  -(In the database, Name is stored as varchar(200).) - - +# }}} -=item SetName VALUE +# {{{ sub Load +=head2 Load <identifer> -Set Name to VALUE.  -Returns (1, 'Status message') on success and (0, 'Error Message') on failure. -(In the database, Name will be stored as a varchar(200).) - +Load a template, either by number or by name  =cut +sub Load { +    my $self       = shift; +    my $identifier = shift; -=item Description - -Returns the current value of Description.  -(In the database, Description is stored as varchar(255).) +    if ( !$identifier ) { +        return (undef); +    } +    if ( $identifier !~ /\D/ ) { +        $self->SUPER::LoadById($identifier); +    } +    else { +        $self->LoadByCol( 'Name', $identifier ); +    } +} -=item SetDescription VALUE +# }}} +# {{{ sub LoadGlobalTemplate -Set Description to VALUE.  -Returns (1, 'Status message') on success and (0, 'Error Message') on failure. -(In the database, Description will be stored as a varchar(255).) +=head2 LoadGlobalTemplate NAME +Load the global tempalte with the name NAME  =cut +sub LoadGlobalTemplate { +    my $self = shift; +    my $id   = shift; -=item Type - -Returns the current value of Type.  -(In the database, Type is stored as varchar(16).) - - +    return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) ); +} -=item SetType VALUE +# }}} +# {{{ sub LoadQueueTemplate -Set Type to VALUE.  -Returns (1, 'Status message') on success and (0, 'Error Message') on failure. -(In the database, Type will be stored as a varchar(16).) +=head2  LoadQueueTemplate (Queue => QUEUEID, Name => NAME) +Loads the Queue template named NAME for Queue QUEUE.  =cut +sub LoadQueueTemplate { +    my $self = shift; +    my %args = ( +        Queue => undef, +        Name  => undef +    ); -=item Language +    return ( $self->LoadByCols( Name => $args{'Name'}, Queue => {'Queue'} ) ); -Returns the current value of Language.  -(In the database, Language is stored as varchar(16).) +} +# }}} +# {{{ sub Create -=item SetLanguage VALUE +=head2 Create +Takes a paramhash of Content, Queue, Name and Description. +Name should be a unique string identifying this Template. +Description and Content should be the template's title and content. +Queue should be 0 for a global template and the queue # for a queue-specific  +template. -Set Language to VALUE.  -Returns (1, 'Status message') on success and (0, 'Error Message') on failure. -(In the database, Language will be stored as a varchar(16).) +Returns the Template's id # if the create was successful. Returns undef for +unknown database failure.  =cut +sub Create { +    my $self = shift; +    my %args = ( +        Content     => undef, +        Queue       => 0, +        Description => '[no description]', +        Type => 'Action',    #By default, template are 'Action' templates +        Name => undef, +        @_ +    ); + +    if ( $args{'Queue'} == 0 ) { +        unless ( $self->CurrentUser->HasSystemRight('ModifyTemplate') ) { +            return (undef); +        } +    } +    else { +        my $QueueObj = new RT::Queue( $self->CurrentUser ); +        $QueueObj->Load( $args{'Queue'} ) || return ( 0, 'Invalid queue' ); + +        unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) { +            return (undef); +        } +    } + +    my $result = $self->SUPER::Create( +        Content => $args{'Content'}, +        Queue   => $args{'Queue'}, +        , +        Description => $args{'Description'}, +        Name        => $args{'Name'} +    ); + +    return ($result); -=item TranslationOf - -Returns the current value of TranslationOf.  -(In the database, TranslationOf is stored as int(11).) - - +} -=item SetTranslationOf VALUE +# }}} +# {{{ sub Delete -Set TranslationOf to VALUE.  -Returns (1, 'Status message') on success and (0, 'Error Message') on failure. -(In the database, TranslationOf will be stored as a int(11).) +=head2 Delete +Delete this template.  =cut +sub Delete { +    my $self = shift; -=item Content +    unless ( $self->CurrentUserHasRight('ModifyTemplate') ) { +        return ( 0, 'Permission Denied' ); +    } -Returns the current value of Content.  -(In the database, Content is stored as blob.) +    return ( $self->SUPER::Delete(@_) ); +} +# }}} +# {{{ sub MIMEObj +sub MIMEObj { +    my $self = shift; +    return ( $self->{'MIMEObj'} ); +} -=item SetContent VALUE +# }}} +# {{{ sub Parse  -Set Content to VALUE.  -Returns (1, 'Status message') on success and (0, 'Error Message') on failure. -(In the database, Content will be stored as a blob.) +=item Parse + This routine performs Text::Template parsing on thte template and then imports the  + results into a MIME::Entity so we can really use it + It returns a tuple of (val, message) + If val is 0, the message contains an error message  =cut +sub Parse { +    my $self = shift; -=item LastUpdated - -Returns the current value of LastUpdated.  -(In the database, LastUpdated is stored as datetime.) - +    #We're passing in whatever we were passed. it's destined for _ParseContent +    my $content = $self->_ParseContent(@_); -=cut +    #Lets build our mime Entity +    my $parser = MIME::Parser->new(); +     +    # Do work on the parsed template in memory, rather than on disk +    $parser->output_to_core(1);  -=item LastUpdatedBy +    ### Should we forgive normally-fatal errors? +    $parser->ignore_errors(1); +    $self->{'MIMEObj'} = eval { $parser->parse_data($content) }; +    $error = ( $@ || $parser->last_error ); -Returns the current value of LastUpdatedBy.  -(In the database, LastUpdatedBy is stored as int(11).) +    if ($error) { +        $RT::Logger->error("$error"); +        return ( 0, $error ); +    } +    # Unfold all headers +    $self->{'MIMEObj'}->head->unfold(); -=cut +    return ( 1, "Template parsed" ); +    +} -=item Creator +# }}} -Returns the current value of Creator.  -(In the database, Creator is stored as int(11).) +# {{{ sub _ParseContent +# Perform Template substitutions on the template -=cut +sub _ParseContent { +    my $self = shift; +    my %args = ( +        Argument       => undef, +        TicketObj      => undef, +        TransactionObj => undef, +        @_ +    ); + +    # Might be subject to change +    use Text::Template; + +    $T::Ticket      = $args{'TicketObj'}; +    $T::Transaction = $args{'TransactionObj'}; +    $T::Argument    = $args{'Argument'}; +    $T::rtname      = $RT::rtname; + +    # We need to untaint the content of the template, since we'll be working +    # with it +    my $content = $self->Content(); +    $content =~ s/^(.*)$/$1/; +    $template = Text::Template->new( +        TYPE   => STRING, +        SOURCE => $content +    ); + +    my $retval = $template->fill_in( PACKAGE => T ); +    return ($retval); +} +# }}} -=item Created +# {{{ sub QueueObj -Returns the current value of Created.  -(In the database, Created is stored as datetime.) +=head2 QueueObj +Takes nothing. returns this ticket's queue object  =cut +sub QueueObj { +    my $self = shift; +    if ( !defined $self->{'queue'} ) { +        require RT::Queue; +        $self->{'queue'} = RT::Queue->new( $self->CurrentUser ); + +        unless ( $self->{'queue'} ) { +            $RT::Logger->crit( +                "RT::Queue->new(" . $self->CurrentUser . ") returned false" ); +            return (undef); +        } +        my ($result) = $self->{'queue'}->Load( $self->__Value('Queue') ); + +    } +    return ( $self->{'queue'} ); +} +# }}} -sub _ClassAccessible { -    { -      -        id => -		{read => 1, type => 'int(11)', default => ''}, -        Queue =>  -		{read => 1, write => 1, type => 'int(11)', default => '0'}, -        Name =>  -		{read => 1, write => 1, type => 'varchar(200)', default => ''}, -        Description =>  -		{read => 1, write => 1, type => 'varchar(255)', default => ''}, -        Type =>  -		{read => 1, write => 1, type => 'varchar(16)', default => ''}, -        Language =>  -		{read => 1, write => 1, type => 'varchar(16)', default => ''}, -        TranslationOf =>  -		{read => 1, write => 1, type => 'int(11)', default => '0'}, -        Content =>  -		{read => 1, write => 1, type => 'blob', default => ''}, -        LastUpdated =>  -		{read => 1, auto => 1, type => 'datetime', default => ''}, -        LastUpdatedBy =>  -		{read => 1, auto => 1, type => 'int(11)', default => '0'}, -        Creator =>  -		{read => 1, auto => 1, type => 'int(11)', default => '0'}, -        Created =>  -		{read => 1, auto => 1, type => 'datetime', default => ''}, - - } -}; - - -        eval "require RT::Template_Overlay"; -        if ($@ && $@ !~ qr{^Can't locate RT/Template_Overlay.pm}) { -            die $@; -        }; - -        eval "require RT::Template_Vendor"; -        if ($@ && $@ !~ qr{^Can't locate RT/Template_Vendor.pm}) { -            die $@; -        }; - -        eval "require RT::Template_Local"; -        if ($@ && $@ !~ qr{^Can't locate RT/Template_Local.pm}) { -            die $@; -        }; - - - - -=head1 SEE ALSO - -This class allows "overlay" methods to be placed -into the following files _Overlay is for a System overlay by the original author, -_Vendor is for 3rd-party vendor add-ons, while _Local is for site-local customizations.   - -These overlay files can contain new subs or subs to replace existing subs in this module. - -If you'll be working with perl 5.6.0 or greater, each of these files should begin with the line  - -   no warnings qw(redefine); +# {{{ sub CurrentUserHasQueueRight -so that perl does not kick and scream when you redefine a subroutine or variable in your overlay. +=head2 CurrentUserHasQueueRight -RT::Template_Overlay, RT::Template_Vendor, RT::Template_Local +Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.  =cut +sub CurrentUserHasQueueRight { +    my $self = shift; +    return ( $self->QueueObj->CurrentUserHasRight(@_) ); +} +# }}}  1; | 
