import torrus 1.0.9
[freeside.git] / rt / lib / RT / Tickets_Overlay.pm
index 8faf3f0..61f2db0 100644 (file)
@@ -146,6 +146,9 @@ our %FIELD_METADATA = (
     WatcherGroup     => [ 'MEMBERSHIPFIELD', ], #loc_left_pair
     HasAttribute     => [ 'HASATTRIBUTE', 1 ],
     HasNoAttribute     => [ 'HASATTRIBUTE', 0 ],
+    Agentnum         => [ 'FREESIDEFIELD', ],
+    Classnum         => [ 'FREESIDEFIELD', ],
+    Tagnum           => [ 'FREESIDEFIELD', 'cust_tag' ],
 );
 
 # Mapping of Field Type to Function
@@ -163,6 +166,7 @@ our %dispatch = (
     CUSTOMFIELD     => \&_CustomFieldLimit,
     DATECUSTOMFIELD => \&_DateCustomFieldLimit,
     HASATTRIBUTE    => \&_HasAttributeLimit,
+    FREESIDEFIELD   => \&_FreesideFieldLimit,
 );
 our %can_bundle = ();# WATCHERFIELD => "yes", );
 
@@ -1697,7 +1701,6 @@ sub _HasAttributeLimit {
     );
 }
 
-
 # End Helper Functions
 
 # End of SQL Stuff -------------------------------------------------
@@ -1724,7 +1727,6 @@ sub OrderByCols {
             next;
         }
         if ( $row->{FIELD} !~ /\./ ) {
-
             my $meta = $self->FIELDS->{ $row->{FIELD} };
             unless ( $meta ) {
                 push @res, $row;
@@ -1833,54 +1835,26 @@ sub OrderByCols {
            push @res, { %$row, FIELD => "Priority", ORDER => $order } ;
 
        } elsif ( $field eq 'Customer' ) { #Freeside
-
-           my $linkalias = $self->Join(
-               TYPE   => 'LEFT',
-               ALIAS1 => 'main',
-               FIELD1 => 'id',
-               TABLE2 => 'Links',
-               FIELD2 => 'LocalBase'
-           );
-
-           $self->SUPER::Limit(
-               LEFTJOIN => $linkalias,
-               FIELD    => 'Type',
-               OPERATOR => '=',
-               VALUE    => 'MemberOf',
-           );
-           $self->SUPER::Limit(
-               LEFTJOIN => $linkalias,
-               FIELD    => 'Target',
-               OPERATOR => 'STARTSWITH',
-               VALUE    => 'freeside://freeside/cust_main/',
-           );
-
            if ( $subkey eq 'Number' ) {
-
+               my ($linkalias, $custnum_sql) = $self->JoinToCustLinks;
                push @res, { %$row,
-                            ALIAS => $linkalias,
-                            FIELD => "CAST(SUBSTR(Target,31) AS INTEGER)",
-                            #ORDER => ($row->{ORDER} || 'ASC')
-                          };
-
-           } elsif ( $subkey eq 'Name' ) {
-
-              my $custalias = $self->Join(
-                  TYPE   => 'LEFT',
-                  #ALIAS1 => $linkalias,
-                  #FIELD1 => 'CAST(SUBSTR(Target,31) AS INTEGER)',
-                  EXPRESSION => "CAST(SUBSTR($linkalias.Target,31) AS INTEGER)",
-                  TABLE2 => 'cust_main',
-                  FIELD2 => 'custnum',
-                  
-              );
-
-              my $field = "COALESCE( $custalias.company,
-                                     $custalias.last || ', ' || $custalias.first
-                                   )";
-
-              push @res, { %$row, ALIAS => '', FIELD => $field };
-
+                            ALIAS => '',
+                            FIELD => $custnum_sql,
+                        };
+           }
+           else {
+               my $custalias = $self->JoinToCustomer;
+               my $field;
+               if ( $subkey eq 'Name' ) {
+                   $field = "COALESCE( $custalias.company,
+                   $custalias.last || ', ' || $custalias.first
+                   )";
+               }
+               else {
+                   # no other cases exist yet, but for obviousness:
+                   $field = $subkey;
+               }
+               push @res, { %$row, ALIAS => '', FIELD => $field };
            }
 
        } #Freeside
@@ -1892,55 +1866,102 @@ sub OrderByCols {
     return $self->SUPER::OrderByCols(@res);
 }
 
-# }}}
+#Freeside
 
