self-service support usage improvements (1733)
[freeside.git] / FS / FS / TicketSystem / RT_External.pm
1 package FS::TicketSystem::RT_External;
2
3 use strict;
4 use vars qw( $DEBUG $me $conf $dbh $default_queueid $external_url
5              $priority_field $priority_field_queue $field
6            );
7 use URI::Escape;
8 use FS::UID qw(dbh);
9 use FS::Record qw(qsearchs);
10 use FS::cust_main;
11
12 $me = '[FS::TicketSystem::RT_External]';
13 $DEBUG = 0;
14
15 FS::UID->install_callback( sub { 
16   $conf = new FS::Conf;
17   $default_queueid = $conf->config('ticket_system-default_queueid');
18   $priority_field =
19     $conf->config('ticket_system-custom_priority_field');
20   if ( $priority_field ) {
21     $priority_field_queue =
22       $conf->config('ticket_system-custom_priority_field_queue');
23
24     $field = $priority_field_queue
25                   ? $priority_field_queue. '.%7B'. $priority_field. '%7D'
26                   : $priority_field;
27   } else {
28     $priority_field_queue = '';
29     $field = '';
30   }
31
32   $external_url = '';
33   $dbh = dbh;
34   if ($conf->config('ticket_system') eq 'RT_External') {
35     my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
36     $dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 })
37       or die "RT_External DBI->connect error: $DBI::errstr\n";
38
39     $external_url = $conf->config('ticket_system-rt_external_url');
40   }
41
42   #kludge... should *use* the id... but good enough for now
43   if ( $priority_field_queue =~ /^(\d+)$/ ) {
44     my $id = $1;
45     my $sql = 'SELECT Name FROM Queues WHERE Id = ?';
46     my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
47     $sth->execute($id)            or die $sth->errstr. " executing $sql";
48
49     $priority_field_queue = $sth->fetchrow_arrayref->[0];
50
51   }
52
53 } );
54
55 sub num_customer_tickets {
56   my( $self, $custnum, $priority ) = @_;
57
58   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
59
60   my $sql = "SELECT COUNT(*) $from_sql";
61   warn "$me $sql (@param)" if $DEBUG;
62   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
63   $sth->execute(@param)         or die $sth->errstr. " executing $sql";
64
65   $sth->fetchrow_arrayref->[0];
66
67 }
68
69 sub customer_tickets {
70   my( $self, $custnum, $limit, $priority ) = @_;
71   $limit ||= 0;
72
73   my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
74   my $sql="SELECT tickets.*, queues.name, ".
75           "position(tickets.status in 'newopenstalledresolvedrejecteddeleted')".
76           " AS svalue " .
77           ( length($priority) ? ", objectcustomfieldvalues.content" : '' ).
78           " $from_sql ORDER BY svalue, priority DESC, id DESC LIMIT $limit";
79   warn "$me $sql (@param)" if $DEBUG;
80   my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
81   $sth->execute(@param)         or die $sth->errstr. "executing $sql";
82
83   #munge column names???  #httemplate/view/cust_main/tickets.html has column
84   #names that might not make sense now...
85   $sth->fetchall_arrayref({});
86
87 }
88
89 sub _from_customer {
90   my( $self, $custnum, $priority ) = @_;
91
92   my @param = ();
93   my $join = '';
94   my $where = '';
95   if ( defined($priority) ) {
96
97     my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM queues
98                                                        WHERE queues.name = ? )
99                       OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
100
101     my $customfield_sql =
102       "customfield = ( 
103         SELECT CustomFields.Id FROM CustomFields
104                   JOIN ObjectCustomFields
105                     ON ( CustomFields.id = ObjectCustomFields.CustomField )
106          WHERE LookupType = 'RT::Queue-RT::Ticket'
107            AND name = ?
108            AND ( $queue_sql )
109        )";
110
111     push @param, $priority_field,
112                  $priority_field_queue,
113                  $priority_field_queue;
114
115     if ( length($priority) ) {
116       #$where = "    
117       #  and ? = ( select content from TicketCustomFieldValues
118       #             where ticket = tickets.id
119       #               and customfield = ( select id from customfields
120       #                                    where name = ?
121       #                                      and ( $queue_sql )
122       #                                 )
123       #          )
124       #";
125       unshift @param, $priority;
126
127       $join = "JOIN ObjectCustomFieldValues
128                  ON ( tickets.id = ObjectCustomFieldValues.ObjectId )";
129       
130       $where = " AND content = ?
131                  AND ObjectCustomFieldValues.disabled != 1
132                  AND ObjectType = 'RT::Ticket'
133                  AND $customfield_sql";
134
135     } else {
136
137       $where =
138                "AND 0 = ( SELECT count(*) FROM ObjectCustomFieldValues
139                            WHERE ObjectId    = tickets.id
140                              AND ObjectType  = 'RT::Ticket'
141                              AND $customfield_sql
142                         )
143                ";
144     }
145
146   }
147
148   my $sql = "
149                     FROM tickets
150                     JOIN queues ON ( tickets.queue = queues.id )
151                     JOIN links ON ( tickets.id = links.localbase )
152                     $join 
153        WHERE ( ". join(' OR ', map "status = '$_'", $self->statuses ). " )
154          AND target = 'freeside://freeside/cust_main/$custnum'
155          $where
156   ";
157
158   ( $sql, @param );
159
160 }
161
162 sub statuses {
163   #my $self = shift;
164   my @statuses = grep { ! /^\s*$/ } $conf->config('cust_main-ticket_statuses');
165   @statuses = (qw( new open stalled )) unless scalar(@statuses);
166   @statuses;
167 }
168
169 sub href_customer_tickets {
170   my( $self, $custnum, $priority ) = @_;
171
172   #my $href = $self->baseurl;
173
174   #i snarfed this from an RT bookmarked search, then unescaped (some of) it with
175   #perl -npe 's/%([0-9A-F]{2})/pack('C', hex($1))/eg;'
176
177   my $href .= 
178     "Search/Results.html?Order=ASC&".
179     "Query= MemberOf = 'freeside://freeside/cust_main/$custnum' ".
180     #" AND ( Status = 'open'  OR Status = 'new'  OR Status = 'stalled' )"
181     " AND ( ". join(' OR ', map "Status = '$_'", $self->statuses ). " ) "
182   ;
183
184   if ( defined($priority) && $field && $priority_field_queue ) {
185     $href .= " AND Queue = '$priority_field_queue' ";
186   }
187   if ( defined($priority) && $field ) {
188     $href .= " AND 'CF.$field' ";
189     if ( $priority ) {
190       $href .= "= '$priority' ";
191     } else {
192       $href .= "IS 'NULL' "; #this is "RTQL", not SQL
193     }
194   }
195
196   #$href = 
197   uri_escape($href);
198   #eventually should unescape all of it...
199
200   $href .= '&Rows=100'.
201            '&OrderBy=id&Page=1'.
202            '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
203            $self->baseurl.
204            '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'.
205            $self->baseurl.
206            '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';
207
208   if ( defined($priority) && $field ) {
209     $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
210   }
211
212   $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';
213
214   if ( defined($priority) && $field ) {
215     $href .=   '%20%0A%27__-__%27%2C';
216   }
217
218   $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';
219
220   #$href =
221   #uri_escape($href);
222
223   $self->baseurl. $href;
224
225 }
226
227 sub href_new_ticket {
228   my( $self, $custnum_or_cust_main, $requestors ) = @_;
229
230   my( $custnum, $cust_main );
231   if ( ref($custnum_or_cust_main) ) {
232     $cust_main = $custnum_or_cust_main;
233     $custnum = $cust_main->custnum;
234   } else {
235     $custnum = $custnum_or_cust_main;
236     $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
237   }
238   my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
239
240   $self->baseurl.
241   'Ticket/Create.html?'.
242     "Queue=$queueid".
243     "&new-MemberOf=freeside://freeside/cust_main/$custnum".
244     ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
245     ;
246 }
247
248 sub href_ticket {
249   my($self, $ticketnum) = @_;
250   $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
251 }
252
253 sub queues {
254   my($self) = @_;
255
256   my $sql = "SELECT id, name FROM queues WHERE disabled = 0";
257   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
258   $sth->execute()               or die $sth->errstr. " executing $sql";
259
260   map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
261
262 }
263
264 sub queue {
265   my($self, $queueid) = @_;
266
267   return '' unless $queueid;
268
269   my $sql = "SELECT name FROM queues WHERE id = ?";
270   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
271   $sth->execute($queueid)       or die $sth->errstr. " executing $sql";
272
273   my $rows = $sth->fetchrow_arrayref;
274   $rows ? $rows->[0] : '';
275
276 }
277
278 sub baseurl {
279   #my $self = shift;
280   $external_url;
281 }
282
283 sub _retrieve_single_value {
284   my( $self, $sql ) = @_;
285
286   warn "$me $sql" if $DEBUG;
287   my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
288   $sth->execute                 or die $sth->errstr. "executing $sql";
289
290   my $arrayref = $sth->fetchrow_arrayref;
291   $arrayref ? $arrayref->[0] : $arrayref;
292 }
293
294 sub transaction_creator {
295   my( $self, $transaction_id ) = @_;
296
297   my $sql = "SELECT name from transactions JOIN users ON ".
298             "transactions.creator=users.id WHERE transactions.id = ".
299             $transaction_id;
300
301   $self->_retrieve_single_value($sql);
302 }
303
304 sub transaction_ticketid {
305   my( $self, $transaction_id ) = @_;
306
307   my $sql = "SELECT objectid from transactions WHERE transactions.id = ".
308             $transaction_id;
309   
310   $self->_retrieve_single_value($sql);
311 }
312
313 sub transaction_subject {
314   my( $self, $transaction_id ) = @_;
315
316   my $sql = "SELECT subject from transactions JOIN tickets ON objectid=".
317             "tickets.id WHERE transactions.id = ".  $transaction_id;
318   
319   $self->_retrieve_single_value($sql);
320 }
321
322 sub transaction_status {
323   my( $self, $transaction_id ) = @_;
324
325   my $sql = "SELECT status from transactions JOIN tickets ON objectid=".
326             "tickets.id WHERE transactions.id = ".  $transaction_id;
327   
328   $self->_retrieve_single_value($sql);
329 }
330
331 1;
332