c7a756bdecc319c933cd44cb879970380fbc1429
[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 = "
75     SELECT Tickets.*,
76            Queues.Name AS Queue,
77            Users.Name  AS Owner,
78            position(Tickets.Status in 'newopenstalledresolvedrejecteddeleted')
79              AS svalue
80            ". ( length($priority) ? ", ObjectCustomFieldValues.Content" : '' )."
81       $from_sql
82       ORDER BY svalue,
83                Priority DESC,
84                id DESC
85       LIMIT $limit
86   ";
87   warn "$me $sql (@param)" if $DEBUG;
88   my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
89   $sth->execute(@param)         or die $sth->errstr. "executing $sql";
90
91   #munge column names???  #httemplate/view/cust_main/tickets.html has column
92   #names that might not make sense now...
93   $sth->fetchall_arrayref({});
94
95 }
96
97 sub _from_customer {
98   my( $self, $custnum, $priority ) = @_;
99
100   my @param = ();
101   my $join = '';
102   my $where = '';
103   if ( defined($priority) ) {
104
105     my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM Queues
106                                                        WHERE Queues.Name = ? )
107                       OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
108
109     my $customfield_sql =
110       "customfield = ( 
111         SELECT CustomFields.Id FROM CustomFields
112                   JOIN ObjectCustomFields
113                     ON ( CustomFields.id = ObjectCustomFields.CustomField )
114          WHERE LookupType = 'RT::Queue-RT::Ticket'
115            AND Name = ?
116            AND ( $queue_sql )
117        )";
118
119     push @param, $priority_field,
120                  $priority_field_queue,
121                  $priority_field_queue;
122
123     if ( length($priority) ) {
124       #$where = "    
125       #  and ? = ( select content from TicketCustomFieldValues
126       #             where ticket = tickets.id
127       #               and customfield = ( select id from customfields
128       #                                    where name = ?
129       #                                      and ( $queue_sql )
130       #                                 )
131       #          )
132       #";
133       unshift @param, $priority;
134
135       $join = "JOIN ObjectCustomFieldValues
136                  ON ( Tickets.id = ObjectCustomFieldValues.ObjectId )";
137       
138       $where = " AND Content = ?
139                  AND ObjectCustomFieldValues.Disabled != 1
140                  AND ObjectType = 'RT::Ticket'
141                  AND $customfield_sql";
142
143     } else {
144
145       $where =
146                "AND 0 = ( SELECT COUNT(*) FROM ObjectCustomFieldValues
147                            WHERE ObjectId    = Tickets.id
148                              AND ObjectType  = 'RT::Ticket'
149                              AND $customfield_sql
150                         )
151                ";
152     }
153
154   }
155
156   my $sql = "
157                     FROM Tickets
158                     JOIN Queues ON ( Tickets.Queue = Queues.id       )
159                     JOIN Links  ON ( Tickets.id    = Links.LocalBase )
160                     JOIN Users  ON ( Tickets.Owner = Users.id        )
161                     $join 
162        WHERE ( ". join(' OR ', map "Status = '$_'", $self->statuses ). " )
163          AND Target = 'freeside://freeside/cust_main/$custnum'
164          $where
165   ";
166
167   ( $sql, @param );
168
169 }
170
171 sub statuses {
172   #my $self = shift;
173   my @statuses = grep { ! /^\s*$/ } $conf->config('cust_main-ticket_statuses');
174   @statuses = (qw( new open stalled )) unless scalar(@statuses);
175   @statuses;
176 }
177
178 sub href_customer_tickets {
179   my( $self, $custnum ) = ( shift, shift );
180   my( $priority, @statuses);
181   if ( ref($_[0]) ) {
182     my $opt = shift;
183     $priority = $opt->{'priority'};
184     @statuses = $opt->{'statuses'} ? @{$opt->{'statuses'}} : $self->statuses;
185   } else {
186     $priority = shift;
187     @statuses = $self->statuses;
188   }
189
190   #my $href = $self->baseurl;
191
192   #i snarfed this from an RT bookmarked search, then unescaped (some of) it with
193   #perl -npe 's/%([0-9A-F]{2})/pack('C', hex($1))/eg;'
194
195   #$href .= 
196   my $href = 
197     "Search/Results.html?Order=ASC&".
198     "Query= MemberOf = 'freeside://freeside/cust_main/$custnum' ".
199     #" AND ( Status = 'open'  OR Status = 'new'  OR Status = 'stalled' )"
200     " AND ( ". join(' OR ', map "Status = '$_'", @statuses ). " ) "
201   ;
202
203   if ( defined($priority) && $field && $priority_field_queue ) {
204     $href .= " AND Queue = '$priority_field_queue' ";
205   }
206   if ( defined($priority) && $field ) {
207     $href .= " AND 'CF.$field' ";
208     if ( $priority ) {
209       $href .= "= '$priority' ";
210     } else {
211       $href .= "IS 'NULL' "; #this is "RTQL", not SQL
212     }
213   }
214
215   #$href = 
216   uri_escape($href);
217   #eventually should unescape all of it...
218
219   $href .= '&Rows=100'.
220            '&OrderBy=id&Page=1'.
221            '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
222            $self->baseurl.
223            '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'.
224            $self->baseurl.
225            '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';
226
227   if ( defined($priority) && $field ) {
228     $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
229   }
230
231   $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';
232
233   if ( defined($priority) && $field ) {
234     $href .=   '%20%0A%27__-__%27%2C';
235   }
236
237   $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';
238
239   #$href =
240   #uri_escape($href);
241
242   $self->baseurl. $href;
243
244 }
245
246 sub href_new_ticket {
247   my( $self, $custnum_or_cust_main, $requestors ) = @_;
248
249   my( $custnum, $cust_main );
250   if ( ref($custnum_or_cust_main) ) {
251     $cust_main = $custnum_or_cust_main;
252     $custnum = $cust_main->custnum;
253   } else {
254     $custnum = $custnum_or_cust_main;
255     $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
256   }
257   my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
258
259   $self->baseurl.
260   'Ticket/Create.html?'.
261     "Queue=$queueid".
262     "&new-MemberOf=freeside://freeside/cust_main/$custnum".
263     ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
264     ;
265 }
266
267 sub href_ticket {
268   my($self, $ticketnum) = @_;
269   $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
270 }
271
272 sub queues {
273   my($self) = @_;
274
275   my $sql = "SELECT id, Name FROM Queues WHERE Disabled = 0";
276   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
277   $sth->execute()               or die $sth->errstr. " executing $sql";
278
279   map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
280
281 }
282
283 sub queue {
284   my($self, $queueid) = @_;
285
286   return '' unless $queueid;
287
288   my $sql = "SELECT Name FROM Queues WHERE id = ?";
289   my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
290   $sth->execute($queueid)       or die $sth->errstr. " executing $sql";
291
292   my $rows = $sth->fetchrow_arrayref;
293   $rows ? $rows->[0] : '';
294
295 }
296
297 sub baseurl {
298   #my $self = shift;
299   $external_url. '/';
300 }
301
302 1;
303