update custom priorioty field BS for RT 3.4.4
[freeside.git] / FS / FS / TicketSystem / RT_External.pm
index be08238..f8f5472 100644 (file)
@@ -2,11 +2,14 @@ package FS::TicketSystem::RT_External;
 
 use strict;
 use vars qw( $conf $default_queueid
-             $priority_field $priority_field_queue $field );
+             $priority_field $priority_field_queue $field
+            $dbh $external_url );
 use URI::Escape;
-use FS::UID;
+use FS::UID qw(dbh);
+use FS::Record qw(qsearchs);
+use FS::cust_main;
 
-install_callback FS::UID sub { 
+FS::UID->install_callback( sub { 
   my $conf = new FS::Conf;
   $default_queueid = $conf->config('ticket_system-default_queueid');
   $priority_field =
@@ -21,12 +24,21 @@ install_callback FS::UID sub {
     $priority_field_queue = '';
     $field = '';
   }
-};
 
-sub num_customer_tickets {
-  my( $self, $custnum, $priority, $dbh ) = @_;
+  $external_url = '';
+  $dbh = dbh;
+  if ($conf->config('ticket_system') eq 'RT_External') {
+    my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
+    $dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 })
+      or die "RT_External DBI->connect error: $DBI::errstr\n";
+
+    $external_url = $conf->config('ticket_system-rt_external_url');
+  }
+
+} );
 
-  #$dbh ||= create one from some config options
+sub num_customer_tickets {
+  my( $self, $custnum, $priority ) = @_;
 
   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
 
@@ -39,14 +51,12 @@ sub num_customer_tickets {
 }
 
 sub customer_tickets {
-  my( $self, $custnum, $limit, $priority, $dbh ) = @_;
+  my( $self, $custnum, $limit, $priority ) = @_;
   $limit ||= 0;
 
-  #$dbh ||= create one from some config options
-
   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
   my $sql = "select tickets.*, queues.name".
-            ( length($priority) ? ", ticketcustomfieldvalues.content" : '' ).
+            ( length($priority) ? ", objectcustomfieldvalues.content" : '' ).
             " $from_sql order by priority desc limit $limit";
   my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
   $sth->execute(@param)         or die $sth->errstr. "executing $sql";
@@ -81,10 +91,11 @@ sub _from_customer {
       #";
       push @param, $priority;
 
-      $join = "join TicketCustomFieldValues
-                 on ( tickets.id = TicketCustomFieldValues.ticket )";
+      $join = "join ObjectCustomFieldValues
+                 on ( tickets.id = ObjectCustomFieldValues.ObjectId )";
       
-      $where = "and content = ?
+      $where = "and ObjectType = 'RT::Ticket'
+                and content = ?
                 and customfield = ( select id from customfields
                                      where name = ?
                                        and ( $queue_sql )
@@ -92,8 +103,9 @@ sub _from_customer {
                ";
     } else {
       $where =
-               "and 0 = ( select count(*) from TicketCustomFieldValues
-                           where ticket = tickets.id
+               "and 0 = ( select count(*) from ObjectCustomFieldValues
+                           where ObjectId    = tickets.id
+                             and ObjectType  = 'RT::Ticket'
                              and customfield = ( select id from customfields
                                                   where name = ?
                                                     and ( $queue_sql )
@@ -123,9 +135,11 @@ sub _from_customer {
 sub href_customer_tickets {
   my( $self, $custnum, $priority ) = @_;
 
+  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
-  my $href 
+  $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'
@@ -145,7 +159,11 @@ sub href_customer_tickets {
 
   $href .= '&Rows=100'.
            '&OrderBy=id&Page=1'.
-           '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22%2Ffreeside%2Frt%2FTicket%2FDisplay.html%3Fid%3D__id__%22%3E__id__%3C%2Fa%3E%3C%2Fb%3E%2FTITLE%3A%23%27%2C%20%0A%27%3Cb%3E%3Ca%20href%3D%22%2Ffreeside%2Frt%2FTicket%2FDisplay.html%3Fid%3D__id__%22%3E__Subject__%3C%2Fa%3E%3C%2Fb%3E%2FTITLE%3ASubject%27%2C%20%0A%27__Status__%27%2C%20';
+           '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
+          $self->baseurl.
+          'Ticket%2FDisplay.html%3Fid%3D__id__%22%3E__id__%3C%2Fa%3E%3C%2Fb%3E%2FTITLE%3A%23%27%2C%20%0A%27%3Cb%3E%3Ca%20href%3D%22'.
+          $self->baseurl.
+          'Ticket%2FDisplay.html%3Fid%3D__id__%22%3E__Subject__%3C%2Fa%3E%3C%2Fb%3E%2FTITLE%3ASubject%27%2C%20%0A%27__Status__%27%2C%20';
 
   if ( defined($priority) && $field ) {
     $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
@@ -160,13 +178,25 @@ 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;
-}
 
+}
 
 sub href_new_ticket {
-  my( $self, $custnum, $requestors ) = @_;
+  my( $self, $custnum_or_cust_main, $requestors ) = @_;
+
+  my( $custnum, $cust_main );
+  if ( ref($custnum_or_cust_main) ) {
+    $cust_main = $custnum_or_cust_main;
+    $custnum = $cust_main->custnum;
+  } else {
+    $custnum = $custnum_or_cust_main;
+    $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
+  }
+  my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
+
+  $self->baseurl.
   'Ticket/Create.html?'.
-    "Queue=$default_queueid".
+    "Queue=$queueid".
     "&new-MemberOf=freeside://freeside/cust_main/$custnum".
     ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
     ;
@@ -174,7 +204,36 @@ sub href_new_ticket {
 
 sub href_ticket {
   my($self, $ticketnum) = @_;
-  'Ticket/Display.html?id='.$ticketnum;
+  $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
+}
+
+sub queues {
+  my($self) = @_;
+
+  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";
+
+  map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
+
+}
+
+sub queue {
+  my($self, $queueid) = @_;
+
+  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];
+
+}
+
+sub baseurl {
+  #my $self = shift;
+  $external_url;
 }
 
 1;