Freeside's URI handler for RT3.
[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     } else {
90       $label = "Freeside ${table}, ${pkeyfield} == ${pkey}";
91     }
92
93     #... other cases
94
95   };
96
97   if ($label and !$@) {
98     return($label);
99   } else {
100     return(undef);
101   }
102       
103
104 }
105
106 sub ParseURI { 
107     my $self = shift;
108     my $uri = shift;
109     my ($table, $pkey);
110
111     my $uriprefix = $self->FreesideURIPrefix;
112     if ($uri =~ /^$uriprefix\/(\w+)\/(\d+)$/) {
113       $table = $1;
114       $pkey = $2;
115       $self->{'scheme'} = $self->Scheme;
116     } else {
117       return(undef);
118     }
119
120     $self->{'uri'} = "${uriprefix}/${table}/${pkey}";
121     $self->{'fstable'} = $table;
122     $self->{'fspkey'} = $pkey;
123
124     my $p;
125
126     eval {
127       use FS::UID qw(dbh);
128       use FS::CGI qw(popurl);
129
130       if (dbh) {
131         $p = popurl(3);
132       }
133
134     };
135
136     if ($@ or (!$p)) {
137       $self->{'href'} = $self->{'uri'};
138     } else {
139       $self->{'href'} = "${p}view/${table}.cgi?${pkey}";
140     }
141
142     $self->{'uri'};
143
144 }
145
146 sub Scheme { 
147     my $self = shift;
148     return('freeside');
149
150 }
151
152 sub HREF {
153     my $self = shift;
154     return($self->{'href'} || $self->{'uri'});
155 }
156
157 sub IsLocal {
158     my $self = shift;
159     return undef;
160 }
161
162 =head2 AsString
163
164 Return a "pretty" string representing the URI object.
165
166 This is meant to be used like this:
167
168  % $re = $uri->Resolver;
169  <A HREF="<% $re->HREF %>"><% $re->AsString %></A>
170
171 =cut
172
173 sub AsString {
174     my $self = shift;
175     my $prettystring;
176     if ($prettystring = $self->FreesideURILabel) {
177       return $prettystring;
178     } else {
179       return $self->URI;
180     }
181 }
182
183 eval "require RT::URI::base_Vendor";
184 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI/base_Vendor.pm});
185 eval "require RT::URI::base_Local";
186 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI/base_Local.pm});
187
188 1;