package DBIx::HTML::PopupRadio;
# Name:
# DBIx::HTML::PopupRadio.
#
# Purpose:
# Allow caller to specify a database handle, an sql statement,
# and a name for the menu, and from that build the HTML for the menu.
# Menu here means either popup menu or radio group.
#
# Documentation:
# POD-style documentation is at the end. Extract it with pod2html.*.
#
# Note:
# o tab = 4 spaces || die
#
# V 1.00 1-Oct-2002
# -----------------
# o Original version
#
# Author:
# Ron Savage
# Home page: http://www.deakin.edu.au/~rons
use strict;
use warnings;
require 5.005_62;
require Exporter;
use Carp;
use HTML::Entities::Interpolate;
our @ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use Image::MagickWrapper ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
);
our $VERSION = '1.13';
# -----------------------------------------------
# Preloaded methods go here.
# -----------------------------------------------
# Encapsulated class data.
{
my(%_attr_data) =
( # Alphabetical order.
_dbh => '',
_default => '', # For popup_menu or radio_group.
_javascript => '',
_linebreak => 0, # For radio_group.
_name => 'dbix_menu',
_options => {},
_prompt => '', # For popup_menu.
_sql => '',
);
sub _default_for
{
my($self, $attr_name) = @_;
$_attr_data{$attr_name};
}
sub _read_data
{
my($self) = @_;
my($sth) = $$self{'_dbh'} -> prepare($$self{'_sql'});
$$self{'_data'} = {};
my($order) = 0;
$sth -> execute();
my($data);
while ($data = $sth -> fetch() )
{
$$self{'_data'}{$$data[0]} =
{
order => $order++,
value => $$data[1],
};
}
$$self{'_size'} = $order;
} # End of _read_data.
sub _standard_keys
{
sort keys %_attr_data;
}
sub _validate_options
{
my($self) = @_;
croak(__PACKAGE__ . ". You must supply values for these parameters: dbh, name and sql") if (! $$self{'_dbh'} || ! $$self{'_name'} || ! $$self{'_sql'});
# # Reset empty parameters to their defaults.
# # This could be optional, depending on another option.
#
# for my $attr_name ($self -> _standard_keys() )
# {
# $$self{$attr_name} = $self -> _default_for($attr_name) if (! $$self{$attr_name});
# }
} # End of _validate_options.
} # End of Encapsulated class data.
# -----------------------------------------------
sub new
{
my($class, %arg) = @_;
my($self) = bless({}, $class);
for my $attr_name ($self -> _standard_keys() )
{
my($arg_name) = $attr_name =~ /^_(.*)/;
if (exists($arg{$arg_name}) )
{
$$self{$attr_name} = $arg{$arg_name};
}
else
{
$$self{$attr_name} = $self -> _default_for($attr_name);
}
}
# This is the size (# if items) in the menu.
# Ie, it is the number of rows returned by the SQL.
$$self{'_size'} = 0;
return $self;
} # End of new.
# -----------------------------------------------
sub param
{
my($self, $id) = @_;
$id ? $$self{'_data'}{$id}{'value'} : '';
} # End of param.
# -----------------------------------------------
sub popup_menu
{
my($self, %arg) = @_;
# Give the user one last chance to set some parameters.
$self -> set(%arg);
$self -> _validate_options();
$self -> _read_data() if (! $$self{'_data'});
my(@html, $s);
$s = qq|', '';
join "\n", @html;
} # End of popup_menu.
# -----------------------------------------------
sub radio_group
{
my($self, %arg) = @_;
# Give the user one last chance to set some parameters.
$self -> set(%arg);
$self -> _validate_options();
$self -> _read_data() if (! $$self{'_data'});
my($count) = 0;
my(@html, $s);
push @html, '';
for (sort{$$self{'_data'}{$a}{'order'} <=> $$self{'_data'}{$b}{'order'} } keys %{$$self{'_data'} })
{
$s = qq|$Entitize{$$self{'_data'}{$_}{'value'} }|;
$s .= ' ' if ($$self{'_linebreak'});
push @html, $s;
}
push @html, '';
join "\n", @html;
} # End of radio_group.
# -----------------------------------------------
sub set
{
my($self, %arg) = @_;
for my $arg (keys %arg)
{
$$self{"_$arg"} = $arg{$arg} if (exists($$self{"_$arg"}) );
}
} # End of set.
# -----------------------------------------------
sub size
{
my($self) = @_;
$$self{'_size'};
} # End of size.
# -----------------------------------------------
1;
__END__
=head1 NAME
C - Convert sql into a popup menu or radio group.
=head1 Synopsis
use DBIx::HTML::PopupRadio;
my($popup_object) = DBIx::HTML::PopupRadio -> new
(
dbh => $dbh,
sql => 'select campus_id, campus_name from campus order by campus_name',
);
$popup_object -> set(default => '1');
my($popup_menu) = $popup_object -> popup_menu();
my($radio_group) = $popup_object -> radio_group();
print $popup_menu;
=head1 Description
This module takes a db handle and an SQL statement, and builds a hash.
Then you ask for that hash in HTML, as a popup menu or as a radio group.
The reading of the db table is delayed until you actually call one of the
methods 'popup_menu' or 'radio_group'. Even then, it is delayed until any
parameters passed in to these 2 methods are processed.
After a call to one of these 2 methods, you can call the 'size' method if
you need to check how many rows were returned by the SQL you used.
Neither the module CGI.pm, nor any of that kidney, are used by this module.
We simply output pure HTML.
=head1 Distributions
This module is available both as a Unix-style distro (*.tgz) and an
ActiveState-style distro (*.ppd). The latter is shipped in a *.zip file.
See http://savage.net.au/Perl-modules/html/installing-a-module.html for
help on unpacking and installing each type of distro.
=head1 Usage
You create an object of the class by calling the constructor, 'new'.
You then call 'set', if you wish, to set any options.
Now call 'popup_menu' or 'radio_group' to get the HTML.
Lastly, display the HTML as part of a form.
The method names 'popup_menu' and 'radio_group' (and 'param') were chosen to be
reminiscent of methods with the same names in the CGI.pm module. But let
me repeat, my module does not use CGI.
=head1 Options
Here, in alphabetical order, are the options accepted by the constructor,
together with their default values.
=over 4
=item dbh => ''
Pass in an open database handle.
This option is mandatory, in the call to new, set, popup_menu or radio_group.
Ie By the time you call one of the latter 2 methods, dbh must be set.
=item default => ''
Pass in the string (from SQL column 2) which is to be the default item on the popup
menu or radio group. You supply here the visible menu item, not the value associated with
that menu item.
Starting with V 1.13, a case-insensitive match is used to compare the value provided here with the
values read in from the database.
If default is not given a value, the first menu item becomes the default.
See the discussion of the sql option for details about the menu items.
This option is not mandatory.
=item javascript => ''
Pass in a string of JavaScript, eg an event handler.
By using, say, javascript => 'onChange = "replicate()"', you can change the first line of HTML output from this: