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