1 # Copyright (c) 2004 Ivan Kohler <ivan-rt@420.am>
2 # Copyright (c) 2008 Freeside Internet Services, Inc.
4 # This work is made available to you under the terms of Version 2 of
5 # the GNU General Public License. A copy of that license should have
6 # been provided with this software, but in any event can be snarfed
9 # This work is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
16 RT::Interface::Web_Vendor
22 Freeside vendor overlay for RT::Interface::Web.
26 use_ok(RT::Interface::Web_Vendor);
32 #package RT::Interface::Web;
35 package HTML::Mason::Commands;
37 no warnings qw(redefine);
39 =head2 ProcessTicketCustomers
43 sub ProcessTicketCustomers {
52 my $Ticket = $args{'TicketObj'};
53 my $ARGSRef = $args{'ARGSRef'};
54 my $Debug = $args{'Debug'};
55 my $me = 'ProcessTicketCustomers';
57 ### false laziness w/RT::Interface::Web::ProcessTicketLinks
58 # Delete links that are gone gone gone.
59 foreach my $arg ( keys %$ARGSRef ) {
60 if ( $arg =~ /DeleteLink-(.*?)-(DependsOn|MemberOf|RefersTo)-(.*)$/ ) {
66 "Trying to delete: Base: $base Target: $target Type $type";
67 my ( $val, $msg ) = $Ticket->DeleteLink( Base => $base,
82 my @svcnums = map { /^Ticket-AddService-(\d+)$/; $1 }
83 grep { /^Ticket-AddService-(\d+)$/ && $ARGSRef->{$_} }
87 foreach my $svcnum (@svcnums) {
88 my @link = ( 'Type' => 'MemberOf',
89 'Target' => "freeside://freeside/cust_svc/$svcnum",
92 my( $val, $msg ) = $Ticket->AddLink(@link);
102 push @custnums, map { /^Ticket-AddCustomer-(\d+)$/; $1 }
103 grep { /^Ticket-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
106 #my @delete_custnums =
107 # map { /^Ticket-AddCustomer-(\d+)$/; $1 }
108 # grep { /^Ticket-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
112 #figure out if we're going to auto-link requestors, and find them if so
115 my $num_cur_cust = $Ticket->Customers->Count;
116 my $num_new_cust = scalar(@custnums);
117 warn "$me: $num_cur_cust current customers / $num_new_cust new customers\n"
120 #if we're linking the first ticket to one customer
121 my $link_requestors = ( $num_cur_cust == 0 && $num_new_cust == 1 );
122 warn "$me: adding a single customer to a previously customerless".
123 " ticket, so linking customers to requestor too\n"
124 if $Debug && $link_requestors;
127 if ( $link_requestors ) {
129 #find any requestors without customers
131 grep { ! $_->Customers->Count }
132 @{ $Ticket->Requestors->UserMembersObj->ItemsArrayRef };
134 warn "$me: found ". scalar(@Requestors). " requestors without".
135 " customers; linking them\n"
141 #remove any declared non-customer addresses
144 my $exclude_regexp = RT->Config->Get('NonCustomerEmailRegexp');
145 @Requestors = grep { not $_->EmailAddress =~ $exclude_regexp } @Requestors
146 if defined $exclude_regexp;
149 #link ticket (and requestors) to customers
152 foreach my $custnum ( @custnums ) {
154 my @link = ( 'Type' => 'MemberOf',
155 'Target' => "freeside://freeside/cust_main/$custnum",
158 my( $val, $msg ) = $Ticket->AddLink(@link);
161 #add customer links to requestors
162 foreach my $Requestor ( @Requestors ) {
163 my( $val, $msg ) = $Requestor->AddLink(@link);
165 warn "$me: linking requestor to custnum $custnum: $msg\n"
168 ## check if FS contact email exists, if not create it.
170 'table' => 'contact_email',
171 'hashref' => { 'emailaddress' => $Requestor->{'values'}->{'emailaddress'}, },
175 my $lname = $Requestor->{'values'}->{'realname'} ?
176 (split (/ /, $Requestor->{'values'}->{'realname'}))[-1] :
179 my $fname = $Requestor->{'values'}->{'realname'} ?
180 (split (/ /, $Requestor->{'values'}->{'realname'}))[0] :
183 my $contact = new FS::contact {
184 'custnum' => $custnum,
187 'emailaddress' => $Requestor->{'values'}->{'emailaddress'},
188 'comment' => 'Auto created from RT requestor',
190 my $error = $contact->insert;
191 push @results, 'Created Freeside contact for requestor ' . $Requestor->{'values'}->{'emailaddress'}
202 #false laziness w/above... eventually it should go away in favor of this
203 sub ProcessObjectCustomers {
211 my $Object = $args{'Object'};
212 my $ARGSRef = $args{'ARGSRef'};
214 ### false laziness w/RT::Interface::Web::ProcessTicketLinks
215 # Delete links that are gone gone gone.
216 foreach my $arg ( keys %$ARGSRef ) {
217 if ( $arg =~ /DeleteLink-(.*?)-(DependsOn|MemberOf|RefersTo)-(.*)$/ ) {
223 "Trying to delete: Base: $base Target: $target Type $type";
224 my ( $val, $msg ) = $Object->DeleteLink( Base => $base,
235 #my @delete_custnums =
236 # map { /^Object-AddCustomer-(\d+)$/; $1 }
237 # grep { /^Object-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
240 my @custnums = map { /^Object-AddCustomer-(\d+)$/; $1 }
241 grep { /^Object-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
244 foreach my $custnum ( @custnums ) {
246 $Object->AddLink( 'Type' => 'MemberOf',
247 'Target' => "freeside://freeside/cust_main/$custnum",
256 =head2 ProcessTicketBasics ( TicketObj => $Ticket, ARGSRef => \%ARGS );
258 Updates all core ticket fields except Status, and returns an array of results
263 sub ProcessTicketBasics {
271 my $TicketObj = $args{'TicketObj'};
272 my $ARGSRef = $args{'ARGSRef'};
274 # {{{ Set basic fields
287 # the UI for editing WillResolve through Ticket Basics should allow
289 if ( exists $ARGSRef->{'WillResolve_Date'} ) {
290 my $to_date = delete($ARGSRef->{'WillResolve_Date'});
291 my $DateObj = RT::Date->new($session{'CurrentUser'});
293 $DateObj->Set(Format => 'unknown', Value => $to_date);
294 if ( $DateObj->Unix > time ) {
295 $ARGSRef->{'WillResolve'} = $DateObj->ISO;
297 warn "Ticket ".$TicketObj->Id.": WillResolve date '$to_date' not accepted.\n";
298 # and then don't set it in ARGSRef
300 } elsif ( $TicketObj and $TicketObj->WillResolveObj->Unix > 0 ) {
301 $DateObj->Set(Value => 0);
302 $ARGSRef->{'WillResolve'} = $DateObj->ISO;
306 if ( $ARGSRef->{'Queue'} and ( $ARGSRef->{'Queue'} !~ /^(\d+)$/ ) ) {
307 my $tempqueue = RT::Queue->new($RT::SystemUser);
308 $tempqueue->Load( $ARGSRef->{'Queue'} );
309 if ( $tempqueue->id ) {
310 $ARGSRef->{'Queue'} = $tempqueue->id;
314 # RT core _will_ allow Set transactions that change these
315 # fields to empty strings, but internally change the values
316 # to zero. This is sloppy and causes some problems.
317 foreach my $field (qw(TimeWorked TimeEstimated TimeLeft)) {
318 if (exists $ARGSRef->{$field}) {
319 $ARGSRef->{$field} =~ s/\s//g;
320 $ARGSRef->{$field} ||= 0;
324 my @results = UpdateRecordObject(
325 AttributesRef => \@attribs,
326 Object => $TicketObj,
330 # We special case owner changing, so we can use ForceOwnerChange
331 if ( $ARGSRef->{'Owner'} && ( $TicketObj->Owner != $ARGSRef->{'Owner'} ) ) {
333 if ( $ARGSRef->{'ForceOwnerChange'} ) {
334 $ChownType = "Force";
339 my ( $val, $msg ) = $TicketObj->SetOwner( $ARGSRef->{'Owner'}, $ChownType );
340 push( @results, $msg );
346 =head2 ProcessTicketDates (TicketObj => RT::Ticket, ARGSRef => {})
348 Process updates to the Starts, Started, Told, Resolved, and WillResolve
353 sub ProcessTicketDates {
360 my $Ticket = $args{'TicketObj'};
361 my $ARGSRef = $args{'ARGSRef'};
365 # {{{ Set date fields
366 my @date_fields = qw(
375 #Run through each field in this list. update the value if apropriate
376 foreach my $field (@date_fields) {
377 next unless exists $ARGSRef->{ $field . '_Date' };
378 next if $ARGSRef->{ $field . '_Date' } eq '';
382 my $DateObj = RT::Date->new( $session{'CurrentUser'} );
385 Value => $ARGSRef->{ $field . '_Date' }
388 if ( $field eq 'WillResolve'
389 and $DateObj->Unix > 0
390 and $DateObj->Unix <= time ) {
391 push @results, "Can't set WillResolve date in the past.";
395 my $obj = $field . "Obj";
396 if ( ( defined $DateObj->Unix )
397 and ( $DateObj->Unix != $Ticket->$obj()->Unix() ) )
399 my $method = "Set$field";
400 my ( $code, $msg ) = $Ticket->$method( $DateObj->ISO );
401 push @results, "$msg";
409 =head2 ProcessTicketStatus (TicketObj => RT::Ticket, ARGSRef => {})
411 Process updates to the 'Status' field of the ticket. If the new value
412 of Status is 'resolved', this will check required custom fields before
417 sub ProcessTicketStatus {
424 my $TicketObj = $args{'TicketObj'};
425 my $ARGSRef = $args{'ARGSRef'};
428 return () if !$ARGSRef->{'Status'};
430 if ( lc( $ARGSRef->{'Status'} ) eq 'resolved' ) {
431 foreach my $field ( $TicketObj->MissingRequiredFields ) {
432 push @results, loc('Missing required field: [_1]', $field->Name);
436 $m->notes('RedirectToBasics' => 1);
440 return UpdateRecordObject(
441 AttributesRef => [ 'Status' ],
442 Object => $TicketObj,
447 sub default_FormatDate { $_[0]->AsString }
449 sub ProcessColumnMapValue {
451 my %args = ( Arguments => [],
453 FormatDate => \&default_FormatDate,
457 if ( ref $value eq 'RT::Date' ) {
458 return $args{FormatDate}->($value);
459 } elsif ( UNIVERSAL::isa( $value, 'CODE' ) ) {
460 my @tmp = $value->( @{ $args{'Arguments'} } );
461 return ProcessColumnMapValue( ( @tmp > 1 ? \@tmp : $tmp[0] ), %args );
462 } elsif ( UNIVERSAL::isa( $value, 'ARRAY' ) ) {
463 return join '', map ProcessColumnMapValue( $_, %args ), @$value;
464 } elsif ( UNIVERSAL::isa( $value, 'SCALAR' ) ) {
469 return $m->interp->apply_escapes( $value, 'h' ) if $args{'Escape'};