[Bps-public-commit] r11632 - RT-Extension-Reports/lib/RT/Extension/Reports

sunnavy at bestpractical.com sunnavy at bestpractical.com
Tue Apr 8 08:34:25 EDT 2008


Author: sunnavy
Date: Tue Apr  8 08:34:24 2008
New Revision: 11632

Modified:
   RT-Extension-Reports/lib/RT/Extension/Reports/Filter.pm

Log:
added Age key for filter

Modified: RT-Extension-Reports/lib/RT/Extension/Reports/Filter.pm
==============================================================================
--- RT-Extension-Reports/lib/RT/Extension/Reports/Filter.pm	(original)
+++ RT-Extension-Reports/lib/RT/Extension/Reports/Filter.pm	Tue Apr  8 08:34:24 2008
@@ -2,7 +2,9 @@
 use strict;
 use warnings;
 
-my $map;
+# $map is for fields in db, $extra_map is for others that need to be calculated
+# and it's value is a sub
+my ( $map, $extra_map );
 
 # for ticket
 for (
@@ -55,13 +57,29 @@
     }
 }
 
+$extra_map->{Age} = sub {
+    my %args   = @_;
+    my $ticket = $args{Ticket};
+    my $date   = $args{Date};
+    return unless $date;
+
+    # +1 is for reports date, since it's the beginning of the day, and what we
+    # want is the end of the day
+    my $age = int( $date->Diff( $ticket->CreatedObj ) / 3600 / 24 ) + 1;
+    return $age;
+};
+
 sub _filter_ticket {
     my %args = @_;
     return unless $args{Ticket};
     return _test(
         Operator => $args{Operator},
-        Value1   => _value( Ticket => $args{Ticket}, Field => $args{Field} ),
-        Value2   => $args{Value},
+        Value1   => _value(
+            Ticket => $args{Ticket},
+            Field  => $args{Field},
+            Date   => $args{Date},
+        ),
+        Value2 => $args{Value},
     );
 }
 
@@ -80,27 +98,39 @@
 sub filter_ticket {
     my %args   = @_;
     my $filter = $args{Filter};
-    my $ticket = $args{Ticket};
     $filter =~ s/\r/\n/g;
     my @filters = split /\n+/, $filter;
     for (@filters) {
         next unless /\S/;
         s/^\s+//;
         my ( $field, $op, $value ) = split /\s+/, $_;
+
+        # OnlyDate means we just want to filter date related stuff
+        next if $field ne 'Age' && $args{OnlyDate};
+        next if $field eq 'Age' && !$args{Date};
+
         return 0
           unless _filter_ticket(
-            Ticket   => $ticket,
+            Ticket   => $args{Ticket},
             Value    => $value,
             Operator => $op,
-            Field    => $field
+            Field    => $field,
+            Date     => $args{Date},
           );
     }
     return 1;
 }
 
 sub _value {
-    my %args   = @_;
-    my $ticket = $args{Ticket};
+    my %args = @_;
+
+    if ( $extra_map->{ $args{Field} } ) {
+        return $extra_map->{ $args{Field} }->(
+            Ticket => $args{Ticket},
+            Date   => $args{Date}
+        );
+    }
+
     my @fields = split /\./, $args{Field};
 
     # check if we support that field
@@ -113,7 +143,7 @@
         }
     }
 
-    my $v = $ticket;
+    my $v = $args{Ticket};
     for my $f (@fields) {
         $v = $v->$f;
     }



More information about the Bps-public-commit mailing list