1 package FS::TicketSystem::RT_External;
4 use vars qw( $DEBUG $me $conf $dbh $default_queueid $external_url
5 $priority_field $priority_field_queue $field
9 use FS::Record qw(qsearchs);
12 $me = '[FS::TicketSystem::RT_External]';
15 FS::UID->install_callback( sub {
17 $default_queueid = $conf->config('ticket_system-default_queueid');
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');
24 $field = $priority_field_queue
25 ? $priority_field_queue. '.%7B'. $priority_field. '%7D'
28 $priority_field_queue = '';
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";
39 $external_url = $conf->config('ticket_system-rt_external_url');
42 #kludge... should *use* the id... but good enough for now
43 if ( $priority_field_queue =~ /^(\d+)$/ ) {
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";
49 $priority_field_queue = $sth->fetchrow_arrayref->[0];
55 sub num_customer_tickets {
56 my( $self, $custnum, $priority ) = @_;
58 my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
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";
65 $sth->fetchrow_arrayref->[0];
69 sub customer_tickets {
70 my( $self, $custnum, $limit, $priority ) = @_;
73 my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
78 position(Tickets.Status in 'newopenstalledresolvedrejecteddeleted')
80 ". ( length($priority) ? ", ObjectCustomFieldValues.Content" : '' )."
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";
91 #munge column names??? #httemplate/view/cust_main/tickets.html has column
92 #names that might not make sense now...
93 $sth->fetchall_arrayref({});
98 my( $self, $custnum, $priority ) = @_;
103 if ( defined($priority) ) {
105 my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM Queues
106 WHERE Queues.Name = ? )
107 OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
109 my $customfield_sql =
111 SELECT CustomFields.Id FROM CustomFields
112 JOIN ObjectCustomFields
113 ON ( CustomFields.id = ObjectCustomFields.CustomField )
114 WHERE LookupType = 'RT::Queue-RT::Ticket'
119 push @param, $priority_field,
120 $priority_field_queue,
121 $priority_field_queue;
123 if ( length($priority) ) {
125 # and ? = ( select content from TicketCustomFieldValues
126 # where ticket = tickets.id
127 # and customfield = ( select id from customfields
133 unshift @param, $priority;
135 $join = "JOIN ObjectCustomFieldValues
136 ON ( Tickets.id = ObjectCustomFieldValues.ObjectId )";
138 $where = " AND Content = ?
139 AND ObjectCustomFieldValues.Disabled != 1
140 AND ObjectType = 'RT::Ticket'
141 AND $customfield_sql";
146 "AND 0 = ( SELECT COUNT(*) FROM ObjectCustomFieldValues
147 WHERE ObjectId = Tickets.id
148 AND ObjectType = 'RT::Ticket'
158 JOIN Queues ON ( Tickets.Queue = Queues.id )
159 JOIN Links ON ( Tickets.id = Links.LocalBase )
160 JOIN Users ON ( Tickets.Owner = Users.id )
162 WHERE ( ". join(' OR ', map "Status = '$_'", $self->statuses ). " )
163 AND Target = 'freeside://freeside/cust_main/$custnum'
173 my @statuses = grep { ! /^\s*$/ } $conf->config('cust_main-ticket_statuses');
174 @statuses = (qw( new open stalled )) unless scalar(@statuses);
178 sub href_customer_tickets {
179 my( $self, $custnum ) = ( shift, shift );
180 my( $priority, @statuses);
183 $priority = $opt->{'priority'};
184 @statuses = $opt->{'statuses'} ? @{$opt->{'statuses'}} : $self->statuses;
187 @statuses = $self->statuses;
190 #my $href = $self->baseurl;
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;'
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 ). " ) "
203 if ( defined($priority) && $field && $priority_field_queue ) {
204 $href .= " AND Queue = '$priority_field_queue' ";
206 if ( defined($priority) && $field ) {
207 $href .= " AND 'CF.$field' ";
209 $href .= "= '$priority' ";
211 $href .= "IS 'NULL' "; #this is "RTQL", not SQL
217 #eventually should unescape all of it...
219 $href .= '&Rows=100'.
220 '&OrderBy=id&Page=1'.
221 '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
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'.
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';
227 if ( defined($priority) && $field ) {
228 $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
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';
233 if ( defined($priority) && $field ) {
234 $href .= '%20%0A%27__-__%27%2C';
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';
242 $self->baseurl. $href;
246 sub href_new_ticket {
247 my( $self, $custnum_or_cust_main, $requestors ) = @_;
249 my( $custnum, $cust_main );
250 if ( ref($custnum_or_cust_main) ) {
251 $cust_main = $custnum_or_cust_main;
252 $custnum = $cust_main->custnum;
254 $custnum = $custnum_or_cust_main;
255 $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
257 my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
260 'Ticket/Create.html?'.
262 "&new-MemberOf=freeside://freeside/cust_main/$custnum".
263 ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
268 my($self, $ticketnum) = @_;
269 $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
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";
279 map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
284 my($self, $queueid) = @_;
286 return '' unless $queueid;
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";
292 my $rows = $sth->fetchrow_arrayref;
293 $rows ? $rows->[0] : '';