687f923229d2dad6a69c1f976aab2419d3a03d91
[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 $IntegrationType $URL);
32 @ISA = qw/RT::URI::base/;
33
34
35 =head1 NAME
36
37 RT::URI::freeside
38
39 =head1 DESCRIPTION
40
41 URI handler for freeside URIs.  See http://www.sisd.com/freeside/ for
42 more 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' and $table eq 'cust_main') {
151     my $name = $rec->{'last'} . ', ' . $rec->{'first'};
152     $name = $rec->{'company'} . " ($name)" if $rec->{'company'};
153     $label = "$pkey: $name";
154   } else {
155     $label = "$pkey: $table";
156   }
157
158   if ($label and !$@) {
159     return($label);
160   } else {
161     return;
162   }
163
164 }
165
166 =item _FreesideURILabel
167
168 Method that returns a longer string describing the customer referenced
169 in the URI.
170
171 =cut
172
173 sub _FreesideURILabelLong {
174
175   my $self = shift;
176
177   return $self->_FreesideURILabel();
178
179 }
180
181 =back
182
183 =head1 Public methods
184
185 =over 4
186
187 =cut
188
189 sub ParseURI { 
190     my $self = shift;
191     my $uri = shift;
192     my ($table, $pkey);
193
194     my $uriprefix = $self->_FreesideURIPrefix;
195     if ($uri =~ /^$uriprefix\/(\w+)\/(\d+)$/) {
196       $table = $1;
197       $pkey = $2;
198       $self->{'scheme'} = $self->Scheme;
199     } else {
200       return(undef);
201     }
202
203     $self->{'uri'} = "${uriprefix}/${table}/${pkey}";
204     $self->{'fstable'} = $table;
205     $self->{'fspkey'} = $pkey;
206
207
208     my $url = $self->FreesideURL();
209
210     if ($url ne '') {
211       $self->{'href'} = "${url}/view/${table}.cgi?${pkey}";
212     } else {
213       $self->{'href'} = $self->{'uri'};
214     }
215
216     $self->{'uri'};
217
218 }
219
220 sub Scheme { 
221     my $self = shift;
222     return('freeside');
223
224 }
225
226 sub HREF {
227     my $self = shift;
228     return($self->{'href'} || $self->{'uri'});
229 }
230
231 sub IsLocal {
232     my $self = shift;
233     return undef;
234 }
235
236 =item AsString
237
238 Return a "pretty" string representing the URI object.
239
240 This is meant to be used like this:
241
242  % $re = $uri->Resolver;
243  <A HREF="<% $re->HREF %>"><% $re->AsString %></A>
244
245 =cut
246
247 sub AsString {
248     my $self = shift;
249     my $prettystring;
250     if ($prettystring = $self->_FreesideURILabel) {
251       return $prettystring;
252     } else {
253       return $self->URI;
254     }
255 }
256
257 =item AsStringLong
258
259 Return a longer (HTML) string representing the URI object.
260
261 =cut
262
263 sub AsStringLong {
264     my $self = shift;
265     my $prettystring;
266     if ($prettystring = $self->_FreesideURILabelLong || $self->_FreesideURILabel){
267       return $prettystring;
268     } else {
269       return $self->URI;
270     }
271 }
272
273 $IntegrationType ||= 'Internal';
274 eval "require RT::URI::freeside::${RT::URI::freeside::IntegrationType}";
275 warn $@ if $@;
276 if ($@ &&
277     $@ !~ qr(^Can't locate RT/URI/freeside/${RT::URI::freeside::IntegrationType}.pm)) {
278   die $@;
279 };
280
281 =back
282
283 =cut
284
285 1;