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;
12 @ISA = qw( FS::TicketSystem::RT_Libs );
15 $me = '[FS::TicketSystem::RT_Internal]';
17 sub sql_num_customer_tickets {
18 "( select count(*) from tickets
19 join links on ( tickets.id = links.localbase )
20 where ( status = 'new' or status = 'open' or status = 'stalled' )
21 and target = 'freeside://freeside/cust_main/' || custnum
27 if ( $RT::URI::freeside::URL ) {
28 $RT::URI::freeside::URL. '/rt/';
30 'http://you_need_to_set_RT_URI_freeside_URL_in_SiteConfig.pm/';
35 #ShowConfigTab ModifySelf
37 my( $self, $session, $right ) = @_;
39 #return '' unless $conf->config('ticket_system');
40 return '' unless FS::Conf->new->config('ticket_system');
42 $session = $self->session($session);
44 $session->{'CurrentUser'}->HasRight( Right => $right,
45 Object => $RT::System );
49 my( $self, $session ) = @_;
51 if ( $session && $session->{'Current_User'} ) {
52 warn "$me session: using existing session and CurrentUser: \n".
53 Dumper($session->{'CurrentUser'})
56 warn "$me session: loading session and CurrentUser\n" if $DEBUG > 1;
57 $session = $self->_web_external_auth($session);
66 warn "$me init: loading RT libraries\n" if $DEBUG;
68 use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
70 #it looks like the rest are taken care of these days in RT::InitClasses
72 #use RT::Transactions;
77 #use RT::ScripActions;
78 #use RT::ScripConditions;
81 #use RT::GroupMembers;
82 #use RT::CustomFields;
83 #use RT::CustomFieldValues;
84 #use RT::ObjectCustomFieldValues;
86 #for web external auth...
87 use RT::Interface::Web;
91 warn "$me init: loading RT config\n" if $DEBUG;
94 eval 'RT::LoadConfig();';
98 warn "$me init: initializing RT\n" if $DEBUG;
101 eval 'RT::Init("NoSignalHandlers"=>1);';
105 warn "$me init: complete" if $DEBUG;
108 =item create_ticket SESSION_HASHREF, OPTION => VALUE ...
110 Class method. Creates a ticket. If there is an error, returns the scalar
111 error, otherwise returns the newly created RT::Ticket object.
113 Accepts the following options:
127 Requestor email address or arrayref of addresses
131 Cc: email address or arrayref of addresses
139 Customer number (see L<FS::cust_main>) to associate with ticket.
143 Service number (see L<FS::cust_svc>) to associate with ticket. Will also
144 associate the customer who has this service (unless the service is unlinked).
151 my($self, $session, %param) = @_;
153 $session = $self->session($session);
155 my $Queue = RT::Queue->new($session->{'CurrentUser'});
156 $Queue->Load( $param{'queue'} );
158 my $req = ref($param{'requestor'})
159 ? $param{'requestor'}
160 : ( $param{'requestor'} ? [ $param{'requestor'} ] : [] );
162 my $cc = ref($param{'cc'})
164 : ( $param{'cc'} ? [ $param{'cc'} ] : [] );
166 my $mimeobj = MIME::Entity->build(
167 'Data' => $param{'message'},
168 'Type' => 'text/plain',
172 'Queue' => $Queue->Id,
173 'Subject' => $param{'subject'},
176 'MIMEObj' => $mimeobj,
178 warn Dumper(\%ticket) if $DEBUG > 1;
180 my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
181 my( $id, $Transaction, $ErrStr );
184 ( $id, $Transaction, $ErrStr ) = $Ticket->Create( %ticket );
186 return $ErrStr if $id == 0;
188 warn "ticket got id $id\n" if $DEBUG;
190 #XXX check errors adding custnum/svcnum links (put it in a transaction)...
191 # but we do already know they're good
193 if ( $param{'custnum'} ) {
194 my( $val, $msg ) = $Ticket->_AddLink(
195 'Type' => 'MemberOf',
196 'Target' => 'freeside://freeside/cust_main/'. $param{'custnum'},
200 if ( $param{'svcnum'} ) {
201 my( $val, $msg ) = $Ticket->_AddLink(
202 'Type' => 'MemberOf',
203 'Target' => 'freeside://freeside/cust_svc/'. $param{'svcnum'},
210 #shameless false laziness w/rt/html/autohandler to get logged into RT from afar
211 sub _web_external_auth {
212 my( $self, $session ) = @_;
214 my $user = $FS::CurrentUser::CurrentUser->username;
217 $session->{'CurrentUser'} = RT::CurrentUser->new();
219 warn "$me _web_external_auth loading RT user for $user\n"
222 $session->{'CurrentUser'}->Load($user);
224 if ( ! $session->{'CurrentUser'}->Id() ) {
226 # Create users on-the-fly
228 warn "can't load RT user for $user; auto-creating\n"
231 my $UserObj = RT::User->new( RT::CurrentUser->new('RT_System') );
233 my ( $val, $msg ) = $UserObj->Create(
234 %{ ref($RT::AutoCreate) ? $RT::AutoCreate : {} },
241 # now get user specific information, to better create our user.
243 = RT::Interface::Web::WebExternalAutoInfo($user);
245 # set the attributes that have been defined.
246 # FIXME: this is a horrible kludge. I'm sure there's something cleaner
247 foreach my $attribute (
249 'Signature', 'EmailAddress',
250 'PagerEmailAddress', 'FreeformContactInfo',
251 'Organization', 'Disabled',
252 'Privileged', 'RealName',
254 'EmailEncoding', 'WebEncoding',
255 'ExternalContactInfoId', 'ContactInfoSystem',
256 'ExternalAuthId', 'Gecos',
257 'HomePhone', 'WorkPhone',
258 'MobilePhone', 'PagerPhone',
259 'Address1', 'Address2',
265 #$m->comp( '/Elements/Callback', %ARGS,
266 # _CallbackName => 'NewUser' );
268 my $method = "Set$attribute";
269 $UserObj->$method( $new_user_info->{$attribute} )
270 if ( defined $new_user_info->{$attribute} );
272 $session->{'CurrentUser'}->Load($user);
276 # we failed to successfully create the user. abort abort abort.
277 delete $session->{'CurrentUser'};
279 die "can't auto-create RT user"; #an error message would be nice :/
280 #$m->abort() unless $RT::WebFallbackToInternalAuth;
281 #$m->comp( '/Elements/Login', %ARGS,
282 # Error => loc( 'Cannot create user: [_1]', $msg ) );
286 unless ( $session->{'CurrentUser'}->Id() ) {
287 delete $session->{'CurrentUser'};
289 die "can't auto-create RT user";
292 #if ($RT::WebExternalOnly) {
293 # $m->comp( '/Elements/Login', %ARGS,
294 # Error => loc('You are not an authorized user') );