borg RT menus, RT#1169
[freeside.git] / FS / FS / TicketSystem / RT_External.pm
index e1aec58..c788c03 100644 (file)
@@ -1,22 +1,28 @@
 package FS::TicketSystem::RT_External;
 
 use strict;
-use vars qw( $conf $default_queueid
+use vars qw( $DEBUG $me $conf $dbh $default_queueid $external_url
+             $priority_reverse
              $priority_field $priority_field_queue $field
-            $dbh $external_url );
+          );
 use URI::Escape;
 use FS::UID qw(dbh);
 use FS::Record qw(qsearchs);
 use FS::cust_main;
 
+$me = '[FS::TicketSystem::RT_External]';
+$DEBUG = 0;
+
 FS::UID->install_callback( sub { 
-  my $conf = new FS::Conf;
+  $conf = new FS::Conf;
   $default_queueid = $conf->config('ticket_system-default_queueid');
+  $priority_reverse = $conf->exists('ticket_system-priority_reverse');
   $priority_field =
     $conf->config('ticket_system-custom_priority_field');
   if ( $priority_field ) {
     $priority_field_queue =
       $conf->config('ticket_system-custom_priority_field_queue');
+
     $field = $priority_field_queue
                   ? $priority_field_queue. '.%7B'. $priority_field. '%7D'
                   : $priority_field;
@@ -35,6 +41,17 @@ FS::UID->install_callback( sub {
     $external_url = $conf->config('ticket_system-rt_external_url');
   }
 
+  #kludge... should *use* the id... but good enough for now
+  if ( $priority_field_queue =~ /^(\d+)$/ ) {
+    my $id = $1;
+    my $sql = 'SELECT Name FROM Queues WHERE Id = ?';
+    my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
+    $sth->execute($id)            or die $sth->errstr. " executing $sql";
+
+    $priority_field_queue = $sth->fetchrow_arrayref->[0];
+
+  }
+
 } );
 
 sub num_customer_tickets {
@@ -42,7 +59,8 @@ sub num_customer_tickets {
 
   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
 
-  my $sql = "select count(*) $from_sql";
+  my $sql = "SELECT COUNT(*) $from_sql";
+  warn "$me $sql (@param)" if $DEBUG;
   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
   $sth->execute(@param)         or die $sth->errstr. " executing $sql";
 
@@ -55,9 +73,20 @@ sub customer_tickets {
   $limit ||= 0;
 
   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
-  my $sql = "select tickets.*, queues.name".
-            ( length($priority) ? ", ticketcustomfieldvalues.content" : '' ).
-            " $from_sql order by priority desc limit $limit";
+  my $sql = "
+    SELECT Tickets.*,
+           Queues.Name AS Queue,
+           Users.Name  AS Owner,
+           position(Tickets.Status in 'newopenstalledresolvedrejecteddeleted')
+             AS svalue
+           ". ( length($priority) ? ", ObjectCustomFieldValues.Content" : '' )."
+      $from_sql
+      ORDER BY svalue,
+               Priority ". ( $priority_reverse ? 'ASC' : 'DESC' ). ",
+               id DESC
+      LIMIT $limit
+  ";
+  warn "$me $sql (@param)" if $DEBUG;
   my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
   $sth->execute(@param)         or die $sth->errstr. "executing $sql";
 
@@ -75,9 +104,23 @@ sub _from_customer {
   my $where = '';
   if ( defined($priority) ) {
 
-    my $queue_sql = " customfields.queue = ( select id from queues
-                                              where queues.name = ? )
-                      or ( ? = '' and customfields.queue = 0 )";
+    my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM Queues
+                                                       WHERE Queues.Name = ? )
+                      OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
+
+    my $customfield_sql =
+      "customfield = ( 
+        SELECT CustomFields.Id FROM CustomFields
+                  JOIN ObjectCustomFields
+                    ON ( CustomFields.id = ObjectCustomFields.CustomField )
+         WHERE LookupType = 'RT::Queue-RT::Ticket'
+           AND Name = ?
+           AND ( $queue_sql )
+       )";
+
+    push @param, $priority_field,
+                 $priority_field_queue,
+                 $priority_field_queue;
 
     if ( length($priority) ) {
       #$where = "    
@@ -89,40 +132,37 @@ sub _from_customer {
       #                                 )
       #          )
       #";
-      push @param, $priority;
+      unshift @param, $priority;
 
-      $join = "join TicketCustomFieldValues
-                 on ( tickets.id = TicketCustomFieldValues.ticket )";
+      $join = "JOIN ObjectCustomFieldValues
+                 ON ( Tickets.id = ObjectCustomFieldValues.ObjectId )";
       
-      $where = "and content = ?
-                and customfield = ( select id from customfields
-                                     where name = ?
-                                       and ( $queue_sql )
-                                  )
-               ";
+      $where = " AND Content = ?
+                 AND ObjectCustomFieldValues.Disabled != 1
+                 AND ObjectType = 'RT::Ticket'
+                 AND $customfield_sql";
+
     } else {
+
       $where =
-               "and 0 = ( select count(*) from TicketCustomFieldValues
-                           where ticket = tickets.id
-                             and customfield = ( select id from customfields
-                                                  where name = ?
-                                                    and ( $queue_sql )
-                                               )
+               "AND 0 = ( SELECT COUNT(*) FROM ObjectCustomFieldValues
+                           WHERE ObjectId    = Tickets.id
+                             AND ObjectType  = 'RT::Ticket'
+                             AND $customfield_sql
                         )
                ";
     }
-    push @param, $priority_field,
-                 $priority_field_queue,
-                 $priority_field_queue;
+
   }
 
   my $sql = "
-                    from tickets
-                    join queues on ( tickets.queue = queues.id )
-                    join links on ( tickets.id = links.localbase )
+                    FROM Tickets
+                    JOIN Queues ON ( Tickets.Queue = Queues.id       )
+                    JOIN Links  ON ( Tickets.id    = Links.LocalBase )
+                    JOIN Users  ON ( Tickets.Owner = Users.id        )
                     $join 
-       where ( status = 'new' or status = 'open' or status = 'stalled' )
-         and target = 'freeside://freeside/cust_main/$custnum'
+       WHERE ( ". join(' OR ', map "Status = '$_'", $self->statuses ). " )
+         AND Target = 'freeside://freeside/cust_main/$custnum'
          $where
   ";
 
@@ -130,31 +170,54 @@ sub _from_customer {
 
 }
 
+sub statuses {
+  #my $self = shift;
+  my @statuses = grep { ! /^\s*$/ } $conf->config('cust_main-ticket_statuses');
+  @statuses = (qw( new open stalled )) unless scalar(@statuses);
+  @statuses;
+}
+
 sub href_customer_tickets {
-  my( $self, $custnum, $priority ) = @_;
+  my( $self, $custnum ) = ( shift, shift );
+  my( $priority, @statuses);
+  if ( ref($_[0]) ) {
+    my $opt = shift;
+    $priority = $opt->{'priority'};
+    @statuses = $opt->{'statuses'} ? @{$opt->{'statuses'}} : $self->statuses;
+  } else {
+    $priority = shift;
+    @statuses = $self->statuses;
+  }
 
-  my $href = $self->baseurl;
+  #my $href = $self->baseurl;
 
-  #i snarfed this from an RT bookmarked search, it could be unescaped in the
-  #source for readability and run through uri_escape
-  $href .= 
-    'Search/Results.html?Order=ASC&Query=%20MemberOf%20%3D%20%27freeside%3A%2F%2Ffreeside%2Fcust_main%2F'.
-    $custnum.
-    '%27%20%20AND%20%28%20Status%20%3D%20%27open%27%20%20OR%20Status%20%3D%20%27new%27%20%20OR%20Status%20%3D%20%27stalled%27%20%29%20'
+  #i snarfed this from an RT bookmarked search, then unescaped (some of) it with
+  #perl -npe 's/%([0-9A-F]{2})/pack('C', hex($1))/eg;'
+
+  #$href .= 
+  my $href = 
+    "Search/Results.html?Order=ASC&".
+    "Query= MemberOf = 'freeside://freeside/cust_main/$custnum' ".
+    #" AND ( Status = 'open'  OR Status = 'new'  OR Status = 'stalled' )"
+    " AND ( ". join(' OR ', map "Status = '$_'", @statuses ). " ) "
   ;
 
   if ( defined($priority) && $field && $priority_field_queue ) {
-    $href .= 'AND%20Queue%20%3D%20%27'. $priority_field_queue. '%27%20';
+    $href .= " AND Queue = '$priority_field_queue' ";
   }
   if ( defined($priority) && $field ) {
-    $href .= '%20AND%20%27CF.'. $field. '%27%20';
+    $href .= " AND 'CF.$field' ";
     if ( $priority ) {
-      $href .= '%3D%20%27'. $priority. '%27%20';
+      $href .= "= '$priority' ";
     } else {
-      $href .= 'IS%20%27NULL%27%20';
+      $href .= "IS 'NULL' "; #this is "RTQL", not SQL
     }
   }
 
+  #$href = 
+  uri_escape($href);
+  #eventually should unescape all of it...
+
   $href .= '&Rows=100'.
            '&OrderBy=id&Page=1'.
            '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
@@ -175,7 +238,10 @@ sub href_customer_tickets {
 
   $href .= '%20%0A%27%3Csmall%3E__ToldRelative__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__LastUpdatedRelative__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__TimeLeft__%3C%2Fsmall%3E%27';
 
-  $href;
+  #$href =
+  #uri_escape($href);
+
+  $self->baseurl. $href;
 
 }
 
@@ -208,7 +274,7 @@ sub href_ticket {
 sub queues {
   my($self) = @_;
 
-  my $sql = "select id, name from queues where disabled = 0";
+  my $sql = "SELECT id, Name FROM Queues WHERE Disabled = 0";
   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
   $sth->execute()               or die $sth->errstr. " executing $sql";
 
@@ -219,17 +285,73 @@ sub queues {
 sub queue {
   my($self, $queueid) = @_;
 
-  my $sql = "select name from queues where id = ?";
+  return '' unless $queueid;
+
+  my $sql = "SELECT Name FROM Queues WHERE id = ?";
   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
   $sth->execute($queueid)       or die $sth->errstr. " executing $sql";
 
-  $sth->fetchrow_arrayref->[0];
+  my $rows = $sth->fetchrow_arrayref;
+  $rows ? $rows->[0] : '';
 
 }
 
 sub baseurl {
   #my $self = shift;
-  $external_url;
+  $external_url. '/';
+}
+
+sub _retrieve_single_value {
+  my( $self, $sql ) = @_;
+
+  warn "$me $sql" if $DEBUG;
+  my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
+  $sth->execute                 or die $sth->errstr. "executing $sql";
+
+  my $arrayref = $sth->fetchrow_arrayref;
+  $arrayref ? $arrayref->[0] : $arrayref;
+}
+
+sub transaction_creator {
+  my( $self, $transaction_id ) = @_;
+
+  my $sql = "SELECT Name FROM Transactions JOIN Users ON ".
+            "Transactions.Creator=Users.id WHERE Transactions.id = ".
+            $transaction_id;
+
+  $self->_retrieve_single_value($sql);
+}
+
+sub transaction_ticketid {
+  my( $self, $transaction_id ) = @_;
+
+  my $sql = "SELECT ObjectId FROM Transactions WHERE Transactions.id = ".
+            $transaction_id;
+  
+  $self->_retrieve_single_value($sql);
+}
+
+sub transaction_subject {
+  my( $self, $transaction_id ) = @_;
+
+  my $sql = "SELECT Subject FROM Transactions JOIN Tickets ON ObjectId=".
+            "Tickets.id WHERE Transactions.id = ".  $transaction_id;
+  
+  $self->_retrieve_single_value($sql);
+}
+
+sub transaction_status {
+  my( $self, $transaction_id ) = @_;
+
+  my $sql = "SELECT Status FROM Transactions JOIN Tickets ON ObjectId=".
+            "Tickets.id WHERE Transactions.id = ".  $transaction_id;
+  
+  $self->_retrieve_single_value($sql);
+}
+
+sub access_right {
+  warn "WARNING: no access rights available w/ external RT";
+  0;
 }
 
 1;