1 package FS::TicketSystem::RT_Internal;
4 use vars qw( @ISA $DEBUG $me );
8 use FS::CGI qw(popurl);
9 use FS::TicketSystem::RT_Libs;
11 @ISA = qw( FS::TicketSystem::RT_Libs );
14 $me = '[FS::TicketSystem::RT_Internal]';
16 sub sql_num_customer_tickets {
17 "( select count(*) from Tickets
18 join Links on ( Tickets.id = Links.LocalBase )
19 where ( Status = 'new' or Status = 'open' or Status = 'stalled' )
20 and Target = 'freeside://freeside/cust_main/' || custnum
26 if ( $RT::URI::freeside::URL ) {
27 $RT::URI::freeside::URL. '/rt/';
29 'http://you_need_to_set_RT_URI_freeside_URL_in_SiteConfig.pm/';
34 #ShowConfigTab ModifySelf
36 my( $self, $session, $right ) = @_;
38 #return '' unless $conf->config('ticket_system');
39 return '' unless FS::Conf->new->config('ticket_system');
41 $session = $self->session($session);
43 #warn "$me access_right: CurrentUser ". $session->{'CurrentUser'}. ":\n".
44 # ( $DEBUG>1 ? Dumper($session->{'CurrentUser'}) : '' )
47 $session->{'CurrentUser'}->HasRight( Right => $right,
48 Object => $RT::System );
52 my( $self, $session ) = @_;
54 if ( $session && $session->{'Current_User'} ) {
55 warn "$me session: using existing session and CurrentUser: \n".
56 Dumper($session->{'CurrentUser'})
59 warn "$me session: loading session and CurrentUser\n" if $DEBUG > 1;
60 $session = $self->_web_external_auth($session);
69 warn "$me init: loading RT libraries\n" if $DEBUG;
71 use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
73 #it looks like the rest are taken care of these days in RT::InitClasses
75 #use RT::Transactions;
80 #use RT::ScripActions;
81 #use RT::ScripConditions;
84 #use RT::GroupMembers;
85 #use RT::CustomFields;
86 #use RT::CustomFieldValues;
87 #use RT::ObjectCustomFieldValues;
89 #for web external auth...
90 use RT::Interface::Web;
94 warn "$me init: loading RT config\n" if $DEBUG;
97 eval 'RT::LoadConfig();';
101 warn "$me init: initializing RT\n" if $DEBUG;
104 eval 'RT::Init("NoSignalHandlers"=>1);';
108 warn "$me init: complete" if $DEBUG;
111 =item create_ticket SESSION_HASHREF, OPTION => VALUE ...
113 Class method. Creates a ticket. If there is an error, returns the scalar
114 error, otherwise returns the newly created RT::Ticket object.
116 Accepts the following options:
130 Requestor email address or arrayref of addresses
134 Cc: email address or arrayref of addresses
142 MIME type to use for message. Defaults to text/plain. Specifying text/html
143 can be useful to use HTML markup in message.
147 Customer number (see L<FS::cust_main>) to associate with ticket.
151 Service number (see L<FS::cust_svc>) to associate with ticket. Will also
152 associate the customer who has this service (unless the service is unlinked).
159 my($self, $session, %param) = @_;
161 $session = $self->session($session);
163 my $Queue = RT::Queue->new($session->{'CurrentUser'});
164 $Queue->Load( $param{'queue'} );
166 my $req = ref($param{'requestor'})
167 ? $param{'requestor'}
168 : ( $param{'requestor'} ? [ $param{'requestor'} ] : [] );
170 my $cc = ref($param{'cc'})
172 : ( $param{'cc'} ? [ $param{'cc'} ] : [] );
174 my $mimeobj = MIME::Entity->build(
175 'Data' => $param{'message'},
176 'Type' => ( $param{'mime_type'} || 'text/plain' ),
180 'Queue' => $Queue->Id,
181 'Subject' => $param{'subject'},
184 'MIMEObj' => $mimeobj,
186 warn Dumper(\%ticket) if $DEBUG > 1;
188 my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
189 my( $id, $Transaction, $ErrStr );
192 ( $id, $Transaction, $ErrStr ) = $Ticket->Create( %ticket );
194 return $ErrStr if $id == 0;
196 warn "ticket got id $id\n" if $DEBUG;
198 #XXX check errors adding custnum/svcnum links (put it in a transaction)...
199 # but we do already know they're good
201 if ( $param{'custnum'} ) {
202 my( $val, $msg ) = $Ticket->_AddLink(
203 'Type' => 'MemberOf',
204 'Target' => 'freeside://freeside/cust_main/'. $param{'custnum'},
208 if ( $param{'svcnum'} ) {
209 my( $val, $msg ) = $Ticket->_AddLink(
210 'Type' => 'MemberOf',
211 'Target' => 'freeside://freeside/cust_svc/'. $param{'svcnum'},
218 =item get_ticket SESSION_HASHREF, OPTION => VALUE ...
220 Class method. Retrieves a ticket. If there is an error, returns the scalar
221 error. Otherwise, currently returns a slightly tricky data structure containing
222 a list of the linked customers and each transaction's content, description, and
225 Accepts the following options:
238 my($self, $session, %param) = @_;
240 $session = $self->session($session);
242 my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
243 my $ticketid = $Ticket->Load( $param{'ticket_id'} );
244 return 'Could not load ticket' unless $ticketid;
247 foreach my $link ( @{ $Ticket->Customers->ItemsArrayRef } ) {
248 my $cust = $link->Target;
249 push @custs, $1 if $cust =~ /\/(\d+)$/;
253 my $transactions = $Ticket->Transactions;
254 while ( my $transaction = $transactions->Next ) {
255 my $t = { created => $transaction->Created,
256 content => $transaction->Content,
257 description => $transaction->Description,
268 =item correspond_ticket SESSION_HASHREF, OPTION => VALUE ...
270 Class method. Correspond on a ticket. If there is an error, returns the scalar
271 error. Otherwise, returns the transaction id, error message, and
272 RT::Transaction object.
274 Accepts the following options:
284 Correspondence content
290 sub correspond_ticket {
291 my($self, $session, %param) = @_;
293 $session = $self->session($session);
295 my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
296 my $ticketid = $Ticket->Load( $param{'ticket_id'} );
297 return 'Could not load ticket' unless $ticketid;
298 return 'No content' unless $param{'content'};
300 $Ticket->Correspond( Content => $param{'content'} );
303 #shameless false laziness w/RT::Interface::Web::AttemptExternalAuth
304 # to get logged into RT from afar
305 sub _web_external_auth {
306 my( $self, $session ) = @_;
308 my $user = $FS::CurrentUser::CurrentUser->username;
310 eval 'use RT::CurrentUser;';
314 $session->{'CurrentUser'} = RT::CurrentUser->new();
316 warn "$me _web_external_auth loading RT user for $user\n"
319 $session->{'CurrentUser'}->Load($user);
321 if ( ! $session->{'CurrentUser'}->Id() ) {
323 # Create users on-the-fly
325 warn "can't load RT user for $user; auto-creating\n"
328 my $UserObj = RT::User->new( RT::CurrentUser->new('RT_System') );
330 my ( $val, $msg ) = $UserObj->Create(
331 %{ ref($RT::AutoCreate) ? $RT::AutoCreate : {} },
338 # now get user specific information, to better create our user.
340 = RT::Interface::Web::WebExternalAutoInfo($user);
342 # set the attributes that have been defined.
343 # FIXME: this is a horrible kludge. I'm sure there's something cleaner
344 foreach my $attribute (
346 'Signature', 'EmailAddress',
347 'PagerEmailAddress', 'FreeformContactInfo',
348 'Organization', 'Disabled',
349 'Privileged', 'RealName',
351 'EmailEncoding', 'WebEncoding',
352 'ExternalContactInfoId', 'ContactInfoSystem',
353 'ExternalAuthId', 'Gecos',
354 'HomePhone', 'WorkPhone',
355 'MobilePhone', 'PagerPhone',
356 'Address1', 'Address2',
362 #$m->comp( '/Elements/Callback', %ARGS,
363 # _CallbackName => 'NewUser' );
365 my $method = "Set$attribute";
366 $UserObj->$method( $new_user_info->{$attribute} )
367 if ( defined $new_user_info->{$attribute} );
369 $session->{'CurrentUser'}->Load($user);
373 # we failed to successfully create the user. abort abort abort.
374 delete $session->{'CurrentUser'};
376 die "can't auto-create RT user"; #an error message would be nice :/
377 #$m->abort() unless $RT::WebFallbackToInternalAuth;
378 #$m->comp( '/Elements/Login', %ARGS,
379 # Error => loc( 'Cannot create user: [_1]', $msg ) );
383 unless ( $session->{'CurrentUser'}->Id() ) {
384 delete $session->{'CurrentUser'};
386 die "can't auto-create RT user";
389 #if ($RT::WebExternalOnly) {
390 # $m->comp( '/Elements/Login', %ARGS,
391 # Error => loc('You are not an authorized user') );