This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / rt / lib / RT / URI / freeside.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 2004 Kristian Hoffmann <khoff@fire2wire.com>
4 # Based on the original RT::URI::base and RT::URI::fsck_com_rt.
5
6 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
7
8 # (Except where explictly superceded by other copyright notices)
9
10 # This work is made available to you under the terms of Version 2 of
11 # the GNU General Public License. A copy of that license should have
12 # been provided with this software, but in any event can be snarfed
13 # from www.gnu.org.
14
15 # This work is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # General Public License for more details.
19
20 # Unless otherwise specified, all modifications, corrections or
21 # extensions to this work which alter its source code become the
22 # property of Best Practical Solutions, LLC when submitted for
23 # inclusion in the work.
24
25
26 # END LICENSE BLOCK
27 package RT::URI::freeside;
28
29 use RT::URI::base;
30 use strict;
31 use vars qw(@ISA);
32
33 @ISA = qw/RT::URI::base/;
34
35
36 =head1 NAME
37
38 RT::URI::base
39
40 =head1 DESCRIPTION
41
42 URI handler for freeside URIs.  See http://www.sisd.com/freeside/ for
43 more information on freeside.
44
45 =cut
46
47
48 sub FreesideURIPrefix {
49
50   my $self = shift;
51   return($self->Scheme . '://freeside');
52
53 }
54
55 sub FreesideURILabel {
56
57   my $self = shift;
58
59   return(undef) unless (exists($self->{'fstable'}) and
60                         exists($self->{'fspkey'}));
61
62   my $label;
63   my ($table, $pkey) = ($self->{'fstable'}, $self->{'fspkey'});
64
65   eval {
66     use FS::UID qw(dbh);
67     use FS::Record qw(qsearchs qsearch dbdef);
68     eval "use FS::$table;";
69     use FS::cust_svc;
70
71     my $dbdef = dbdef or die "No dbdef";
72     my $pkeyfield = $dbdef->table($table)->primary_key
73       or die "No primary key for table $table";
74
75     my $rec = qsearchs($table, { $pkeyfield => $pkey })
76       or die "Record with $pkeyfield == $pkey does not exist in table $table";
77
78     if ($table =~ /^svc_/) {
79       if ($rec->can('cust_svc')) {
80         my $cust_svc = $rec->cust_svc or die '$rec->cust_svc failed';
81         my ($svc, $tag, $svcdb) = $cust_svc->label;
82         $label = "Freeside service ${svc}: ${tag}";
83       }
84     } elsif ($table eq 'cust_main') {
85       #my ($last, $first, $company) = map { $rec->getfield($_) }
86       #                                   qw(last first company);
87       #$label = "Freeside customer ${last}, ${first}";
88       #$label .= ($company ne '') ? " with ${company}" : '';
89       $label = "$pkey: ". $rec->name;
90     } else {
91       $label = "Freeside ${table}, ${pkeyfield} == ${pkey}";
92     }
93
94     #... other cases
95
96   };
97
98   if ($label and !$@) {
99     return($label);
100   } else {
101     return(undef);
102   }
103       
104
105 }
106
107 sub FreesideURILabelLong {
108
109   my $self = shift;
110
111   return(undef) unless (exists($self->{'fstable'}) and
112                         exists($self->{'fspkey'}));
113
114   my $label;
115   my ($table, $pkey) = ($self->{'fstable'}, $self->{'fspkey'});
116
117   eval {
118     use FS::UID qw(dbh);
119     use FS::Record qw(qsearchs qsearch dbdef);
120     eval "use FS::$table;";
121     use FS::cust_svc;
122
123     my $dbdef = dbdef or die "No dbdef";
124     my $pkeyfield = $dbdef->table($table)->primary_key
125       or die "No primary key for table $table";
126
127     my $rec = qsearchs($table, { $pkeyfield => $pkey })
128       or die "Record with $pkeyfield == $pkey does not exist in table $table";
129
130     if ($table =~ /^svc_/) {
131       #if ($rec->can('cust_svc')) {
132       #  my $cust_svc = $rec->cust_svc or die '$rec->cust_svc failed';
133       #  my ($svc, $tag, $svcdb) = $cust_svc->label;
134       #  $label = "Freeside service ${svc}: ${tag}";
135       #}
136       $label = '';
137     } elsif ($table eq 'cust_main') {
138       use FS::CGI qw(small_custview);
139       $label = small_custview( $rec,
140                                scalar(FS::Conf->new->config('countrydefault')),
141                                1 #nobalance
142                              );
143     } else {
144       #$label = "Freeside ${table}, ${pkeyfield} == ${pkey}";
145       $label = '';
146     }
147
148     #... other cases
149
150   };
151
152   if ($label and !$@) {
153     return($label);
154   } else {
155     warn $@;
156     return(undef);
157   }
158       
159
160 }
161
162 sub ParseURI { 
163     my $self = shift;
164     my $uri = shift;
165     my ($table, $pkey);
166
167     my $uriprefix = $self->FreesideURIPrefix;
168     if ($uri =~ /^$uriprefix\/(\w+)\/(\d+)$/) {
169       $table = $1;
170       $pkey = $2;
171       $self->{'scheme'} = $self->Scheme;
172     } else {
173       return(undef);
174     }
175
176     $self->{'uri'} = "${uriprefix}/${table}/${pkey}";
177     $self->{'fstable'} = $table;
178     $self->{'fspkey'} = $pkey;
179
180     my $p;
181
182     eval {
183       use FS::UID qw(dbh);
184       use FS::CGI qw(popurl);
185
186       if (dbh) {
187         $p = popurl(3);
188       }
189
190     };
191
192     if ($@ or (!$p)) {
193       $self->{'href'} = $self->{'uri'};
194     } else {
195       $self->{'href'} = "${p}view/${table}.cgi?${pkey}";
196     }
197
198     $self->{'uri'};
199
200 }
201
202 sub Scheme { 
203     my $self = shift;
204     return('freeside');
205
206 }
207
208 sub HREF {
209     my $self = shift;
210     return($self->{'href'} || $self->{'uri'});
211 }
212
213 sub IsLocal {
214     my $self = shift;
215     return undef;
216 }
217
218 =head2 AsString
219
220 Return a "pretty" string representing the URI object.
221
222 This is meant to be used like this:
223
224  % $re = $uri->Resolver;
225  <A HREF="<% $re->HREF %>"><% $re->AsString %></A>
226
227 =cut
228
229 sub AsString {
230     my $self = shift;
231     my $prettystring;
232     if ($prettystring = $self->FreesideURILabel) {
233       return $prettystring;
234     } else {
235       return $self->URI;
236     }
237 }
238
239 =head2 AsStringLong
240
241 Return a longer (HTML) string representing the URI object.
242
243 =cut
244
245 sub AsStringLong {
246     my $self = shift;
247     my $prettystring;
248     if ($prettystring = $self->FreesideURILabelLong || $self->FreesideURILabel){
249       return $prettystring;
250     } else {
251       return $self->URI;
252     }
253 }
254
255 eval "require RT::URI::base_Vendor";
256 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI/base_Vendor.pm});
257 eval "require RT::URI::base_Local";
258 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI/base_Local.pm});
259
260 1;