when using a side menubar, put search boxes on the side too
[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.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       $table = $1;
201       $pkey = $2;
202       $self->{'scheme'} = $self->Scheme;
203     } else {
204       return(undef);
205     }
206
207     $self->{'uri'} = "${uriprefix}/${table}/${pkey}";
208     $self->{'fstable'} = $table;
209     $self->{'fspkey'} = $pkey;
210
211
212     my $url = $self->FreesideURL();
213
214     if ($url ne '') {
215       $self->{'href'} = "${url}/view/${table}.cgi?${pkey}";
216     } else {
217       $self->{'href'} = $self->{'uri'};
218     }
219
220     $self->{'uri'};
221
222 }
223
224 sub Scheme { 
225     my $self = shift;
226     return('freeside');
227
228 }
229
230 sub HREF {
231     my $self = shift;
232     return($self->{'href'} || $self->{'uri'});
233 }
234
235 sub IsLocal {
236     my $self = shift;
237     return undef;
238 }
239
240 =item AsString
241
242 Return a "pretty" string representing the URI object.
243
244 This is meant to be used like this:
245
246  % $re = $uri->Resolver;
247  <A HREF="<% $re->HREF %>"><% $re->AsString %></A>
248
249 =cut
250
251 sub AsString {
252     my $self = shift;
253     my $prettystring;
254     if ($prettystring = $self->_FreesideURILabel) {
255       return $prettystring;
256     } else {
257       return $self->URI;
258     }
259 }
260
261 =item AsStringLong
262
263 Return a longer (HTML) string representing the URI object.
264
265 =cut
266
267 sub AsStringLong {
268     my $self = shift;
269     my $prettystring;
270     if ($prettystring = $self->_FreesideURILabelLong || $self->_FreesideURILabel){
271       return $prettystring;
272     } else {
273       return $self->URI;
274     }
275 }
276
277 $IntegrationType ||= 'Internal';
278 eval "require RT::URI::freeside::${RT::URI::freeside::IntegrationType}";
279 warn $@ if $@;
280 if ($@ &&
281     $@ !~ qr(^Can't locate RT/URI/freeside/${RT::URI::freeside::IntegrationType}.pm)) {
282   die $@;
283 };
284
285 =back
286
287 =cut
288
289 1;