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 );
74 my $sql="SELECT tickets.*, queues.name, ".
75 "position(tickets.status in 'newopenstalledresolvedrejecteddeleted')".
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";
83 #munge column names??? #httemplate/view/cust_main/tickets.html has column
84 #names that might not make sense now...
85 $sth->fetchall_arrayref({});
90 my( $self, $custnum, $priority ) = @_;
95 if ( defined($priority) ) {
97 my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM queues
98 WHERE queues.name = ? )
99 OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
101 my $customfield_sql =
103 SELECT CustomFields.Id FROM CustomFields
104 JOIN ObjectCustomFields
105 ON ( CustomFields.id = ObjectCustomFields.CustomField )
106 WHERE LookupType = 'RT::Queue-RT::Ticket'
111 push @param, $priority_field,
112 $priority_field_queue,
113 $priority_field_queue;
115 if ( length($priority) ) {
117 # and ? = ( select content from TicketCustomFieldValues
118 # where ticket = tickets.id
119 # and customfield = ( select id from customfields
125 unshift @param, $priority;
127 $join = "JOIN ObjectCustomFieldValues
128 ON ( tickets.id = ObjectCustomFieldValues.ObjectId )";
130 $where = " AND content = ?
131 AND ObjectCustomFieldValues.disabled != 1
132 AND ObjectType = 'RT::Ticket'
133 AND $customfield_sql";
138 "AND 0 = ( SELECT count(*) FROM ObjectCustomFieldValues
139 WHERE ObjectId = tickets.id
140 AND ObjectType = 'RT::Ticket'
150 JOIN queues ON ( tickets.queue = queues.id )
151 JOIN links ON ( tickets.id = links.localbase )
153 WHERE ( ". join(' OR ', map "status = '$_'", $self->statuses ). " )
154 AND target = 'freeside://freeside/cust_main/$custnum'
164 my @statuses = grep { ! /^\s*$/ } $conf->config('cust_main-ticket_statuses');
165 @statuses = (qw( new open stalled )) unless scalar(@statuses);
169 sub href_customer_tickets {
170 my( $self, $custnum, $priority ) = @_;
172 #my $href = $self->baseurl;
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;'
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 ). " ) "
184 if ( defined($priority) && $field && $priority_field_queue ) {
185 $href .= " AND Queue = '$priority_field_queue' ";
187 if ( defined($priority) && $field ) {
188 $href .= " AND 'CF.$field' ";
190 $href .= "= '$priority' ";
192 $href .= "IS 'NULL' "; #this is "RTQL", not SQL
198 #eventually should unescape all of it...
200 $href .= '&Rows=100'.
201 '&OrderBy=id&Page=1'.
202 '&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22'.
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'.
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';
208 if ( defined($priority) && $field ) {
209 $href .= '%0A%27__CustomField.'. $field. '__%2FTITLE%3ASeverity%27%2C%20';
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';
214 if ( defined($priority) && $field ) {
215 $href .= '%20%0A%27__-__%27%2C';
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';
223 $self->baseurl. $href;
227 sub href_new_ticket {
228 my( $self, $custnum_or_cust_main, $requestors ) = @_;
230 my( $custnum, $cust_main );
231 if ( ref($custnum_or_cust_main) ) {
232 $cust_main = $custnum_or_cust_main;
233 $custnum = $cust_main->custnum;
235 $custnum = $custnum_or_cust_main;
236 $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
238 my $queueid = $cust_main->agent->ticketing_queueid || $default_queueid;
241 'Ticket/Create.html?'.
243 "&new-MemberOf=freeside://freeside/cust_main/$custnum".
244 ( $requestors ? '&Requestors='. uri_escape($requestors) : '' )
249 my($self, $ticketnum) = @_;
250 $self->baseurl. 'Ticket/Display.html?id='.$ticketnum;
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";
260 map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref([]) };
265 my($self, $queueid) = @_;
267 return '' unless $queueid;
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";
273 $sth->fetchrow_arrayref->[0];