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