[freeside-commits] freeside/FS/FS/TicketSystem RT_External.pm, 1.40, 1.41 RT_Internal.pm, 1.21, 1.22

Mark Wells mark at wavetail.420.am
Mon Jun 27 00:11:03 PDT 2011


Update of /home/cvs/cvsroot/freeside/FS/FS/TicketSystem
In directory wavetail.420.am:/tmp/cvs-serv29076/FS/FS/TicketSystem

Modified Files:
	RT_External.pm RT_Internal.pm 
Log Message:
self-service ticket priority and edit subject, #13199

Index: RT_External.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/TicketSystem/RT_External.pm,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -w -d -r1.40 -r1.41
--- RT_External.pm	4 Mar 2011 02:26:14 -0000	1.40
+++ RT_External.pm	27 Jun 2011 07:11:00 -0000	1.41
@@ -403,5 +403,9 @@
   return 'create_ticket unimplemented w/external RT (write something w/RT::Client::REST?)';
 }
 
+sub init { } #unimplemented
+
+sub selfservice_priority { '' } #unimplemented
+
 1;
 

Index: RT_Internal.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/TicketSystem/RT_Internal.pm,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -w -d -r1.21 -r1.22
--- RT_Internal.pm	1 Jan 2011 00:46:59 -0000	1.21
+++ RT_Internal.pm	27 Jun 2011 07:11:01 -0000	1.22
@@ -35,7 +35,6 @@
 sub access_right {
   my( $self, $session, $right ) = @_;
 
-  #return '' unless $conf->config('ticket_system');
   return '' unless FS::Conf->new->config('ticket_system');
 
   $session = $self->session($session);
@@ -63,28 +62,17 @@
   $session;
 }
 
+my $firsttime = 1;
+
 sub init {
   my $self = shift;
+  if ( $firsttime ) {
 
+    # this part only needs to be done once
   warn "$me init: loading RT libraries\n" if $DEBUG;
   eval '
     use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
     use RT;
-    #it looks like the rest are taken care of these days in RT::InitClasses
-    #use RT::Ticket;
-    #use RT::Transactions;
-    #use RT::Users;
-    #use RT::CurrentUser;
-    #use RT::Templates;
-    #use RT::Queues;
-    #use RT::ScripActions;
-    #use RT::ScripConditions;
-    #use RT::Scrips;
-    #use RT::Groups;
-    #use RT::GroupMembers;
-    #use RT::CustomFields;
-    #use RT::CustomFieldValues;
-    #use RT::ObjectCustomFieldValues;
 
     #for web external auth...
     use RT::Interface::Web;
@@ -98,6 +86,10 @@
   }
   die $@ if $@;
 
+    $firsttime = 0;
+  }
+
+  # this needs to be done on each fork
   warn "$me init: initializing RT\n" if $DEBUG;
   {
     local $SIG{__DIE__};
@@ -108,6 +100,106 @@
   warn "$me init: complete" if $DEBUG;
 }
 
