line
stmt
bran
cond
sub
pod
time
code
1
package DBIx::HTML::PopupRadio;
2
3
# Name:
4
# DBIx::HTML::PopupRadio.
5
#
6
# Purpose:
7
# Allow caller to specify a database handle, an sql statement,
8
# and a name for the menu, and from that build the HTML for the menu.
9
# Menu here means either popup menu or radio group.
10
#
11
# Documentation:
12
# POD-style documentation is at the end. Extract it with pod2html.*.
13
#
14
# Note:
15
# o tab = 4 spaces || die
16
#
17
# V 1.00 1-Oct-2002
18
# -----------------
19
# o Original version
20
#
21
# Author:
22
# Ron Savage
23
# Home page: http://www.deakin.edu.au/~rons
24
25
1
1
25881
use strict;
1
3
1
33
26
1
1
5
use warnings;
1
1
1
30
27
28
require 5.005_62;
29
30
require Exporter;
31
32
1
1
6
use Carp;
1
1
1
86
33
1
1
832
use HTML::Entities::Interpolate;
1
8218
1
7
34
35
our @ISA = qw(Exporter);
36
37
# Items to export into callers namespace by default. Note: do not export
38
# names by default without a very good reason. Use EXPORT_OK instead.
39
# Do not simply export all your public functions/methods/constants.
40
41
# This allows declaration use Image::MagickWrapper ':all';
42
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
43
# will save memory.
44
our %EXPORT_TAGS = ( 'all' => [ qw(
45
46
) ] );
47
48
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
49
50
our @EXPORT = qw(
51
52
);
53
our $VERSION = '1.16';
54
55
# -----------------------------------------------
56
57
# Preloaded methods go here.
58
59
# -----------------------------------------------
60
61
# Encapsulated class data.
62
63
{
64
my(%_attr_data) =
65
( # Alphabetical order.
66
_dbh => '',
67
_default => '', # For popup_menu or radio_group.
68
_javascript => '',
69
_linebreak => 0, # For radio_group.
70
_name => 'dbix_menu',
71
_options => {},
72
_prompt => '', # For popup_menu.
73
_sql => '',
74
);
75
76
sub _default_for
77
{
78
0
0
my($self, $attr_name) = @_;
79
80
0
$_attr_data{$attr_name};
81
}
82
83
sub _read_data
84
{
85
0
0
my($self) = @_;
86
0
my($sth) = $$self{'_dbh'} -> prepare($$self{'_sql'});
87
0
$$self{'_data'} = {};
88
0
my($order) = 0;
89
90
0
$sth -> execute();
91
92
0
my($data);
93
94
0
while ($data = $sth -> fetch() )
95
{
96
0
$$self{'_data'}{$$data[0]} =
97
{
98
order => $order++,
99
value => $$data[1],
100
};
101
}
102
103
0
$$self{'_size'} = $order;
104
105
} # End of _read_data.
106
107
sub _standard_keys
108
{
109
0
0
sort keys %_attr_data;
110
}
111
112
sub _validate_options
113
{
114
0
0
my($self) = @_;
115
116
0
0
0
croak(__PACKAGE__ . ". You must supply values for these parameters: dbh, name and sql") if (! $$self{'_dbh'} || ! $$self{'_name'} || ! $$self{'_sql'});
117
118
# # Reset empty parameters to their defaults.
119
# # This could be optional, depending on another option.
120
#
121
# for my $attr_name ($self -> _standard_keys() )
122
# {
123
# $$self{$attr_name} = $self -> _default_for($attr_name) if (! $$self{$attr_name});
124
# }
125
126
} # End of _validate_options.
127
128
} # End of Encapsulated class data.
129
130
# -----------------------------------------------
131
132
sub new
133
{
134
0
0
1
my($class, %arg) = @_;
135
0
my($self) = bless({}, $class);
136
137
0
for my $attr_name ($self -> _standard_keys() )
138
{
139
0
my($arg_name) = $attr_name =~ /^_(.*)/;
140
141
0
0
if (exists($arg{$arg_name}) )
142
{
143
0
$$self{$attr_name} = $arg{$arg_name};
144
}
145
else
146
{
147
0
$$self{$attr_name} = $self -> _default_for($attr_name);
148
}
149
}
150
151
# This is the size (# if items) in the menu.
152
# Ie, it is the number of rows returned by the SQL.
153
154
0
$$self{'_size'} = 0;
155
156
0
return $self;
157
158
} # End of new.
159
160
# -----------------------------------------------
161
162
sub param
163
{
164
0
0
1
my($self, $id) = @_;
165
166
0
0
$id ? $$self{'_data'}{$id}{'value'} : '';
167
168
} # End of param.
169
170
# -----------------------------------------------
171
172
sub popup_menu
173
{
174
0
0
1
my($self, %arg) = @_;
175
176
# Give the user one last chance to set some parameters.
177
178
0
$self -> set(%arg);
179
0
$self -> _validate_options();
180
0
0
$self -> _read_data() if (! $$self{'_data'});
181
182
0
my(@html, $s);
183
184
0
$s = qq|
185
0
$s .= qq|$_="$Entitize{$$self{'_options'}{$_} }" | for sort keys %{$$self{'_options'} };
0
186
0
0
$s .= $$self{'_javascript'} if ($$self{'_javascript'});
187
0
$s .= '>';
188
189
0
push(@html, '', $s);
190
191
0
my($prompt) = $$self{'_prompt'};
192
193
0
0
if ($prompt)
194
{
195
0
0
if (ref($prompt) eq 'HASH')
196
{
197
0
push @html, qq|$Entitize{$$prompt{$_} } | for sort keys %$prompt;
198
}
199
else
200
{
201
0
push @html, qq|$Entitize{$prompt} |;
202
}
203
}
204
205
0
for (sort{$$self{'_data'}{$a}{'order'} <=> $$self{'_data'}{$b}{'order'} } keys %{$$self{'_data'} })
0
0
206
{
207
0
$s = qq|
208
0
0
0
$s .= qq| selected="selected"| if (defined($$self{'_default'}) && (lc $$self{'_default'} eq lc $$self{'_data'}{$_}{'value'}) );
209
0
$s .= qq|>$Entitize{$$self{'_data'}{$_}{'value'} }|;
210
211
0
push @html, $s;
212
}
213
214
0
push @html, '', '';
215
216
0
join "\n", @html;
217
218
} # End of popup_menu.
219
220
# -----------------------------------------------
221
222
sub radio_group
223
{
224
0
0
1
my($self, %arg) = @_;
225
226
# Give the user one last chance to set some parameters.
227
228
0
$self -> set(%arg);
229
0
$self -> _validate_options();
230
0
0
$self -> _read_data() if (! $$self{'_data'});
231
232
0
my($count) = 0;
233
234
0
my(@html, $s);
235
236
0
push @html, '';
237
238
0
for (sort{$$self{'_data'}{$a}{'order'} <=> $$self{'_data'}{$b}{'order'} } keys %{$$self{'_data'} })
0
0
239
{
240
0
$s = qq|
241
242
0
0
if ($$self{'_default'})
243
{
244
0
0
$s .= qq| checked="checked"| if (lc $$self{'_default'} eq lc $$self{'_data'}{$_}{'value'});
245
}
246
else
247
{
248
0
$count++;
249
250
0
0
$s .= qq| checked="checked"| if ($count == 1);
251
}
252
253
0
$s .= qq| />$Entitize{$$self{'_data'}{$_}{'value'} }|;
254
0
0
$s .= ' ' if ($$self{'_linebreak'});
255
256
0
push @html, $s;
257
}
258
259
0
push @html, '';
260
261
0
join "\n", @html;
262
263
} # End of radio_group.
264
265
# -----------------------------------------------
266
267
sub set
268
{
269
0
0
1
my($self, %arg) = @_;
270
271
0
for my $arg (keys %arg)
272
{
273
0
0
$$self{"_$arg"} = $arg{$arg} if (exists($$self{"_$arg"}) );
274
}
275
276
} # End of set.
277
278
# -----------------------------------------------
279
280
sub size
281
{
282
0
0
1
my($self) = @_;
283
284
0
$$self{'_size'};
285
286
} # End of size.
287
288
# -----------------------------------------------
289
290
1;
291
292
__END__