This commit was generated by cvs2svn to compensate for changes in r9232,
[freeside.git] / rt / lib / RT / Interface / Web_Vendor.pm
1 # Copyright (c) 2004 Ivan Kohler <ivan-rt@420.am>
2 # Copyright (c) 2008 Freeside Internet Services, Inc.
3 #
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
7 # from www.gnu.org.
8
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.
13
14 =head1 NAME
15
16 RT::Interface::Web_Vendor
17
18 =head1 SYNOPSIS
19
20 =head1 DESCRIPTION
21
22 Freeside vendor overlay for RT::Interface::Web.
23
24 =begin testing
25
26 use_ok(RT::Interface::Web_Vendor);
27
28 =end testing
29
30 =cut
31
32 #package RT::Interface::Web;
33 #use strict;
34
35 package HTML::Mason::Commands;
36 use strict;
37
38 =head2 ProcessTicketCustomers 
39
40 =cut
41
42 sub ProcessTicketCustomers {
43     my %args = (
44         TicketObj => undef,
45         ARGSRef   => undef,
46         Debug     => 0,
47         @_
48     );
49     my @results = ();
50
51     my $Ticket  = $args{'TicketObj'};
52     my $ARGSRef = $args{'ARGSRef'};
53     my $Debug   = $args{'Debug'};
54     my $me = 'ProcessTicketCustomers';
55
56     ### false laziness w/RT::Interface::Web::ProcessTicketLinks
57     # Delete links that are gone gone gone.
58     foreach my $arg ( keys %$ARGSRef ) {
59         if ( $arg =~ /DeleteLink-(.*?)-(DependsOn|MemberOf|RefersTo)-(.*)$/ ) {
60             my $base   = $1;
61             my $type   = $2;
62             my $target = $3;
63
64             push @results,
65               "Trying to delete: Base: $base Target: $target  Type $type";
66             my ( $val, $msg ) = $Ticket->DeleteLink( Base   => $base,
67                                                      Type   => $type,
68                                                      Target => $target );
69
70             push @results, $msg;
71
72         }
73
74     }
75     ###
76
77     ###
78     #find new customers
79     ###
80
81     my @custnums = map  { /^Ticket-AddCustomer-(\d+)$/; $1 }
82                    grep { /^Ticket-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
83                    keys %$ARGSRef;
84
85     #my @delete_custnums =
86     #  map  { /^Ticket-AddCustomer-(\d+)$/; $1 }
87     #  grep { /^Ticket-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
88     #  keys %$ARGSRef;
89
90     ###
91     #figure out if we're going to auto-link requestors, and find them if so
92     ###
93
94     my $num_cur_cust = $Ticket->Customers->Count;
95     my $num_new_cust = scalar(@custnums);
96     warn "$me: $num_cur_cust current customers / $num_new_cust new customers\n"
97       if $Debug;
98
99     #if we're linking the first ticket to one customer
100     my $link_requestors = ( $num_cur_cust == 0 && $num_new_cust == 1 );
101     warn "$me: adding a single customer to a previously customerless".
102          " ticket, so linking customers to requestor too\n"
103       if $Debug && $link_requestors;
104
105     my @Requestors = ();
106     if ( $link_requestors ) {
107
108       #find any requestors without customers
109       @Requestors =
110         grep { ! $_->Customers->Count }
111              @{ $Ticket->Requestors->UserMembersObj->ItemsArrayRef };
112
113       warn "$me: found ". scalar(@Requestors). " requestors without".
114            " customers; linking them\n"
115         if $Debug;
116
117     }
118
119     ###
120     #link ticket (and requestors) to customers
121     ###
122
123     foreach my $custnum ( @custnums ) {
124
125       my @link = ( 'Type'   => 'MemberOf',
126                    'Target' => "freeside://freeside/cust_main/$custnum",
127                  );
128
129       my( $val, $msg ) = $Ticket->AddLink(@link);
130       push @results, $msg;
131
132       #add customer links to requestors
133       foreach my $Requestor ( @Requestors ) {
134         my( $val, $msg ) = $Requestor->AddLink(@link);
135         push @results, $msg;
136         warn "$me: linking requestor to custnum $custnum: $msg\n"
137           if $Debug > 1;
138       }
139
140     }
141
142     return @results;
143
144 }
145
146 #false laziness w/above... eventually it should go away in favor of this
147 sub ProcessObjectCustomers {
148     my %args = (
149         Object => undef,
150         ARGSRef   => undef,
151         @_
152     );
153     my @results = ();
154
155     my $Object  = $args{'Object'};
156     my $ARGSRef = $args{'ARGSRef'};
157
158     ### false laziness w/RT::Interface::Web::ProcessTicketLinks
159     # Delete links that are gone gone gone.
160     foreach my $arg ( keys %$ARGSRef ) {
161         if ( $arg =~ /DeleteLink-(.*?)-(DependsOn|MemberOf|RefersTo)-(.*)$/ ) {
162             my $base   = $1;
163             my $type   = $2;
164             my $target = $3;
165
166             push @results,
167               "Trying to delete: Base: $base Target: $target  Type $type";
168             my ( $val, $msg ) = $Object->DeleteLink( Base   => $base,
169                                                      Type   => $type,
170                                                      Target => $target );
171
172             push @results, $msg;
173
174         }
175
176     }
177     ###
178
179     #my @delete_custnums =
180     #  map  { /^Object-AddCustomer-(\d+)$/; $1 }
181     #  grep { /^Object-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
182     #  keys %$ARGSRef;
183
184     my @custnums = map  { /^Object-AddCustomer-(\d+)$/; $1 }
185                    grep { /^Object-AddCustomer-(\d+)$/ && $ARGSRef->{$_} }
186                    keys %$ARGSRef;
187
188     foreach my $custnum ( @custnums ) {
189       my( $val, $msg ) =
190         $Object->AddLink( 'Type'   => 'MemberOf',
191                           'Target' => "freeside://freeside/cust_main/$custnum",
192                         );
193       push @results, $msg;
194     }
195
196     return @results;
197
198 }
199
200 1;
201