+=item customer_tickets CUSTNUM [ LIMIT ] [ PRIORITYVALUE ]
+
+Replacement for the one in RT_External so that we can access custom fields 
+properly.
+
+=cut
+
+sub _customer_tickets_search {
+  my ( $self, $custnum, $limit, $priority ) = @_;
+
+  $custnum =~ /^\d+$/ or die "invalid custnum: $custnum";
+  $limit =~ /^\d+$/ or die "invalid limit: $limit";
+
+  my $session = $self->session();
+  my $CurrentUser = $session->{CurrentUser} 
+    or die "unable to create an RT session";
+
+  my $Tickets = RT::Tickets->new($CurrentUser);
+
+  my $rtql = "MemberOf = 'freeside://freeside/cust_main/$custnum'";
+
+  if ( defined( $priority ) ) {
+    my $custom_priority = FS::Conf->new->config('ticket_system-custom_priority_field');
+    $rtql .= " AND CF.{$custom_priority} = '$priority'";
+  }
+
+  $rtql .= ' AND ( ' .
+           join(' OR ', map { "Status = '$_'" } $self->statuses) .
+           ' )';
+
+  $Tickets->FromSQL($rtql);
+
+  $Tickets->RowsPerPage($limit);
+
+  return $Tickets;
+}
+
+sub customer_tickets {
+  my $Tickets = _customer_tickets_search(@_);
+
+  my $conf = FS::Conf->new;
+  my $priority_order =
+    $conf->exists('ticket_system-priority_reverse') ? 'ASC' : 'DESC';
+  my $custom_priority = 
+    $conf->config('ticket_system-custom_priority_field') || '';
+
+  my @order_by;
+  my $ss_priority = selfservice_priority();
+  push @order_by, { FIELD => "CF.{$ss_priority}", ORDER => $priority_order }
+    if $ss_priority;
+  push @order_by,
+    { FIELD => 'Priority', ORDER => $priority_order },
+    { FIELD => 'Id',       ORDER => 'DESC' },
+  ;
+
+  $Tickets->OrderByCols(@order_by);
+
+  my @tickets;
+  while ( my $t = $Tickets->Next ) {
+    push @tickets, _ticket_info($t);
+  }
+  return \@tickets;
+}
+
+sub num_customer_tickets {
+  my $Tickets = _customer_tickets_search(@_);
+  return $Tickets->CountAll;
+}
+
+sub _ticket_info {
+  # Takes an RT::Ticket; returns a hashref of the ticket's fields, including 
+  # custom fields.  Also returns custom and selfservice priority values as 
+  # _custom_priority and _selfservice_priority.
+  my $t = shift;
+
+  my $custom_priority = 
+    FS::Conf->new->config('ticket_system-custom_priority_field') || '';
+  my $ss_priority = selfservice_priority();
+
+  my %ticket_info;
+  foreach my $name ( $t->ReadableAttributes ) {
+    # lowercase names, and skip attributes with non-scalar values
+    $ticket_info{lc($name)} = $t->$name if !ref($t->$name);
+  }
+  $ticket_info{'owner'} = $t->OwnerObj->Name;
+  $ticket_info{'queue'} = $t->QueueObj->Name;
+  foreach my $CF ( @{ $t->CustomFields->ItemsArrayRef } ) {
+    my $name = 'CF.{'.$CF->Name.'}';
+    $ticket_info{$name} = $t->CustomFieldValuesAsString($CF->Id);
+  }
+  # make this easy to find
+  if ( $custom_priority ) {
+    $ticket_info{'_custom_priority'} = $ticket_info{"CF.{$custom_priority}"};
+  }
+  if ( $ss_priority ) {
+    $ticket_info{'_selfservice_priority'} = $ticket_info{"CF.{$ss_priority}"};
+  }
+  return \%ticket_info;
+}
+
 =item create_ticket SESSION_HASHREF, OPTION => VALUE ...
 
 Class method.  Creates a ticket.  If there is an error, returns the scalar
@@ -219,8 +311,8 @@
 
 Class method. Retrieves a ticket. If there is an error, returns the scalar
 error. Otherwise, currently returns a slightly tricky data structure containing
-a list of the linked customers and each transaction's content, description, and
-create time.
+the ticket's attributes, a list of the linked customers, each transaction's 
+content, description, and create time.
 
 Accepts the following options:
 
@@ -262,9 +354,50 @@
 
   { txns => [ @txns ],
     custs => [ @custs ],
+    fields => _ticket_info($Ticket),
   };
 }
 
+=item get_ticket_object SESSION_HASHREF, OPTION => VALUE...
+
+Class method.  Retrieve the RT::Ticket object with the specified 
+ticket_id.  If custnum is supplied, will also check that the object 
+is a member of that customer.  If there is no ticket or the custnum 
+check fails, returns nothing.  The meaning of that case is 
+"to this customer, the ticket does not exist".
+
+Options:
+
+=over 4
+
+=item ticket_id
+
+=item custnum
+
+=back
+
+=cut
+
+sub get_ticket_object {
+  my $self = shift;
+  my ($session, %opt) = @_;
+  $session = $self->session(shift);
+  my $Ticket = RT::Ticket->new($session->{CurrentUser});
+  $Ticket->Load($opt{'ticket_id'});
+  return if ( !$Ticket->id );
+  my $custnum = $opt{'custnum'};
+  if ( defined($custnum) && $custnum =~ /^\d+$/ ) {
+    # probably the most efficient way to check ticket ownership
+    my $Link = RT::Link->new($session->{CurrentUser});
+    $Link->LoadByCols( LocalBase => $opt{'ticket_id'},
+                       Type      => 'MemberOf',
+                       Target    => "freeside://freeside/cust_main/$custnum",
+                     );
+    return if ( !$Link->id );
+  }
+  return $Ticket;
+}
+
 
 =item correspond_ticket SESSION_HASHREF, OPTION => VALUE ...
 
@@ -427,5 +560,20 @@
 
 }
 
+=item selfservice_priority
+
+Returns the configured self-service priority field.
+
+=cut
+
+my $selfservice_priority;
+
+sub selfservice_priority {
+  return $selfservice_priority ||= do {
+    my $conf = FS::Conf->new;
+    $conf->config('ticket_system-selfservice_priority_field') || '';
+  }
+}
+
 1;
 



More information about the freeside-commits mailing list