split [paths].libraries on the path sep
[bse.git] / site / cgi-bin / modules / BSE / CfgInfo.pm
CommitLineData
00dd8d82
TC
1package BSE::CfgInfo;
2use strict;
9d15b022 3use Config;
00dd8d82 4
9d15b022 5our $VERSION = "1.006";
cb7fd78d 6
00dd8d82 7use vars qw(@ISA @EXPORT_OK);
9d15b022
TC
8use Exporter qw(import);
9
1ff7f2d3 10@EXPORT_OK = qw(custom_class admin_base_url cfg_image_dir cfg_image_uri cfg_dist_image_uri cfg_data_dir cfg_scalecache_dir cfg_scalecache_uri credit_card_class product_options bse_default_country load_class);
00dd8d82
TC
11
12=head1 NAME
13
14BSE::CfgInfo - functions that return information derived from configuration
15
16=head1 SYNOPSIS
17
18 my $cfg = BSE::Cfg->new;
19 use BSE::CfgInfo 'admin_base_url';
20 my $admin_base = admin_base_url($cfg);
21
22 use BSE::CfgInfo 'custom_class';
23 my $class = custom_class($cfg);
24
41e7c841
TC
25 use BSE::CfgInfo 'credit_card_class';
26 my $class = credit_card_class($cfg);
27
f2bf0d11
TC
28 use BSE::CfgInfo 'product_options';
29 my $options = product_options($cfg);
30
cadb5bfa
TC
31 use BSE::CfgInfo 'bse_default_country';
32 my $country = bse_default_country($cfg);
33
00dd8d82
TC
34=head1 DESCRIPTION
35
36This module contains functions which examine the BSE configuration and
37return information useful at the application level.
38
39=over
40
f2bf0d11
TC
41=item custom_class
42
43Returns an object of the class of the configured custom class.
00dd8d82
TC
44
45=cut
46
47sub custom_class {
48 my ($cfg) = @_;
49
00dd8d82 50 my $class = $cfg->entry('basic', 'custom_class', 'BSE::Custom');
1ff7f2d3
TC
51
52 load_class($class, $cfg);
53
54 return $class->new(cfg=>$cfg);
55}
56
57=item load_class($class)
58
59Load a class, also searching the configured library directories.
60
61Should be wrapped in an eval if load failures need to be captured.
62
63=cut
64
65sub load_class {
66 my ($class, $cfg) = @_;
67
68 $cfg ||= BSE::Cfg->single;
69
00dd8d82
TC
70 (my $file = $class . ".pm") =~ s!::!/!g;
71
1c794b36
TC
72 local @INC = @INC;
73
6a04abe7 74 _do_local_inc($cfg);
00dd8d82
TC
75
76 require $file;
77
1ff7f2d3 78 1;
00dd8d82
TC
79}
80
f2bf0d11
TC
81=item admin_base_url($cfg)
82
83=cut
84
00dd8d82
TC
85sub admin_base_url {
86 my ($cfg) = @_;
87
88 my $base = $cfg->entryIfVar('site', 'adminurl');
89 unless ($base) {
90 my $sec_admin = $cfg->entryBool('site', 'secureadmin', 0);
91 if ($sec_admin) {
92 $base = $cfg->entryErr('site', 'secureurl');
93 }
94 else {
95 $base = $cfg->entryErr('site', 'url');
96 }
97 }
98
99 return $base;
100}
101
771ab646
TC
102=item cfg_image_dir()
103
104Return the directory configured for storage of managed images.
105
106=cut
107
ab2cd916
TC
108sub cfg_image_dir {
109 my ($cfg) = @_;
110
771ab646
TC
111 $cfg ||= BSE::Cfg->single;
112
113 return $cfg->entryIfVar('paths', 'images', $Constants::IMAGEDIR);
114}
115
116=item cfg_image_uri()
117
118Return the configured base URI for managed images.
119
120This should correspond to the directory specified by [paths].images
121
122Configured with [uri].images
123
124=cut
125
126sub cfg_image_uri {
127 my ($cfg) = @_;
128
129 $cfg ||= BSE::Cfg->single;
130
131 require Constants;
132 return $cfg->entryIfVar('uri', 'images', $Constants::IMAGES_URI);
133}
134
135=item cfg_dist_image_uri()
136
137Return the configured base URI for images distributed with BSE.
138
139This imcludes images such as F<trans_pixel.gif> and C<admin/error.gif>.
140
141Must not include a trailing C</>.
142
143Configured with [uri].dist_images
144
145=cut
146
147sub cfg_dist_image_uri {
148 my ($cfg) = @_;
149
150 $cfg ||= BSE::Cfg->single;
151
152 return $cfg->entryIfVar('uri', 'dist_images', "/images");
153}
154
155=item cfg_data_dir()
156
157Returns the directory configured for storage of files such as
158F<stopwords.txt>.
159
160Configured with [paths].data
161
162=cut
163
164sub cfg_data_dir {
165 my ($cfg) = @_;
166
167 $cfg ||= BSE::Cfg->single;
168
da24de93
TC
169 my $dir = $cfg->entryIfVar('paths', 'data', $Constants::DATADIR);
170 -d $dir
171 or die "[paths].data value '$dir' isn't a directory\n";
172
173 return $dir;
ab2cd916
TC
174}
175
d2bde75c
TC
176=item cfg_scalecache_dir()
177
178Returns the directory configured for storage of generated thumbnails.
179
180Controlled with [paths].scalecache.
181
182Default: C<cfg_image_dir() . "/scaled">
183
184=cut
185
186sub cfg_scalecache_dir {
187 my ($cfg) = @_;
188
189 $cfg ||= BSE::Cfg->single;
190
191 my $dir = $cfg->entryIfVar('paths', 'scalecache', cfg_image_dir($cfg) . "/scaled");
192 -d $dir
193 or die "[paths].scalecache value '$dir' isn't a directory\n";
194
195 return $dir;
196}
197
198=item cfg_scalecache_uri()
199
200Returns the uri for the directory configured for storage of generated
201thumbnails.
202
203Controlled with C<[uri].scalecache> with a fallback to
204C<[paths].scalecacheurl>.
205
206=cut
207
208sub cfg_scalecache_uri {
209 my ($cfg) = @_;
210
211 $cfg ||= BSE::Cfg->single;
212
213 my $uri = $cfg->entryIfVar('uri', 'scalecache');
214 defined $uri
215 or $uri = $cfg->entryIfVar('path', 'scalecacheurl');
216 defined $uri
217 or $uri = cfg_image_uri($cfg) . "/scaled";
218
219 return $uri;
220}
221
f2bf0d11
TC
222=item credit_card_class
223
224Loads the configured credit card class and instantiates it.
225
226=cut
227
41e7c841
TC
228sub credit_card_class {
229 my ($cfg) = @_;
230
231 local @INC = @INC;
232
233 my $class = $cfg->entry('shop', 'cardprocessor')
234 or return;
235 (my $file = $class . ".pm") =~ s!::!/!g;
236
6a04abe7 237 _do_local_inc($cfg);
41e7c841
TC
238
239 require $file;
240
241 return $class->new($cfg);
242}
243
f2bf0d11
TC
244=item product_options
245
246Returns a hashref of product options, where the key is the option id,
247the values are each a hashref with the following keys:
248
249=over
250
251=item *
252
253desc - description of the option
254
255=item *
256
257values - array ref of possible values for the option
258
259=item *
260
261labels - hashref of labels for the different values. This is always
262filled out with the labels defaulting to the values.
263
264=back
265
266=cut
267
2076966c
TC
268sub product_options {
269 my ($cfg) = @_;
270
271 my %options = %Constants::SHOP_PRODUCT_OPTS;
272
273 my %cfg_opts = $cfg->entriesCS('shop product options');
274 for my $option (keys %cfg_opts) {
275 my ($option_desc, @values) = split /;/, $cfg_opts{$option};
276 my %value_labels;
277 for my $value (@values) {
278 $value_labels{$value} = $cfg->entry("shop product option $option",
279 $value, $value);
280 }
281 $options{$option} =
282 {
283 desc => $option_desc,
284 values => \@values,
285 labels => \%value_labels,
286 };
287 }
288
289 \%options;
290}
291
cadb5bfa
TC
292sub bse_default_country {
293 my ($cfg) = @_;
294
295 return $cfg->entry("basic", "country", "Australia");
296}
297
6a04abe7
TC
298sub _do_local_inc {
299 my ($cfg) = @_;
300
301 my $local_inc = $cfg->entryIfVar('paths', 'libraries');
302
9d15b022 303 unshift @INC, split /\Q$Config{path_sep}/, $local_inc if $local_inc;
6a04abe7
TC
304}
305
00dd8d82
TC
3061;
307
308__END__
309
310=back
311
312=head1 AUTHOR
313
314Tony Cook <tony@develop-help.com>
315
316=cut