-#this duplicates/ovverrides the DBIx::SearchBuilder version..
-# we need to fix the "handle FUNCTION(FIELD)" stuff and this is much easier
-# than patching SB
-# but does this have other terrible ramifications?  maybe a flag to trigger
-# this specific case?
-sub _OrderClause {
+sub JoinToCustLinks {
+    # Set up join to links (id = localbase),
+    # limit link type to 'MemberOf',
+    # and target value to any Freeside custnum URI.
+    # Return the linkalias for further join/limit action,
+    # and an sql expression to retrieve the custnum.
     my $self = shift;
+    my $linkalias = $self->Join(
+        TYPE   => 'LEFT',
+        ALIAS1 => 'main',
+        FIELD1 => 'id',
+        TABLE2 => 'Links',
+        FIELD2 => 'LocalBase',
+    );
 
-    return '' unless $self->{'order_by'};
-
-    my $clause = '';
-    foreach my $row ( @{$self->{'order_by'}} ) {
-
-        my %rowhash = ( ALIAS => 'main',
-                        FIELD => undef,
-                        ORDER => 'ASC',
-                        %$row
-                      );
-        if ($rowhash{'ORDER'} && $rowhash{'ORDER'} =~ /^des/i) {
-            $rowhash{'ORDER'} = "DESC";
-        }
-        else {
-            $rowhash{'ORDER'} = "ASC";
-        }
-        $rowhash{'ALIAS'} = 'main' unless defined $rowhash{'ALIAS'};
+    $self->SUPER::Limit(
+        LEFTJOIN => $linkalias,
+        FIELD    => 'Type',
+        OPERATOR => '=',
+        VALUE    => 'MemberOf',
+    );
+    $self->SUPER::Limit(
+        LEFTJOIN => $linkalias,
+        FIELD    => 'Target',
+        OPERATOR => 'STARTSWITH',
+        VALUE    => 'freeside://freeside/cust_main/',
+    );
+    my $custnum_sql = "CAST(SUBSTR($linkalias.Target,31) AS ";
+    if ( RT->Config->Get('DatabaseType') eq 'mysql' ) {
+        $custnum_sql .= 'SIGNED INTEGER)';
+    }
+    else {
+        $custnum_sql .= 'INTEGER)';
+    }
+    return ($linkalias, $custnum_sql);
+}
 
-        if ( defined $rowhash{'ALIAS'} and
-             $rowhash{'FIELD'} and
-             $rowhash{'ORDER'} ) {
+sub JoinToCustomer {
+    my $self = shift;
+    my ($linkalias, $custnum_sql) = $self->JoinToCustLinks;
 
-            if ( length $rowhash{'ALIAS'} && $rowhash{'FIELD'} =~ /^((\w+\()+)(.*\)+)$/ ) {
-                # handle 'FUNCTION(FIELD)' formatted fields
-                $rowhash{'FIELD'} = $1. $rowhash{'ALIAS'}. '.'. $3;
-                $rowhash{'ALIAS'} = '';
-            }
+    my $custalias = $self->Join(
+        TYPE       => 'LEFT',
+        EXPRESSION => $custnum_sql,
+        TABLE2     => 'cust_main',
+        FIELD2     => 'custnum',
+    );
+    return $custalias;
+}
 
-            $clause .= ($clause ? ", " : " ");
-            $clause .= $rowhash{'ALIAS'} . "." if length $rowhash{'ALIAS'};
-            $clause .= $rowhash{'FIELD'} . " ";
-            $clause .= $rowhash{'ORDER'};
-        }
+sub _FreesideFieldLimit {
+    my ( $self, $field, $op, $value, %rest ) = @_;
+    my $alias = $self->JoinToCustomer;
+    my $is_negative = 0;
+    if ( $op eq '!=' || $op =~ /\bNOT\b/i ) {
+        # if the op is negative, do the join as though
+        # the op were positive, then accept only records
+        # where the right-side join key is null.
+        $is_negative = 1;
+        $op = '=' if $op eq '!=';
+        $op =~ s/\bNOT\b//;
+    }
+    my $meta = $FIELD_METADATA{$field};
+    if ( $meta->[1] ) {
+        $alias = $self->Join(
+            TYPE        => 'LEFT',
+            ALIAS1      => $alias,
+            FIELD1      => 'custnum',
+            TABLE2      => $meta->[1],
+            FIELD2      => 'custnum',
+        );
     }
-    $clause = " ORDER BY$clause " if $clause;
 
-    return $clause;
+    $self->SUPER::Limit(
+        LEFTJOIN        => $alias,
+        FIELD           => lc($field),
+        OPERATOR        => $op,
+        VALUE           => $value,
+        ENTRYAGGREGATOR => 'AND',
+    );
+    $self->_SQLLimit(
+        %rest,
+        ALIAS           => $alias,
+        FIELD           => lc($field),
+        OPERATOR        => $is_negative ? 'IS' : 'IS NOT',
+        VALUE           => 'NULL',
+        QUOTEVALUE      => 0,
+    );
 }
 
+#Freeside
+
+# }}}
+
 # {{{ Limit the result set based on content
 
 # {{{ sub Limit