generate a stack backtrace for mystery freeside link resolution problems
[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 base qw( RT::URI::base );
30 use strict;
31 use vars qw( $IntegrationType $URL );
32 use Carp qw( cluck );
33
34
35 =head1 NAME
36
37 RT::URI::freeside
38
39 =head1 DESCRIPTION
40
41 URI handler for Freeside URIs.  See http://www.freeside.biz/ for more
42 information on Freeside.
43
44
45 =head1 Public subroutines
46
47 =over 4
48
49 =item FreesideGetConfig CONFKEY
50
51 Subroutine that returns the freeside's configuration value(s) for CONFKEY
52 as a scalar or list.
53
54 =cut
55
56 sub FreesideGetConfig { return undef; }
57
58
59 =item FreesideURL
60
61 Returns the URL for freeside's web interface.
62
63 =cut
64
65 sub FreesideURL { return $URL; }
66
67
68 =item FreesideVersion
69
70 Returns a string describing the freeside version being used.
71
72 =cut
73
74 sub FreesideVersion { return undef; }
75
76
77 =item smart_search
78
79 A wrapper for the FS::cust_main::smart_search subroutine.
80
81 =cut
82
83 sub smart_search { return undef; }
84
85
86 =item small_custview
87
88 A wrapper for the FS::CGI::small_custview subroutine.
89
90 =cut
91
92 sub small_custview { return 'Freeside integration error!</A>'; }
93
94
95 =back
96
97 =head1 Private methods
98
99 =over 4
100
101 =item _FreesideGetRecord
102
103 Method returns a hashref of the freeside record referenced in the URI.
104 Must be called after ParseURI.
105
106 =cut
107
108 sub _FreesideGetRecord { return undef; }
109
110
111 =item _FreesideURIPrefix
112
113 Method that returns the URI prefix for freeside URIs.
114
115 =cut
116
117 sub _FreesideURIPrefix {
118
119   my $self = shift;
120   return($self->Scheme . '://freeside');
121
122 }
123
124 =item _FreesideURILabel
125
126 Method that returns a short string describing the customer referenced
127 in the URI.
128
129 =cut
130
131 sub _FreesideURILabel {
132
133   my $self = shift;
134
135   #$RT::Logger->debug("Called _FreesideURILabel()");
136
137   return unless (exists($self->{'fstable'}) and
138                  exists($self->{'fspkey'}));
139
140   my $label;
141   my ($table, $pkey) = ($self->{'fstable'}, $self->{'fspkey'});
142
143   #if ($table ne 'cust_main') {
144   #  warn "FS::${table} not currently supported";
145   #  return;
146   #}
147
148   my $rec = $self->_FreesideGetRecord();
149
150   if (ref($rec) eq 'HASH' && $table eq 'cust_main') {
151     my $name = $rec->{'last'} . ', ' . $rec->{'first'};
152     $name = $rec->{'company'} . " ($name)" if $rec->{'company'};
153     $label = "$pkey: $name";
154   } elsif ( $table eq 'cust_svc' && ref($rec) && $rec->{'_object'} ) {
155     #Internal only
156     my($l,$v) = $rec->{'_object'}->label;
157     $label = "$l: $v";
158   } else {
159     $label = "$pkey: $table";
160   }
161
162   if ($label and !$@) {
163     return($label);
164   } else {
165     return;
166   }
167
168 }
169
170 =item _FreesideURILabelLong
171
172 Method that returns a longer string describing the customer referenced
173 in the URI.
174
175 =cut
176
177 sub _FreesideURILabelLong {
178
179   my $self = shift;
180
181   return $self->_FreesideURILabel();
182
183 }
184
185 =back
186
187 =head1 Public methods
188
189 =over 4
190
191 =cut
192
193 sub ParseURI { 
194     my $self = shift;
195     my $uri = shift;
196     my ($table, $pkey);
197
198     my $uriprefix = $self->_FreesideURIPrefix;
199     if ($uri =~ /^$uriprefix\/(\w+)\/(\d*)$/) {
200
201       $table = $1;
202       $pkey = $2;
203
204       unless ( $pkey ) {
205         cluck "bad URL $uri";
206         return(undef);
207       }
208
209       $self->{'scheme'} = $self->Scheme;
210
211     } else {
212       return(undef);
213     }
214
215     $self->{'uri'} = "${uriprefix}/${table}/${pkey}";
216     $self->{'fstable'} = $table;
217     $self->{'fspkey'} = $pkey;
218
219
220     my $url = $self->FreesideURL();
221
222     if ($url ne '') {
223       $self->{'href'} = "${url}/view/${table}.cgi?${pkey}";
224     } else {
225       $self->{'href'} = $self->{'uri'};
226     }
227
228     $self->{'uri'};
229
230 }
231
232 sub Scheme { 
233     my $self = shift;
234     return('freeside');
235
236 }
237
238 sub HREF {
239     my $self = shift;
240     return($self->{'href'} || $self->{'uri'});
241 }
242
243 sub IsLocal {
244     my $self = shift;
245     return undef;
246 }
247
248 =item AsString
249
250 Return a "pretty" string representing the URI object.
251
252 This is meant to be used like this:
253
254  % $re = $uri->Resolver;
255  <A HREF="<% $re->HREF %>"><% $re->AsString %></A>
256
257 =cut
258
259 sub AsString {
260     my $self = shift;
261     my $prettystring;
262     if ($prettystring = $self->_FreesideURILabel) {
263       return $prettystring;
264     } else {
265       return $self->URI;
266     }
267 }
268
269 =item AsStringLong
270
271 Return a longer (HTML) string representing the URI object.
272
273 =cut
274
275 sub AsStringLong {
276     my $self = shift;
277     my $prettystring;
278     if ($prettystring = $self->_FreesideURILabelLong || $self->_FreesideURILabel){
279       return $prettystring;
280     } else {
281       return $self->URI;
282     }
283 }
284
285 $IntegrationType ||= 'Internal';
286 eval "require RT::URI::freeside::${RT::URI::freeside::IntegrationType}";
287 warn $@ if $@;
288 if ($@ &&
289     $@ !~ qr(^Can't locate RT/URI/freeside/${RT::URI::freeside::IntegrationType}.pm)) {
290   die $@;
291 };
292
293 =back
294
295 =cut
296
297 1;