TimeWorked-like custom fields, RT#11168
[freeside.git] / rt / lib / RT / Transaction_Overlay.pm
index e51e5b5..d4b1ab2 100644 (file)
@@ -1,40 +1,40 @@
 # BEGIN BPS TAGGED BLOCK {{{
-# 
+#
 # COPYRIGHT:
-# 
-# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
-#                                          <jesse@bestpractical.com>
-# 
+#
+# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
+#                                          <sales@bestpractical.com>
+#
 # (Except where explicitly superseded by other copyright notices)
-# 
-# 
+#
+#
 # LICENSE:
-# 
+#
 # 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.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301 or visit their web page on the internet at
 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
-# 
-# 
+#
+#
 # CONTRIBUTION SUBMISSION POLICY:
-# 
+#
 # (The following paragraph is not intended to limit the rights granted
 # to you to modify and distribute this software under the terms of
 # the GNU General Public License and is only of importance to you if
 # you choose to contribute your changes and enhancements to the
 # community by submitting them to Best Practical Solutions, LLC.)
-# 
+#
 # By intentionally submitting any modifications, corrections or
 # derivatives to this work, or any other work intended for use with
 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
@@ -43,7 +43,7 @@
 # royalty-free, perpetual, license to use, copy, create derivative
 # works based on those contributions, and sublicense and distribute
 # those contributions and any derivatives thereof.
-# 
+#
 # END BPS TAGGED BLOCK }}}
 
 =head1 NAME
@@ -110,12 +110,13 @@ sub Create {
         NewValue       => undef,
         MIMEObj        => undef,
         ActivateScrips => 1,
-        CommitScrips => 1,
-       ObjectType => 'RT::Ticket',
-       ObjectId => 0,
-       ReferenceType => undef,
-        OldReference       => undef,
-        NewReference       => undef,
+        CommitScrips   => 1,
+       ObjectType     => 'RT::Ticket',
+       ObjectId       => 0,
+       ReferenceType  => undef,
+        OldReference   => undef,
+        NewReference   => undef,
+        CustomFields   => {},
         @_
     );
 
@@ -130,17 +131,17 @@ sub Create {
 
     #lets create our transaction
     my %params = (
-        Type      => $args{'Type'},
-        Data      => $args{'Data'},
-        Field     => $args{'Field'},
-        OldValue  => $args{'OldValue'},
-        NewValue  => $args{'NewValue'},
-        Created   => $args{'Created'},
-       ObjectType => $args{'ObjectType'},
-       ObjectId => $args{'ObjectId'},
+        Type          => $args{'Type'},
+        Data          => $args{'Data'},
+        Field         => $args{'Field'},
+        OldValue      => $args{'OldValue'},
+        NewValue      => $args{'NewValue'},
+        Created       => $args{'Created'},
+       ObjectType    => $args{'ObjectType'},
+       ObjectId      => $args{'ObjectId'},
        ReferenceType => $args{'ReferenceType'},
-       OldReference => $args{'OldReference'},
-       NewReference => $args{'NewReference'},
+       OldReference  => $args{'OldReference'},
+       NewReference  => $args{'NewReference'},
     );
 
     # Parameters passed in during an import that we probably don't want to touch, otherwise
@@ -158,6 +159,10 @@ sub Create {
         }
     }
 
+    # Set up any custom fields passed at creation.  Has to happen 
+    # before scrips.
+    
+    $self->UpdateCustomFields(%{ $args{'CustomFields'} });
 
     #Provide a way to turn off scrips if we need to
         $RT::Logger->debug('About to think about scrips for transaction #' .$self->Id);
