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 @custnums = map { /^Ticket-AddCustomer-(\d+)$/; $1 }
83 grep { /^Ticket-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
86 #my @delete_custnums =
87 # map { /^Ticket-AddCustomer-(\d+)$/; $1 }
88 # grep { /^Ticket-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
92 #figure out if we're going to auto-link requestors, and find them if so
95 my $num_cur_cust = $Ticket->Customers->Count;
96 my $num_new_cust = scalar(@custnums);
97 warn "$me: $num_cur_cust current customers / $num_new_cust new customers\n"
100 #if we're linking the first ticket to one customer
101 my $link_requestors = ( $num_cur_cust == 0 && $num_new_cust == 1 );
102 warn "$me: adding a single customer to a previously customerless".
103 " ticket, so linking customers to requestor too\n"
104 if $Debug && $link_requestors;
107 if ( $link_requestors ) {
109 #find any requestors without customers
111 grep { ! $_->Customers->Count }
112 @{ $Ticket->Requestors->UserMembersObj->ItemsArrayRef };
114 warn "$me: found ". scalar(@Requestors). " requestors without".
115 " customers; linking them\n"
121 #remove any declared non-customer addresses
124 my $exclude_regexp = RT->Config->Get('NonCustomerEmailRegexp');
125 @Requestors = grep { not $_->EmailAddress =~ $exclude_regexp } @Requestors
126 if defined $exclude_regexp;
129 #link ticket (and requestors) to customers
132 foreach my $custnum ( @custnums ) {
134 my @link = ( 'Type' => 'MemberOf',
135 'Target' => "freeside://freeside/cust_main/$custnum",
138 my( $val, $msg ) = $Ticket->AddLink(@link);
141 #add customer links to requestors
142 foreach my $Requestor ( @Requestors ) {
143 my( $val, $msg ) = $Requestor->AddLink(@link);
145 warn "$me: linking requestor to custnum $custnum: $msg\n"
155 #false laziness w/above... eventually it should go away in favor of this
156 sub ProcessObjectCustomers {
164 my $Object = $args{'Object'};
165 my $ARGSRef = $args{'ARGSRef'};
167 ### false laziness w/RT::Interface::Web::ProcessTicketLinks
168 # Delete links that are gone gone gone.
169 foreach my $arg ( keys %$ARGSRef ) {
170 if ( $arg =~ /DeleteLink-(.*?)-(DependsOn|MemberOf|RefersTo)-(.*)$/ ) {
176 "Trying to delete: Base: $base Target: $target Type $type";
177 my ( $val, $msg ) = $Object->DeleteLink( Base => $base,
188 #my @delete_custnums =
189 # map { /^Object-AddCustomer-(\d+)$/; $1 }
190 # grep { /^Object-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
193 my @custnums = map { /^Object-AddCustomer-(\d+)$/; $1 }
194 grep { /^Object-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
197 foreach my $custnum ( @custnums ) {
199 $Object->AddLink( 'Type' => 'MemberOf',
200 'Target' => "freeside://freeside/cust_main/$custnum",
209 =head2 ProcessTicketBasics ( TicketObj => $Ticket, ARGSRef => \%ARGS );
211 Updates all core ticket fields except Status, and returns an array of results
216 sub ProcessTicketBasics {
224 my $TicketObj = $args{'TicketObj'};
225 my $ARGSRef = $args{'ARGSRef'};
227 # {{{ Set basic fields
239 if ( $ARGSRef->{'Queue'} and ( $ARGSRef->{'Queue'} !~ /^(\d+)$/ ) ) {
240 my $tempqueue = RT::Queue->new($RT::SystemUser);
241 $tempqueue->Load( $ARGSRef->{'Queue'} );
242 if ( $tempqueue->id ) {
243 $ARGSRef->{'Queue'} = $tempqueue->id;
247 my @results = UpdateRecordObject(
248 AttributesRef => \@attribs,
249 Object => $TicketObj,
253 # We special case owner changing, so we can use ForceOwnerChange
254 if ( $ARGSRef->{'Owner'} && ( $TicketObj->Owner != $ARGSRef->{'Owner'} ) ) {
256 if ( $ARGSRef->{'ForceOwnerChange'} ) {
257 $ChownType = "Force";
262 my ( $val, $msg ) = $TicketObj->SetOwner( $ARGSRef->{'Owner'}, $ChownType );
263 push( @results, $msg );
269 =head2 ProcessTicketDates (TicketObj => RT::Ticket, ARGSRef => {})
271 Process updates to the Starts, Started, Told, Resolved, and WillResolve
276 sub ProcessTicketDates {
283 my $Ticket = $args{'TicketObj'};
284 my $ARGSRef = $args{'ARGSRef'};
288 # {{{ Set date fields
289 my @date_fields = qw(
298 #Run through each field in this list. update the value if apropriate
299 foreach my $field (@date_fields) {
300 next unless exists $ARGSRef->{ $field . '_Date' };
301 next if $ARGSRef->{ $field . '_Date' } eq '';
305 my $DateObj = RT::Date->new( $session{'CurrentUser'} );
308 Value => $ARGSRef->{ $field . '_Date' }
311 my $obj = $field . "Obj";
312 if ( ( defined $DateObj->Unix )
313 and ( $DateObj->Unix != $Ticket->$obj()->Unix() ) )
315 my $method = "Set$field";
316 my ( $code, $msg ) = $Ticket->$method( $DateObj->ISO );
317 push @results, "$msg";
325 =head2 ProcessTicketStatus (TicketObj => RT::Ticket, ARGSRef => {})
327 Process updates to the 'Status' field of the ticket. If the new value
328 of Status is 'resolved', this will check required custom fields before
333 sub ProcessTicketStatus {
340 my $TicketObj = $args{'TicketObj'};
341 my $ARGSRef = $args{'ARGSRef'};
344 return () if !$ARGSRef->{'Status'};
346 if ( lc( $ARGSRef->{'Status'} ) eq 'resolved' ) {
347 foreach my $field ( $TicketObj->MissingRequiredFields ) {
348 push @results, loc('Missing required field: [_1]', $field->Name);
352 $m->notes('RedirectToBasics' => 1);
356 return UpdateRecordObject(
357 AttributesRef => [ 'Status' ],
358 Object => $TicketObj,
363 sub default_FormatDate { $_[0]->AsString }
365 sub ProcessColumnMapValue {
367 my %args = ( Arguments => [],
369 FormatDate => \&default_FormatDate,
373 if ( ref $value eq 'RT::Date' ) {
374 return $args{FormatDate}->($value);
375 } elsif ( UNIVERSAL::isa( $value, 'CODE' ) ) {
376 my @tmp = $value->( @{ $args{'Arguments'} } );
377 return ProcessColumnMapValue( ( @tmp > 1 ? \@tmp : $tmp[0] ), %args );
378 } elsif ( UNIVERSAL::isa( $value, 'ARRAY' ) ) {
379 return join '', map ProcessColumnMapValue( $_, %args ), @$value;
380 } elsif ( UNIVERSAL::isa( $value, 'SCALAR' ) ) {
385 return $m->interp->apply_escapes( $value, 'h' ) if $args{'Escape'};