63eee867b347220f3cdeac59753bed548875b809
[freeside.git] / FS / FS / TicketSystem / RT_External.pm
1 package FS::TicketSystem::RT_External;
2
3 use strict;
4 use vars qw( $conf $default_queueid
5              $priority_field $priority_field_queue $field
6              $external_dbh $external_url );
7 use URI::Escape;
8 use FS::UID;
9
10 install_callback FS::UID sub { 
11   my $conf = new FS::Conf;
12   $default_queueid = $conf->config('ticket_system-default_queueid');
13   $priority_field =
14     $conf->config('ticket_system-custom_priority_field');
15   if ( $priority_field ) {
16     $priority_field_queue =
17       $conf->config('ticket_system-custom_priority_field_queue');
18     $field = $priority_field_queue
19                   ? $priority_field_queue. '.%7B'. $priority_field. '%7D'
20                   : $priority_field;
21   } else {
22     $priority_field_queue = '';
23     $field = '';
24   }
25
26   $external_url = '';
27   if ($conf->config('ticket_system') eq 'RT_External') {
28     my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
29     $external_dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 })
30       or die "RT_External DBI->connect error: $DBI::errstr\n";
31
32     $external_url = $conf->config('ticket_system-rt_external_url');
33   }
34
35 };
36
37 sub num_customer_tickets {
38   my( $self, $custnum, $priority, $dbh ) = @_;
39
40   $dbh ||= $external_dbh;
41
42   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
43
44   my $sql = "select count(*) $from_sql";
45   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
46   $sth->execute(@param)         or die $sth->errstr. " executing $sql";
47
48   $sth->fetchrow_arrayref->[0];
49
50 }
51
52 sub customer_tickets {
53   my( $self, $custnum, $limit, $priority, $dbh ) = @_;
54   $limit ||= 0;
55
56   $dbh ||= $external_dbh;
57
58   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
59   my $sql = "select tickets.*, queues.name".
60             ( length($priority) ? ", ticketcustomfieldvalues.content" : '' ).
61             " $from_sql order by priority desc limit $limit";
62   my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
63   $sth->execute(@param)         or die $sth->errstr. "executing $sql";
64
65   #munge column names???  #httemplate/view/cust_main/tickets.html has column
66   #names that might not make sense now...
67   $sth->fetchall_arrayref({});
68
69 }
70
71 sub _from_customer {
72   my( $self, $custnum, $priority ) = @_;
73
74   my @param = ();
75   my $join = '';
76   my $where = '';
77   if ( defined($priority) ) {
78
79     my $queue_sql = " customfields.queue = ( select id from queues
80                                               where queues.name = ? )
81                       or ( ? = '' and customfields.queue = 0 )";
82
83     if ( length($priority) ) {
84       #$where = "    
85       #  and ? = ( select content from TicketCustomFieldValues
86       #             where ticket = tickets.id
87       #               and customfield = ( select id from customfields
88       #                                    where name = ?
89       #                                      and ( $queue_sql )
90       #                                 )
91       #          )
92       #";
93       push @param, $priority;
94
95       $join = "join TicketCustomFieldValues
96                  on ( tickets.id = TicketCustomFieldValues.ticket )";
97       
98       $where = "and content = ?
99                 and customfield = ( select id from customfields
100                                      where name = ?
101                                        and ( $queue_sql )
102                                   )
103                ";
104     } else {
105       $where =
106                "and 0 = ( select count(*) from TicketCustomFieldValues
107                            where ticket = tickets.id
108                              and customfield = ( select id from customfields
109                                                   where name = ?
110                                                     and ( $queue_sql )
111                                                )
112                         )
113                ";
114     }
115     push @param, $priority_field,
116                  $priority_field_queue,
117                  $priority_field_queue;
118   }
119
120   my $sql = "
121                     from tickets
122                     join queues on ( tickets.queue = queues.id )
123                     join links on ( tickets.id = links.localbase )
124                     $join 
125        where ( status = 'new' or status = 'open' or status = 'stalled' )
126          and target = 'freeside://freeside/cust_main/$custnum'
127          $where
128   ";
129
130   ( $sql, @param );
131
132 }
133
134 sub _href_customer_tickets {
135   my( $self, $custnum, $priority ) = @_;
136
137   #i snarfed this from an RT bookmarked search, it could be unescaped in the
138   #source for readability and run through uri_escape
139   my $href = 
140     'Search/Results.html?Order=ASC&Query=%20MemberOf%20%3D%20%27freeside%3A%2F%2Ffreeside%2Fcust_main%2F'.
141     $custnum.
142     '%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'
143   ;
144
145   if ( defined($priority) && $field && $priority_field_queue ) {
146     $href .= 'AND%20Queue%20%3D%20%27'. $priority_field_queue. '%27%20';
147   }
148   if ( defined($priority) && $field ) {
149     $href .= '%20AND%20%27CF.'. $field. '%27%20';
150     if ( $priority ) {
151       $href .= '%3D%20%27'. $priority. '%27%20';
152     } else {
153       $href .= 'IS%20%27NULL%27%20';
154     }
155   }
156
157   $href .= '&Rows=100'.
158            '&OrderBy=id&Page=1'.
159            '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
160            $self->baseurl.
161            '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'.
162            $self->baseurl.
163            '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';
164
165   if ( defined($priority) && $field ) {
166     $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
167   }
168
169   $href .= '%0A%27__QueueName__%27%2C%20%0A%27__OwnerName__%27%2C%20%0A%27__Priority__%27%2C%20%0A%27__NEWLINE__%27%2C%20%0A%27%27%2C%20%0A%27%3Csmall%3E__Requestors__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__CreatedRelative__%3C%2Fsmall%3E%27%2C';
170
171   if ( defined($priority) && $field ) {
172     $href .=   '%20%0A%27__-__%27%2C';
173   }
174
175   $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';
176
177   $href;
178
179 }
180
181 sub href_customer_tickets {
182   my $self = shift;
183   $self->baseurl. $self->_href_customer_tickets(@_);
184 }
185
186
187 sub _href_new_ticket {
188   my( $self, $custnum, $requestors ) = @_;
189
190   'Ticket/Create.html?'.
191     "Queue=$default_queueid".
192     "&new-MemberOf=freeside://freeside/cust_main/$custnum".
193     ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
194     ;
195 }
196
197 sub href_new_ticket {
198   my $self = shift;
199   $self->baseurl. $self->_href_new_ticket(@_);
200 }
201
202 sub _href_ticket {
203   my($self, $ticketnum) = @_;
204   'Ticket/Display.html?id='.$ticketnum;
205 }
206
207 sub href_ticket {
208   my $self = shift;
209   $self->baseurl. $self->_href_ticket(@_);
210 }
211
212 sub baseurl {
213   #my $self = shift;
214   $external_url;
215 }
216
217 1;
218