@@ -176,7 +181,7 @@ sub Create {
        # Entry point of the rule system
        my $ticket = RT::Ticket->new($RT::SystemUser);
        $ticket->Load($args{'ObjectId'});
-       my $rules = RT::Ruleset->FindAllRules(
+       my $rules = $self->{rules} = RT::Ruleset->FindAllRules(
             Stage       => 'TransactionCreate',
             Type        => $args{'Type'},
             TicketObj   => $ticket,
@@ -211,6 +216,22 @@ sub Scrips {
 }
 
 
+=head2 Rules
+
+Returns the array of Rule objects for this transaction.
+This routine is only useful on a freshly created transaction object.
+Rules do not get persisted to the database with transactions.
+
+
+=cut
+
+
+sub Rules {
+    my $self = shift;
+    return($self->{'rules'});
+}
+
+
 # {{{ sub Delete
 
 =head2 Delete
@@ -292,14 +313,14 @@ If $args{'Type'} is set to C<text/html>, this will return an HTML
 part of the message, if available.  Otherwise it looks for a text/plain
 part. If $args{'Type'} is missing, it defaults to the value of 
 C<$RT::Transaction::PreferredContentType>, if that's missing too, 
-defaults to 'text/plain'.
+defaults to textual.
 
 =cut
 
 sub Content {
     my $self = shift;
     my %args = (
-        Type  => $PreferredContentType || 'text/plain',
+        Type => $PreferredContentType || '',
         Quote => 0,
         Wrap  => 70,
         Wrap  => ( $RT::MessageBoxWidth || 72 ) - 2,
@@ -307,7 +328,9 @@ sub Content {
     );
 
     my $content;
-    if ( my $content_obj = $self->ContentObj( Type => $args{Type} ) ) {
+    if ( my $content_obj =
+        $self->ContentObj( $args{Type} ? ( Type => $args{Type} ) : () ) )
+    {
         $content = $content_obj->Content ||'';
 
         if ( lc $content_obj->ContentType eq 'text/html' ) {
@@ -398,12 +421,16 @@ Returns the RT::Attachment object which contains the content for this Transactio
 
 sub ContentObj {
     my $self = shift;
-    my %args = ( Type => $PreferredContentType || 'text/plain',
-                 @_ );
+    my %args = ( Type => $PreferredContentType, Attachment => undef, @_ );
 
     # If we don't have any content, return undef now.
     # Get the set of toplevel attachments to this transaction.
-    return undef unless my $Attachment = $self->Attachments->First;
+
+    my $Attachment = $args{'Attachment'};
+
+    $Attachment ||= $self->Attachments->First;
+
+    return undef unless ($Attachment);
 
     # If it's a textual part, just return the body.
     if ( RT::I18N::IsTextualContentType($Attachment->ContentType) ) {
@@ -413,14 +440,23 @@ sub ContentObj {
     # If it's a multipart object, first try returning the first part with preferred
     # MIME type ('text/plain' by default).
 
-    elsif ( $Attachment->ContentType =~ '^multipart/' ) {
-        my $plain_parts = $Attachment->Children;
-        $plain_parts->ContentType( VALUE => $args{Type} );
-        $plain_parts->LimitNotEmpty;
-
-        # If we actully found a part, return its content
-        if ( my $first = $plain_parts->First ) {
-            return $first;
+    elsif ( $Attachment->ContentType =~ qr|^multipart/mixed|i ) {
+        my $kids = $Attachment->Children;
+        while (my $child = $kids->Next) {
+            my $ret =  $self->ContentObj(%args, Attachment => $child);
+            return $ret if ($ret);
+        }
+    }
+    elsif ( $Attachment->ContentType =~ qr|^multipart/|i ) {
+        if ( $args{Type} ) {
+            my $plain_parts = $Attachment->Children;
+            $plain_parts->ContentType( VALUE => $args{Type} );
+            $plain_parts->LimitNotEmpty;
+
+            # If we actully found a part, return its content
+            if ( my $first = $plain_parts->First ) {
+                return $first;
+            }
         }
 
         # If that fails, return the first textual part which has some content.
@@ -527,6 +563,8 @@ sub ContentAsMIME {
     my $self = shift;
 
     my $main_content = $self->ContentObj;
+    return unless $main_content;
+
     my $entity = $main_content->ContentAsMIME;
 
     if ( $main_content->Parent ) {
@@ -558,11 +596,7 @@ sub ContentAsMIME {
         OPERATOR => 'NOT STARTSWITH',
         VALUE => 'multipart/',
     );
-    $attachments->Limit(
-        FIELD => 'Content',
-        OPERATOR => '!=',
-        VALUE => '',
-    );
+    $attachments->LimitNotEmpty;
     while ( my $a = $attachments->Next ) {
         $entity->make_multipart unless $entity->is_multipart;
         $entity->add_part( $a->ContentAsMIME );
@@ -656,6 +690,9 @@ sub BriefDescription {
             )
         );
     }
+    elsif ( $type =~ /SystemError/ ) {
+        return $self->loc("System error");
+    }
 
     if ( my $code = $_BriefDescriptions{$type} ) {
         return $code->($self);